1 package club.wpia.gigi.dbObjects;
3 import java.io.IOException;
5 import java.util.HashMap;
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;
15 public class Contract {
17 public enum ContractType implements DBEnum {
18 RA_AGENT_CONTRACT("RA Agent Contract"), ORG_RA_AGENT_CONTRACT("Org RA Agent Contract");
20 private final String description;
22 private ContractType(String description) {
23 this.description = description;
26 public String getDBName() {
31 private final ContractType contractType;
33 private final User user;
35 private String agentname = "";
37 private String token = "";
39 private Date dateSigned = null;
41 private Date dateRevoked = null;
43 private int contractID;
45 private static final MailTemplate contractNotice = new MailTemplate(Contract.class.getResource("ContractNotice.templ"));
47 public Contract(User u, ContractType contractType) throws GigiApiException {
48 this.contractType = contractType;
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();
55 throw new GigiApiException("Contract exists");
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);
72 contractID = ps.lastInsertId();
73 dateSigned = new Date();
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");
82 contractNotice.sendMail(l, vars, user.getEmail());
83 } catch (IOException e) {
84 throw new GigiApiException("Sending the notification mail failed.");
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);
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");
102 contractNotice.sendMail(l, vars, user.getEmail());
103 } catch (IOException e) {
104 throw new GigiApiException("Sending the notification mail failed.");
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");
118 public static Contract getById(int id) {
119 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT * FROM `user_contracts` WHERE `id` = ?")) {
121 GigiResultSet rs = ps.executeQuery();
126 Contract c = new Contract(rs);
136 public Date getDateSigned() {
140 public Date getDateRevoked() {
144 public String getRAAgentName() {
148 public ContractType getContractType() {
152 public String getToken() {
156 public static boolean hasSignedContract(User u, Contract.ContractType ct) {
157 return getContractByUser(u, ct) != null;
160 public static Contract getRAAgentContractByUser(User u) {
161 return getContractByUser(u, Contract.ContractType.RA_AGENT_CONTRACT);
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();
172 Contract c = new Contract(rs);
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();
184 Contract c = new Contract(rs);