@@ -155,14 +184,13 @@ This function displays a list of devices that a user has currently in their shop
**/
function show(){
var cart = getCartItems(); //get all the cart items you want to show
- alert("cart: "+JSON.stringify(cart));
var html = ''; //html string initially empty
var part1 = "nhpup.popup('";
var part2 = "');"
if(cart.length !== 0){ //only continue if there are cart items
html += '
Tickets have been generated below based on your desired selections.
'
for(var i = 0; i < cart.length; i++){ //iterate cart
- var id = cart[i]; //this gets the id value from the cart
+ var id = cart[i].id; //this gets the id value from the cart
html += '
'; //create listing of all the devices
}
}
@@ -201,7 +229,6 @@ function clickTicket(){
document.getElementById(id).checked = true;
checked.push(id);
}
- alert("the 'checked' array looks like this: "+JSON.stringify(cart));
}
/**
@@ -218,7 +245,6 @@ function changeStatus(){
checked.splice(i,1); //remove from the array
}
}
- alert("the 'checked' array looks like this: "+JSON.stringify(cart));
}
// These functions just redirect button pressed to call the showPopup method with the appropriate parameter
@@ -266,19 +292,19 @@ function submitOrderForm() {
// we must first append the employees ID as well as the ids of whatever devices are selected.
// First, employee id
userID=getUserID();
- document.orderForm.append('
');
+ document.orderForm.userID.value=userID;
// Now selected devices.
if(document.orderForm.orderAll.value=="true") //if the order form was opened using the order all button
{selectAllDevices();}
var selectedDeviceString=JSON.stringify(checked);
- document.orderForm.append('
');
+ document.orderForm.deviceIDs.value=selectedDeviceString;
// Finally remove selected items from the cart
var cart = getCartItems();
for(var i = 0; i < checked.length; i++){ //iterate all the checked off devices
for(var j = 0; j < cart.length; j++){ //iterate cart
- if(cart[j] == id) //match id's
+ if(cart[j] == checked[i]) //match id's
cart.splice(j,1); //remove from the cart
}
}
@@ -342,12 +368,20 @@ function deleteAll(){
}
}
+function getUserID() {
+ return 9; //TODO actually get a userID
+}
function debug_add_item_to_cart() {
employee.id=5;
employee.location="test";
employee.name="test";
employee.phone="test";
var cart = [employee];
+ employee.id=6;
+ employee.location="test";
+ employee.name="test";
+ employee.phone="test";
+ cart.push(employee)
localStorage.setItem("cart",JSON.stringify(cart));
}
diff --git a/src/database/MySQLAccess.java b/src/database/MySQLAccess.java
index b009049..53b97a0 100644
--- a/src/database/MySQLAccess.java
+++ b/src/database/MySQLAccess.java
@@ -1,6 +1,7 @@
package database;
import java.sql.*;
+import java.util.Date;
import entities.User;
@@ -41,22 +42,52 @@ public void initializeEmployee(int id) throws ClassNotFoundException, SQLExcepti
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database,user,password);
Statement statement = connect.createStatement();
- statement.executeQuery("INSERT INTO employee (Employee_ID) VALUES ("+id+");");
+ statement.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");");
}
- public void updateEmployee(int id, String name, String phone, int location) throws ClassNotFoundException, SQLException
+ public void updateEmployee(int id, String name, String phone, String location) throws ClassNotFoundException, SQLException
{
System.out.print("Updating employee "+id+": "+name+".\nPhone: "+phone+", location: "+location);
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database,user,password);
Statement statement = connect.createStatement();
String query = "";
- query+="UPDATE employee SET ";
- query+="Name="+name;
- query+=", Phone_Number="+phone;
- query+=", Location_ID="+location;
- query+=" WHERE Employee_ID="+id;
- statement.executeQuery(query);
+ query+="SELECT Location_ID FROM Location ";
+ query+="WHERE Address="+location;
+ System.out.println(query);
+ ResultSet rs = statement.executeQuery(query);
+ if(rs.next())
+ // If there is already a record in locations matching given string, set
+ // employee's location FK to that location ID
+ {
+ query="";
+ query+="UPDATE employee SET ";
+ query+="Name='"+name;
+ query+="', Phone_Number='"+phone;
+ query+="', Location_ID="+rs.getInt("Location_ID");
+ query+=" WHERE Employee_ID="+id;
+ }
+ else
+ // Otherwise we need to make a whole new location entry
+ {
+ query+="SELECT COUNT(Location_ID) FROM location"; //generate a new key. Probably will run into trouble if a location is ever deleted
+ rs=statement.executeQuery(query);
+ rs.next();
+ query+="INSERT INTO location ";
+ query+="(Location_ID, Address) ";
+ query+="VALUES ("+(rs.getInt("COUNT(Location_ID)")+1)+", '"+location+"')";
+ statement.executeQuery(query);
+
+ // then update employee's location id
+ query="";
+ query+="UPDATE employee SET ";
+ query+="Name='"+name;
+ query+="', Phone_Number='"+phone;
+ query+="', Location_ID="+(rs.getInt("(COUNT(Location_ID)")+1);
+ query+=" WHERE Employee_ID="+id;
+ }
+ System.out.println(query);
+ statement.executeUpdate(query);
}
public User getEmployeeByID(int id) throws ClassNotFoundException, SQLException
@@ -78,22 +109,47 @@ public User getEmployeeByID(int id) throws ClassNotFoundException, SQLException
return employee;
}
- public void generateTicket(int id, int requester, int location, int device, String stat, String std) throws ClassNotFoundException, SQLException
+ public int locationStringToInt(String locString) throws SQLException, ClassNotFoundException
+ {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection connect = DriverManager.getConnection(database,user,password);
+ Statement statement = connect.createStatement();
+ ResultSet resultSet = statement.executeQuery("SELECT Location_ID FROM location WHERE Address = '"+locString+"'");
+ resultSet.next();
+ return resultSet.getInt("Location_ID");
+ }
+
+ public String locationIntToString(int locInt) throws ClassNotFoundException, SQLException
+ {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection connect = DriverManager.getConnection(database,user,password);
+ Statement statement = connect.createStatement();
+ ResultSet resultSet = statement.executeQuery("SELECT Address FROM location WHERE Location_ID = "+locInt);
+ resultSet.next();
+ return resultSet.getString("Address");
+ }
+
+ public void generateTicket(int requester, String location, int device) throws ClassNotFoundException, SQLException
{
+ long time = new Date().getTime(); //Using milliseconds because sql is annoying with dates.
+ int locID = locationStringToInt(location);
+ long ticketID = time+requester+device+locID;
+ String stat = "Requested";
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database,user,password);
Statement statement = connect.createStatement();
String query = "";
- query+="INSERT INTO ticket (Ticket_ID, Requester, Request_Date, Location, Device_ID, Status, Status_Date_Fields) ";
+ query+="INSERT INTO ticket (Ticket_ID, Requestor, Request_Date, Location, Device_ID, Status, Status_Date_Fields) ";
query+="VALUES (";
- query+= id +", ";
+ query+= ticketID +", ";
query+= requester +", ";
- query+= 0 +", ";
+ query+= "'"+time+"'" +", ";
query+= location +", ";
query+= device +", ";
- query+= stat +", ";
- query+= std +");";
- statement.executeQuery(query);
+ query+= "'"+stat+"'" +", ";
+ query+= "'"+time+"'" +");";
+ System.out.println(query);
+ statement.executeUpdate(query);
}
}
diff --git a/src/entities/ListedDevice.java b/src/entities/ListedDevice.java
new file mode 100644
index 0000000..4de1aac
--- /dev/null
+++ b/src/entities/ListedDevice.java
@@ -0,0 +1,50 @@
+package entities;
+/**
+ * Simple Modification of the RentedDevice class for use in the listed devices page.
+ * @author John Costa III
+ *
+ */
+public class ListedDevice {
+ private String Device_Name;
+ private String Device_Description;
+ private String Hardware;
+ private String Model;
+ public ListedDevice(String name, String desc, String hardware, String model) {
+ Device_Name = name;
+ Device_Description = desc;
+ Hardware = hardware;
+ Model = model;
+ }
+ /**
+ * Formatting the device to fit a JSON object.
+ * @author - Connor
+ */
+ public String toString(){
+ StringBuilder sb = new StringBuilder();
+ String comma = ", ";
+ sb.append("{\"name\": \"").append(Device_Name).append("\"").append(comma);
+ sb.append("\"description\": \"").append(Device_Description).append("\"").append(comma);
+ sb.append("\"hardware\": \"").append(Hardware).append("\"").append(comma);
+ sb.append("\"model\": \"").append(Model).append("\"");
+ sb.append("}");
+ return sb.toString();
+ }
+ /**
+ * This is a static function which will turn a Listed Device array into its proper string. (modification)
+ * @author - Connor
+ * @param array
+ * @return
+ */
+ public static String arrayToString(ListedDevice[] array){
+ StringBuilder sb = new StringBuilder();
+ sb.append("[");
+ for(int i = 0; i < array.length; i++){
+ sb.append(array[i].toString());
+ if(i+1 != array.length){
+ sb.append(",");
+ }
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+}