]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/EmailAddress.java
UPD: Cleanup User-class.
[gigi.git] / src / org / cacert / gigi / dbObjects / EmailAddress.java
index 3ca39b6d2c9d2178b6e2564965d772c1e5d804db..b7bb080fa16adf67bc676e6de6b0edf94fbb74c6 100644 (file)
@@ -1,6 +1,7 @@
 package org.cacert.gigi.dbObjects;
 
 import java.io.IOException;
+import java.util.Locale;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
@@ -11,7 +12,7 @@ import org.cacert.gigi.email.MailProbe;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.RandomToken;
 
-public class EmailAddress implements IdCachable {
+public class EmailAddress implements IdCachable, Verifyable {
 
     private String address;
 
@@ -22,7 +23,7 @@ public class EmailAddress implements IdCachable {
     private String hash = null;
 
     private EmailAddress(int id) {
-        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `emails` WHERE id=? AND deleted is NULL");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid`, `email`, `hash` FROM `emails` WHERE `id`=? AND `deleted` IS NULL");
         ps.setInt(1, id);
 
         GigiResultSet rs = ps.executeQuery();
@@ -36,27 +37,28 @@ public class EmailAddress implements IdCachable {
         rs.close();
     }
 
-    public EmailAddress(User owner, String address) {
+    public EmailAddress(User owner, String address, Locale mailLocale) throws GigiApiException {
         if ( !EmailProvider.MAIL.matcher(address).matches()) {
             throw new IllegalArgumentException("Invalid email.");
         }
         this.address = address;
         this.owner = owner;
         this.hash = RandomToken.generateToken(16);
+        insert(Language.getInstance(mailLocale));
     }
 
-    public void insert(Language l) throws GigiApiException {
+    private void insert(Language l) throws GigiApiException {
         try {
-            if (id != 0) {
-                throw new IllegalStateException("already inserted.");
-            }
-            GigiPreparedStatement psCheck = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL");
-            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
-            ps.setInt(1, owner.getId());
-            ps.setString(2, hash);
-            ps.setString(3, address);
-            psCheck.setString(1, address);
             synchronized (EmailAddress.class) {
+                if (id != 0) {
+                    throw new IllegalStateException("already inserted.");
+                }
+                GigiPreparedStatement psCheck = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL");
+                GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
+                ps.setInt(1, owner.getId());
+                ps.setString(2, hash);
+                ps.setString(3, address);
+                psCheck.setString(1, address);
                 GigiResultSet res = psCheck.executeQuery();
                 if (res.next()) {
                     throw new GigiApiException("The email is currently valid");