package com.brink.main; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.brink.main.api.NativeLogin; import com.brink.main.models.Bar; import com.brink.main.models.User; import android.app.Application; import android.graphics.Bitmap; import android.util.Log; public class OutgoingAPI extends Application{ //API link associated with all API calls public static String ApiUrl = "http://brink.herokuapp.com/api/"; /* * Method: SendBarRating * @param ratingPicked: This is the rating the user wants to submit to the server. * @param bar: The Bar the user wants to submit a rating to the server for. */ public static Boolean SendBarRating(int ratingPicked, String bar) { Bar mBar = new Bar(); mBar = retrieveBar(bar); User mUser = new User(); mUser = NativeLogin.RetrieveUser(); String url = ApiUrl + "checkins"; RestClient client = new RestClient(url); client.AddJsonParam("barid", mBar.Id); client.AddJsonParam("userid", mUser.Id); client.AddJsonParam("rating", ""+ratingPicked); client.AddJsonParam("imageurl", mUser.ImageUrl); try { client.Execute(RequestMethod.POST); } catch (Exception e) { Log.w("Caught Exception", "OutgoingAPI.sendBarRating"); e.printStackTrace(); return false; } Log.w("Bar Rating Being Sent", "" + ratingPicked); return true; } /* * Description: Method that makes a network call to our server to retrieve the _id of the bar that is sent to it. * @param barsName: String that contains the name of the bar. */ public static Bar retrieveBar(String barsName) { Bar mBar = new Bar(); //barsName = barsName.replace("'", "\\'"); barsName = URLEncoder.encode(barsName); String url = OutgoingAPI.ApiUrl+ "bar/" + barsName + "/Baton%20Rouge"; Log.w("OutgoingAPI.retrieveBarsID", "true"); RestClient client = new RestClient(url); client.AddHeader("Content-Type", "application/json"); try { client.Execute(RequestMethod.GET); } catch (Exception e) { e.printStackTrace(); Log.w("Caught Exception","OutgoingAPI.retrieveBarsID"); return null; } String result = client.getResponse(); JSONArray response = null; try { response = new JSONArray(result); } catch (JSONException e1) { e1.printStackTrace(); } try { for(int i = 0; i < response.length(); i++) { JSONObject barObject = new JSONObject(); barObject = response.getJSONObject(i); mBar.Id = barObject.getString("_id"); mBar.City = barObject.getString("city"); mBar.Address = barObject.getString("address"); mBar.Name = barObject.getString("barname"); mBar.Zone = barObject.getString("zone"); mBar.Description = barObject.getString("description"); mBar.Lattitude = barObject.getString("lattitude"); mBar.Longitude = barObject.getString("longitude"); mBar.ZipCode = barObject.getString("zipcode"); mBar.ImageUrl = barObject.getString("logourl"); mBar.PhoneNumber = barObject.getString("phonenumber"); mBar.Rating = barObject.getString("rating"); mBar.CheckinCount = barObject.getInt("checkincount"); //TODO Need to add ability to grab checkins and user pictures taken } } catch (JSONException e) { e.printStackTrace(); Log.w("Caught Exception - 2","OutgoingAPI.retrieveBarsID"); } return mBar; } /* * Checks the see the users Status with our server(Banned or not Banned). * * @param true: User is not banned, proceed with login process. * @param false: User is Banned, send them back to login and toast a error message. * @param 404Error: User is not in the database, send user info to the create API * and log them in. */ public static Boolean checkUsersStatus(String userId) { //Send our server the userID to make sure we are allowing the user to login. Log.w("Inside OutgoingAPI.checkUsersStatus", "true"); String url = ApiUrl + "users/login?id=" + userId; RestClient client = new RestClient(url); client.AddHeader("Content-Type", "application/json"); try { client.Execute(RequestMethod.GET); } catch (Exception e) { // TODO Auto-generated catch block Log.w("Caught Exception", "true"); e.printStackTrace(); return false; } if (client.getResponseCode() == 404) { // TODO: Oh shit, they don't exist, let's create them Log.w("404 Error", "True"); } String result = client.getResponse(); Log.w("Inside CheckUsersStatus - OutgoingAPI", "Returning result to Task"); Log.w("Check Result", result); if(result == "false") { return false; } return true; } /* * Checks the Users List to see if the UserName(E-Mail) and Password are correct. * @param email: The Users Email * @param password: the password that we will be comparing with the server stored password * to determine if the user entered the correct UserName - Password Combination. */ public static Boolean checkUsersList(String email, String password) { //Check these values with the server and send back the result whether that be String url = OutgoingAPI.ApiUrl + "/users?$filter=UserId%20eq%20'" + email +"'"; String tempPassw = ""; RestClient client = new RestClient(url); client.AddHeader("Content-Type", "application/json"); try { client.Execute(RequestMethod.GET); } catch (Exception e) { e.printStackTrace(); return null; } JSONArray jsonObject; try { jsonObject = new JSONArray(client.getResponse()); if(jsonObject.length() == 0) { Log.w("Email wasn't found on server", "OutgoingAPI.checkUsersList"); return false; } for (int i = 0; i < jsonObject.length(); i++) { JSONObject item = jsonObject.getJSONObject(i); Stored.UserInformation.setUsersName(item.getString("Name")); Stored.UserInformation.setUsersID(item.getString("_id")); tempPassw = item.getString("PasswordHash"); Stored.UserInformation.setUsersPictureURL(item.getString("ImageUrl")); } } catch (JSONException e) { Log.w("Catching exception-2", "OutgoingAPI.checkUsersList"); e.printStackTrace(); } if(client.getResponseCode() == 404) { Log.w("404 ERROR - Login problem there chief", "Send them back to Login Activity"); return false; } if(Stored.UserInformation.retrieveUsersPassword().equals(tempPassw)) { Stored.UserInformation.setUsersPicture(Utility.getBitmap(Stored.UserInformation.retrieveUsersPictureURL())); Log.w("UsersName Retrieved & Stored", Stored.UserInformation.retrieveUsersName()); Log.w("UsersId Retrieved & Stored", Stored.UserInformation.retrieveUsersID()); Log.w("Picture URL Retrieved & Stored", Stored.UserInformation.retrieveUsersPictureURL()); Log.w("Returning from OutgoingAPI.checkUsersList", "true"); return true; } Log.w("OutgoingAPI.checkUsersList - Login Error", "Passwords Aren't Matching up"); Stored.UserInformation.setUsersPassword(""); return false; } /* * Description: Network call to our server to retrieve the Users _id * @param isTrue: Boolean is no longer used. */ public static String retrieveUsersID(Boolean isTrue) { String url = OutgoingAPI.ApiUrl + "/users?"; String encodedURL = new String(); String finalURL = new String(); try { encodedURL = URLEncoder.encode("$filter=UserId eq '" + Stored.UserInformation.retrieveUsersEmail() +"'", "UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } finalURL = url + encodedURL; RestClient client = new RestClient(finalURL); client.AddHeader("Content-Type", "application/json"); try { client.Execute(RequestMethod.GET); } catch (Exception e) { e.printStackTrace(); return null; } JSONArray jsonObject; try { jsonObject = new JSONArray(client.getResponse()); if(jsonObject.length() == 0) { Log.w("Email wasn't found on server", "OutgoingAPI.retrieveUsersId"); if(Stored.FbLoginType) { Log.w("OutgoingAPI.retrieveUsersID - Login Error", "New Facebook User, Send information to the create User API"); createUser(false); return null; } } for (int i = 0; i < jsonObject.length(); i++) { JSONObject item = jsonObject.getJSONObject(i); Stored.UserInformation.setUsersID(item.getString("_id")); } Stored.UserInformation.setUsersPicture(Utility.getBitmap(Stored.UserInformation.retrieveUsersPictureURL())); Log.w("UsersId Retrieved & Stored", Stored.UserInformation.retrieveUsersID()); } catch (JSONException e) { e.printStackTrace(); } if(client.getResponseCode() == 404) { } Log.w("Returning from OutgoingAPI.retrieveUsersID", "true"); return Stored.UserInformation.retrieveUsersID(); } /* * Description: This will be used to create a new user on our server should this be the user's first time logging in with our app. This * method will be used for the registration page and for a first time Facebook/Twitter user. * * Return Value: Boolean * @param true: User was created successfully. * @param false: Problem creating the user. */ public static Boolean createUser(Boolean isLocal) { String url = OutgoingAPI.ApiUrl + "users"; RestClient client = new RestClient(url); client.AddHeader("Content-Type", "application/json"); if(isLocal == false) { try{ client.AddJsonParam("name", Stored.UserInformation.retrieveUsersName()); client.AddJsonParam("userId", Stored.UserInformation.retrieveUsersEmail()); client.AddJsonParam("imageUrl", Stored.UserInformation.retrieveUsersPictureURL()); } catch (Exception e) { e.printStackTrace(); Log.w("Hit Exception 2", "OutgoingAPI.createUser"); return false; } } else { try{ client.AddJsonParam("name", Stored.UserInformation.retrieveUsersName()); client.AddJsonParam("userId", Stored.UserInformation.retrieveUsersEmail()); client.AddJsonParam("passwordHash", Stored.UserInformation.retrieveUsersPassword()); } catch (Exception e) { e.printStackTrace(); Log.w("Hit Exception 2", "OutgoingAPI.createUser"); return false; } } try { client.Execute(RequestMethod.PUT); } catch (Exception e) { e.printStackTrace(); Log.w("Hit Exception 2", "OutgoingAPI.createUser"); return false; } if(isLocal == false) { Log.w("Retrieving new User ID for Facebook/Twitter User", "OutgoingAPI.createUser"); String holder = retrieveUsersID(false); } return true; } /* * Description: Network call to see if a email is already existent on our server. * @param email: Email being sent to the method for checking. * * Return Value: Boolean * @param true: Email does exist on our server. * @param false: Email does not exist on our server. */ public static Boolean doesEmailExist(String email) { String url = OutgoingAPI.ApiUrl + "/users?$filter=UserId%20eq%20'" + email +"'"; RestClient client = new RestClient(url); client.AddHeader("Content-Type", "application/json"); try { client.Execute(RequestMethod.GET); } catch (Exception e) { e.printStackTrace(); return null; } JSONArray jsonObject; try { jsonObject = new JSONArray(client.getResponse()); if(jsonObject.length() == 0) { Log.w("Email wasn't found on server - false value sent back", "OutgoingAPI.doesEmailExist"); return false; } else { Log.w("Email was found on server - true value sent back", "OutgoingAPI.doesEmailExist"); return true; } } catch (JSONException e) { e.printStackTrace(); Log.w("Caught Exception 2", "OutgoingAPI.doesEmailExist"); } if(client.getResponseCode() == 404) { Log.w("404 ERROR - Login problem there chief", "Send them back to Login Activity"); return false; } return true; } /* * Method: sendPictureToServer * Description: Send picture to the server and categorize it under which bar it was taken at. * @param barID: The identification number of the bar which the picture was taken at. * @param sendPic: The Picture that will be sent to our server for storage. * * ----This method must be run using Async Task---- */ public static Boolean sendPictureToServer(String barID, Bitmap sendPic) { return true; } }