diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml new file mode 100644 index 0000000..c98448e --- /dev/null +++ b/WebContent/WEB-INF/web.xml @@ -0,0 +1,11 @@ + + + Application Name + + index.jsp + + + 404 + /html/webpages/error.jsp + + \ No newline at end of file diff --git a/WebContent/adminLogin.jsp b/WebContent/adminLogin.jsp index de40b3c..6341098 100644 --- a/WebContent/adminLogin.jsp +++ b/WebContent/adminLogin.jsp @@ -8,10 +8,15 @@ String pass; Cookie adminCookie; ssoNum = Integer.parseInt(request.getParameter("ssoNum2")); pass = request.getParameter("pass"); -MySQLAccess myaccess = new MySQLAccess(); -Statement statement = myaccess.getStatement(); +String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; +String user = "b372dfe7409692"; +String password = "74f6e317"; +System.getenv("VCAP_SERVICES"); +Class.forName("com.mysql.jdbc.Driver"); +Connection connection = DriverManager.getConnection(database, user, password); +Statement stmt = connection.createStatement(); ResultSet result; -result = statement.executeQuery("select * FROM admin where Admin_ID='" + ssoNum + "' AND Password='" + pass + "'"); +result = stmt.executeQuery("select * FROM admin where Admin_ID='" + ssoNum + "' AND Password='" + pass + "'"); if (result.next()){ request.getSession(); @@ -19,10 +24,12 @@ if (result.next()){ adminCookie = new Cookie("ssoNum", Integer.toString(ssoNum)); adminCookie.setMaxAge(30*60); response.addCookie(adminCookie); - statement.close(); + stmt.close(); + connection.close(); response.sendRedirect("html/webpages/admin.jsp"); } else { - statement.close(); + stmt.close(); + connection.close(); response.sendRedirect("index.jsp"); } diff --git a/WebContent/html/imgs/my-icons-collection-128px/png/warning.png b/WebContent/html/imgs/my-icons-collection-128px/png/warning.png new file mode 100644 index 0000000..68f62c6 Binary files /dev/null and b/WebContent/html/imgs/my-icons-collection-128px/png/warning.png differ diff --git a/WebContent/html/imgs/my-icons-collection-128px/png/wrong.png b/WebContent/html/imgs/my-icons-collection-128px/png/wrong.png new file mode 100644 index 0000000..6107cee Binary files /dev/null and b/WebContent/html/imgs/my-icons-collection-128px/png/wrong.png differ diff --git a/WebContent/html/webpages/adminDeviceSettings.jsp b/WebContent/html/webpages/adminDeviceSettings.jsp index 798da3e..65db663 100644 --- a/WebContent/html/webpages/adminDeviceSettings.jsp +++ b/WebContent/html/webpages/adminDeviceSettings.jsp @@ -265,10 +265,13 @@ window.onclick = function(e){var add = document.getElementById('addModal'); var var manuHTML = ''; var hardwareHTML = ''; var availabilityHTML = ''; +manufacturers.sort(); for(var i = 0; i < manufacturers.length; i++) manuHTML += ''; + hardwares.sort(); for(var i = 0; i < hardwares.length; i++) hardwareHTML += ''; +availabilities.sort(); for(var i = 0; i < availabilities.length; i++) availabilityHTML += ''; document.getElementById('addManu').innerHTML = manuHTML; diff --git a/WebContent/html/webpages/adminLocation.jsp b/WebContent/html/webpages/adminLocation.jsp index 1e75e6d..1a22413 100644 --- a/WebContent/html/webpages/adminLocation.jsp +++ b/WebContent/html/webpages/adminLocation.jsp @@ -230,8 +230,8 @@ - - + + @@ -323,6 +323,15 @@ function modifyModal(){ $("#modifyState").val(locations[i].state); $("#modifyZip").val(locations[i].zip); $("#modifyID").val(locations[i].id); + + if(locations[i].numDevices > 0 || locations[i].numEmployees > 0){ + $('#thedeletebutton').attr('disabled',true); + $('#thedeletebutton').attr('title','Cannot delete because devices and/or employees are at this location!'); + } + else{ + $('#thedeletebutton').attr('disabled',false); + $('#thedeletebutton').removeAttr('title'); + } $("#modifyModal").show(); } diff --git a/WebContent/html/webpages/deviceRedirect.jsp b/WebContent/html/webpages/deviceRedirect.jsp index d0e4ceb..d3e9558 100644 --- a/WebContent/html/webpages/deviceRedirect.jsp +++ b/WebContent/html/webpages/deviceRedirect.jsp @@ -20,6 +20,7 @@ @@ -28,7 +29,6 @@ <% //make instance -MySQLAccess myaccess = new MySQLAccess(); String name = request.getParameter("name").replace("\"","\\\""); String description = request.getParameter("description").replace("\"","\\\""); String status = request.getParameter("status"); diff --git a/WebContent/html/webpages/error.jsp b/WebContent/html/webpages/error.jsp new file mode 100644 index 0000000..91076ee --- /dev/null +++ b/WebContent/html/webpages/error.jsp @@ -0,0 +1,40 @@ + + + + + + + + + + + Synchrony Financial + + + + + + + + + + + +
+

Error

+ +

Page Does Not Exist

+
+ + \ No newline at end of file diff --git a/WebContent/html/webpages/locationRedirect.jsp b/WebContent/html/webpages/locationRedirect.jsp index 5483488..3995a44 100644 --- a/WebContent/html/webpages/locationRedirect.jsp +++ b/WebContent/html/webpages/locationRedirect.jsp @@ -34,6 +34,7 @@ String address = request.getParameter("address").replace("\"","\\\""); String town = request.getParameter("town").replace("\"","\\\""); String state = request.getParameter("state"); String zip = request.getParameter("zip"); +int er = 0; //add form was submitted if(request.getParameter("add") != null){ Location location = new Location(name,address,town,state,zip,0); @@ -52,11 +53,14 @@ if(request.getParameter("modify") != null){ if(request.getParameter("delete") != null){ String strID = request.getParameter("id"); int id = Integer.parseInt(strID); - LocationQueries.deleteLocation(id); + er = LocationQueries.deleteLocation(id); } %> diff --git a/WebContent/html/webpages/navbar.jsp b/WebContent/html/webpages/navbar.jsp index ab6850c..b44f3f2 100644 --- a/WebContent/html/webpages/navbar.jsp +++ b/WebContent/html/webpages/navbar.jsp @@ -8,10 +8,11 @@ - + -      -

+
+ +

+ +<% +Cookie[] usercookies = request.getCookies(); +String navsso = "invalid"; +String navname = "error"; +User self = new User(); +Cookie newCookie; +//iterate cookies +if(usercookies != null){ + for(Cookie c : usercookies){ + if(c.getName().equals("ssoNum")){//when (and if) we get to user cookie we want to reset it + navsso = c.getValue(); + c.setMaxAge(0);//delete current + newCookie = new Cookie("ssoNum",navsso); //make new one + newCookie.setMaxAge(30*60); + response.addCookie(newCookie); + break; + } + } +} +if(navsso.equals("invalid") == false)//if we did get cookie, redirect to the homepage! + response.sendRedirect("html/webpages/index.jsp"); + %> diff --git a/WebContent/userLogin.jsp b/WebContent/userLogin.jsp index 5c3805d..54f84ca 100644 --- a/WebContent/userLogin.jsp +++ b/WebContent/userLogin.jsp @@ -6,10 +6,15 @@ int ssoNum; Cookie userCookie; ssoNum = Integer.parseInt(request.getParameter("ssoNum")); -MySQLAccess myaccess = new MySQLAccess(); -Statement statement = myaccess.getStatement(); +System.getenv("VCAP_SERVICES"); +Class.forName("com.mysql.jdbc.Driver"); +String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; +String user = "b372dfe7409692"; +String password = "74f6e317"; +Connection connection = DriverManager.getConnection(database, user, password); +Statement stmt = connection.createStatement(); ResultSet result; -result = statement.executeQuery("select * FROM employee where Employee_ID='" + ssoNum + "'"); +result = stmt.executeQuery("select * FROM employee where Employee_ID='" + ssoNum + "'"); if (result.next()){ request.getSession(); @@ -17,10 +22,12 @@ if (result.next()){ userCookie = new Cookie("ssoNum", Integer.toString(ssoNum)); userCookie.setMaxAge(30*60); response.addCookie(userCookie); - statement.close(); + stmt.close(); + connection.close(); response.sendRedirect("html/webpages/index.jsp"); } else { - statement.close(); + stmt.close(); + connection.close(); response.sendRedirect("index.jsp"); } diff --git a/src/database/EmployeeQueries.java b/src/database/EmployeeQueries.java index 5930e08..3c5b3af 100644 --- a/src/database/EmployeeQueries.java +++ b/src/database/EmployeeQueries.java @@ -12,17 +12,22 @@ public class EmployeeQueries { public static void initializeEmployee(int id) throws ClassNotFoundException, SQLException { - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); - statement.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");"); - access.closeConnection(); + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + stmt.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");"); + stmt.close(); + connection.close(); } public static void updateEmployee(int id, String name, String phone, int location, String email) throws ClassNotFoundException, SQLException { System.out.println("Updating employee "+id+": "+name+".\nPhone: "+phone+", location: "+location); - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); String query = ""; query+="UPDATE employee SET "; query+="Name='"+name; @@ -31,8 +36,9 @@ public static void updateEmployee(int id, String name, String phone, int locatio query+=", Email='"+email; query+="' WHERE Employee_ID="+id; System.out.println(query); - statement.executeUpdate(query); - access.closeConnection(); + stmt.executeUpdate(query); + stmt.close(); + connection.close(); } public static void updateEmployee(int id, String name, int icon, String phone, String email) throws ClassNotFoundException, SQLException @@ -57,9 +63,11 @@ public static void updateEmployee(int id, String name, int icon, String phone, S public static User getEmployeeByID(int id) throws ClassNotFoundException, SQLException { - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); - ResultSet resultSet = statement.executeQuery("Select * FROM employee WHERE Employee_ID = "+id); + 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 employee WHERE Employee_ID = "+id); resultSet.next(); User employee = new User( id, @@ -69,18 +77,20 @@ public static User getEmployeeByID(int id) throws ClassNotFoundException, SQLExc resultSet.getString("Email"), resultSet.getInt("Img_Index") ); - resultSet = statement.executeQuery("SELECT Address FROM location WHERE Location_ID = "+employee.getLocation()); + resultSet = stmt.executeQuery("SELECT Address FROM location WHERE Location_ID = "+employee.getLocation()); resultSet.next(); employee.setLocationName(resultSet.getString("Address")); - access.closeConnection(); + stmt.close(); + connection.close(); return employee; } public static User[] getAllUsers() 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 employee"); int counter = 0; @@ -97,7 +107,8 @@ public static User[] getAllUsers() throws SQLException, ClassNotFoundException{ counter++; } - access.closeConnection(); + stmt.close(); + connection.close(); return users; } } \ No newline at end of file diff --git a/src/database/LocationQueries.java b/src/database/LocationQueries.java index 7dc01a1..eb5d6f5 100644 --- a/src/database/LocationQueries.java +++ b/src/database/LocationQueries.java @@ -6,24 +6,36 @@ public class LocationQueries { + 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 int locationStringToInt(String locString) throws SQLException, ClassNotFoundException { - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); - ResultSet resultSet = statement.executeQuery("SELECT Location_ID FROM location WHERE Address = '"+locString+"'"); + 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 Location_ID FROM location WHERE Address = '"+locString+"'"); resultSet.next(); - access.closeConnection(); - return resultSet.getInt("Location_ID"); + int returnResult = resultSet.getInt("Location_ID"); + stmt.close(); + connection.close(); + return returnResult; } public static String locationIntToString(int locInt) throws ClassNotFoundException, SQLException { - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); - ResultSet resultSet = statement.executeQuery("SELECT Address FROM location WHERE Location_ID = "+locInt); + 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 Address FROM location WHERE Location_ID = "+locInt); resultSet.next(); - access.closeConnection(); - return resultSet.getString("Address"); + String returnResult = resultSet.getString("Address"); + stmt.close(); + connection.close(); + return returnResult; } // returns ID of new location @@ -36,8 +48,9 @@ public static int addLocation(String name, String state, String address, String public static int addLocation(Location location) throws SQLException, ClassNotFoundException { 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(); int i = -1; int id=0; while(i <= 0){ @@ -50,16 +63,18 @@ public static int addLocation(Location location) throws SQLException, ClassNotFo System.out.println("INSERT INTO location (Location_ID,Name,Address,Town,State,Zip_Code,Employee_Flag) VALUES (" + id +",\"" + location.getName() + "\",\"" + location.getAddress()+ "\",\"" + location.getTown() + "\",\"" + location.getState() + "\",\"" + location.getZip()+"\","+location.getEmployeeFlag() + ")"); i = stmt.executeUpdate("INSERT INTO location (Location_ID,Name,Address,Town,State,Zip_Code,Employee_Flag) VALUES (" + id +",\"" + location.getName() + "\",\"" + location.getAddress()+ "\",\"" + location.getTown() + "\",\"" + location.getState() + "\",\"" + location.getZip()+"\","+location.getEmployeeFlag() + ")"); } - access.closeConnection(); + stmt.close(); + connection.close(); return id; } public static Location[] getAdminLocations() throws ClassNotFoundException, SQLException{ //database connect System.getenv("VCAP_SERVICES"); - MySQLAccess access = new MySQLAccess(); - Statement stmt = access.getStatement(); - ResultSet resultSet = stmt.executeQuery("SELECT * FROM location WHERE Zip_Code <> \"00000\""); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM location WHERE Employee_Flag = 0"); int counter = 0; resultSet.last(); @@ -74,7 +89,8 @@ public static Location[] getAdminLocations() throws ClassNotFoundException, SQLE locations[counter] = new Location(resultSet.getInt("Location_ID"),resultSet.getString("Name"),resultSet.getString("Address"),resultSet.getString("Town"),resultSet.getString("State"),resultSet.getString("Zip_Code")); counter++; } - access.closeConnection(); + stmt.close(); + connection.close(); return locations; } @@ -85,12 +101,14 @@ public static Location[] getLocations() throws ClassNotFoundException, SQLExcept public static Location[] getLocations(int userID) throws SQLException, ClassNotFoundException { String query = "SELECT * FROM location WHERE Employee_Flag = 0 OR Employee_Flag = "+userID+" ORDER BY Name ASC"; //TODO filter for custom locations (needs db change) - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); - ResultSet rs = statement.executeQuery("SELECT COUNT(Location_ID) FROM location WHERE Employee_Flag = 0 OR Employee_Flag = "+userID); + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT COUNT(Location_ID) FROM location WHERE Employee_Flag = 0 OR Employee_Flag = "+userID); rs.next(); Location[] locations = new Location[rs.getInt("COUNT(Location_ID)")]; - rs = statement.executeQuery(query); + rs = stmt.executeQuery(query); int i=0; while(rs.next()) { @@ -104,7 +122,8 @@ public static Location[] getLocations(int userID) throws SQLException, ClassNotF ); i++; } - access.closeConnection(); + stmt.close(); + connection.close(); return locations; } @@ -130,38 +149,79 @@ public static void modifyLocation(Location location) throws ClassNotFoundExcepti * @throws ClassNotFoundException * @throws SQLException */ - public static void deleteLocation(int id) throws ClassNotFoundException, SQLException{ + public static int deleteLocation(int id) throws ClassNotFoundException, SQLException{ 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(); - stmt.executeUpdate("DELETE from location WHERE Location_ID = " + id); + try { + stmt.executeUpdate("DELETE from location WHERE Location_ID = " + id); + } catch (SQLException e) { + stmt.close(); + connect.close(); + return -1; + } stmt.close(); connect.close(); + return 0; } public static Location[] getAllLocations() throws SQLException, ClassNotFoundException{ //database connect - System.getenv("VCAP_SERVICES"); - MySQLAccess access = new MySQLAccess(); - Statement stmt = access.getStatement(); - ResultSet resultSet = stmt.executeQuery("SELECT * FROM location"); - int counter = 0; - - resultSet.last(); - int rows = resultSet.getRow(); - resultSet.beforeFirst(); - - //Covers amount of rows, and 6 attributes (indices 0-5) - Location[] locations = new Location[rows]; - - //iterate result set - while(resultSet.next()){ - locations[counter] = new Location(resultSet.getInt("Location_ID"),resultSet.getString("Name"),resultSet.getString("Address"),resultSet.getString("Town"),resultSet.getString("State"),resultSet.getString("Zip_Code")); - counter++; - } - - access.closeConnection(); - return locations; + 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 location"); + int counter = 0; + + resultSet.last(); + int rows = resultSet.getRow(); + resultSet.beforeFirst(); + + //Covers amount of rows, and 6 attributes (indices 0-5) + Location[] locations = new Location[rows]; + + //iterate result set + while(resultSet.next()){ + locations[counter] = new Location(resultSet.getInt("Location_ID"),resultSet.getString("Name"),resultSet.getString("Address"),resultSet.getString("Town"),resultSet.getString("State"),resultSet.getString("Zip_Code")); + counter++; + } + + stmt.close(); + connection.close(); + return locations; + } + + /** + * This method returns the number of devices at a given location + * @return + * @throws ClassNotFoundException + * @throws SQLException + */ + public static int numberDevices(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(); + ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM devices WHERE Location = " + id ); + resultSet.next(); + int num = resultSet.getInt(1); + stmt.close(); + connection.close(); + return num; + } + + public static int numberEmployees(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(); + ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM employee WHERE Location_ID = " + id ); + resultSet.next(); + int num = resultSet.getInt(1); + stmt.close(); + connection.close(); + return num; } } \ No newline at end of file diff --git a/src/database/MySQLAccess.java b/src/database/MySQLAccess.java deleted file mode 100644 index 66714ee..0000000 --- a/src/database/MySQLAccess.java +++ /dev/null @@ -1,32 +0,0 @@ -package database; - -import java.sql.*; - -public class MySQLAccess { - - 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; - - public MySQLAccess() throws ClassNotFoundException, SQLException { - connectDB(); - } - - public void connectDB() throws SQLException, ClassNotFoundException { - Class.forName("com.mysql.jdbc.Driver"); - connection = DriverManager.getConnection(database, user, password); - statement = connection.createStatement(); - } - - public Statement getStatement() { - return statement; - } - - public void closeConnection() throws SQLException { - statement.close(); - connection.close(); - } -} \ No newline at end of file diff --git a/src/database/TicketQueries.java b/src/database/TicketQueries.java index ca8eff3..53ffa3d 100644 --- a/src/database/TicketQueries.java +++ b/src/database/TicketQueries.java @@ -6,13 +6,19 @@ public class TicketQueries { + 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 void generateTicket(int requester, int location, int device, String returnDate) throws ClassNotFoundException, SQLException { long time = new Date().getTime(); //Using milliseconds because sql is annoying with dates. int ticketID = (int)((time+requester+device+location)%10000); String stat = "Requested"; - MySQLAccess access = new MySQLAccess(); - Statement statement = access.getStatement(); + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); String query = ""; query+="INSERT INTO ticket (Ticket_ID, Requestor, Request_Date, Location, Device_ID, Status, Status_Date_Fields, Return_Date) "; query+="VALUES ("; @@ -25,20 +31,23 @@ public static void generateTicket(int requester, int location, int device, Strin query+= "'"+time+"'" +","; query+= "'"+returnDate+"'" +");"; System.out.println(query); - statement.executeUpdate(query); - access.closeConnection(); + stmt.executeUpdate(query); + stmt.close(); + connection.close(); } //get all tickets public Ticket[] getTickets() throws SQLException, ClassNotFoundException{ String status = "Requested"; int i = 0; - MySQLAccess myaccess = new MySQLAccess(); - Statement statement = myaccess.getStatement(); - ResultSet resultSet = statement.executeQuery("Select COUNT(Ticket_ID) FROM ticket WHERE Status = '" +status + "'"); + 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 COUNT(Ticket_ID) FROM ticket WHERE Status = '" +status + "'"); resultSet.next(); Ticket[] tickets = new Ticket[resultSet.getInt("COUNT(Ticket_ID)")]; - resultSet = statement.executeQuery("Select * FROM ticket WHERE Status = '" +status + "'"); + resultSet = stmt.executeQuery("Select * FROM ticket WHERE Status = '" +status + "'"); while(resultSet.next()){ tickets[i] = new Ticket( resultSet.getInt("Ticket_ID"), @@ -51,14 +60,15 @@ public Ticket[] getTickets() throws SQLException, ClassNotFoundException{ ); i++; } - myaccess.closeConnection(); + stmt.close(); + connection.close(); return tickets; } public static void acceptTicket(int ticketid, int deviceid, int locationid, int sso) 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"); + Connection connect = DriverManager.getConnection(database, user, password); Statement stmt = connect.createStatement(); stmt.executeUpdate("UPDATE ticket SET Status = \"Approved\" WHERE Ticket_ID = " + ticketid); stmt.executeUpdate("UPDATE devices SET Ticket_ID = " + ticketid + ", Status = \"Shipped\", Renter = " + sso + ", Location = " + locationid + " WHERE Device_ID = " + deviceid); @@ -69,7 +79,7 @@ public static void acceptTicket(int ticketid, int deviceid, int locationid, int public static void rejectTicket(int id) 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"); + Connection connect = DriverManager.getConnection(database, user, password); Statement stmt = connect.createStatement(); stmt.executeUpdate("UPDATE ticket SET Status = \"Rejected\" WHERE Ticket_ID = " + id); stmt.close(); diff --git a/src/entities/Location.java b/src/entities/Location.java index a2a19d8..97c044e 100644 --- a/src/entities/Location.java +++ b/src/entities/Location.java @@ -1,5 +1,9 @@ package entities; +import java.sql.SQLException; + +import database.LocationQueries; + /** * Representation of locations from database. * @author conno @@ -13,6 +17,8 @@ public class Location { private String state; private String zip; private int employee_flag; + private int numDevices; + private int numEmployees; /** * Constructor for existing location. @@ -21,14 +27,18 @@ public class Location { * @param town * @param state * @param zip + * @throws SQLException + * @throws ClassNotFoundException */ - public Location(int id, String name, String address, String town, String state, String zip){ + public Location(int id, String name, String address, String town, String state, String zip) throws ClassNotFoundException, SQLException{ this.id = id; this.name = name; this.address = address; this.town = town; this.state = state; this.zip = zip; + this.numDevices = LocationQueries.numberDevices(id); + this.numEmployees = LocationQueries.numberEmployees(id); } /** @@ -38,22 +48,28 @@ public Location(int id, String name, String address, String town, String state, * @param town * @param state * @param zip + * @throws SQLException + * @throws ClassNotFoundException */ - public Location(String name, String address, String town, String state, String zip){ + public Location(String name, String address, String town, String state, String zip) throws ClassNotFoundException, SQLException{ this.name = name; this.address = address; this.town = town; this.state = state; this.zip = zip; + this.numDevices = LocationQueries.numberDevices(id); + this.numEmployees = LocationQueries.numberEmployees(id); } - public Location(String name, String address, String town, String state, String zip, int employee_flag) { + public Location(String name, String address, String town, String state, String zip, int employee_flag) throws ClassNotFoundException, SQLException { this.name = name; this.address = address; this.town = town; this.state = state; this.zip = zip; this.employee_flag = employee_flag; + this.numDevices = LocationQueries.numberDevices(id); + this.numEmployees = LocationQueries.numberEmployees(id); } /** @@ -67,6 +83,8 @@ public String toString(){ sb.append("\"address\": \"").append(address.replace("\"", """)).append("\"").append(comma); sb.append("\"town\": \"").append(town.replace("\"", """)).append("\"").append(comma); sb.append("\"state\": \"").append(state).append("\"").append(comma); + sb.append("\"numDevices\":").append(numDevices).append(comma); + sb.append("\"numEmployees\":").append(numEmployees).append(comma); sb.append("\"zip\": \"").append(zip).append("\""); sb.append("}"); return sb.toString(); @@ -120,6 +138,5 @@ public void setEmployeeFlag(int ID) public int getEmployeeFlag() { return employee_flag; - } - + } } diff --git a/src/entities/User.java b/src/entities/User.java index 6b600fd..34fdd60 100644 --- a/src/entities/User.java +++ b/src/entities/User.java @@ -9,6 +9,10 @@ public class User { private String email; private int icon; + public User(){ + //empty + } + public User(int id, int location, String name, String phone, String email, int icon){ this.id = id; this.location = location;