Skip to content

Commit

Permalink
Merge branch 'email'
Browse files Browse the repository at this point in the history
  • Loading branch information
arc12012 committed Apr 19, 2017
2 parents 49d3dbe + c77c2df commit ef21eba
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 15 deletions.
3 changes: 2 additions & 1 deletion WebContent/html/webpages/redirect/orderFormHandler.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ for(int i=0; i<devIDs.length; i++)
// Now that tickets have been generated we will want to notify users.
boolean emailNotificationEnabled = true; //If/when we enable opting out of notifications this is where that check will take place
boolean emailNotificationEnabled = (self.getNotificationPreferences()%2==1);
// The rightmost, least significant bit of the notificationPreferences integer is used for ticket generation emails.
if(emailNotificationEnabled)
{
new Mail(self).sendTicketConfirmation(ticketIDs); //'self' is the current user, and is defined in navbar.jsp
Expand Down
3 changes: 2 additions & 1 deletion WebContent/html/webpages/redirect/ticketAdminRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ if(request.getParameter("approve") != null){
//TODO include in time of action so that the status fields can be updated in the ticket
TicketQueries.acceptTicket(ticketid,deviceid,locationid,Integer.parseInt(navsso));
User client = EmployeeQueries.getEmployeeByID(TicketQueries.getUserID(ticketid));
boolean notificationPreferences = true; //TODO get real preference
boolean notificationPreferences = (Math.floor(client.getNotificationPreferences()/2)%2==1);
//The second least significant bit of notificationPreferences - the 4's place - is used for ticket approval notifications.
if(notificationPreferences){
NotificationQueue q = new NotificationQueue(client,"ticketConfirmations");
q.start();
Expand Down
22 changes: 17 additions & 5 deletions src/database/TicketQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public static long getLatestTicketActivity(User employee, String status) throws
System.out.println("Executing query '"+query+"'");
ResultSet results = stmt.executeQuery(query);
results.next();
stmt.close();
connect.close();
return results.getLong("MAX(Status_Date_Fields)");
}
// Returns all tickets approved since 'millieconds' and associated with 'employee'
Expand All @@ -120,7 +122,12 @@ public static Ticket[] getRecentApprovedTickets(int userID, long milliseconds) t
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();
Expand All @@ -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{
Expand All @@ -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;
}
}

3 changes: 3 additions & 0 deletions src/entities/Ticket.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,7 @@ public void setStatusDatefields(long milliseconds){
public long getStatusDatefields(){
return _statusDateFields;
}
public String getDeviceName(){
return devicename;
}
}
54 changes: 52 additions & 2 deletions src/utilities/Mail.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.mail.*;
import javax.mail.internet.*;

import entities.Ticket;
import entities.User;

import javax.activation.*;
Expand Down Expand Up @@ -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 = "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<h4>Your ticket{s} {have/has} been approved!</h4>"
+ "<p>I hope you're exicted, because the device{s} you ordered {is/are} about to ship. Happy testing!</p>"
+ "{list_header}"
+ "<div name = 'tickets' style='text-indent: 20px'>"
+ "{TICKETS GO HERE}"
+ "</div>"
+ "</body>"
+ "<footer style='text-align: center;'><font size='1'>To change notification settings, please visit your <a href='../WebContent/html/webpages/profileSettings.jsp'>profile settings page</a></font></footer>"
+ "</html>";

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+="<p>Ticket #<a href='../WebContent/html/webpages/'>"+tic.getId()+"</a> - "+tic.getDeviceName()+"</p>";
}
}
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;
Expand Down
19 changes: 13 additions & 6 deletions src/utilities/NotificationQueue.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,10 +36,10 @@ public void run(){
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)) {
System.out.println(" Match!");
match=true;
}
}
System.out.println(match ? " Match!" : " No match");
if(match){
System.out.println("Redundant NotificationQueue thread will terminate");
return; //should kill thread
Expand All @@ -59,6 +61,9 @@ public void run(){
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
Expand All @@ -70,13 +75,15 @@ public void run(){
e.printStackTrace();
}
}
private void startTicketApproveQueue(User employee) throws InterruptedException, ClassNotFoundException, SQLException{
long milliseconds = TicketQueries.getLatestTicketActivity(employee,"Available");
private void startTicketApproveQueue(User employee) throws InterruptedException, ClassNotFoundException, SQLException, IOException{
// long milliseconds = TicketQueries.getLatestTicketActivity(employee,"Available");
// Database connections are becoming a serious problem, so I'm just going to estimate that 5 seconds is more than enough
// to account for the time it took to get from the ticket generation to here.
long milliseconds = new Date().getTime()-5000;
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());
}
Mail mail = new Mail(employee);
mail.sendTicketApproval(tickets);
}
}

0 comments on commit ef21eba

Please sign in to comment.