]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Group.java
ADD: TTP-Admin draft
[gigi.git] / src / org / cacert / gigi / dbObjects / Group.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.HashMap;
4
5 import org.cacert.gigi.database.DatabaseConnection;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8
9 public class Group {
10
11     private static HashMap<String, Group> cache = new HashMap<>();
12
13     private final String dbName;
14
15     private Group(String name) {
16         dbName = name;
17     }
18
19     @Override
20     public int hashCode() {
21         final int prime = 31;
22         int result = 1;
23         result = prime * result + ((dbName == null) ? 0 : dbName.hashCode());
24         return result;
25     }
26
27     @Override
28     public boolean equals(Object obj) {
29         if (this == obj) {
30             return true;
31         }
32         if (obj == null) {
33             return false;
34         }
35         if (getClass() != obj.getClass()) {
36             return false;
37         }
38         Group other = (Group) obj;
39         if (dbName == null) {
40             if (other.dbName != null) {
41                 return false;
42             }
43         } else if ( !dbName.equals(other.dbName)) {
44             return false;
45         }
46         return true;
47     }
48
49     public static synchronized Group getByString(String name) {
50         Group g = cache.get(name);
51         if (g == null) {
52             g = new Group(name);
53             cache.put(name, g);
54         }
55         return g;
56     }
57
58     public String getDatabaseName() {
59         return dbName;
60     }
61
62     public User[] getMembers(int offset, int count) {
63         GigiPreparedStatement gps = DatabaseConnection.getInstance().prepare("SELECT user FROM user_groups WHERE permission=? AND deleted is NULL LIMIT ?,?");
64         gps.setString(1, dbName);
65         gps.setInt(2, offset);
66         gps.setInt(3, count);
67         GigiResultSet grs = gps.executeQuery();
68         grs.last();
69         User[] users = new User[grs.getRow()];
70         int i = 0;
71         grs.beforeFirst();
72         while (grs.next()) {
73             users[i++] = User.getById(grs.getInt(1));
74         }
75         return users;
76     }
77 }