diff --git a/WebContent/html/javascript/request.jsp b/WebContent/html/javascript/request.jsp index 5414824..dd78f9a 100644 --- a/WebContent/html/javascript/request.jsp +++ b/WebContent/html/javascript/request.jsp @@ -8,7 +8,7 @@ <% -Device[] mydevices = DeviceQueries.getAllDevices(); +Device[] mydevices = DeviceQueries.getAvailableDevices(); //string representation of array. String deviceString = Device.arrayToString(mydevices); //out.println(description); @@ -150,7 +150,8 @@ function addToCart(){ if(!inCart(id)){ //if not in the cart var cart = getCartItems(); //this is an array id = parseInt(id,10); //this gets just the numerical value from the id! - cart.push(devices[id - 1]); //push to bottom of cart + var device = getDevice(id); + cart.push(device); //push to bottom of cart localStorage.setItem('cart', JSON.stringify(cart)); $('#added').fadeIn(1000); $('#added').fadeIn(1000); @@ -189,6 +190,12 @@ function makeDeviceArray(){ window.json = '<%=deviceString%>'; return JSON.parse(window.json); } +function getDevice(id){ + for(var i = 0; i < devices.length; i++){ + if(id == devices[i].id) + return devices[i]; + } +} //this code allows a message to appear that indicates that the item was successfully placed in the shopping cart. diff --git a/src/database/DeviceQueries.java b/src/database/DeviceQueries.java index 5e3dc3a..54986d5 100644 --- a/src/database/DeviceQueries.java +++ b/src/database/DeviceQueries.java @@ -62,6 +62,32 @@ public static Device[] getAllDevices() throws SQLException, ClassNotFoundExcepti return devices; } + public static Device[] getAvailableDevices() throws SQLException, ClassNotFoundException{ + //database connect + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM devices WHERE Status = \"Available\""); + int counter = 0; + + resultSet.last(); + int rows = resultSet.getRow(); + resultSet.beforeFirst(); + + //Covers amount of rows, and 6 attributes (indices 0-5) + Device[] devices = new Device[rows]; + + //iterate result set + while(resultSet.next()){ + 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++; + } + stmt.close(); + connection.close(); + return devices; + } + /** * This function will take devices as input and return them. * More specifically, the input will be a string that looks like an array.