1 package org.cacert.gigi.util;
3 import java.text.ParseException;
4 import java.util.Calendar;
6 import java.util.GregorianCalendar;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.dbObjects.Group;
12 import org.cacert.gigi.dbObjects.Name;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.output.DateSelector;
18 public static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
19 try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
20 q.setInt(1, member.getId());
21 q.setInt(2, secmemid);
22 q.setString(3, document);
23 q.setBoolean(4, active);
24 q.setString(5, method);
25 q.setString(6, comment);
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.");
34 try (GigiPreparedStatement ps = new GigiPreparedStatement("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();
40 throw new GigiApiException("You have already assured this member.");
43 if ( !assurer.canAssure()) {
44 throw new GigiApiException("You are not an assurer.");
48 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
50 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
53 * This method assures another user.
55 * @see User#canAssure() (for assurer)
56 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
58 * the person that wants to assure
60 * the person that should be assured
62 * the Name that was personally verified
64 * the Date of birth that the assurer verified
66 * the points that should be awarded in total
68 * the location where the assurance took place
70 * the date when the assurance took place
71 * @throws GigiApiException
72 * if the assurance fails (for various reasons)
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."));
79 if (assurer.isInGroup(ASSURER_BLOCKED)) {
80 gae.mergeInto(new GigiApiException("The assurer is blocked."));
82 if ( !gae.isEmpty()) {
85 if (date == null || date.equals("")) {
86 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
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."));
96 } catch (ParseException e) {
97 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
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."));
108 checkAssuranceIsPossible(assurer, assuree);
109 } catch (GigiApiException e) {
113 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
114 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
116 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
117 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
119 if ( !gae.isEmpty()) {
123 try (GigiPreparedStatement ps = new GigiPreparedStatement("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);
131 assurer.invalidateMadeAssurances();
132 assuree.invalidateReceivedAssurances();