1 package org.cacert.gigi.util;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.text.ParseException;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.DatabaseConnection;
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(int memid, String document, String method, String comment, boolean active, int secmemid) throws SQLException {
18 PreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
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.");
33 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
34 ps.setInt(1, target.getId());
35 ps.setInt(2, assurer.getId());
36 ResultSet rs = ps.executeQuery();
39 throw new GigiApiException("You have already assured this member.");
42 if ( !assurer.canAssure()) {
43 throw new GigiApiException("You are not an assurer.");
45 } catch (SQLException e) {
46 throw new GigiApiException(e);
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 SQLException
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 SQLException, GigiApiException {
75 GigiApiException gae = new GigiApiException();
77 if (date == null || date.equals("")) {
78 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
81 Date d = DateSelector.getDateFormat().parse(date);
82 if (d.getTime() > System.currentTimeMillis()) {
83 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
85 } catch (ParseException e) {
86 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
89 // check location, min 3 characters
90 if (location == null || location.equals("")) {
91 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
92 } else if (location.length() <= 2) {
93 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
97 checkAssuranceIsPossible(assurer, assuree);
98 } catch (GigiApiException e) {
102 if ( !assuree.getName().equals(assureeName) || !assuree.getDob().equals(dob)) {
103 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
105 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
106 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
108 if ( !gae.isEmpty()) {
112 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
113 ps.setInt(1, assurer.getId());
114 ps.setInt(2, assuree.getId());
115 ps.setInt(3, awarded);
116 ps.setString(4, location);
117 ps.setString(5, date);
119 assurer.invalidateMadeAssurances();
120 assuree.invalidateReceivedAssurances();