]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
Convert strange flags to Groups.
[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.Group;
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) {
18         GigiPreparedStatement 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         GigiPreparedStatement 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         GigiResultSet 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     }
45
46     public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
47
48     public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
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 GigiApiException
70      *             if the assurance fails (for various reasons)
71      */
72     public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date) throws GigiApiException {
73         GigiApiException gae = new GigiApiException();
74         if (assuree.isInGroup(ASSUREE_BLOCKED)) {
75             gae.mergeInto(new GigiApiException("The assuree is blocked."));
76         }
77         if (assurer.isInGroup(ASSURER_BLOCKED)) {
78             gae.mergeInto(new GigiApiException("The assurer is blocked."));
79         }
80         if ( !gae.isEmpty()) {
81             throw gae;
82         }
83         if (date == null || date.equals("")) {
84             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
85         } else {
86             try {
87                 Date d = DateSelector.getDateFormat().parse(date);
88                 if (d.getTime() > System.currentTimeMillis()) {
89                     gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
90                 }
91             } catch (ParseException e) {
92                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
93             }
94         }
95         // check location, min 3 characters
96         if (location == null || location.equals("")) {
97             gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
98         } else if (location.length() <= 2) {
99             gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
100         }
101
102         try {
103             checkAssuranceIsPossible(assurer, assuree);
104         } catch (GigiApiException e) {
105             gae.mergeInto(e);
106         }
107
108         if ( !assuree.getName().equals(assureeName) || !assuree.getDob().equals(dob)) {
109             gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
110         }
111         if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
112             gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
113         }
114         if ( !gae.isEmpty()) {
115             throw gae;
116         }
117
118         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
119         ps.setInt(1, assurer.getId());
120         ps.setInt(2, assuree.getId());
121         ps.setInt(3, awarded);
122         ps.setString(4, location);
123         ps.setString(5, date);
124         ps.execute();
125         assurer.invalidateMadeAssurances();
126         assuree.invalidateReceivedAssurances();
127     }
128 }