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;
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`=?");
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);
23 public static AssuranceResult checkAssuranceIsPossible(User assurer, User target) {
24 if (assurer.getId() == target.getId()) {
25 return AssuranceResult.CANNOT_ASSURE_SELF;
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();
34 return AssuranceResult.ALREADY_ASSUREED;
37 if ( !assurer.canAssure()) {
38 return AssuranceResult.CANNOT_ASSURE;
40 } catch (SQLException e) {
43 return AssuranceResult.ASSURANCE_SUCCEDED;
46 public enum AssuranceResult {
47 CANNOT_ASSURE("You cannot assure."), 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.");
49 private final String message;
51 private AssuranceResult(String message) {
52 this.message = message;
55 public String getMessage() {
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) {
65 User u = new User(target.getId());
66 if ( !u.equals(target)) {
67 return AssuranceResult.ASSUREE_CHANGED;
69 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
70 return AssuranceResult.POINTS_OUT_OF_RANGE;
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);
80 return AssuranceResult.ASSURANCE_SUCCEDED;