]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Contract.java
add: handling of RA Agent Contract
[gigi.git] / src / club / wpia / gigi / dbObjects / Contract.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.Date;
5 import java.util.HashMap;
6
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.database.DBEnum;
9 import club.wpia.gigi.database.GigiPreparedStatement;
10 import club.wpia.gigi.database.GigiResultSet;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.MailTemplate;
13 import club.wpia.gigi.util.RandomToken;
14
15 public class Contract {
16
17     public enum ContractType implements DBEnum {
18         RA_AGENT_CONTRACT("RA Agent Contract"), ORG_RA_AGENT_CONTRACT("Org RA Agent Contract");
19
20         private final String description;
21
22         private ContractType(String description) {
23             this.description = description;
24         }
25
26         public String getDBName() {
27             return description;
28         }
29     }
30
31     private final ContractType contractType;
32
33     private final User user;
34
35     private String agentname = "";
36
37     private String token = "";
38
39     private Date dateSigned = null;
40
41     private Date dateRevoked = null;
42
43     private int contractID;
44
45     private static final MailTemplate contractNotice = new MailTemplate(Contract.class.getResource("ContractNotice.templ"));
46
47     public Contract(User u, ContractType contractType) throws GigiApiException {
48         this.contractType = contractType;
49         this.user = u;
50         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM `user_contracts` WHERE `memid`=? AND `document`=?::`contractType` and `daterevoked` IS NULL ORDER BY `datesigned` DESC LIMIT 1")) {
51             query.setInt(1, user.getId());
52             query.setEnum(2, contractType);
53             GigiResultSet rs = query.executeQuery();
54             if (rs.next()) {
55                 throw new GigiApiException("Contract exists");
56             } else {
57                 signContract();
58             }
59         }
60
61     }
62
63     private void signContract() throws GigiApiException {
64         agentname = user.getPreferredName().toString();
65         token = RandomToken.generateToken(32);
66         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_contracts` SET `memid`=?, `token`=?, `document`=?::`contractType`,`agentname`=?")) {
67             ps.setInt(1, user.getId());
68             ps.setString(2, token);
69             ps.setEnum(3, this.contractType);
70             ps.setString(4, agentname);
71             ps.execute();
72             contractID = ps.lastInsertId();
73             dateSigned = new Date();
74
75             HashMap<String, Object> vars = new HashMap<>();
76             Language l = Language.getInstance(user.getPreferredLocale());
77             vars.put("user", agentname);
78             vars.put("actionsubject", "Signing");
79             vars.put("actionbody", "signed");
80
81             try {
82                 contractNotice.sendMail(l, vars, user.getEmail());
83             } catch (IOException e) {
84                 throw new GigiApiException("Sending the notification mail failed.");
85             }
86         }
87     }
88
89     public void revokeContract() throws GigiApiException {
90         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `user_contracts` SET `daterevoked`=NOW() WHERE `id`=?")) {
91             ps.setInt(1, contractID);
92             ps.execute();
93         }
94         dateRevoked = new Date();
95         HashMap<String, Object> vars = new HashMap<>();
96         Language l = Language.getInstance(user.getPreferredLocale());
97         vars.put("user", user.getPreferredName());
98         vars.put("actionsubject", "Revoking");
99         vars.put("actionbody", "revoked");
100
101         try {
102             contractNotice.sendMail(l, vars, user.getEmail());
103         } catch (IOException e) {
104             throw new GigiApiException("Sending the notification mail failed.");
105         }
106     }
107
108     private Contract(GigiResultSet rs) {
109         contractID = rs.getInt("id");
110         user = User.getById(rs.getInt("memid"));
111         token = rs.getString("token");
112         contractType = ContractType.valueOf(rs.getString("document").toUpperCase().replace(" ", "_"));
113         dateSigned = rs.getDate("datesigned");
114         dateRevoked = rs.getDate("daterevoked");
115         agentname = rs.getString("agentname");
116     }
117
118     public static Contract getById(int id) {
119         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT * FROM `user_contracts` WHERE `id` = ?")) {
120             ps.setInt(1, id);
121             GigiResultSet rs = ps.executeQuery();
122             if ( !rs.next()) {
123                 return null;
124             }
125
126             Contract c = new Contract(rs);
127
128             return c;
129         }
130     }
131
132     public int getID() {
133         return contractID;
134     }
135
136     public Date getDateSigned() {
137         return dateSigned;
138     }
139
140     public Date getDateRevoked() {
141         return dateRevoked;
142     }
143
144     public String getRAAgentName() {
145         return agentname;
146     }
147
148     public ContractType getContractType() {
149         return contractType;
150     }
151
152     public String getToken() {
153         return token;
154     }
155
156     public static boolean hasSignedContract(User u, Contract.ContractType ct) {
157         return getContractByUser(u, ct) != null;
158     }
159
160     public static Contract getRAAgentContractByUser(User u) {
161         return getContractByUser(u, Contract.ContractType.RA_AGENT_CONTRACT);
162     }
163
164     public static Contract getContractByUser(User u, ContractType ct) {
165         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM `user_contracts` WHERE `memid`=? AND `document`=?::`contractType` and `daterevoked` IS NULL ORDER BY `datesigned` DESC LIMIT 1")) {
166             query.setInt(1, u.getId());
167             query.setEnum(2, ct);
168             GigiResultSet rs = query.executeQuery();
169             if ( !rs.next()) {
170                 return null;
171             }
172             Contract c = new Contract(rs);
173             return c;
174         }
175     }
176
177     public static Contract getRAAgentContractByToken(String token) throws GigiApiException {
178         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM `user_contracts` WHERE `token`=? LIMIT 1")) {
179             query.setString(1, token);
180             GigiResultSet rs = query.executeQuery();
181             if ( !rs.next()) {
182                 return null;
183             }
184             Contract c = new Contract(rs);
185             return c;
186         }
187     }
188 }