]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
ADD: Enable Language selection.
[gigi.git] / src / org / cacert / gigi / User.java
index b3fdb26ceaf23b6bb2cd2d36a1a4d3ca7a1e972f..764873812d29b4ae3c7e89e4d3a44af67fa3a5fd 100644 (file)
@@ -5,8 +5,11 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Calendar;
+import java.util.Locale;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.util.Notary;
 import org.cacert.gigi.util.PasswordHash;
 import org.cacert.gigi.util.PasswordStrengthChecker;
 
@@ -14,22 +17,36 @@ public class User {
 
     private int id;
 
-    Name name = new Name(null, null, null, null);
+    private Name name = new Name(null, null, null, null);
 
-    Date dob;
+    private Date dob;
 
-    String email;
+    private String email;
+
+    private Assurance[] receivedAssurances, madeAssurances;
+
+    private Locale locale;
 
     public User(int id) {
         this.id = id;
+        updateName(id);
+    }
+
+    private void updateName(int id) {
         try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?");
             ps.setInt(1, id);
             ResultSet rs = ps.executeQuery();
             if (rs.next()) {
                 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
                 dob = rs.getDate(5);
                 email = rs.getString(6);
+                String localeStr = rs.getString(7);
+                if (localeStr == null || localeStr.equals("")) {
+                    locale = Locale.getDefault();
+                } else {
+                    locale = Language.getLocaleFromString(localeStr);
+                }
             }
             rs.close();
         } catch (SQLException e) {
@@ -103,7 +120,7 @@ public class User {
         if (id != 0) {
             throw new Error("refusing to insert");
         }
-        PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
+        PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?");
         query.setString(1, email);
         query.setString(2, PasswordHash.hash(password));
         query.setString(3, name.fname);
@@ -111,6 +128,7 @@ public class User {
         query.setString(5, name.lname);
         query.setString(6, name.suffix);
         query.setDate(7, new java.sql.Date(dob.getTime()));
+        query.setString(8, locale.toString());
         query.execute();
         id = DatabaseConnection.lastInsertId(query);
     }
@@ -377,4 +395,105 @@ public class User {
         }
         throw new GigiApiException("Email not one of user's email addresses.");
     }
+
+    public Assurance[] getReceivedAssurances() throws SQLException {
+        if (receivedAssurances == null) {
+            PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
+            query.setInt(1, getId());
+            ResultSet res = query.executeQuery();
+            res.last();
+            Assurance[] assurances = new Assurance[res.getRow()];
+            res.beforeFirst();
+            for (int i = 0; i < assurances.length; i++) {
+                res.next();
+                assurances[i] = new Assurance(res);
+            }
+            this.receivedAssurances = assurances;
+            return assurances;
+        }
+        return receivedAssurances;
+    }
+
+    public Assurance[] getMadeAssurances() throws SQLException {
+        if (madeAssurances == null) {
+            PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
+            query.setInt(1, getId());
+            ResultSet res = query.executeQuery();
+            res.last();
+            Assurance[] assurances = new Assurance[res.getRow()];
+            res.beforeFirst();
+            for (int i = 0; i < assurances.length; i++) {
+                res.next();
+                assurances[i] = new Assurance(res);
+            }
+            this.madeAssurances = assurances;
+            return assurances;
+        }
+        return madeAssurances;
+    }
+
+    public void invalidateMadeAssurances() {
+        madeAssurances = null;
+    }
+
+    public void invalidateReceivedAssurances() {
+        receivedAssurances = null;
+    }
+
+    public void updateUserData() throws SQLException, GigiApiException {
+        synchronized (Notary.class) {
+            if (getAssurancePoints() != 0) {
+                updateUserData();
+                throw new GigiApiException("No change after assurance allowed.");
+            }
+            PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
+            update.setString(1, getFname());
+            update.setString(2, getLname());
+            update.setString(3, getMname());
+            update.setString(4, getSuffix());
+            update.setDate(5, getDob());
+            update.setInt(6, getId());
+            update.executeUpdate();
+        }
+    }
+
+    public Locale getPreferredLocale() {
+        return locale;
+    }
+
+    public void setPreferredLocale(Locale locale) {
+        this.locale = locale;
+
+    }
+
+    public boolean wantsDirectoryListing() throws SQLException {
+        PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
+        get.setInt(1, getId());
+        ResultSet exec = get.executeQuery();
+        exec.next();
+        return exec.getBoolean("listme");
+    }
+
+    public String getContactInformation() throws SQLException {
+        PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
+        get.setInt(1, getId());
+        ResultSet exec = get.executeQuery();
+        exec.next();
+        return exec.getString("contactinfo");
+    }
+
+    public void setDirectoryListing(boolean on) throws SQLException {
+        PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
+        update.setBoolean(1, on);
+        update.setInt(2, getId());
+        update.executeUpdate();
+    }
+
+    public void setContactInformation(String contactInfo) throws SQLException {
+        PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
+        update.setString(1, contactInfo);
+        update.setInt(2, getId());
+        update.executeUpdate();
+    }
+
 }