]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/User.java
FIX: synchronization for IDCachable-Objects
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
index 9166b0443f4b564bb8f37894c01592ba515e7508..bc927fa9849bd647033fe8a50dfdf2baa7a4f645 100644 (file)
@@ -28,7 +28,7 @@ public class User implements IdCachable {
 
     private Locale locale;
 
-    public User(int id) {
+    private User(int id) {
         this.id = id;
         updateName(id);
     }
@@ -162,6 +162,9 @@ public class User implements IdCachable {
     }
 
     public boolean canAssure() throws SQLException {
+        if ( !isOfAge(14)) { // PoJAM
+            return false;
+        }
         if (getAssurancePoints() < 100) {
             return false;
         }
@@ -227,17 +230,12 @@ public class User implements IdCachable {
      * @throws SQLException
      */
     public int getMaxAssurePoints() throws SQLException {
+        if ( !isOfAge(18)) {
+            return 10; // PoJAM
+        }
+
         int exp = getExperiencePoints();
         int points = 10;
-        Calendar c = Calendar.getInstance();
-        c.setTime(dob);
-        int year = c.get(Calendar.YEAR);
-        int month = c.get(Calendar.MONTH);
-        int day = c.get(Calendar.DAY_OF_MONTH);
-        c.set(year + 18, month, day);
-        if (System.currentTimeMillis() < c.getTime().getTime()) {
-            return points; // not 18 Years old.
-        }
 
         if (exp >= 10) {
             points += 5;
@@ -257,6 +255,17 @@ public class User implements IdCachable {
         return points;
     }
 
+    public boolean isOfAge(int desiredAge) {
+        Calendar c = Calendar.getInstance();
+        c.setTime(dob);
+        int year = c.get(Calendar.YEAR);
+        int month = c.get(Calendar.MONTH);
+        int day = c.get(Calendar.DAY_OF_MONTH);
+        c.set(year, month, day);
+        c.add(Calendar.YEAR, desiredAge);
+        return System.currentTimeMillis() >= c.getTime().getTime();
+    }
+
     public EmailAddress[] getEmails() {
         try {
             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
@@ -497,12 +506,10 @@ public class User implements IdCachable {
 
     private static ObjectCache<User> myCache = new ObjectCache<>();
 
-    public static User getById(int id) {
+    public static synchronized User getById(int id) {
         User u = myCache.get(id);
         if (u == null) {
-            synchronized (User.class) {
-                myCache.put(u = new User(id));
-            }
+            myCache.put(u = new User(id));
         }
         return u;
     }