1 package org.cacert.gigi.util;
3 import java.text.ParseException;
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;
17 public static void writeUserAgreement(User member, 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, member.getId());
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);
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.");
32 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted` IS NULL");
33 ps.setInt(1, target.getId());
34 ps.setInt(2, assurer.getId());
35 GigiResultSet rs = ps.executeQuery();
38 throw new GigiApiException("You have already assured this member.");
41 if ( !assurer.canAssure()) {
42 throw new GigiApiException("You are not an assurer.");
46 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
48 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
51 * This method assures another user.
53 * @see User#canAssure() (for assurer)
54 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
56 * the person that wants to assure
58 * the person that should be assured
60 * the Name that was personally verified
62 * the Date of birth that the assurer verified
64 * the points that should be awarded in total
66 * the location where the assurance took place
68 * the date when the assurance took place
69 * @throws GigiApiException
70 * if the assurance fails (for various reasons)
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."));
77 if (assurer.isInGroup(ASSURER_BLOCKED)) {
78 gae.mergeInto(new GigiApiException("The assurer is blocked."));
80 if ( !gae.isEmpty()) {
83 if (date == null || date.equals("")) {
84 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
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."));
91 } catch (ParseException e) {
92 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
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."));
103 checkAssuranceIsPossible(assurer, assuree);
104 } catch (GigiApiException e) {
108 if ( !assuree.getName().equals(assureeName) || !assuree.getDob().equals(dob)) {
109 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
111 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
112 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
114 if ( !gae.isEmpty()) {
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);
125 assurer.invalidateMadeAssurances();
126 assuree.invalidateReceivedAssurances();