Skip to content

Commit

Permalink
Latitude and Longitude Support
Browse files Browse the repository at this point in the history
  • Loading branch information
clj13001 committed Apr 18, 2017
1 parent b33a427 commit a8b1ed8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 60 deletions.
51 changes: 47 additions & 4 deletions WebContent/html/webpages/administration/adminLocation.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCndOcexx4GAOGkRKBeK1E43u_sB9gjcaU">
</script>
<link rel = "stylesheet" type = "text/css" href = "../../css/stylesheet.css">
<link rel = "shortcut icon" href = "../../imgs/synchrony-financial-logo-dlpx_1.ico">
<style>
Expand Down Expand Up @@ -72,7 +75,7 @@
</div>
<div class = "form-group">
<label for="addState">State *</label>
<select required style = "width: 150px;" name = "state" class="form-control">
<select id = "addState" required style = "width: 150px;" name = "state" class="form-control">
<option value="" disabled selected>Choose State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
Expand Down Expand Up @@ -131,7 +134,15 @@
<label for="addZip">Zip Code *</label>
<input style = "width: 100px;" name = "zip" type = "number" class="form-control" id="addZip" placeholder="Zip Code" required = "true">
</div>
<button type = "submit" name = "add" value = "Add" class="btn btn-primary">Add</button>
<div class = "form-group">
<label for="addlat">Latitude</label>
<input style = "width: 150px;" type = "text" id = "addlat" class="form-control" name = "lat">
</div>
<div class = "form-group">
<label for="addlng">Longitude</label>
<input style = "width: 150px;" type = "text" id = "addlng" class="form-control" name = "lng">
</div>
<button id = "theaddbutton" type = "submit" name = "add" value = "Add" class="btn btn-primary">Add</button>
</form>
</div>
</div>
Expand Down Expand Up @@ -222,6 +233,14 @@
<label for="modifyZip">Zip Code *</label>
<input style = "width: 100px;" name = "zip" type = "number" class="form-control" id="modifyZip" required = "true">
</div>
<div class = "form-group">
<label for="modifylat">Latitude</label>
<input style = "width: 150px;" type = "text" id = "modifylat" class="form-control" name = "lat">
</div>
<div class = "form-group">
<label for="modifylng">Longitude</label>
<input style = "width: 150px;" type = "text" id = "modifylng" class="form-control" name = "lng">
</div>
<button id = "themodifybutton" type = "submit" name = "modify" value = "Save Changes" class="btn btn-primary">Save Changes</button>
<button id = "thedeletebutton" type = "submit" name = "delete" value = "Delete Entry" class="btn btn-primary" style = "display: inline-block;" title = "hello">Delete</button>
<input type = "text" id = "modifyID" name = "id" style = "display: none;">
Expand Down Expand Up @@ -249,21 +268,22 @@
<button id ="add" class="btn btn-primary">Add Location</button>
</div>
<%
//database query to get locations that all employees can access
Location[] locations = LocationQueries.getAdminLocations();
//string representation of array.
String locationString = Location.arrayToString(locations);
locationString = locationString.replace("'","\\'");
%>

<script type=text/javascript>
//JSON representation of array.
var locations = makeLocationArray();
//Populate locations into table!
populateLocations();
//geocode listeners
$("#addZip").keyup(geocodeTown);
$("#modifyZip").keyup(geocodeTown);
//adds event listeners to all table records
$("tr.entry").click(modifyModal);
//Exits modal when x is clicked.
Expand Down Expand Up @@ -317,6 +337,14 @@ function modifyModal(){
$("#modifyZip").val(locations[i].zip);
$("#modifyID").val(locations[i].id);
var geocoder = new google.maps.Geocoder();
//this function assumes that you are using a proper zip code...
geocoder.geocode({'address':locations[i].zip}, function(results,status)
{if (status == google.maps.GeocoderStatus.OK)
{$("#modifylat").val(results[0].geometry.location.lat());
$("#modifylng").val(results[0].geometry.location.lng());}
});
//if the location has a foreign key constraint, then the delete button should be restricted
if(locations[i].numDevices > 0 || locations[i].numEmployees > 0){
$('#thedeletebutton').attr('disabled',true);
Expand All @@ -334,6 +362,21 @@ function closeModifyModal(){
$("#modifyModal").hide();
}
//generate the lat & long of a location in real time!
function geocodeTown(){
var id = this.getAttribute('id');
var zip = $("#"+id).val();
id = id.replace("Zip","");
var geocoder = new google.maps.Geocoder();
//this function assumes that you are using a proper zip code...
geocoder.geocode({'address':zip}, function(results,status)
{if (status == google.maps.GeocoderStatus.OK)
{$("#" + id + "lat").val(results[0].geometry.location.lat());
$("#" + id + "lng").val(results[0].geometry.location.lng());}
});
return true;
}
</script>
</body>
</html>
6 changes: 4 additions & 2 deletions WebContent/html/webpages/redirect/locationRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ String address = request.getParameter("address").replace("\"","\\\"");
String town = request.getParameter("town").replace("\"","\\\"");
String state = request.getParameter("state");
String zip = request.getParameter("zip");
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");
int er = 0;
//add form was submitted
if(request.getParameter("add") != null){
Location location = new Location(name,address,town,state,zip,0);
Location location = new Location(0,name,address,town,state,zip,0,lat,lng);
LocationQueries.addLocation(location);
}
//modify form was submitted
Expand All @@ -44,7 +46,7 @@ if(request.getParameter("modify") != null){
String strID = request.getParameter("id");
//turn to int for constructor
int id = Integer.parseInt(strID);
Location location = new Location(id,name,address,town,state,zip);
Location location = new Location(id,name,address,town,state,zip,0,lat,lng);
LocationQueries.modifyLocation(location);
}
//delete form was submitted
Expand Down
11 changes: 7 additions & 4 deletions WebContent/html/webpages/redirect/orderFormHandler.jsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%@ page import = "database.*" %>
<%@ page import = "database.*,entities.Location" %>
<%@ page import = "entities.User" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Expand All @@ -23,13 +23,16 @@ pageEncoding="ISO-8859-1"%>
int location=-1;
if(Integer.parseInt(request.getParameter("location_dropdown"))==0)
{
location = LocationQueries.addLocation(
location = LocationQueries.addLocation(new Location(
0,
request.getParameter("locname"),
request.getParameter("state"),
request.getParameter("address"),
request.getParameter("town"),
request.getParameter("state"),
request.getParameter("zip"),
Integer.parseInt(navsso)
Integer.parseInt(navsso),
"",
"")
);
}
Expand Down
40 changes: 28 additions & 12 deletions src/database/LocationQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ public static String locationIntToString(int locInt) throws ClassNotFoundExcepti
return returnResult;
}

// returns ID of new location
public static int addLocation(String name, String state, String address, String town, String zip, int userID) throws SQLException, ClassNotFoundException
{
return addLocation(new Location(name, address, town, state, zip, userID));
}

// returns ID of new location
public static int addLocation(Location location) throws SQLException, ClassNotFoundException
{
Expand All @@ -60,8 +54,7 @@ public static int addLocation(Location location) throws SQLException, ClassNotFo
id = results.getInt("Location_ID");
id++;
//tries this statement, otherwise tries again with a new id
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() + ")");
i = stmt.executeUpdate("INSERT INTO location (Location_ID,Name,Address,Town,State,Zip_Code,Employee_Flag,Latitude,Longitude) VALUES (" + id +",\"" + location.getName() + "\",\"" + location.getAddress()+ "\",\"" + location.getTown() + "\",\"" + location.getState() + "\",\"" + location.getZip() + "\"," + location.getEmployeeFlag() + ",\"" + location.getLat() + "\",\"" + location.getLng() + "\")");
}
stmt.close();
connection.close();
Expand All @@ -86,7 +79,17 @@ public static Location[] getAdminLocations() throws ClassNotFoundException, SQLE

//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"));
locations[counter] = new Location(
resultSet.getInt("Location_ID"),
resultSet.getString("Name"),
resultSet.getString("Address"),
resultSet.getString("Town"),
resultSet.getString("State"),
resultSet.getString("Zip_Code"),
resultSet.getInt("Employee_Flag"),
resultSet.getString("Latitude"),
resultSet.getString("Longitude")
);
counter++;
}
stmt.close();
Expand Down Expand Up @@ -118,7 +121,10 @@ public static Location[] getLocations(int userID) throws SQLException, ClassNotF
rs.getString("Address"),
rs.getString("Town"),
rs.getString("State"),
rs.getString("Zip_Code")
rs.getString("Zip_Code"),
rs.getInt("Employee_Flag"),
rs.getString("Latitude"),
rs.getString("Longitude")
);
i++;
}
Expand All @@ -138,7 +144,7 @@ public static void modifyLocation(Location location) throws ClassNotFoundExcepti
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("UPDATE location SET Name = \"" + location.getName() + "\", Address = \"" + location.getAddress() + "\", Town = \"" + location.getTown() + "\", State = \"" + location.getState() + "\", Zip_Code = \"" + location.getZip() + "\" WHERE Location_ID = " + location.getID());
stmt.executeUpdate("UPDATE location SET Name = \"" + location.getName() + "\", Address = \"" + location.getAddress() + "\", Town = \"" + location.getTown() + "\", State = \"" + location.getState() + "\", Zip_Code = \"" + location.getZip() + "\", Latitude = \"" + location.getLat() + "\", Longitude = \"" + location.getLng() + "\" WHERE Location_ID = " + location.getID());
stmt.close();
connect.close();
}
Expand Down Expand Up @@ -184,7 +190,17 @@ public static Location[] getAllLocations() throws SQLException, ClassNotFoundExc

//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"));
locations[counter] = new Location(
resultSet.getInt("Location_ID"),
resultSet.getString("Name"),
resultSet.getString("Address"),
resultSet.getString("Town"),
resultSet.getString("State"),
resultSet.getString("Zip_Code"),
resultSet.getInt("Employee_Flag"),
resultSet.getString("Latitude"),
resultSet.getString("Longitude")
);
counter++;
}

Expand Down
65 changes: 27 additions & 38 deletions src/entities/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ public class Location {
private int employee_flag;
private int numDevices;
private int numEmployees;
private String lat;
private String lng;

/**
* Constructor for existing location.
* @param name
* @param address
* @param town
* @param state
* @param zip
* Constructor for location bean.
* @throws SQLException
* @throws ClassNotFoundException
*/
public Location(int id, String name, String address, String town, String state, String zip) throws ClassNotFoundException, SQLException{
public Location(
int id,
String name,
String address,
String town,
String state,
String zip,
int flag,
String lat,
String lng
) throws ClassNotFoundException, SQLException{
this.id = id;
this.name = name;
this.address = address;
Expand All @@ -39,37 +46,9 @@ public Location(int id, String name, String address, String town, String state,
this.zip = zip;
this.numDevices = LocationQueries.numberDevices(id);
this.numEmployees = LocationQueries.numberEmployees(id);
}

/**
* Constructor for brand new location.
* @param name
* @param address
* @param town
* @param state
* @param zip
* @throws SQLException
* @throws ClassNotFoundException
*/
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) 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);
this.employee_flag = flag;
this.lat = lat;
this.lng = lng;
}

/**
Expand All @@ -85,6 +64,8 @@ public String toString(){
sb.append("\"state\": \"").append(state).append("\"").append(comma);
sb.append("\"numDevices\":").append(numDevices).append(comma);
sb.append("\"numEmployees\":").append(numEmployees).append(comma);
sb.append("\"lat\": \"").append(lat).append("\"").append(comma);
sb.append("\"long\": \"").append(lng).append("\"").append(comma);
sb.append("\"zip\": \"").append(zip).append("\"");
sb.append("}");
return sb.toString();
Expand Down Expand Up @@ -139,4 +120,12 @@ public int getEmployeeFlag()
{
return employee_flag;
}

public String getLat(){
return lat;
}

public String getLng(){
return lng;
}
}

0 comments on commit a8b1ed8

Please sign in to comment.