]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/Group.java
upd: convert to PostgreSQL
[gigi.git] / src / org / cacert / gigi / dbObjects / Group.java
index bdce278da0979f047d379ed0f1f1648481ec888f..8a2c1de479755ed0bce9d529dfffd014e93f1295 100644 (file)
@@ -1,10 +1,11 @@
 package org.cacert.gigi.dbObjects;
 
-import java.util.HashMap;
+import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 
-public class Group {
-
-    private static HashMap<String, Group> cache = new HashMap<>();
+public enum Group {
+    SUPPORTER("supporter"), ARBITRATOR("arbitrator"), BLOCKEDASSURER("blockedassurer"), BLOCKEDASSUREE("blockedassuree"), BLOCKEDLOGIN("blockedlogin"), TTP_ASSURER("ttp-assurer"), TTP_APPLICANT("ttp-applicant"), CODESIGNING("codesigning"), ORGASSURER("orgassurer");
 
     private final String dbName;
 
@@ -12,46 +13,27 @@ public class Group {
         dbName = name;
     }
 
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((dbName == null) ? 0 : dbName.hashCode());
-        return result;
+    public static Group getByString(String name) {
+        return valueOf(name.toUpperCase().replace('-', '_'));
     }
 
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        Group other = (Group) obj;
-        if (dbName == null) {
-            if (other.dbName != null) {
-                return false;
-            }
-        } else if ( !dbName.equals(other.dbName)) {
-            return false;
-        }
-        return true;
+    public String getDatabaseName() {
+        return dbName;
     }
 
-    public static synchronized Group getByString(String name) {
-        Group g = cache.get(name);
-        if (g == null) {
-            g = new Group(name);
-            cache.put(name, g);
+    public User[] getMembers(int offset, int count) {
+        GigiPreparedStatement gps = DatabaseConnection.getInstance().prepareScrollable("SELECT `user` FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL OFFSET ? LIMIT ?");
+        gps.setString(1, dbName);
+        gps.setInt(2, offset);
+        gps.setInt(3, count);
+        GigiResultSet grs = gps.executeQuery();
+        grs.last();
+        User[] users = new User[grs.getRow()];
+        int i = 0;
+        grs.beforeFirst();
+        while (grs.next()) {
+            users[i++] = User.getById(grs.getInt(1));
         }
-        return g;
-    }
-
-    public String getDatabaseName() {
-        return dbName;
+        return users;
     }
 }