From c77c2dfbb87cd1f8394219ecdfe9b3cf3403d78a Mon Sep 17 00:00:00 2001 From: Adam R Claxton Date: Wed, 19 Apr 2017 15:08:32 -0400 Subject: [PATCH] The notification queue works!!!! Now I just need to make notification preferences actually editable, make the reject email and then figure out what emails need to be sent to admins. --- .../webpages/redirect/orderFormHandler.jsp | 3 +- .../webpages/redirect/ticketAdminRedirect.jsp | 3 +- src/database/TicketQueries.java | 22 ++++++-- src/entities/Ticket.java | 3 ++ src/utilities/Mail.java | 54 ++++++++++++++++++- src/utilities/NotificationQueue.java | 19 ++++--- 6 files changed, 89 insertions(+), 15 deletions(-) diff --git a/WebContent/html/webpages/redirect/orderFormHandler.jsp b/WebContent/html/webpages/redirect/orderFormHandler.jsp index 6752650..eb7cc34 100644 --- a/WebContent/html/webpages/redirect/orderFormHandler.jsp +++ b/WebContent/html/webpages/redirect/orderFormHandler.jsp @@ -70,7 +70,8 @@ for(int i=0; i= " + 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(); @@ -137,13 +144,15 @@ public static Ticket[] getRecentApprovedTickets(int userID, long milliseconds) t results.getInt("Device_ID"), results.getString("Status"), results.getLong("Status_Date_Fields"), - results.getString("ReturnDate"), + results.getString("Return_Date"), results.getString("username"), - results.getString("Device_Name"), - results.getString("locationname") + results.getString("locationname"), + results.getString("Device_Name") ); i++; } + stmt.close(); + connect.close(); return tickets; } public static int getUserID(int ticketID) throws ClassNotFoundException, SQLException{ @@ -154,7 +163,10 @@ public static int getUserID(int ticketID) throws ClassNotFoundException, SQLExce String query = "SELECT Requestor FROM ticket WHERE Ticket_ID = "+ticketID+";"; ResultSet results = stmt.executeQuery(query); results.next(); - return results.getInt("Requestor"); + int answer = results.getInt("Requestor"); + stmt.close(); + connect.close(); + return answer; } } diff --git a/src/entities/Ticket.java b/src/entities/Ticket.java index 5b71975..f66ec62 100644 --- a/src/entities/Ticket.java +++ b/src/entities/Ticket.java @@ -113,4 +113,7 @@ public void setStatusDatefields(long milliseconds){ public long getStatusDatefields(){ return _statusDateFields; } + public String getDeviceName(){ + return devicename; + } } diff --git a/src/utilities/Mail.java b/src/utilities/Mail.java index 8cb97ac..9c061b9 100644 --- a/src/utilities/Mail.java +++ b/src/utilities/Mail.java @@ -10,6 +10,7 @@ import javax.mail.*; import javax.mail.internet.*; +import entities.Ticket; import entities.User; import javax.activation.*; @@ -74,8 +75,57 @@ protected PasswordAuthentication getPasswordAuthentication(){ } - public void sendTicketApproval(int[] ticketIDs) throws IOException, InterruptedException { - + public void sendTicketApproval(Ticket[] tickets) throws IOException, InterruptedException { + String subject = "{DEVICE(S)} {is/are(3)} on the way!"; + String messege = "" + + "" + + "" + + "

Your ticket{s} {have/has} been approved!

" + + "

I hope you're exicted, because the device{s} you ordered {is/are} about to ship. Happy testing!

" + + "{list_header}" + + "
" + + "{TICKETS GO HERE}" + + "
" + + "" + + "" + + ""; + + subject=subject.replace("{DEVICE(S)}", tickets[0].getDeviceName() + (tickets.length>1 ? " and "+(tickets.length-1)+" more" : "")); + subject=subject.replace("{is/are(3)}",tickets.length>2 ? "are" : "is"); + messege=messege.replace("{s}",tickets.length>1 ? "s" : ""); + messege=messege.replace("{have/has}",tickets.length>1 ? "have" : "has"); + messege=messege.replace("{is/are}",tickets.length>1 ? "are" : "is"); + messege=messege.replace("{list_header}",tickets.length>1 ? "The following tickets were approved:" : ""); + String ticketList = ""; + if(tickets.length>1) + { + for (Ticket tic : tickets) { + ticketList+="

Ticket #"+tic.getId()+" - "+tic.getDeviceName()+"

"; + } + } + messege=messege.replace("{TICKETS GO HERE}",ticketList); + Properties properties = System.getProperties(); + properties = setProp(sender, client.getEmail()); + Session session = Session.getInstance(properties, new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication(){ + return new PasswordAuthentication(sender, password); + } + }); + + try { + Address address = new InternetAddress(client.getEmail()); + MimeMessage message = new MimeMessage(session); + message.setFrom(address); + message.addRecipient(Message.RecipientType.TO, address); + message.setSubject(subject); + message.setText(messege,"utf-8","html"); + message.saveChanges(); + Transport.send(message); + System.out.println("Sent message successfully...."); + + } catch (Exception mex) { + mex.printStackTrace(); + } } private Properties setProp(String email, String targetEmail) { Properties props = null; diff --git a/src/utilities/NotificationQueue.java b/src/utilities/NotificationQueue.java index 8bd8744..83abfb8 100644 --- a/src/utilities/NotificationQueue.java +++ b/src/utilities/NotificationQueue.java @@ -1,5 +1,7 @@ package utilities; +import java.io.IOException; import java.sql.SQLException; +import java.util.Date; import java.util.Set; import database.TicketQueries; @@ -34,10 +36,10 @@ public void run(){ System.out.println("NotificationQueue: Checking active threads for match with "+threadName+"..."); for (int i=0; i