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/javascript/listing.jsp b/WebContent/html/javascript/listing.jsp index 94fda82..b8bd152 100644 --- a/WebContent/html/javascript/listing.jsp +++ b/WebContent/html/javascript/listing.jsp @@ -1,4 +1,4 @@ -<%@ page import = "database.MySQLAccess,entities.Device" %> +<%@ page import = "database.MySQLAccess,entities.Device,database.DeviceQueries" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> @@ -8,8 +8,7 @@ <% -MySQLAccess myaccess = new MySQLAccess(); -Device[] mydevices = myaccess.getAllDevices(); +Device[] mydevices = DeviceQueries.getAllDevices(); //string representation of array. String deviceString = Device.arrayToString(mydevices); diff --git a/WebContent/html/javascript/request.jsp b/WebContent/html/javascript/request.jsp index 086c24f..7e7ba73 100644 --- a/WebContent/html/javascript/request.jsp +++ b/WebContent/html/javascript/request.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,10 +8,10 @@ <% -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); @@ -26,37 +26,6 @@ var devices = makeDeviceArray(); var hardwareOptions = document.getElementsByClassName('hw-data-type'); var softwareOptions = document.getElementsByClassName('sw-data-type'); - -function ajaxFunction(){ - var request; - try { - request = new XMLHttpRequest(); - } - catch(e){ - try { - request = new ActiveXObject("Msxml2.XMLHTTP"); - } - catch(e){ - try { - request = new ActiveXObject(Microsoft.XMLHTTP); - } catch(e) { - alert("Something is wrong with your browser"); - return false; - } - - } - } - - - request.onreadystatechange = function(){ - if(request.readyState == 4){ - var display = document.getElementById('ajaxDiv'); - display.innerHTML = request.responseText; - } - } - -} - //add event listeners to the options in the left sidebar for(var a = 0; a < hardwareOptions.length; a++){ hardwareOptions[a].addEventListener('click', show); @@ -115,8 +84,7 @@ function show(){ } - if(hardwareMatch == true && softwareMatch == true){ - //if((hw_type.localeCompare(devices[i].hardware) == 0 && sw_type.localeCompare(devices[i].software) == 0) && !isUnavailable(i)){ + if(hardwareMatch == true && softwareMatch == true && devices[i].status.localeCompare("Available") == 0){ html += '' + devices[i].name + '' + devices[i].description + 'Order device' } } diff --git a/WebContent/html/webpages/adminDeviceSettings.jsp b/WebContent/html/webpages/adminDeviceSettings.jsp index 5036700..35a010d 100644 --- a/WebContent/html/webpages/adminDeviceSettings.jsp +++ b/WebContent/html/webpages/adminDeviceSettings.jsp @@ -1,4 +1,4 @@ -<%@ page import = "database.MySQLAccess,entities.Device" %> +<%@ page import = "database.*,entities.Device" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> @@ -68,10 +68,11 @@ + <%@ include file="navbar.html"%> - + Admin Options @@ -86,7 +87,7 @@ × - Add a New Device Below + Add a New Device Below @@ -99,6 +100,11 @@ Description + + Availability * + + + MAC Address @@ -136,7 +142,7 @@ × - Modify Device Below + Modify Device Below @@ -149,6 +155,11 @@ Description + + Availability * + + + MAC Address @@ -191,6 +202,7 @@ Name Description + Availability MAC Address Manufacturer Hardware Type @@ -205,10 +217,8 @@ Add Device <% -//database connection -MySQLAccess myaccess = new MySQLAccess(); //database query -Device[] devices = myaccess.getAllDevices(); +Device[] devices = DeviceQueries.getAllDevices(); //string representation of array. String deviceString = Device.arrayToString(devices); deviceString = deviceString.replace("'","\\'"); @@ -220,6 +230,7 @@ deviceString = deviceString.replace("'","\\'"); var devices = makeDeviceArray(); var manufacturers = new Array; var hardwares = new Array; +var availabilities = new Array; //Populate locations into table! populateDevices(); @@ -238,25 +249,32 @@ window.onclick = function(e){var add = document.getElementById('addModal'); var //set up options in modals var manuHTML = 'Choose Manufacturer'; var hardwareHTML = 'Choose Hardware'; +var availabilityHTML = 'Choose Availability'; for(var i = 0; i < manufacturers.length; i++) manuHTML += '' + manufacturers[i] + ''; for(var i = 0; i < hardwares.length; i++) hardwareHTML += '' + hardwares[i] + ''; +for(var i = 0; i < availabilities.length; i++) + availabilityHTML += '' + availabilities[i] + ''; document.getElementById('addManu').innerHTML = manuHTML; document.getElementById('addHardware').innerHTML = hardwareHTML; +document.getElementById('addAvailability').innerHTML = availabilityHTML; document.getElementById('modifyManu').innerHTML = manuHTML; document.getElementById('modifyHardware').innerHTML = hardwareHTML; +document.getElementById('modifyAvailability').innerHTML = availabilityHTML; //Places all locations from query into page function populateDevices(){ var html = ""; for(var i = 0; i < devices.length; i++){ - html += "" + devices[i].name + "" + devices[i].description + "" + devices[i].mac + "" + devices[i].manufacturer + "" + devices[i].hardware + "" + devices[i].model + "" + devices[i].serial + "" + devices[i].nfc + ""; + html += "" + devices[i].name + "" + devices[i].description + "" + devices[i].status + "" + devices[i].mac + "" + devices[i].manufacturer + "" + devices[i].hardware + "" + devices[i].model + "" + devices[i].serial + "" + devices[i].nfc + ""; if(findInArray(devices[i].manufacturer,manufacturers) == false){ manufacturers.push(devices[i].manufacturer); } if(findInArray(devices[i].hardware,hardwares) == false) hardwares.push(devices[i].hardware); + if(findInArray(devices[i].status,availabilities) == false) + availabilities.push(devices[i].status); } document.getElementById("tablebodymain").innerHTML = html; } @@ -286,6 +304,7 @@ function modifyModal(){ } $("#modifyName").val(devices[i].name.replace(/"/g,"\"")); $("#modifyDesc").val(devices[i].description.replace(/"/g,"\"")); + $("#modifyAvailability").val(devices[i].status); $("#modifyMAC").val(devices[i].mac); $("#modifyManu").val(devices[i].manufacturer.replace(/"/g,"\"")); $("#modifyHardware").val(devices[i].hardware.replace(/"/g,"\"")); @@ -312,6 +331,5 @@ function findInArray(string,array){ - \ No newline at end of file diff --git a/WebContent/html/webpages/deviceRedirect.jsp b/WebContent/html/webpages/deviceRedirect.jsp index 3dc00d4..d0e4ceb 100644 --- a/WebContent/html/webpages/deviceRedirect.jsp +++ b/WebContent/html/webpages/deviceRedirect.jsp @@ -1,7 +1,7 @@ -<%@ page import = "database.MySQLAccess,entities.Device" %> +<%@ page import = "database.*,entities.Device" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> - + @@ -31,16 +31,17 @@ 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"); -String nfc = request.getParameter("NFC"); +int nfc = Integer.parseInt(request.getParameter("NFC")); //add form was submitted if(request.getParameter("add") != null){ - Device device = new Device(name,description,mac,manufacturer,hardware,model,serial,nfc); - myaccess.addDevice(device); + Device device = new Device(name,1,description,hardware,model,manufacturer,status,mac,serial,nfc); + DeviceQueries.addDevice(device); } //modify form was submitted @@ -49,20 +50,20 @@ if(request.getParameter("modify") != null){ String strID = request.getParameter("id"); //turn to int for constructor int id = Integer.parseInt(strID); - Device device = new Device(name,id,description,mac,manufacturer,hardware,model,serial,nfc); - myaccess.modifyDevice(device); + 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); - myaccess.deleteLocation(id); + DeviceQueries.deleteDevice(id); } -*/ + %> diff --git a/src/database/DeviceQueries.java b/src/database/DeviceQueries.java index 9b05c33..2bbb939 100644 --- a/src/database/DeviceQueries.java +++ b/src/database/DeviceQueries.java @@ -3,16 +3,20 @@ import java.sql.*; import entities.Device; -import entities.ListedDevice; 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; @@ -27,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; @@ -47,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; } @@ -70,22 +74,25 @@ 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 void addDevice(Device device) throws SQLException, ClassNotFoundException{ + public static void addDevice(Device device) throws SQLException, ClassNotFoundException{ System.getenv("VCAP_SERVICES"); Class.forName("com.mysql.jdbc.Driver"); - Connection connect = DriverManager.getConnection("jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317", "b372dfe7409692", "74f6e317"); - Statement stmt = connect.createStatement(); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + int i = -1; int id=0; while(i <= 0){ @@ -95,11 +102,31 @@ public void addDevice(Device device) throws SQLException, ClassNotFoundException 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,Added_By,NFC_ID) " + "VALUES (" + id +",\"" + device.getName() + "\",\"" + device.getDesc()+ "\",\"" + device.getMAC() + "\",\"" + device.getManufacturer() + "\",\""+device.getHardware()+ "\",\"" + device.getModel() + "\",\"" + device.getSerial() + "\",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() + ");"; //TODO update the Added_By to include cookies System.out.println(command); i = stmt.executeUpdate(command); } stmt.close(); - connect.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 82e5645..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; diff --git a/src/entities/Device.java b/src/entities/Device.java index 5b339e5..2111da6 100644 --- a/src/entities/Device.java +++ b/src/entities/Device.java @@ -18,8 +18,8 @@ public class Device { 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_ID = id; Device_Name = name; + Device_ID = id; Device_Description = desc; Hardware = hardware; Model = model; @@ -29,28 +29,6 @@ public Device(String name, int id, String desc, String hardware, String model, S Serial = serial; NFC = nfc; } - - /** - * Constructor for newly created devices (so we omit the ID) - * @param name - * @param description - * @param mac - * @param manufacturer - * @param hardware - * @param model - * @param serial - * @param nfc - */ - public Device(String name, String description, String mac, String manufacturer, String hardware, String model, String serial, String nfc){ - Device_Name = name; - Device_Description = description; - MAC = mac; - Manu = manufacturer; - Hardware = hardware; - Model = model; - Serial = serial; - NFC = Integer.parseInt(nfc); - } /** * Formatting the device to fit a JSON object. */ @@ -124,4 +102,8 @@ public String getSerial(){ 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) {
' + devices[i].description + '