From: Janis Streib Date: Sat, 23 Aug 2014 23:24:24 +0000 (+0200) Subject: ADD: Assurance fetch X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=85a496e506f437afe7be233f624c0471b6c9bb1d;hp=736968682d49753f1cfb9cbf0970fe95435f0658 ADD: Assurance fetch --- diff --git a/src/org/cacert/gigi/Assurance.java b/src/org/cacert/gigi/Assurance.java new file mode 100644 index 00000000..973f0ee8 --- /dev/null +++ b/src/org/cacert/gigi/Assurance.java @@ -0,0 +1,61 @@ +package org.cacert.gigi; + +import java.sql.ResultSet; +import java.sql.SQLException; + + +public class Assurance { + private int id; + + private User from; + + private User to; + + private String location; + + private String method; + + private int points; + + private String date; + + public Assurance(ResultSet result) throws SQLException { + super(); + this.id = result.getInt("id"); + this.from = User.getById(result.getInt("from")); + this.to = User.getById(result.getInt("to")); + this.location = result.getString("location"); + this.method = result.getString("method"); + this.points = result.getInt("points"); + this.date = result.getString("date"); + } + + public User getFrom() { + return from; + } + + public int getId() { + return id; + } + + public String getLocation() { + return location; + } + + public int getPoints() { + return points; + } + + public User getTo() { + return to; + } + + public String getMethod() { + return method; + } + + public String getDate() { + return date; + } + +} diff --git a/src/org/cacert/gigi/User.java b/src/org/cacert/gigi/User.java index eebf9317..fd1989b2 100644 --- a/src/org/cacert/gigi/User.java +++ b/src/org/cacert/gigi/User.java @@ -20,6 +20,8 @@ public class User { private String email; + private Assurance[] receivedAssurances, madeAssurances; + public User(int id) { this.id = id; try { @@ -377,4 +379,48 @@ public class User { } throw new GigiApiException("Email not one of user's email addresses."); } + + public Assurance[] getReceivedAssurances() throws SQLException { + if (receivedAssurances == null) { + PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0"); + query.setInt(1, getId()); + ResultSet res = query.executeQuery(); + res.last(); + Assurance[] assurances = new Assurance[res.getRow()]; + res.beforeFirst(); + for (int i = 0; i < assurances.length; i++) { + res.next(); + assurances[i] = new Assurance(res); + } + this.receivedAssurances = assurances; + return assurances; + } + return receivedAssurances; + } + + public Assurance[] getMadeAssurances() throws SQLException { + if (madeAssurances == null) { + PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0"); + query.setInt(1, getId()); + ResultSet res = query.executeQuery(); + res.last(); + Assurance[] assurances = new Assurance[res.getRow()]; + res.beforeFirst(); + for (int i = 0; i < assurances.length; i++) { + res.next(); + assurances[i] = new Assurance(res); + } + this.madeAssurances = assurances; + return assurances; + } + return madeAssurances; + } + + public void invalidateMadeAssurances() { + madeAssurances = null; + } + + public void invalidateReceivedAssurances() { + receivedAssurances = null; + } }