]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
[EMPTY] Formatting with configured formatter.
[gigi.git] / src / org / cacert / gigi / util / Notary.java
1 package org.cacert.gigi.util;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7 import org.cacert.gigi.User;
8 import org.cacert.gigi.database.DatabaseConnection;
9
10 public class Notary {
11         public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active,
12                 int secmemid) throws SQLException {
13                 PreparedStatement q = DatabaseConnection.getInstance().prepare(
14                         "insert into `user_agreements` set `memid`=?, `secmemid`=?,"
15                                 + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
16                 q.setInt(1, memid);
17                 q.setInt(2, secmemid);
18                 q.setString(3, document);
19                 q.setInt(4, active ? 1 : 0);
20                 q.setString(5, method);
21                 q.setString(6, comment);
22                 q.execute();
23         }
24
25         public static AssuranceResult checkAssuranceIsPossible(User assurer, User target) {
26                 if (assurer.getId() == target.getId()) {
27                         return AssuranceResult.CANNOT_ASSURE_SELF;
28                 }
29                 try {
30                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
31                                 "SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
32                         ps.setInt(1, target.getId());
33                         ps.setInt(2, assurer.getId());
34                         ResultSet rs = ps.executeQuery();
35                         if (rs.next()) {
36                                 rs.close();
37                                 return AssuranceResult.ALREADY_ASSUREED;
38                         }
39                         rs.close();
40                         if (!assurer.canAssure()) {
41                                 return AssuranceResult.CANNOT_ASSURE;
42                         }
43                 } catch (SQLException e) {
44                         e.printStackTrace();
45                 }
46                 return AssuranceResult.ASSURANCE_SUCCEDED;
47         }
48
49         public enum AssuranceResult {
50                 CANNOT_ASSURE("You cannot assure."), ALREADY_ASSUREED("You already assured this person."), CANNOT_ASSURE_SELF(
51                         "Cannot assure myself."), ASSURANCE_SUCCEDED(""), ASSUREE_CHANGED(
52                         "Person details changed. Please start over again."), POINTS_OUT_OF_RANGE("Points out of range.");
53                 private final String message;
54
55                 private AssuranceResult(String message) {
56                         this.message = message;
57                 }
58
59                 public String getMessage() {
60                         return message;
61                 }
62         }
63
64         public synchronized static AssuranceResult assure(User assurer, User target, int awarded, String location,
65                 String date) throws SQLException {
66                 AssuranceResult can = checkAssuranceIsPossible(assurer, target);
67                 if (can != AssuranceResult.ASSURANCE_SUCCEDED) {
68                         return can;
69                 }
70                 User u = new User(target.getId());
71                 if (!u.equals(target)) {
72                         return AssuranceResult.ASSUREE_CHANGED;
73                 }
74                 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
75                         return AssuranceResult.POINTS_OUT_OF_RANGE;
76                 }
77
78                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
79                         "INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
80                 ps.setInt(1, assurer.getId());
81                 ps.setInt(2, target.getId());
82                 ps.setInt(3, awarded);
83                 ps.setString(4, location);
84                 ps.setString(5, date);
85                 ps.execute();
86                 return AssuranceResult.ASSURANCE_SUCCEDED;
87         }
88 }