1 package org.cacert.gigi.util;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.User;
8 import org.cacert.gigi.database.DatabaseConnection;
11 public static void writeUserAgreement(int memid, String document,
12 String method, String comment, boolean active, int secmemid)
14 PreparedStatement q = DatabaseConnection
17 "insert into `user_agreements` set `memid`=?, `secmemid`=?,"
18 + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
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);
28 public static AssuranceResult checkAssuranceIsPossible(User assurer,
30 if (assurer.getId() == target.getId()) {
31 return AssuranceResult.CANNOT_ASSURE_SELF;
34 PreparedStatement ps = DatabaseConnection
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();
43 return AssuranceResult.ALREADY_ASSUREED;
46 if (!assurer.canAssure()) {
47 return AssuranceResult.CANNOT_ASSURE;
49 } catch (SQLException e) {
52 return AssuranceResult.ASSURANCE_SUCCEDED;
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 private final String message;
62 private AssuranceResult(String message) {
63 this.message = message;
65 public String getMessage() {
70 public synchronized static AssuranceResult assure(User assurer,
71 User target, int awarded, String location, String date)
73 AssuranceResult can = checkAssuranceIsPossible(assurer, target);
74 if (can != AssuranceResult.ASSURANCE_SUCCEDED) {
77 User u = new User(target.getId());
78 if (!u.equals(target)) {
79 return AssuranceResult.ASSUREE_CHANGED;
81 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
82 return AssuranceResult.POINTS_OUT_OF_RANGE;
85 PreparedStatement ps = DatabaseConnection
88 "INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
89 ps.setInt(1, assurer.getId());
90 ps.setInt(2, target.getId());
91 ps.setInt(3, awarded);
92 ps.setString(4, location);
93 ps.setString(5, date);
95 return AssuranceResult.ASSURANCE_SUCCEDED;