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