diff --git a/WebContent/html/webpages/redirect/ticketAdminRedirect.jsp b/WebContent/html/webpages/redirect/ticketAdminRedirect.jsp index c33d7f7..7b45f4a 100644 --- a/WebContent/html/webpages/redirect/ticketAdminRedirect.jsp +++ b/WebContent/html/webpages/redirect/ticketAdminRedirect.jsp @@ -1,4 +1,5 @@ <%@ page import = "database.*" %> +<%@ page import = "utilities.NotificationQueue" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> @@ -37,6 +38,12 @@ int locationid = Integer.parseInt(locationidstr); //add form was submitted if(request.getParameter("approve") != null){ TicketQueries.acceptTicket(ticketid,deviceid,locationid,Integer.parseInt(navsso)); + User client = EmployeeQueries.getEmployeeByID(TicketQueries.getUserID(ticketid)); + boolean notificationPreferences = true; //TODO get real preference + if(notificationPreferences){ + NotificationQueue q = new NotificationQueue(client,"ticketConfirmations"); + q.run(); + } } //modify form was submitted if(request.getParameter("reject") != null){ diff --git a/src/database/DeviceQueries.java b/src/database/DeviceQueries.java index 248a400..5372d00 100644 --- a/src/database/DeviceQueries.java +++ b/src/database/DeviceQueries.java @@ -126,7 +126,7 @@ public static void addDevice(Device device) throws SQLException, ClassNotFoundEx id = results.getInt("Device_ID"); id++; //tries this statement, otherwise tries again with a new id - String command = "INSERT INTO devices (Device_ID,Device_Name,Device_Description,MAC_Address,Manufacturer,Hardware,Model,Serial_Num,Status,Added_By,NFC_ID) " + "VALUES (" + id +",\"" + device.getName() + "\",\"" + device.getDesc()+ "\",\"" + device.getMAC() + "\",\"" + device.getManufacturer() + "\",\""+device.getHardware()+ "\",\"" + device.getModel() + "\",\"" + device.getSerial() + "\",\"" + device.getStatus() + "\",30,\"" + device.getNFC() + "\");"; //TODO update the Added_By to include cookies + String command = "INSERT INTO devices (Device_ID,Device_Name,Device_Description,MAC_Address,Manufacturer,Hardware,Model,Serial_Num,Status,Added_By,NFC_ID) " + "VALUES (" + id +",\"" + device.getName() + "\",\"" + device.getDesc()+ "\",\"" + device.getMAC() + "\",\"" + device.getManufacturer() + "\",\""+device.getHardware()+ "\",\"" + device.getModel() + "\",\"" + device.getSerial() + "\",\"" + device.getStatus() + "\",30,\"" + device.getNFC() + "\");"; System.out.println(command); i = stmt.executeUpdate(command); } diff --git a/src/database/LocationQueries.java b/src/database/LocationQueries.java index eb5d6f5..234637b 100644 --- a/src/database/LocationQueries.java +++ b/src/database/LocationQueries.java @@ -100,7 +100,7 @@ public static Location[] getLocations() throws ClassNotFoundException, SQLExcept public static Location[] getLocations(int userID) throws SQLException, ClassNotFoundException { - String query = "SELECT * FROM location WHERE Employee_Flag = 0 OR Employee_Flag = "+userID+" ORDER BY Name ASC"; //TODO filter for custom locations (needs db change) + String query = "SELECT * FROM location WHERE Employee_Flag = 0 OR Employee_Flag = "+userID+" ORDER BY Name ASC"; System.getenv("VCAP_SERVICES"); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(database, user, password); diff --git a/src/database/TicketQueries.java b/src/database/TicketQueries.java index 3005bbd..a86878c 100644 --- a/src/database/TicketQueries.java +++ b/src/database/TicketQueries.java @@ -53,10 +53,11 @@ public Ticket[] getTickets() throws SQLException, ClassNotFoundException{ tickets[i] = new Ticket( resultSet.getInt("Ticket_ID"), resultSet.getInt("Requestor"), - //resultSet.getInt("Request_Date"), + resultSet.getLong("Request_Date"), resultSet.getInt("Location"), resultSet.getInt("Device_ID"), status, + resultSet.getLong("Status_Date_Fields"), resultSet.getString("Return_Date") ); i++; @@ -71,8 +72,9 @@ public static void acceptTicket(int ticketid, int deviceid, int locationid, int Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection(database, user, password); Statement stmt = connect.createStatement(); + long milliseconds = new java.util.Date().getTime(); stmt.executeUpdate("UPDATE ticket SET Status = \"Approved\" WHERE Ticket_ID = " + ticketid); - stmt.executeUpdate("UPDATE devices SET Ticket_ID = " + ticketid + ", Status = \"Shipped\", Renter = " + sso + ", Location = " + locationid + " WHERE Device_ID = " + deviceid); + stmt.executeUpdate("UPDATE devices SET Ticket_ID = " + ticketid + ", Status = \"Shipped\", Renter = " + sso + ", Location = " + locationid + " Status_Date_Fields = "+milliseconds+" WHERE Device_ID = " + deviceid); stmt.close(); connect.close(); } @@ -86,5 +88,42 @@ public static void rejectTicket(int id) throws SQLException, ClassNotFoundExcept stmt.close(); connect.close(); } + + // Returns all tickets approved since 'millieconds' and associated with 'employee' + public static Ticket[] getApprovedTickets(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 COUNT(Ticket_ID) FROM ticket WHERE Requestor = "+userID+" AND Status = 'Approved' AND Status_Date_Fields > "+milliseconds+";"; + ResultSet results = stmt.executeQuery(query); + results.next(); + Ticket[] tickets = new Ticket[results.getInt("COUNT(Ticket_ID)")]; + query= "SELECT * FROM ticket WHERE Requestor = "+userID+" AND Status = 'Approved' AND Status_Date_Fields > "+milliseconds+";"; + int i=0; + while(results.next()) + { + tickets[i]=new Ticket( results.getInt("Ticket_ID"), + results.getInt("Requestor"), + results.getLong("Request_Date"), + results.getInt("Location"), + results.getInt("Device_ID"), + results.getString("Status"), + results.getLong("Status_Date_Fields"), + results.getString("ReturnDate")); + i++; + } + return null; + } + public static int getUserID(int ticketID) throws ClassNotFoundException, SQLException{ + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connect = DriverManager.getConnection(database, user, password); + Statement stmt = connect.createStatement(); + String query = "SELECT Requestor FROM ticket WHERE Ticket_ID = "+ticketID+";"; + ResultSet results = stmt.executeQuery(query); + results.next(); + return results.getInt("Requestor"); + } } diff --git a/src/entities/Ticket.java b/src/entities/Ticket.java index a8f8479..697fa11 100644 --- a/src/entities/Ticket.java +++ b/src/entities/Ticket.java @@ -6,19 +6,21 @@ public class Ticket { private int _id; private int _requestor; - //private BigInteger _requestDate; + private long _requestDate; private int _location; private int _deviceId; private String _status; + private long _statusDateFields; private String _returnDate; - public Ticket(int id, int requestor, int location, int deviceId, String status, String returnDate){ + public Ticket(int id, int requestor, long requestDate, int location, int deviceId, String status, long statusDateFields, String returnDate){ this._id = id; this._requestor = requestor; - //this._requestDate = requestDate; + this._requestDate = requestDate; this._location = location; this._deviceId = deviceId; this._status = status; + this._statusDateFields=statusDateFields; this._returnDate = returnDate; } @@ -62,12 +64,12 @@ public void setRequestor(int requestor){ this._requestor = requestor; } - /*public BigInteger getRequestDate(){ + public long getRequestDate(){ return _requestDate; } - public void setRequestDate(BigInteger requestDate){ + public void setRequestDate(long requestDate){ this._requestDate = requestDate; - }*/ + } public int getLocation(){ return _location; @@ -96,4 +98,10 @@ public String getReturnDate(){ public void setReturnDate(String returnDate){ this._returnDate = returnDate; } + public void setStatusDatefields(long milliseconds){ + _statusDateFields=milliseconds; + } + public long getStatusDatefields(){ + return _statusDateFields; + } } diff --git a/src/utilities/NotificationQueue.java b/src/utilities/NotificationQueue.java new file mode 100644 index 0000000..ff38932 --- /dev/null +++ b/src/utilities/NotificationQueue.java @@ -0,0 +1,61 @@ +package utilities; +import java.util.Set; + +import entities.User; + + +public class NotificationQueue extends Thread{ + // The purpose of this class is to help reduce notification spam. + // Instead of firing off emails immediately, spawn a thread in this class which + // waits patiently for a few minutes and then collects all relevent activity that happened + // after the event that created it. Then send a single email summerizing it all. + // Right now I'm using it when admins approve tickets. It may be usefull for notifications to admins as well. + private String instructions=""; + // this determines what information the notification queue will collect when it is finished waiting. + // >'ticketConfirmations' will cause it to query approved tickets since initialization associated with 'employee' + private User employee; + public NotificationQueue(User employee, String initialize){ + instructions=initialize; + this.employee=employee; + } + public void run(){ + try { + String threadName = instructions+employee.getID(); + // We don't want this to do anything if there is already a thread waiting to send a summary email + // That would defeat the purpose of this class + // So first we check active threads for one that matches threadName; which would be another thread that is + // both assigned to the user in question, and monitoring the same activity. + Set threadSet = Thread.getAllStackTraces().keySet(); + Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); + boolean match = false; + for (int i=0; i