public CloudSearch(String[] tableName, SearchQuery searchObject, SearchFilter searchFilter) { this.collectionArray = new ArrayList<String>(); this.query = new JSONObject(); this.bool = new JSONObject(); filtered = new JSONObject(); for (int i = 0; i < tableName.length; i++) { this.collectionArray.add(tableName[i]); } this.query = new JSONObject(); filtered = new JSONObject(); try { if (searchObject != null) { this.bool.put("bool", searchObject.bool); filtered.put("query", this.bool); } else { filtered.put("query", new JSONObject()); } if (searchFilter != null) { this.bool.put("bool", searchFilter.bool); filtered.put("filter", this.bool); } else { filtered.put("filter", new JSONObject()); } } catch (JSONException e) { e.printStackTrace(); } this.from = 0; this.size = 10; this.sort = new ArrayList<Object>(); }
/** * Order by Desc * * @param columnName * @return CloudSearch */ public CloudSearch orderByDesc(String columnName) { if (columnName.equals("id") || columnName.equals("isSearchable") || columnName.equals("expires")) { columnName = "_" + columnName; } JSONObject obj = new JSONObject(); JSONObject column = new JSONObject(); try { column.put("order", "desc"); obj.put(columnName, column); } catch (JSONException e) { e.printStackTrace(); } this.sort.add(obj); return this; }
/** * Search * * @param callbackObject * @throws CloudException * @throws IOException * @throws ExecutionException * @throws InterruptedException * @throws JSONException */ public void search(CloudObjectArrayCallback callbackObject) throws CloudException { String collectionString; if (this.collectionArray.size() > 0) { collectionString = ""; for (int i = 0; i < this.collectionArray.size(); i++) { collectionString += (i > 0 ? "," + this.collectionArray.get(i) : this.collectionArray.get(i)); } this.collectionName = collectionString; } else { collectionString = this.collectionName; } try { this.query.put("filtered", this.filtered); JSONObject params = new JSONObject(); params.put("collectionName", collectionString); params.put("query", this.query); params.put("sort", this.sort); params.put("limit", this.size); params.put("skip", this.from); params.put("key", CloudApp.getAppKey()); String url = CloudApp.getServerUrl() + "/data/" + CloudApp.getAppId() + "/" + this.collectionName + "/search"; CBResponse response = CBParser.callJson(url, "POST", params); if (response.getStatusCode() == 200) { JSONArray body = new JSONArray(response.getResponseBody()); CloudObject[] object = new CloudObject[body.length()]; for (int i = 0; i < object.length; i++) { object[i] = new CloudObject(body.getJSONObject(i).get("_tableName").toString()); object[i].document = body.getJSONObject(i); } callbackObject.done(object, null); } else { CloudException e = new CloudException(response.getError()); callbackObject.done((CloudObject[]) null, e); } } catch (JSONException e) { CloudException ee = new CloudException(e.getMessage()); callbackObject.done((CloudObject[]) null, ee); } }