Skip to content

Commit

Permalink
Autofill updated for new address filds and location dropdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
arc12012 committed Mar 17, 2017
1 parent b7bddb4 commit 8b3cc0b
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 23 deletions.
7 changes: 7 additions & 0 deletions WebContent/html/css/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ input[type=text]{
width: 60%;
}

select{
margin:0 auto;
width: 65%;
align-self: center;
float: center;
}


span.close{
color: #aaa;
Expand Down
107 changes: 84 additions & 23 deletions WebContent/html/webpages/shoppingCart.jsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<%@ page import = "database.MySQLAccess" %>
<%@ page import = "entities.User" %>
<%@ page import = "entities.Location" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
Expand Down Expand Up @@ -52,7 +54,7 @@ pageEncoding="ISO-8859-1"%>
<%@ include file="navbar.html"%>
</nav>
<div id="orderInfoModal" class="modal">
<div class="modal-content">
<div class="modal-content" style="overflow-y: auto; overflow-x: hidden">
<div class="modal-head">
<span id="closeOrderForm" class="close">&times;</span>
<h4>Please provide additional order information</h4>
Expand All @@ -72,44 +74,43 @@ pageEncoding="ISO-8859-1"%>
</div>

<div class="form-group">
<label for="location">Place to ship to</label><br/>
<input type="text" class="form-control" list="listid" name="location" placeholder="dropdown"/>
<datalist id="listid">
<option>t</option>
</datalist>
<label for="location_dropdown">Place to ship to</label><br/>
<select class="form-control" name="location_dropdown" id="dropdown" onchange="forceUpdateLocationMetadata()" style="width:65%; max-height: 80%; text-align: center; vertical-align: center; align-items: center; display: block; position: relative; margin: 5% auto;">
</select>
</div>
<div class="form-group-row">
<div class="col-md-6" style="margin: 0%">
<div class="form-group-row">
<div class="form-group row" style="display: block; align-items: center;margin: 5% auto;">
<div class="col-md-6" style="width:50%">
<div class="form-group row">
<label for="address">Street Address</label>
<input type="text" class="form-control" name="address" placeholder="Address"/>
</div>
<input type="text" class="form-control" name="address" placeholder="Address"/>
<div class="form-group-row">
<div class="form-group row">
<label for="city">City</label>
<input type="text" class="form-control" name="city" placeholder="City"/>
</div>
<input type="text" class="form-control" name="city" placeholder="City"/>
</div>
<div class="col-md-6" style="margin: 0%">
<div class="form-group-row">
<div class="col-md-6" style="width:50%">
<div class="form-group row">
<label for="state">State</label>
<input type="text" class="form-control" name="state" placeholder="State"/>
</div>
<input type="text" class="form-control" name="state" placeholder="State"/>
<div class="form-group-row">
<div class="form-group row">
<label for="zip">Zip</label>
<input type="text" class="form-control" name="zip" placeholder="Zip Code"/>
</div>
<input type="text" class="form-control" name="zip" placeholder="Zip Code"/>
</div>
</div>
<div class="form-group">
<div class="form-group row">
<label for="timeNeeded">How many weeks do you think you'll need it?</label><br/>
<input type="text" class="form-control" name="timeNeeded" placeholder="Just estimate the number of weeks" />
</div>
<button type="submit" class="btn btn-primary" name="Submit">Submit</button>
</form>

</div>

</div>
</div>

<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<h4>Shopping Cart Options</h4>
Expand All @@ -136,6 +137,11 @@ document.getElementById('ds').addEventListener('click',deleteSelected);
document.getElementById('oa').addEventListener('click',orderAll_showPopup);
document.getElementById('da').addEventListener('click',deleteAll);
document.orderForm.address.addEventListener('onkeyup',forceDropdownCustomLocation);
document.orderForm.city.addEventListener('onkeyup',forceDropdownCustomLocation);
document.orderForm.state.addEventListener('onkeyup',forceDropdownCustomLocation);
document.orderForm.zip.addEventListener('onkeyup',forceDropdownCustomLocation);
document.onkeydown = function(e) {
if (e.keyCode == 27) {
hidePopup();
Expand All @@ -160,6 +166,7 @@ var employee = {
phone:""
}
var checked = new Array();
var locations = getLocations();
debug_add_item_to_cart();
show(); //on load, we want to show everything
Expand Down Expand Up @@ -269,24 +276,78 @@ function hidePopup(){
// Populates orderform fields with database information
// Boolean parameter is applied to the hidden 'orderAll' field on the
// order form, which is read before submition to determine which devices to include
// order form, which is read before submission to determine which devices to include
function autoFillFields(orderAllBool) {
// get employee
<%
MySQLAccess access = new MySQLAccess();
User employee = access.getEmployeeByID(9); //replace with real ID
String location = employee.getLocationName();
int locationID = employee.getLocation();
String name = employee.getName();
String phone = employee.getPhone();
%>
employee.locationName = "<%=location%>";
employee.name = "<%=name%>";
employee.phone = "<%=phone%>";
document.orderForm.name.value=employee.name;
document.orderForm.location.value=employee.locationName;
document.orderForm.phone.value=employee.phone;
// already got locations on page load, now populate dropdown and mark appropriate option selected
document.orderForm.location_dropdown.innerHTML="<option id = -1>New Location...</option>";
for (var i = locations.length - 1; i >= 0; i--) {
document.orderForm.location_dropdown.innerHTML+=
("<option id = "+locations[i].id
+ (locations[i].id == "<%=locationID%>" ? " selected>" : ">")
+ locations[i].name
+"</option>");
}
// and fill in the location sub-fieds
forceUpdateLocationMetadata();
// finally mark hidden orderAll field appropriately
document.orderForm.orderAll.value=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 selectedLocation;
for (var i = locations.length - 1; i >= 0; i--) {
if(locations[i].id==selectedLocID) selectedLocation=locations[i];
}
if(selectedLocID!=-1) //-1 is the 'new location' option
{
document.orderForm.address.value = selectedLocation.address;
document.orderForm.city.value = selectedLocation.town;
document.orderForm.state.value = selectedLocation.state;
document.orderForm.zip.value = selectedLocation.zip;
}
}
function forceDropdownCustomLocation() {
$("#dropdown").val('0');
}
function getLocations() {
<%
Location[] loci = access.getLocations();
%>
var loci = new Array(<%=loci.length%>);
<%
for(int i = 0; i<loci.length; i++)
{
%>
loci[<%=i%>]={
id:<%=loci[i].getId()%>,
name:"<%=loci[i].getName()%>",
address:"<%=loci[i].getAddress()%>",
town:"<%=loci[i].getTown()%>",
state:"<%=loci[i].getState()%>",
zip:<%=loci[i].getZip()%>
};
<%
}
%>
return loci;
}
function submitOrderForm() {
// This is called just before order form is submitted. In order for any database activity to occur,
// we must first append the employees ID as well as the ids of whatever devices are selected.
Expand Down
28 changes: 28 additions & 0 deletions src/database/MySQLAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.*;
import java.util.Date;

import entities.Location;
import entities.User;

public class MySQLAccess {
Expand Down Expand Up @@ -129,6 +130,33 @@ public String locationIntToString(int locInt) throws ClassNotFoundException, SQL
return resultSet.getString("Address");
}

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");
Connection connect = DriverManager.getConnection(database,user,password);
Statement statement = connect.createStatement();
ResultSet rs = statement.executeQuery("SELECT COUNT(Location_ID) FROM location");
rs.next();
Location[] locations = new Location[rs.getInt("COUNT(Location_ID)")];
rs = statement.executeQuery(query);
int i=0;
while(rs.next())
{
locations[i] = new Location(
rs.getInt("Location_ID"),
rs.getString("Name"),
rs.getString("Address"),
rs.getString("Town"),
rs.getString("State"),
rs.getInt("Zip_Code")
);
i++;
}
return locations;
}
public void generateTicket(int requester, String location, int device) throws ClassNotFoundException, SQLException
{
long time = new Date().getTime(); //Using milliseconds because sql is annoying with dates.
Expand Down
68 changes: 68 additions & 0 deletions src/entities/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package entities;

public class Location {
private int id;
private String name;
private String address;
private String town;
private String state;
private int zip;

public Location(int id, String name, String address, String town, String state, int zip)
{
this.id = id;
this.name = name;
this.address = address;
this.town = town;
this.state = state;
this.zip = zip;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getTown()
{
return town;
}
public void setTown(String town)
{
this.town = town;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
public int getZip()
{
return zip;
}
public void setZip(int zip)
{
this.zip = zip;
}
}

0 comments on commit 8b3cc0b

Please sign in to comment.