]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
Prohibit negative points in Bussiness-Logic
[gigi.git] / src / org / cacert / gigi / util / Notary.java
1 package org.cacert.gigi.util;
2
3 import java.io.PrintWriter;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import org.cacert.gigi.User;
9 import org.cacert.gigi.database.DatabaseConnection;
10
11 public class Notary {
12         public static void writeUserAgreement(int memid, String document,
13                         String method, String comment, boolean active, int secmemid)
14                         throws SQLException {
15                 PreparedStatement q = DatabaseConnection
16                                 .getInstance()
17                                 .prepare(
18                                                 "insert into `user_agreements` set `memid`=?, `secmemid`=?,"
19                                                                 + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
20                 q.setInt(1, memid);
21                 q.setInt(2, secmemid);
22                 q.setString(3, document);
23                 q.setInt(4, active ? 1 : 0);
24                 q.setString(5, method);
25                 q.setString(6, comment);
26                 q.execute();
27         }
28
29         public static boolean checkAssuranceIsPossible(User assurer, User target,
30                         PrintWriter errOut) {
31                 if (assurer.getId() == target.getId()) {
32                         if (errOut != null) {
33                                 errOut.println("Cannot assure myself.");
34                         }
35                         return false;
36                 }
37                 try {
38                         PreparedStatement ps = DatabaseConnection
39                                         .getInstance()
40                                         .prepare(
41                                                         "SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
42                         ps.setInt(1, target.getId());
43                         ps.setInt(2, assurer.getId());
44                         ResultSet rs = ps.executeQuery();
45                         if (rs.next()) {
46                                 if (errOut != null) {
47                                         errOut.println("You already assured this person.");
48                                 }
49                                 rs.close();
50                                 return false;
51                         }
52                         rs.close();
53                         if (!assurer.canAssure()) {
54                                 if (errOut != null) {
55                                         errOut.println("You cannot assure.");
56                                 }
57                                 return false;
58                         }
59                 } catch (SQLException e) {
60                         e.printStackTrace();
61                 }
62                 return true;
63         }
64
65         public synchronized static boolean assure(User assurer, User target,
66                         int awarded, String location, String date) throws SQLException {
67                 if (!checkAssuranceIsPossible(assurer, target, null)) {
68                         return false;
69                 }
70                 User u = new User(target.getId());
71                 if (!u.equals(target)) {
72                         return false;
73                 }
74                 System.out.println("Would now assure.");
75                 if (awarded > assurer.getMaxAssurePoints() || awarded < 0) {
76                         return false;
77                 }
78
79                 PreparedStatement ps = DatabaseConnection
80                                 .getInstance()
81                                 .prepare(
82                                                 "INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
83                 ps.setInt(1, assurer.getId());
84                 ps.setInt(2, target.getId());
85                 ps.setInt(3, awarded);
86                 ps.setString(4, location);
87                 ps.setString(5, date);
88                 ps.execute();
89                 return true;
90         }
91 }