]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
ADD: Get infomation about user's directory listing options
[gigi.git] / src / org / cacert / gigi / User.java
index b3fdb26ceaf23b6bb2cd2d36a1a4d3ca7a1e972f..22f76c4c5f8347e968e6d6c65b54948d8e9e3c8b 100644 (file)
@@ -7,6 +7,8 @@ import java.sql.SQLException;
 import java.util.Calendar;
 
 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,14 +16,20 @@ 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;
 
     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=?");
             ps.setInt(1, id);
@@ -377,4 +385,85 @@ 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 Language getPrefferedLanguage() {
+        return Language.getInstance("de");
+    }
+
+    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");
+    }
 }