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