From: Felix Dörre Date: Fri, 27 Jun 2014 16:05:54 +0000 (+0200) Subject: Add assurance functions. X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=7f4a02fc53f56a5507d1d059c5c0636ed977ae62 Add assurance functions. --- diff --git a/src/org/cacert/gigi/util/Notary.java b/src/org/cacert/gigi/util/Notary.java index 92b231da..657264a6 100644 --- a/src/org/cacert/gigi/util/Notary.java +++ b/src/org/cacert/gigi/util/Notary.java @@ -1,8 +1,11 @@ package org.cacert.gigi.util; +import java.io.PrintWriter; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import org.cacert.gigi.User; import org.cacert.gigi.database.DatabaseConnection; public class Notary { @@ -22,4 +25,67 @@ public class Notary { 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; + } }