]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
UPD: Use gigiapiexceptions
[gigi.git] / src / org / cacert / gigi / User.java
index 10a10fd34283e8e03e3940af4bd3db17a736cc76..1dd150116d973e115ce3a3ad71edcc6b5a5ea323 100644 (file)
@@ -1,9 +1,9 @@
 package org.cacert.gigi;
 
+import java.sql.Date;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Date;
 import java.util.Calendar;
 
 import org.cacert.gigi.database.DatabaseConnection;
@@ -267,4 +267,40 @@ public class User {
 
                return null;
        }
+
+       public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
+               try {
+                       EmailAddress[] adrs = getEmails();
+                       for (int i = 0; i < adrs.length; i++) {
+                               if (adrs[i].getAddress().equals(newMail.getAddress())) {
+                                       if (!adrs[i].isVerified()) {
+                                               throw new GigiApiException("Email not verified.");
+                                       }
+                                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                                               "UPDATE users SET email=? WHERE id=?");
+                                       ps.setString(1, newMail.getAddress());
+                                       ps.setInt(2, getId());
+                                       ps.execute();
+                                       email = newMail.getAddress();
+                                       return;
+                               }
+                       }
+                       throw new GigiApiException("Given address not an address of the user.");
+               } catch (SQLException e) {
+                       throw new GigiApiException(e);
+               }
+       }
+
+       public void deleteEmail(EmailAddress mail) throws GigiApiException {
+               if (getEmail().equals(mail.getAddress())) {
+                       throw new GigiApiException("Can't delete user's default e-mail.");
+               }
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare("DELETE FROM email WHERE id=?");
+                       ps.setInt(1, mail.getId());
+                       ps.execute();
+               } catch (SQLException e) {
+                       throw new GigiApiException(e);
+               }
+       }
 }