Exemplo n.º 1
0
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    final String query = req.getParameter(APIConstants.QUERY);
    double lat = 0.0;
    double lng = 0.0;
    int maxDistance = 2000;
    int maxResults = 20;
    String[] searchTerms = {};

    // Get lat and long
    try {
      lat = Double.parseDouble(req.getParameter(APIConstants.LAT));
      lng = Double.parseDouble(req.getParameter(APIConstants.LNG));
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    }

    // Preset Defaults
    try {
      maxDistance = Integer.parseInt(req.getParameter(APIConstants.DISTANCE));
      maxResults = Integer.parseInt(req.getParameter(APIConstants.LIMIT));
    } catch (Exception e) {
      // No big deal, defaults set
    }

    if (null != query && !query.isEmpty()) searchTerms = query.split(" ");

    List<RestaurantLite> restaurants =
        ConvertToLite.convertRestaurants(
            TDQueryUtils.searchGeoItems(
                searchTerms, new Point(lat, lng), maxResults, maxDistance, new Restaurant()));

    if (!restaurants.isEmpty()) {
      final JSONArray jsonArray = new JSONArray();

      // Traverse each restaurant
      for (final RestaurantLite r : restaurants) {
        try {
          // Put it in the array
          jsonArray.put(new JSONObject(new Gson().toJson(r)));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }

      // Write success with Array
      resp.getWriter()
          .write(APIUtils.generateJSONSuccessMessage(RestaurantConstants.RESTAURANTS, jsonArray));
    } else resp.getWriter().write(APIUtils.generateJSONFailureMessage("No restaurants found."));
  }
Exemplo n.º 2
0
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // topdish1.appspot.com/api/dishDetail?id[]=1&id[]=2&id[]=5...

    String[] ids = req.getParameterValues(APIConstants.ID_ARRAY);
    Set<Key> dishKeys = new HashSet<Key>();

    for (int i = 0; i < ids.length; i++) {
      try {
        Long id = Long.parseLong(ids[i]);
        dishKeys.add(KeyFactory.createKey(Dish.class.getSimpleName(), id));
      } catch (NumberFormatException e) {
        // malformed input
      } catch (JDOObjectNotFoundException e) {
        // object not found, skipping
      }
    }

    final Set<Dish> dishes = Datastore.get(dishKeys);
    final Set<DishLite> dishLites = ConvertToLite.convertDishes(dishes);

    if (!dishLites.isEmpty()) {
      final JSONArray array = new JSONArray();

      // Traverse dishes
      for (final DishLite dish : dishLites) {
        try {
          // Put a new JSONObject of the Dish in the Array
          array.put(new JSONObject(new Gson().toJson(dish)));

        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      // Return array (empty or not)
      resp.getWriter().write(APIUtils.generateJSONSuccessMessage(DishConstants.DISHES, array));
    } else resp.getWriter().write(APIUtils.generateJSONFailureMessage("No dishes found"));
  }