Back to project page BackbeamAndroid.
The source code is released under:
Copyright (c) 2012 Level Apps S.L. <http://backbeam.io> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software...
If you think the Android project BackbeamAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.backbeam; //from w w w. ja v a 2 s . c o m import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; public class Query { private String entity; private String query; private Object[] params; private FetchPolicy fetchPolicy; public Query(String entity) { this.entity = entity; this.fetchPolicy = FetchPolicy.REMOTE_ONLY; } public Query setQuery(String query, Object... params) { this.query = query; this.params = params; return this; } public void fetch(int limit, int offset, final FetchCallback callback) { TreeMap<String, Object> prms = new TreeMap<String, Object>(); if (query != null) { prms.put("q", query); if (params != null) { prms.put("params", Utils.stringsFromParams(params)); } } prms.put("limit", Integer.toString(limit)); prms.put("offset", Integer.toString(offset)); Backbeam.instance().perform("GET", "/data/"+entity, prms, fetchPolicy, new RequestCallback() { @Override public void success(Json response, boolean fromCache) { Json values = response.get("objects"); Json ids = response.get("ids"); Map<String, BackbeamObject> objects = BackbeamObject.objectsFromValues(values, null); List<BackbeamObject> list = new ArrayList<BackbeamObject>(ids.size()); for (Json id : ids) { BackbeamObject obj = objects.get(id.asString()); list.add(obj); } callback.success(list, response.get("count").asInt(), fromCache); } @Override public void failure(BackbeamException exception) { callback.failure(exception); } }); } public void near(String field, double lat, double lon, int limit, final NearFetchCallback callback) { TreeMap<String, Object> prms = new TreeMap<String, Object>(); if (query != null) { prms.put("q", query); if (params != null) { prms.put("params", Utils.stringsFromParams(params)); } } prms.put("lat", Double.toString(lat)); prms.put("lon", Double.toString(lon)); prms.put("limit", Integer.toString(limit)); Backbeam.instance().perform("GET", "/data/"+entity+"/near/"+field, prms, fetchPolicy, new RequestCallback() { @Override public void success(Json response, boolean fromCache) { Json values = response.get("objects"); Json ids = response.get("ids"); Json distances = response.get("distances"); Map<String, BackbeamObject> objects = BackbeamObject.objectsFromValues(values, null); List<BackbeamObject> list = new ArrayList<BackbeamObject>(ids.size()); for (Json id : ids) { BackbeamObject obj = objects.get(id.asString()); list.add(obj); } List<Integer> dists = new ArrayList<Integer>(); for (Json json : distances) { dists.add(json.asInt()); } callback.success(list, response.get("count").asInt(), dists, fromCache); } @Override public void failure(BackbeamException exception) { callback.failure(exception); } }); } public void bounding(String field, double swlat, double swlon, double nelat, double nelon, int limit, final FetchCallback callback) { TreeMap<String, Object> prms = new TreeMap<String, Object>(); if (query != null) { prms.put("q", query); if (params != null) { prms.put("params", Utils.stringsFromParams(params)); } } prms.put("swlat", Double.toString(swlat)); prms.put("swlon", Double.toString(swlon)); prms.put("nelat", Double.toString(nelat)); prms.put("nelon", Double.toString(nelon)); prms.put("limit", Integer.toString(limit)); Backbeam.instance().perform("GET", "/data/"+entity+"/bounding/"+field, prms, fetchPolicy, new RequestCallback() { @Override public void success(Json response, boolean fromCache) { Json values = response.get("objects"); Json ids = response.get("ids"); Map<String, BackbeamObject> objects = BackbeamObject.objectsFromValues(values, null); List<BackbeamObject> list = new ArrayList<BackbeamObject>(ids.size()); for (Json id : ids) { BackbeamObject obj = objects.get(id.asString()); list.add(obj); } callback.success(list, response.get("count").asInt(), fromCache); } @Override public void failure(BackbeamException exception) { callback.failure(exception); } }); } public FetchPolicy getFetchPolicy() { return fetchPolicy; } public void setFetchPolicy(FetchPolicy fetchPolicy) { this.fetchPolicy = fetchPolicy; } private void remove(TreeMap<String, Object> prms, final RemoveCallback callback) { Backbeam.instance().perform("DELETE", "/data/"+entity, prms, fetchPolicy, new RequestCallback() { @Override public void success(Json response, boolean fromCache) { Json removed = response.get("removed"); callback.success(removed.asInt()); } @Override public void failure(BackbeamException exception) { callback.failure(exception); } }); } public void remove(int limit, int offset, final RemoveCallback callback) { TreeMap<String, Object> prms = new TreeMap<String, Object>(); if (query != null) { prms.put("q", query); if (params != null) { prms.put("params", Utils.stringsFromParams(params)); } } prms.put("limit", Integer.toString(limit)); prms.put("offset", Integer.toString(offset)); remove(prms, callback); } public void removeAll(final RemoveCallback callback) { TreeMap<String, Object> prms = new TreeMap<String, Object>(); if (query != null) { prms.put("q", query); if (params != null) { prms.put("params", Utils.stringsFromParams(params)); } } prms.put("limit", "all"); remove(prms, callback); } }