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