Exemplo n.º 1
0
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces("application/json")
  public ReturnMessage saveSearch(
      @FormParam("user") String user,
      @FormParam("endDate") String endDate,
      @FormParam("startDate") String startDate,
      @FormParam("make") String make,
      @FormParam("model") String model,
      @FormParam("year") String year,
      @FormParam("searchName") String searchName) {

    JSONArray queryList = new JSONArray();

    System.out.println(user);

    CouchConnection couch = new CouchConnection("http://localhost:5984/", "savedsearches/");

    String db = "gm";

    if (!make.equals("undefined") && !make.equals("All Makes") && !make.equals("")) {
      db = make.toLowerCase();
    }
    if (!model.equals("undefined") && !model.equals("All Models") && !model.equals("")) {
      db = model.toLowerCase();
    }
    if (!year.equals("undefined") && !year.equals("All Years") && !year.equals("")) {
      db = model.toLowerCase() + year;
    }

    JSONObject viewDocument = new JSONObject();

    boolean existingViews = true;
    try {
      viewDocument = new JSONObject(couch.queryDB(user));
      JSONObject search = new JSONObject();
      search.put("viewName", "_design/(" + startDate + ")(" + endDate + ")");
      search.put("db", db);
      viewDocument.put(searchName, search);
      couch.updateDocument(user, viewDocument);
    } catch (Exception e) {
      existingViews = false;
    }

    if (!existingViews) {
      viewDocument.put("_id", user);
      JSONObject search = new JSONObject();
      search.put("viewName", "_design/(" + startDate + ")(" + endDate + ")");
      search.put("db", db);
      viewDocument.put(searchName, search);
      couch.createDocuments(viewDocument, false);
    }

    ReturnMessage rm = new ReturnMessage();
    rm.setResult("success");
    return rm;
  }
Exemplo n.º 2
0
  @DELETE
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces("application/json")
  public ReturnMessage deleteSearch(
      @FormParam("user") String user, @FormParam("searchName") String searchName) {

    CouchConnection couch = new CouchConnection("http://localhost:5984/", "savedsearches/");
    ReturnMessage rm = new ReturnMessage();
    try {
      JSONObject userViews = new JSONObject(couch.queryDB(user));

      userViews.remove(searchName);
      couch.updateDocument(user, userViews);

      rm.setResult("success");
    } catch (Exception e) {
      rm.setResult("failure");
    }
    return rm;
  }