Skip to content

Commit

Permalink
1. Add dbunit jars and a test data xml.
Browse files Browse the repository at this point in the history
2. Change the JSONObject.append() to put(), because append() inserts a JSONArray instead of a JSONObject.
3. Add a param to DBConnect's constructor, add close() and unsetVisitedRestaurants() to make it testable.
  • Loading branch information
pymo committed Sep 21, 2015
1 parent 3cc2a16 commit e54ede4
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 36 deletions.
Binary file added server/WebContent/WEB-INF/lib/dbunit-2.5.1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified server/build/classes/api/GetRestaurantsNearby.class
Binary file not shown.
Binary file modified server/build/classes/api/RecommendRestaurants.class
Binary file not shown.
Binary file modified server/build/classes/api/SetVisitedRestaurants.class
Binary file not shown.
Binary file modified server/build/classes/db/DBConnection.class
Binary file not shown.
Binary file modified server/build/classes/db/DBImport.class
Binary file not shown.
2 changes: 1 addition & 1 deletion server/src/api/GetRestaurantsNearby.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@WebServlet(description = "Get Restaurants near a location with latitude and longitude", urlPatterns = { "/GetRestaurantsNearby" })
public class GetRestaurantsNearby extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final DBConnection connection = new DBConnection();
private static final DBConnection connection = new DBConnection("jdbc:mysql://localhost:3306/mysql?user=root&password=root");

/**
* @see HttpServlet#HttpServlet()
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/RecommendRestaurants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@WebServlet("/RecommendRestaurants")
public class RecommendRestaurants extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final DBConnection connection = new DBConnection();
private static final DBConnection connection = new DBConnection("jdbc:mysql://localhost:3306/mysql?user=root&password=root");

/**
* @see HttpServlet#HttpServlet()
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/SetVisitedRestaurants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@WebServlet("/SetVisitedRestaurants")
public class SetVisitedRestaurants extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final DBConnection connection = new DBConnection();
private static final DBConnection connection = new DBConnection("jdbc:mysql://localhost:3306/mysql?user=root&password=root");

/**
* @see HttpServlet#HttpServlet()
Expand Down
90 changes: 57 additions & 33 deletions server/src/db/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ public class DBConnection {
private static final int MAX_RECOMMENDED_RESTAURANTS = 10;
private static final int MIN_RECOMMENDED_RESTAURANTS = 3;

public DBConnection() {
public DBConnection(String url) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mysql?"
+ "user=root&password=root");
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}

public void close(){
if (conn != null) {
try {
conn.close();
} catch (Exception e) { /* ignored */}
}
}

public void SetVisitedRestaurants(String userId, List<String> businessIds) {
try {
if (conn == null) {
Expand All @@ -49,6 +55,24 @@ public void SetVisitedRestaurants(String userId, List<String> businessIds) {
}
}

public void UnsetVisitedRestaurants(String userId, List<String> businessIds) {
try {
if (conn == null) {
return;
}
Statement stmt = conn.createStatement();
String sql = "";
for (String businessId : businessIds) {
sql = "DELETE FROM USER_VISIT_HISTORY WHERE `user_id`=\"" + userId + "\" and `business_id` = \""
+ businessId + "\"";
stmt.executeUpdate(sql);
}

} catch (Exception e) { /* report an error */
System.out.println(e.getMessage());
}
}

private Set<String> getCategories(String business_id) {
try {
if (conn == null) {
Expand Down Expand Up @@ -118,17 +142,17 @@ private JSONObject getRestaurantsById(String businessId) {
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
JSONObject obj = new JSONObject();
obj.append("business_id", rs.getString("business_id"));
obj.append("name", rs.getString("name"));
obj.append("stars", rs.getFloat("stars"));
obj.append("latitude", rs.getFloat("latitude"));
obj.append("longitude", rs.getFloat("longitude"));
obj.append("full_address", rs.getString("full_address"));
obj.append("city", rs.getString("city"));
obj.append("state", rs.getString("state"));
obj.append("categories",
obj.put("business_id", rs.getString("business_id"));
obj.put("name", rs.getString("name"));
obj.put("stars", rs.getFloat("stars"));
obj.put("latitude", rs.getFloat("latitude"));
obj.put("longitude", rs.getFloat("longitude"));
obj.put("full_address", rs.getString("full_address"));
obj.put("city", rs.getString("city"));
obj.put("state", rs.getString("state"));
obj.put("categories",
DBImport.stringToJSONArray(rs.getString("categories")));
obj.append("image_url", rs.getString("image_url"));
obj.put("image_url", rs.getString("image_url"));
return obj;
}
} catch (Exception e) { /* report an error */
Expand Down Expand Up @@ -228,15 +252,15 @@ public JSONArray GetRestaurantsNearLoation(double lat, double lon) {
List<JSONObject> list = new ArrayList<JSONObject>();
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.append("business_id", rs.getString("business_id"));
obj.append("name", rs.getString("name"));
obj.append("stars", rs.getFloat("stars"));
obj.append("latitude", rs.getFloat("latitude"));
obj.append("longitude", rs.getFloat("longitude"));
obj.append("full_address", rs.getString("full_address"));
obj.append("city", rs.getString("city"));
obj.append("state", rs.getString("state"));
obj.append("categories",
obj.put("business_id", rs.getString("business_id"));
obj.put("name", rs.getString("name"));
obj.put("stars", rs.getFloat("stars"));
obj.put("latitude", rs.getFloat("latitude"));
obj.put("longitude", rs.getFloat("longitude"));
obj.put("full_address", rs.getString("full_address"));
obj.put("city", rs.getString("city"));
obj.put("state", rs.getString("state"));
obj.put("categories",
DBImport.stringToJSONArray(rs.getString("categories")));
list.add(obj);
}
Expand Down Expand Up @@ -274,16 +298,16 @@ public JSONArray GetRestaurantsNearLoationViaYelpAPI(double lat, double lon) {
double longitude = restaurant.getLongitude();
String imageUrl = restaurant.getImageUrl();
JSONObject obj = new JSONObject();
obj.append("business_id", business_id);
obj.append("name", name);
obj.append("stars", stars);
obj.append("latitude", latitude);
obj.append("longitude", longitude);
obj.append("full_address", fullAddress);
obj.append("city", city);
obj.append("state", state);
obj.append("categories", categories);
obj.append("image_url", imageUrl);
obj.put("business_id", business_id);
obj.put("name", name);
obj.put("stars", stars);
obj.put("latitude", latitude);
obj.put("longitude", longitude);
obj.put("full_address", fullAddress);
obj.put("city", city);
obj.put("state", state);
obj.put("categories", categories);
obj.put("image_url", imageUrl);
sql = "INSERT IGNORE INTO RESTAURANTS " + "VALUES ('"
+ business_id + "', \"" + name + "\", \"" + categories
+ "\", '" + city + "', '" + state + "', " + stars
Expand Down
27 changes: 27 additions & 0 deletions server/src/db/DBUnitTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<restaurants business_id="--5jkZ3-nUPZxUvtcbr8Uw" name="Mika&apos;s Greek" categories="Greek,Mediterranean,Restaurants" city="Scottsdale" state="AZ" stars="4.5" full_address="1336 N Scottsdale Rd&#xA;Scottsdale, AZ 85257" latitude="33.4634" longitude="-111.927" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--BlvDO_RG2yElKu9XA1_g" name="Asian Island" categories="Sushi Bars,Hawaiian,Chinese,Restaurants" city="Scottsdale" state="AZ" stars="4.0" full_address="14870 N Northsight Blvd&#xA;Ste 103&#xA;Scottsdale, AZ 85260" latitude="33.6212" longitude="-111.898" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--jFTZmywe7StuZ2hEjxyA" name="Subway" categories="Fast Food,Sandwiches,Restaurants" city="Las Vegas" state="NV" stars="3.5" full_address="3991 Dean Martin Dr&#xA;Las Vegas, NV 89103" latitude="36.1188" longitude="-115.182" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--pOlFxITWnhzc7SHSIP0A" name="Block &amp; Grinder" categories="American (New),Restaurants" city="Charlotte" state="NC" stars="4.0" full_address="2935 Providence Rd&#xA;Sherwood Forest&#xA;Charlotte, NC 28211" latitude="35.1711" longitude="-80.8071" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--qeSYxyn62mMjWvznNTdg" name="Panda Express" categories="Chinese,Restaurants" city="Casa Grande" state="AZ" stars="4.0" full_address="1747 E Florence Blvd&#xA;Casa Grande, AZ 85122" latitude="32.8792" longitude="-111.712" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--UE_y6auTgq3FXlvUMkbw" name="Ritters Diner" categories="Diners,Restaurants" city="Pittsburgh" state="PA" stars="3.5" full_address="5221 Baum Blvd&#xA;Bloomfield&#xA;Pittsburgh, PA 15224" latitude="40.4564" longitude="-79.9413" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--Y_2lDOtVDioX5bwF6GIw" name="Buck &amp; Badger" categories="Bars,Comfort Food,Nightlife,Restaurants" city="Madison" state="WI" stars="3.0" full_address="115 State St&#xA;Capitol&#xA;Madison, WI 53703" latitude="43.0747" longitude="-89.3871" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="--zgHBiQpr8H2ZqSdGmguQ" name="China Star Buffet" categories="Buffets,Chinese,Restaurants" city="Las Vegas" state="NV" stars="3.0" full_address="3743 South Las Vegas Bl&#xA;The Strip&#xA;Las Vegas, NV 89109" latitude="36.1065" longitude="-115.172" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-024YEtnIsPQCrMSHCKLQw" name="Presse Cafe" categories="Cafes,Restaurants" city="Montr&#233;al" state="QC" stars="3.5" full_address="1750 Saint-Denis Rue&#xA;Montr&#233;al, QC H2X 3K6" latitude="45.5157" longitude="-73.5651" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0bl9EllYlei__4dl1W00Q" name="Wee Bite" categories="Breakfast &amp; Brunch,Restaurants" city="Edinburgh" state="EDH" stars="3.5" full_address="Saint Mary&apos;s Street&#xA;Newington&#xA;Edinburgh EH8 8" latitude="55.9496" longitude="-3.18347" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0bUDim5OGuv8R0Qqq6J4A" name="IHOP" categories="Bakeries,Food,Breakfast &amp; Brunch,Restaurants" city="Phoenix" state="AZ" stars="2.0" full_address="7023 N 19th Ave&#xA;Phoenix, AZ 85021" latitude="33.5397" longitude="-112.099" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0GkcDiIgVm0XzDZC8RFOg" name="Crane Alley" categories="Bars,American (Traditional),Nightlife,Restaurants" city="Urbana" state="IL" stars="3.5" full_address="115 W Main St&#xA;Urbana, IL 61801" latitude="40.1122" longitude="-88.208" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0HGqwlfw3I8nkJyMHxAsQ" name="McDonald&apos;s" categories="Burgers,Fast Food,Restaurants" city="Phoenix" state="AZ" stars="4.0" full_address="4750 E Warner Rd&#xA;Phoenix, AZ 85044" latitude="33.3312" longitude="-111.981" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0lOuL7RkZQnjAl96dXTvA" name="New York Pizza Dept" categories="Pizza,Restaurants" city="Gilbert" state="AZ" stars="3.5" full_address="2743 S Market St&#xA;Gilbert, AZ 85295" latitude="33.3008" longitude="-111.743" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0QBrNvhrPQCaeo7mTo0zQ" name="La Salcita" categories="Restaurants" city="Phoenix" state="AZ" stars="4.5" full_address="2526 W Van Buren St&#xA;Phoenix, AZ 85009" latitude="33.4514" longitude="-112.114" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-0tJ4FwcNtJjil-ZUTy30Q" name="Cafe No 9" categories="Food,Coffee &amp; Tea,Internet Cafes,Breakfast &amp; Brunch,Restaurants" city="Edinburgh" state="EDH" stars="4.5" full_address="9 Croall Place&#xA;Leith Walk&#xA;Leith&#xA;Edinburgh EH7 4LT" latitude="55.9616" longitude="-3.18036" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-1B-DEGkLE1kDj5ENAF2NQ" name="Prince Restaurant" categories="Modern European,Restaurants" city="Las Vegas" state="NV" stars="4.0" full_address="6795 W Flamingo Rd&#xA;Ste A&#xA;Las Vegas, NV 89103" latitude="36.1141" longitude="-115.241" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-1bOb2izeJBZjHC7NWxiPA" name="First Watch" categories="Breakfast &amp; Brunch,Cafes,American (Traditional),Restaurants" city="Phoenix" state="AZ" stars="4.0" full_address="61 W. Thomas Road&#xA;Phoenix, AZ 85013" latitude="33.48" longitude="-112.077" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-1BzcQK-HDA6LVOThHMpsw" name="AJ&apos;s Pizzeria and Diner" categories="Pizza,Restaurants" city="Verona" state="WI" stars="4.0" full_address="300 South Main St&#xA;Verona, WI 53593" latitude="42.988" longitude="-89.5337" image_url="http://www.example.com/img.JPG"/>
<restaurants business_id="-1ERbsOf9XOC9wGbZYFr7g" name="Toros Cantina &amp; Gaming" categories="Mexican,Restaurants" city="Las Vegas" state="NV" stars="3.0" full_address="11760 W Charleston Blvd&#xA;Summerlin&#xA;Las Vegas, NV 89138" latitude="36.1595" longitude="-115.352" image_url="http://www.example.com/img.JPG"/>
<users user_id="1111" first_name="John" last_name="Smith"/>
<user_visit_history visit_history_id="1" user_id="1111" business_id="--qeSYxyn62mMjWvznNTdg" last_visited_time="2015-09-16 22:36:34.0"/>
<user_visit_history visit_history_id="2" user_id="1111" business_id="--zgHBiQpr8H2ZqSdGmguQ" last_visited_time="2015-09-16 22:36:34.0"/>
<user_visit_history visit_history_id="3" user_id="1111" business_id="-1B-DEGkLE1kDj5ENAF2NQ" last_visited_time="2015-09-16 22:36:34.0"/>
</dataset>

0 comments on commit e54ede4

Please sign in to comment.