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