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, 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`=?");
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);
25 public static AssuranceResult checkAssuranceIsPossible(User assurer, User target) {
26 if (assurer.getId() == target.getId()) {
27 return AssuranceResult.CANNOT_ASSURE_SELF;
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();
37 return AssuranceResult.ALREADY_ASSUREED;
40 if (!assurer.canAssure()) {
41 return AssuranceResult.CANNOT_ASSURE;
43 } catch (SQLException e) {
46 return AssuranceResult.ASSURANCE_SUCCEDED;
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;
55 private AssuranceResult(String message) {
56 this.message = message;
59 public String getMessage() {
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) {
70 User u = new User(target.getId());
71 if (!u.equals(target)) {
72 return AssuranceResult.ASSUREE_CHANGED;
74 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
75 return AssuranceResult.POINTS_OUT_OF_RANGE;
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);
86 return AssuranceResult.ASSURANCE_SUCCEDED;