]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
UPD: Change the assurance API (and document it)
[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.Name;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.output.DateSelector;
14
15 public class Notary {
16
17     public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active, int secmemid) throws SQLException {
18         PreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
19         q.setInt(1, memid);
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);
25         q.execute();
26     }
27
28     public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
29         if (assurer.getId() == target.getId()) {
30             throw new GigiApiException("You cannot assure yourself.");
31         }
32         try {
33             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
34             ps.setInt(1, target.getId());
35             ps.setInt(2, assurer.getId());
36             ResultSet rs = ps.executeQuery();
37             if (rs.next()) {
38                 rs.close();
39                 throw new GigiApiException("You have already assured this member.");
40             }
41             rs.close();
42             if ( !assurer.canAssure()) {
43                 throw new GigiApiException("You are not an assurer.");
44             }
45         } catch (SQLException e) {
46             throw new GigiApiException(e);
47         }
48     }
49
50     /**
51      * This method assures another user.
52      * 
53      * @see User#canAssure() (for assurer)
54      * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
55      * @param assurer
56      *            the person that wants to assure
57      * @param assuree
58      *            the person that should be assured
59      * @param assureeName
60      *            the Name that was personally verified
61      * @param dob
62      *            the Date of birth that the assurer verified
63      * @param awarded
64      *            the points that should be awarded in total
65      * @param location
66      *            the location where the assurance took place
67      * @param date
68      *            the date when the assurance took place
69      * @throws SQLException
70      *             if SQL goes wrong
71      * @throws GigiApiException
72      *             if the assurance fails (for various reasons)
73      */
74     public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date) throws SQLException, GigiApiException {
75         GigiApiException gae = new GigiApiException();
76
77         if (date == null || date.equals("")) {
78             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
79         } else {
80             try {
81                 Date d = DateSelector.getDateFormat().parse(date);
82                 if (d.getTime() > System.currentTimeMillis()) {
83                     gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
84                 }
85             } catch (ParseException e) {
86                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
87             }
88         }
89         // check location, min 3 characters
90         if (location == null || location.equals("")) {
91             gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
92         } else if (location.length() <= 2) {
93             gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
94         }
95
96         try {
97             checkAssuranceIsPossible(assurer, assuree);
98         } catch (GigiApiException e) {
99             gae.mergeInto(e);
100         }
101
102         if ( !assuree.getName().equals(assureeName) || !assuree.getDob().equals(dob)) {
103             gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
104         }
105         if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
106             gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
107         }
108         if ( !gae.isEmpty()) {
109             throw gae;
110         }
111
112         PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
113         ps.setInt(1, assurer.getId());
114         ps.setInt(2, assuree.getId());
115         ps.setInt(3, awarded);
116         ps.setString(4, location);
117         ps.setString(5, date);
118         ps.execute();
119         assurer.invalidateMadeAssurances();
120         assuree.invalidateReceivedAssurances();
121     }
122 }