]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
UPD: cleanup notary-assurance API
[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 import java.text.ParseException;
7 import java.util.Date;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.DatabaseConnection;
11 import org.cacert.gigi.dbObjects.User;
12 import org.cacert.gigi.output.DateSelector;
13
14 public class Notary {
15
16     public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active, int secmemid) throws SQLException {
17         PreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
18         q.setInt(1, memid);
19         q.setInt(2, secmemid);
20         q.setString(3, document);
21         q.setInt(4, active ? 1 : 0);
22         q.setString(5, method);
23         q.setString(6, comment);
24         q.execute();
25     }
26
27     public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
28         if (assurer.getId() == target.getId()) {
29             throw new GigiApiException("You cannot assure yourself.");
30         }
31         try {
32             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
33             ps.setInt(1, target.getId());
34             ps.setInt(2, assurer.getId());
35             ResultSet rs = ps.executeQuery();
36             if (rs.next()) {
37                 rs.close();
38                 throw new GigiApiException("You have already assured this member.");
39             }
40             rs.close();
41             if ( !assurer.canAssure()) {
42                 throw new GigiApiException("You are not an assurer.");
43             }
44         } catch (SQLException e) {
45             throw new GigiApiException(e);
46         }
47     }
48
49     public synchronized static void assure(User assurer, User target, int awarded, String location, String date) throws SQLException, GigiApiException {
50         GigiApiException gae = new GigiApiException();
51
52         if (date == null || date.equals("")) {
53             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
54         } else {
55             try {
56                 Date d = DateSelector.getDateFormat().parse(date);
57                 if (d.getTime() > System.currentTimeMillis()) {
58                     gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
59                 }
60             } catch (ParseException e) {
61                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
62             }
63         }
64         // check location, min 3 characters
65         if (location == null || location.equals("")) {
66             gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
67         } else if (location.length() <= 2) {
68             gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
69         }
70
71         try {
72             checkAssuranceIsPossible(assurer, target);
73         } catch (GigiApiException e) {
74             gae.mergeInto(e);
75         }
76
77         User u = new User(target.getId());
78         if ( !u.equals(target)) {
79             gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
80         }
81         if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
82             gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
83         }
84         if ( !gae.isEmpty()) {
85             throw gae;
86         }
87
88         PreparedStatement ps = DatabaseConnection.getInstance().prepare("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);
94         ps.execute();
95         assurer.invalidateMadeAssurances();
96         target.invalidateReceivedAssurances();
97     }
98 }