Beispiel #1
0
  @Override
  public JSONArray searchRestaurants(String userId, double lat, double lon) {
    try {
      YelpAPI api = new YelpAPI();
      JSONObject response = new JSONObject(api.searchForBusinessesByLocation(lat, lon));
      JSONArray array = (JSONArray) response.get("businesses");

      List<JSONObject> list = new ArrayList<JSONObject>();
      Set<String> visited = getVisitedRestaurants(userId);

      for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        Restaurant restaurant = new Restaurant(object);
        String businessId = restaurant.getBusinessId();
        String name = restaurant.getName();
        String categories = restaurant.getCategories();
        String city = restaurant.getCity();
        String state = restaurant.getState();
        String fullAddress = restaurant.getFullAddress();
        double stars = restaurant.getStars();
        double latitude = restaurant.getLatitude();
        double longitude = restaurant.getLongitude();
        String imageUrl = restaurant.getImageUrl();
        String url = restaurant.getUrl();
        JSONObject obj = restaurant.toJSONObject();
        if (visited.contains(businessId)) {
          object.put("is_visited", true);
        } else {
          object.put("is_visited", false);
        }
        String sql = "INSERT IGNORE INTO restaurants VALUES (?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, businessId);
        statement.setString(2, name);
        statement.setString(3, categories);
        statement.setString(4, city);
        statement.setString(5, state);
        statement.setDouble(6, stars);
        statement.setString(7, fullAddress);
        statement.setDouble(8, latitude);
        statement.setDouble(9, longitude);
        statement.setString(10, imageUrl);
        statement.setString(11, url);
        statement.execute();
        list.add(obj);
      }
      return new JSONArray(list);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    return null;
  }
  public Restaurant updateRestaurant(long idresto, Restaurant restaurant) {
    ContentValues values = new ContentValues();

    values.put(MySQLiteHelperResto.COLUMN_NOM, restaurant.getNom());
    values.put(MySQLiteHelperResto.COLUMN_longitude, restaurant.getLongitude());
    values.put(MySQLiteHelperResto.COLUMN_latitude, restaurant.getLatitude());
    values.put(MySQLiteHelperResto.COLUMN_description, restaurant.getDescription());
    values.put(MySQLiteHelperResto.COLUMN_email, restaurant.getEmail());
    values.put(MySQLiteHelperResto.COLUMNhorairehouverture, restaurant.getHorairehouverture());
    values.put(MySQLiteHelperResto.COLUMN_horairefermeture, restaurant.getHorairefermeture());
    values.put(MySQLiteHelperResto.COLUMN_siteweb, restaurant.getSiteweb());
    values.put(MySQLiteHelperResto.COLUMN_image, restaurant.getImage());
    values.put(MySQLiteHelperResto.COLUMN_telephone, restaurant.getTelephone());
    values.put(MySQLiteHelperResto.COLUMN_ville, restaurant.getVille());
    values.put(MySQLiteHelperResto.COLUMN_pays, restaurant.getPays());

    database.update(
        MySQLiteHelperResto.TABLE_Restaurant,
        values,
        MySQLiteHelperResto.COLUMN_ID + " = " + restaurant.getIdresto(),
        null);

    return getRestaurantWithId(restaurant.getIdresto());
  }