]> WPIA git - gigi.git/commitdiff
Add assurance functions.
authorFelix Dörre <felix@dogcraft.de>
Fri, 27 Jun 2014 16:05:54 +0000 (18:05 +0200)
committerFelix Dörre <felix@dogcraft.de>
Fri, 27 Jun 2014 16:05:54 +0000 (18:05 +0200)
src/org/cacert/gigi/util/Notary.java

index 92b231da5b3bf82abb266dde9fddb32aba629b88..657264a6e7c90b8dbf589aee579c0136373e6f49 100644 (file)
@@ -1,8 +1,11 @@
 package org.cacert.gigi.util;
 
 package org.cacert.gigi.util;
 
+import java.io.PrintWriter;
 import java.sql.PreparedStatement;
 import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 
 import java.sql.SQLException;
 
+import org.cacert.gigi.User;
 import org.cacert.gigi.database.DatabaseConnection;
 
 public class Notary {
 import org.cacert.gigi.database.DatabaseConnection;
 
 public class Notary {
@@ -22,4 +25,67 @@ public class Notary {
                q.setString(6, comment);
                q.execute();
        }
                q.setString(6, comment);
                q.execute();
        }
+
+       public static boolean checkAssuranceIsPossible(User assurer, User target,
+                       PrintWriter errOut) {
+               if (assurer.getId() == target.getId()) {
+                       if (errOut != null) {
+                               errOut.println("Cannot assure myself.");
+                       }
+                       return false;
+               }
+               try {
+                       PreparedStatement ps = DatabaseConnection
+                                       .getInstance()
+                                       .prepare(
+                                                       "SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
+                       ps.setInt(1, target.getId());
+                       ps.setInt(2, assurer.getId());
+                       ResultSet rs = ps.executeQuery();
+                       if (rs.next()) {
+                               if (errOut != null) {
+                                       errOut.println("You already assured this person.");
+                               }
+                               rs.close();
+                               return false;
+                       }
+                       rs.close();
+                       if (!assurer.canAssure()) {
+                               if (errOut != null) {
+                                       errOut.println("You cannot assure.");
+                               }
+                               return false;
+                       }
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+               return true;
+       }
+
+       public synchronized static boolean assure(User assurer, User target,
+                       int awarded, String location, String date) throws SQLException {
+               if (!checkAssuranceIsPossible(assurer, target, null)) {
+                       return false;
+               }
+               User u = new User(target.getId());
+               if (!u.equals(target)) {
+                       return false;
+               }
+               System.out.println("Would now assure.");
+               if (awarded > assurer.getMaxAssurePoints()) {
+                       return false;
+               }
+
+               PreparedStatement ps = DatabaseConnection
+                               .getInstance()
+                               .prepare(
+                                               "INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
+               ps.setInt(1, assurer.getId());
+               ps.setInt(2, target.getId());
+               ps.setInt(3, awarded);
+               ps.setString(4, location);
+               ps.setString(5, date);
+               ps.execute();
+               return true;
+       }
 }
 }