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.User;
12 import org.cacert.gigi.output.DateSelector;
16 public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active, int secmemid) throws SQLException {
17 PreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
19 q.setInt(2, secmemid);
20 q.setString(3, document);
21 q.setInt(4, active ? 1 : 0);
22 q.setString(5, method);
23 q.setString(6, comment);
27 public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
28 if (assurer.getId() == target.getId()) {
29 throw new GigiApiException("You cannot assure yourself.");
32 PreparedStatement 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 ResultSet 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.");
44 } catch (SQLException e) {
45 throw new GigiApiException(e);
49 public synchronized static void assure(User assurer, User target, int awarded, String location, String date) throws SQLException, GigiApiException {
50 GigiApiException gae = new GigiApiException();
52 if (date == null || date.equals("")) {
53 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
56 Date d = DateSelector.getDateFormat().parse(date);
57 if (d.getTime() > System.currentTimeMillis()) {
58 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
60 } catch (ParseException e) {
61 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
64 // check location, min 3 characters
65 if (location == null || location.equals("")) {
66 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
67 } else if (location.length() <= 2) {
68 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
72 checkAssuranceIsPossible(assurer, target);
73 } catch (GigiApiException e) {
77 User u = new User(target.getId());
78 if ( !u.equals(target)) {
79 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
81 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
82 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
84 if ( !gae.isEmpty()) {
88 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
89 ps.setInt(1, assurer.getId());
90 ps.setInt(2, target.getId());
91 ps.setInt(3, awarded);
92 ps.setString(4, location);
93 ps.setString(5, date);
95 assurer.invalidateMadeAssurances();
96 target.invalidateReceivedAssurances();