]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Group.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / dbObjects / Group.java
1 package club.wpia.gigi.dbObjects;
2
3 import club.wpia.gigi.database.DBEnum;
4 import club.wpia.gigi.database.GigiPreparedStatement;
5 import club.wpia.gigi.database.GigiResultSet;
6 import club.wpia.gigi.output.template.Outputable;
7 import club.wpia.gigi.output.template.TranslateCommand;
8
9 public enum Group implements DBEnum {
10     SUPPORTER("supporter", "supporter", true, false, true), //
11     ARBITRATOR("arbitrator", "arbitrator", true, false, true), //
12     BLOCKEDASSURER("blockedassurer", "may not verify", true, false, false), //
13     BLOCKEDASSUREE("blockedassuree", "may not be verified", true, false, false), //
14     BLOCKEDLOGIN("blockedlogin", "may not login", true, false, false), //
15     BLOCKEDCERT("blockedcert", "may not issue certificates", true, false, false), //
16     TTP_ASSURER("ttp-assurer", "may verify via TTP", true, false, true), //
17     TTP_APPLICANT("ttp-applicant", "requests to be verified via ttp", false, true, false), //
18     CODESIGNING("codesigning", "may issue codesigning certificates", true, false, false), //
19     ORGASSURER("orgassurer", "may verify organisations", true, false, true), //
20     NUCLEUS_ASSURER("nucleus-assurer", "may enter nucleus verifications", true, false, true), //
21     LOCATE_AGENT("locate-agent", "wants access to the locate agent system", false, true, false), //
22     VERIFY_NOTIFICATION("verify-notification", "wants to receive an email notification for any Verification they enter", false, true, false);
23
24     private final String dbName;
25
26     private final TranslateCommand tc;
27
28     private final boolean managedBySupport;
29
30     private final boolean managedByUser;
31
32     private final boolean isSelfViewable;
33
34     /**
35      * Creates a new group. Users can join this group or be put into it
36      * (depending on the value of <code>managedBySupport</code>).
37      * 
38      * @param name
39      *            name of the group, used in database
40      * @param display
41      *            text displayed to user
42      * @param managedBySupport
43      *            true if flag is handled by support, false if handled by user
44      * @param isSelfViewable
45      *            true iff user should be able to see others in the same group
46      */
47     private Group(String name, String display, boolean managedBySupport, boolean managedByUser, boolean isSelfViewable) {
48         dbName = name;
49         tc = new TranslateCommand(display);
50         if (managedByUser && managedBySupport) {
51             throw new IllegalArgumentException("We do not allow groups to be user and support managable.");
52         }
53         if (managedByUser && isSelfViewable) {
54             throw new IllegalArgumentException("We do not allow groups to be self-viewable and managable by user.");
55         }
56         this.managedByUser = managedByUser;
57         this.managedBySupport = managedBySupport;
58         this.isSelfViewable = isSelfViewable;
59     }
60
61     public static Group getByString(String name) {
62         return valueOf(name.toUpperCase().replace('-', '_'));
63     }
64
65     public boolean isManagedBySupport() {
66         return managedBySupport;
67     }
68
69     public boolean isManagedByUser() {
70         return managedByUser;
71     }
72
73     public boolean isSelfViewable() {
74         return isSelfViewable;
75     }
76
77     public User[] getMembers(int offset, int count) {
78         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `user` FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL OFFSET ? LIMIT ?", true)) {
79             gps.setEnum(1, this);
80             gps.setInt(2, offset);
81             gps.setInt(3, count);
82             GigiResultSet grs = gps.executeQuery();
83             grs.last();
84             User[] users = new User[grs.getRow()];
85             int i = 0;
86             grs.beforeFirst();
87             while (grs.next()) {
88                 users[i++] = User.getById(grs.getInt(1));
89             }
90             return users;
91         }
92     }
93
94     public int getMemberCount() {
95         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT COUNT(`user`) FROM `user_groups` WHERE `permission`=?::`userGroup` AND `deleted` IS NULL", true)) {
96             gps.setEnum(1, this);
97             GigiResultSet grs = gps.executeQuery();
98             if ( !grs.next()) {
99                 return 0;
100             }
101             return grs.getInt(1);
102         }
103     }
104
105     public Outputable getName() {
106         return tc;
107     }
108
109     @Override
110     public String getDBName() {
111         return dbName;
112     }
113 }