Skip to content

Commit

Permalink
Should now be generating a new location if needed-database needs adju…
Browse files Browse the repository at this point in the history
…stment though.

I'm storing dates as milliseconds, which don't fit as varchars. Also need to add user ID column to location table
  • Loading branch information
arc12012 committed Mar 18, 2017
1 parent 58f881b commit 9847360
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
23 changes: 19 additions & 4 deletions WebContent/html/webpages/orderFormHandler.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,32 @@ pageEncoding="ISO-8859-1"%>
<body>
<script type="text/javascript">
<%
//First update employee
MySQLAccess access = new MySQLAccess();
// If location id is 0, it is a custom location which must be added to the database before employee preffered location can be updated
int location=-1;
if(Integer.parseInt(request.getParameter("location_dropdown"))==0)
{
location = access.addLocation(
request.getParameter("state"),
request.getParameter("address"),
request.getParameter("town"),
Integer.parseInt(request.getParameter("zip"))
);
}
//Now update employee
String userName = request.getParameter("name");
String phone = request.getParameter("phone");
String location = request.getParameter("location");
MySQLAccess access = new MySQLAccess();
// read location ID, but only if we haven't already generated it when adding new location to db
if(location==-1){ location = Integer.parseInt(request.getParameter("location_dropdown"));}
access.updateEmployee(1, userName, phone, location); //TODO get ID somehow
System.out.print("updated employee "+userName);
%>alert("updated employee "+userName);<%
// And now for the ticket
// First we need to parse the string
// First we need to parse the string of devices
String rawJSONString = request.getParameter("deviceIDs");
rawJSONString = rawJSONString.replace("[","");
rawJSONString = rawJSONString.replace("]","");
Expand Down
4 changes: 2 additions & 2 deletions WebContent/html/webpages/shoppingCart.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function autoFillFields(orderAllBool) {
document.orderForm.location_dropdown.innerHTML="<option id = 0>New Location...</option>";
for (var i = locations.length - 1; i >= 0; i--) {
document.orderForm.location_dropdown.innerHTML+=
("<option id = "+locations[i].id
("<option value = "+locations[i].id
+ (locations[i].id == "<%=locationID%>" ? " selected>" : ">")
+ locations[i].name
+"</option>");
Expand All @@ -308,7 +308,7 @@ function autoFillFields(orderAllBool) {
// Set all the shipping fields to the database values correspoding to the location selected in the dropdown
function forceUpdateLocationMetadata() {
var selectedLocID = document.orderForm.location_dropdown.options[document.orderForm.location_dropdown.selectedIndex].id;
var selectedLocID = document.orderForm.location_dropdown.options[document.orderForm.location_dropdown.selectedIndex].value;
var selectedLocation;
for (var i = locations.length - 1; i >= 0; i--) {
if(locations[i].id==selectedLocID) selectedLocation=locations[i];
Expand Down
64 changes: 24 additions & 40 deletions src/database/MySQLAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,18 @@ public void initializeEmployee(int id) throws ClassNotFoundException, SQLExcepti
statement.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");");
}

public void updateEmployee(int id, String name, String phone, String location) throws ClassNotFoundException, SQLException
public void updateEmployee(int id, String name, String phone, int location) throws ClassNotFoundException, SQLException
{
System.out.print("Updating employee "+id+": "+name+".\nPhone: "+phone+", location: "+location);
System.out.println("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+="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;
}
query+="UPDATE employee SET ";
query+="Name='"+name;
query+="', Phone_Number='"+phone;
query+="', Location_ID="+location;
query+=" WHERE Employee_ID="+id;
System.out.println(query);
statement.executeUpdate(query);
}
Expand Down Expand Up @@ -130,9 +101,23 @@ public String locationIntToString(int locInt) throws ClassNotFoundException, SQL
return resultSet.getString("Address");
}

public Location[] getLocations() throws SQLException, ClassNotFoundException
// returns ID of new location
public int addLocation(String state, String address, String town, int zip) throws SQLException, ClassNotFoundException
{
Class.forName("com.myql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database,user,password);
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(Location_ID) FROM location");
int id = resultSet.getInt("COUNT(Location_ID)")+1; //this will only work if we never delete locations...
String name = "placeholder";
String query = "INSERT INTO location(Location_ID, Name, Address, Town, State, Zip_Code)"
+" VALUES ("+id+", "+name+", "+address+", "+town+", "+state+", "+zip+");";
statement.executeUpdate(query);
return id;
}

public Location[] getLocations() throws SQLException, ClassNotFoundException
{
int userID = 0; //TODO get real ID
String query = "SELECT * FROM location"; //TODO filter for custom locations (needs db change)
Class.forName("com.mysql.jdbc.Driver");
Expand All @@ -157,11 +142,10 @@ public Location[] getLocations() throws SQLException, ClassNotFoundException
}
return locations;
}
public void generateTicket(int requester, String location, int device) throws ClassNotFoundException, SQLException
public void generateTicket(int requester, int 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;
int ticketID = (int)((time+requester+device+location)%10000);
String stat = "Requested";
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(database,user,password);
Expand Down

0 comments on commit 9847360

Please sign in to comment.