Skip to content

Commit

Permalink
max_user_questions. Guess I'm done for tonight.
Browse files Browse the repository at this point in the history
The concept behing my notification queue works, now it just needs to get information needed to compose a summary email.
That's where the database is giving me trouble. I reduced the number of queries it needs to run, but haven't been able to test yet.
We'll see if it works later.  ¯\_(ツ)_/¯
  • Loading branch information
arc12012 committed Apr 19, 2017
1 parent 791e43d commit c474ccc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion WebContent/html/webpages/redirect/ticketAdminRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if(request.getParameter("approve") != null){
boolean notificationPreferences = true; //TODO get real preference
if(notificationPreferences){
NotificationQueue q = new NotificationQueue(client,"ticketConfirmations");
q.run();
q.start();
}
}
//reject form was submitted
Expand Down
30 changes: 24 additions & 6 deletions src/database/TicketQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.*;
import java.util.Date;
import entities.Ticket;
import entities.User;

public class TicketQueries {

Expand Down Expand Up @@ -78,11 +79,15 @@ public static void acceptTicket(int ticketid, int deviceid, int locationid, int
Connection connect = DriverManager.getConnection(database, user, password);
Statement stmt = connect.createStatement();
long milliseconds = new java.util.Date().getTime();
stmt.executeUpdate("UPDATE ticket, devices SET ticket.Status = \"Approved\", devices.Ticket_ID = " + ticketid + ", devices.Status = \"Shipped\", devices.Renter = " + sso + ", devices.Location = " + locationid + " WHERE ticket.Ticket_ID = " + ticketid + " AND devices.Device_ID = " + deviceid);
String query = "UPDATE ticket, devices SET ticket.Status = \"Approved\""
+", ticket.Status_Date_Fields = " + milliseconds
+", devices.Ticket_ID = " + ticketid
+", devices.Status = \"Shipped\", devices.Renter = " + sso
+", devices.Location = " + locationid
+" WHERE ticket.Ticket_ID = " + ticketid + " AND devices.Device_ID = " + deviceid;
System.out.println("Executing query: "+query);
stmt.executeUpdate(query);

// We also need to update ticket's STD field (the one with the date of its last modification)
+ String query = "UPDATE ticket SET Status_Date_Fields = "+milliseconds+" WHERE Ticket_ID = "+ticketid+";";
+ stmt.executeQuery(query);
stmt.close();
connect.close();
}
Expand All @@ -97,13 +102,26 @@ public static void rejectTicket(int id) throws SQLException, ClassNotFoundExcept
connect.close();
}

// Returns millisecond value of the most recently modified ticket assigned to 'employee' with status 'status'
public static long getLatestTicketActivity(User employee, String status) throws SQLException, ClassNotFoundException{
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database, user, password);
Statement stmt = connect.createStatement();
String query = "SELECT MAX(Status_Date_Fields) FROM ticket WHERE Requestor = "+employee.getID()+" AND Status = '"+status+"';";
System.out.println("Executing query '"+query+"'");
ResultSet results = stmt.executeQuery(query);
results.next();
return results.getLong("MAX(Status_Date_Fields)");
}
// Returns all tickets approved since 'millieconds' and associated with 'employee'
public static Ticket[] getApprovedTickets(int userID, long milliseconds) throws SQLException, ClassNotFoundException {
public static Ticket[] getRecentApprovedTickets(int userID, long milliseconds) throws SQLException, ClassNotFoundException {
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database, user, password);
Statement stmt = connect.createStatement();
String query= "SELECT ticket.*, employee.Name AS 'username', devices.Device_Name, location.Name AS 'locationname' FROM ticket INNER JOIN employee ON ticket.Requestor = employee.Employee_ID INNER JOIN devices ON ticket.Device_ID = devices.Device_ID INNER JOIN location ON ticket.Location = location.Location_ID WHERE ticket.Status = 'Approved' AND Requestor = " + userID + " AND Status_Date_Fields > " + milliseconds;
String query= "SELECT ticket.*, employee.Name AS 'username', devices.Device_Name, location.Name AS 'locationname' FROM ticket INNER JOIN employee ON ticket.Requestor = employee.Employee_ID INNER JOIN devices ON ticket.Device_ID = devices.Device_ID INNER JOIN location ON ticket.Location = location.Location_ID WHERE ticket.Status = 'Approved' AND Requestor = " + userID + " AND Status_Date_Fields >= " + milliseconds;
System.out.println("Executing query '"+query+"'");
ResultSet results = stmt.executeQuery(query);
results.last();
int rows = results.getRow();
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/Mail.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ protected PasswordAuthentication getPasswordAuthentication(){

}

public void sendTicketApproval(int[] ticketIDs) throws IOException, InterruptedException {

}
private Properties setProp(String email, String targetEmail) {
Properties props = null;
try {
Expand Down
32 changes: 26 additions & 6 deletions src/utilities/NotificationQueue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utilities;
import java.sql.SQLException;
import java.util.Set;

import database.TicketQueries;
import entities.Ticket;
import entities.User;


Expand Down Expand Up @@ -28,11 +31,15 @@ public void run(){
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
boolean match = false;
System.out.println("NotificationQueue: Checking active threads for match with "+threadName+"...");
for (int i=0; i<threadArray.length; i++) {
if(threadArray[i].getName().equals(threadName)) match=true;
if(threadArray[i].getName().equals(threadName)) {
System.out.println(" Match!");
match=true;
}
}
if(match){
System.out.println("Redundant notification queue thread will terminat"e);
System.out.println("Redundant NotificationQueue thread will terminate");
return; //should kill thread
}
else{
Expand All @@ -44,7 +51,15 @@ public void run(){
// Finally do the thing we came for
switch(instructions){
case "ticketConfirmations":
startTicketConfirmQueue(employee);
try {
startTicketApproveQueue(employee);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
System.out.println("NotificationQueue: Did not recognize instruction string");
Expand All @@ -55,8 +70,13 @@ public void run(){
e.printStackTrace();
}
}
private void startTicketConfirmQueue(User employee) throws InterruptedException{
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+" will send summary email now");
private void startTicketApproveQueue(User employee) throws InterruptedException, ClassNotFoundException, SQLException{
long milliseconds = TicketQueries.getLatestTicketActivity(employee,"Available");
Thread.sleep(30000);
System.out.println("NotificationQueue: sending summary email from thread "+Thread.currentThread().getName());
Ticket[] tickets = TicketQueries.getRecentApprovedTickets(employee.getID(),milliseconds);
for (Ticket t : tickets) {
System.out.println(t.getId());
}
}
}

0 comments on commit c474ccc

Please sign in to comment.