diff --git a/.classpath b/.classpath index 09ba841..3cc104c 100644 --- a/.classpath +++ b/.classpath @@ -22,5 +22,6 @@ + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml index a3bf73b..3b1bc05 100644 --- a/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,6 +1,7 @@ + diff --git a/WebContent/html/css/stylesheet.css b/WebContent/html/css/stylesheet.css index 96b77b9..f063542 100644 --- a/WebContent/html/css/stylesheet.css +++ b/WebContent/html/css/stylesheet.css @@ -85,6 +85,10 @@ input[type=number]{ width: 60%; } +textarea{ + margin: 0 auto; +} + select{ margin:0 auto; width: 65%; diff --git a/WebContent/html/javascript/listing.jsp b/WebContent/html/javascript/listing.jsp index 7da247d..d709c5b 100644 --- a/WebContent/html/javascript/listing.jsp +++ b/WebContent/html/javascript/listing.jsp @@ -1,4 +1,4 @@ -<%@ page import = "database.*,entities.ListedDevice" %> +<%@ page import = "database.*,entities.Device" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> @@ -8,24 +8,20 @@ <% - ListedDevice[] mydevices = DeviceQueries.getAllDevices(); - + Device[] mydevices = DeviceQueries.getAllDevices(); //string representation of array. - String deviceString = ListedDevice.arrayToString(mydevices); + String deviceString = Device.arrayToString(mydevices); //out.println(description); //out.println(hardware); - %> + + + + + + + + + + + + + + + + + +
+ +

Device Listing

+ + + + + + + + + + + + + + + + + +
NameDescriptionAvailabilityMAC AddressManufacturerHardware TypeModel NameSerial NumberNFC ID
+ +
+<% +//database query +Device[] devices = DeviceQueries.getAllDevices(); +//string representation of array. +String deviceString = Device.arrayToString(devices); +deviceString = deviceString.replace("'","\\'"); +%> + + + + + \ No newline at end of file diff --git a/WebContent/html/webpages/adminLocation.jsp b/WebContent/html/webpages/adminLocation.jsp index 3cf0a83..3462a71 100644 --- a/WebContent/html/webpages/adminLocation.jsp +++ b/WebContent/html/webpages/adminLocation.jsp @@ -69,12 +69,10 @@
-
-
diff --git a/WebContent/html/webpages/deviceRedirect.jsp b/WebContent/html/webpages/deviceRedirect.jsp new file mode 100644 index 0000000..d0e4ceb --- /dev/null +++ b/WebContent/html/webpages/deviceRedirect.jsp @@ -0,0 +1,71 @@ +<%@ page import = "database.*,entities.Device" %> +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + + + + + + + + Synchrony Financial + + + + + + + + + + +

Redirect Page

+

You shouldn't be seeing this page :)

+ +<% +//make instance +MySQLAccess myaccess = new MySQLAccess(); +String name = request.getParameter("name").replace("\"","\\\""); +String description = request.getParameter("description").replace("\"","\\\""); +String status = request.getParameter("status"); +String mac = request.getParameter("MAC"); +String manufacturer = request.getParameter("manu").replace("\"","\\\""); +String hardware = request.getParameter("hardware").replace("\"","\\\""); +String model = request.getParameter("model").replace("\"","\\\""); +String serial = request.getParameter("serial"); +int nfc = Integer.parseInt(request.getParameter("NFC")); +//add form was submitted +if(request.getParameter("add") != null){ + Device device = new Device(name,1,description,hardware,model,manufacturer,status,mac,serial,nfc); + DeviceQueries.addDevice(device); +} + +//modify form was submitted +if(request.getParameter("modify") != null){ + //getParameter() always returns string + String strID = request.getParameter("id"); + //turn to int for constructor + int id = Integer.parseInt(strID); + Device device = new Device(name,id,description,hardware,model,manufacturer,status,mac,serial,nfc); + DeviceQueries.modifyDevice(device); +} + +//delete form was submitted +if(request.getParameter("delete") != null){ + String strID = request.getParameter("id"); + int id = Integer.parseInt(strID); + DeviceQueries.deleteDevice(id); +} + +%> + + + + + \ No newline at end of file diff --git a/WebContent/html/webpages/requestPage.jsp b/WebContent/html/webpages/requestPage.jsp index ebf9e87..47b3aa9 100644 --- a/WebContent/html/webpages/requestPage.jsp +++ b/WebContent/html/webpages/requestPage.jsp @@ -70,7 +70,7 @@
  • -
  • +
  • diff --git a/src/database/DeviceQueries.java b/src/database/DeviceQueries.java index cd93bbd..2bbb939 100644 --- a/src/database/DeviceQueries.java +++ b/src/database/DeviceQueries.java @@ -2,16 +2,21 @@ import java.sql.*; -import entities.ListedDevice; +import entities.Device; import entities.RentedDevice; public class DeviceQueries { + private static String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; + private static String user = "b372dfe7409692"; + private static String password = "74f6e317"; + public static RentedDevice[] getUserDevices(String userID) throws SQLException, ClassNotFoundException{ //Not sure how to get cookie information (if that is how we choose to accomplish this...) ? But this should be a passed parameter System.getenv("VCAP_SERVICES"); - MySQLAccess access = new MySQLAccess(); - Statement stmt = access.getStatement(); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT Device_ID, Device_Name, Device_Description, Ticket_ID, Hardware, Model, Borrow_Date FROM devices WHERE Renter = " + userID + " AND Status <> \"Available\" AND Status <> \"Returning \""); int counter = 0; @@ -26,18 +31,18 @@ public static RentedDevice[] getUserDevices(String userID) throws SQLException, while(resultSet.next()){ devices[counter] = new RentedDevice(resultSet.getInt("Device_ID") + "",resultSet.getString("Device_Name"),resultSet.getString("Device_Description"),resultSet.getInt("Ticket_ID") + "",resultSet.getString("Hardware"),resultSet.getString("Model"),resultSet.getString("Borrow_Date")); counter++; - } - - access.closeConnection(); - + } + stmt.close(); + connection.close(); return devices; } - public static ListedDevice[] getAllDevices() throws SQLException, ClassNotFoundException{ + public static Device[] getAllDevices() throws SQLException, ClassNotFoundException{ //database connect System.getenv("VCAP_SERVICES"); - MySQLAccess access = new MySQLAccess(); - Statement stmt = access.getStatement(); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM devices"); int counter = 0; @@ -46,15 +51,15 @@ public static ListedDevice[] getAllDevices() throws SQLException, ClassNotFoundE resultSet.beforeFirst(); //Covers amount of rows, and 6 attributes (indices 0-5) - ListedDevice[] devices = new ListedDevice[rows]; + Device[] devices = new Device[rows]; //iterate result set while(resultSet.next()){ - devices[counter] = new ListedDevice(resultSet.getString("Device_Name"),resultSet.getString("Device_ID"),resultSet.getString("Device_Description"),resultSet.getString("Hardware"), resultSet.getString("Model"), resultSet.getString("Manufacturer"), resultSet.getString("Status")); + devices[counter] = new Device(resultSet.getString("Device_Name"),resultSet.getInt("Device_ID"),resultSet.getString("Device_Description"),resultSet.getString("Hardware"), resultSet.getString("Model"), resultSet.getString("Manufacturer"), resultSet.getString("Status"), resultSet.getString("MAC_Address"), resultSet.getString("Serial_Num"), resultSet.getInt("NFC_ID")); counter++; } - - access.closeConnection(); + stmt.close(); + connection.close(); return devices; } @@ -69,14 +74,59 @@ public static ListedDevice[] getAllDevices() throws SQLException, ClassNotFoundE public static void returnDevices(String devices) throws ClassNotFoundException, SQLException{ //connect to DB System.getenv("VCAP_SERVICES"); - MySQLAccess access = new MySQLAccess(); - Statement stmt = access.getStatement(); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); //make into string array; queries need to have numbers as strings! String[] deviceArray = devices.replaceAll("\\[","").replaceAll("\\]","").replaceAll("\\s","").split(","); for(int i = 0; i < deviceArray.length; i++){ //update statement stmt.executeUpdate("UPDATE devices SET Status = \"Returning\" WHERE Device_ID = " + deviceArray[i]); } - access.closeConnection(); + stmt.close(); + connection.close(); + } + + public static void addDevice(Device device) throws SQLException, ClassNotFoundException{ + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + + int i = -1; + int id=0; + while(i <= 0){ + ResultSet results = stmt.executeQuery("SELECT * from devices ORDER BY Device_ID"); + results.last(); + //gets largest ID + 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 + System.out.println(command); + i = stmt.executeUpdate(command); + } + stmt.close(); + connection.close(); + } + + public static void modifyDevice(Device device) throws ClassNotFoundException, SQLException{ + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + stmt.executeUpdate("UPDATE devices SET Device_Name = \"" + device.getName() + "\", Device_Description = \"" + device.getDesc() + "\", MAC_Address = \"" + device.getMAC() + "\", Manufacturer = \"" + device.getManufacturer() + "\", Hardware = \"" + device.getHardware() + "\", Model = \"" + device.getModel() + "\", Serial_Num = \"" + device.getSerial() + "\", Status = \"" + device.getStatus() + "\", NFC_ID = " + device.getNFC() + " WHERE Device_ID = " + device.getID()); + stmt.close(); + connection.close(); + } + + public static void deleteDevice(int id) throws ClassNotFoundException, SQLException{ + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + stmt.executeUpdate("DELETE from devices WHERE Device_ID = " + id); + stmt.close(); + connection.close(); } } \ No newline at end of file diff --git a/src/database/MySQLAccess.java b/src/database/MySQLAccess.java index 56d779a..66714ee 100644 --- a/src/database/MySQLAccess.java +++ b/src/database/MySQLAccess.java @@ -4,9 +4,9 @@ public class MySQLAccess { - String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; - String user = "b372dfe7409692"; - String password = "74f6e317"; + private String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; + private String user = "b372dfe7409692"; + private String password = "74f6e317"; private Connection connection; private Statement statement; @@ -29,4 +29,4 @@ public void closeConnection() throws SQLException { statement.close(); connection.close(); } -} +} \ No newline at end of file diff --git a/src/entities/Device.java b/src/entities/Device.java new file mode 100644 index 0000000..2111da6 --- /dev/null +++ b/src/entities/Device.java @@ -0,0 +1,109 @@ +package entities; + +/** + * Java representation of a mobile device. + * @author conno + * + */ +public class Device { + private String Device_Name; + private int Device_ID; + private String Device_Description; + private String Hardware; + private String Model; + private String Manu; + private String Status; + private String MAC; + private String Serial; + private int NFC; + + public Device(String name, int id, String desc, String hardware, String model, String manufacturer, String available, String mac, String serial, int nfc) { + Device_Name = name; + Device_ID = id; + Device_Description = desc; + Hardware = hardware; + Model = model; + Manu = manufacturer; + Status = available; + MAC = mac; + Serial = serial; + NFC = nfc; + } +/** + * Formatting the device to fit a JSON object. + */ + public String toString(){ + StringBuilder sb = new StringBuilder(); + String comma = ", "; + sb.append("{\"name\": \"").append(Device_Name).append("\"").append(comma); + sb.append("\"id\": \"").append(Device_ID).append("\"").append(comma); + sb.append("\"description\": \"").append(Device_Description).append("\"").append(comma); + sb.append("\"hardware\": \"").append(Hardware).append("\"").append(comma); + sb.append("\"status\": \"").append(Status).append("\"").append(comma); + sb.append("\"model\": \"").append(Model).append("\"").append(comma); + sb.append("\"manufacturer\": \"").append(Manu).append("\"").append(comma); + sb.append("\"mac\": \"").append(MAC).append("\"").append(comma); + sb.append("\"serial\": \"").append(Serial).append("\"").append(comma); + sb.append("\"nfc\": ").append(NFC); + sb.append("}"); + return sb.toString(); + } + + /** + * This is a static function which will turn a Rented Device array into its proper string. + * @param array + * @return + */ + public static String arrayToString(Device[] 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(); + } + + public int getID(){ + return Device_ID; + } + + public String getName(){ + return Device_Name; + } + + public String getDesc(){ + return Device_Description; + } + + public String getModel(){ + return Model; + } + + public String getHardware(){ + return Hardware; + } + + public String getMAC(){ + return MAC; + } + + public String getManufacturer(){ + return Manu; + } + + public String getSerial(){ + return Serial; + } + + public int getNFC() { + return NFC; + } + + public String getStatus(){ + return Status; + } +} diff --git a/src/main/Main.java b/src/main/Main.java index f6f1cfa..373be4b 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,7 +1,5 @@ package main; -import java.util.Scanner; - public class Main { public static void main(String[] args) {