Android Open Source - BackbeamAndroid Query






From Project

Back to project page BackbeamAndroid.

License

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.

Java Source Code

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);
  }

}




Java Source Code List

io.backbeam.BackbeamException.java
io.backbeam.BackbeamObject.java
io.backbeam.Backbeam.java
io.backbeam.Callback.java
io.backbeam.CollectionConstraint.java
io.backbeam.ControllerRequestCallback.java
io.backbeam.FetchCallback.java
io.backbeam.FetchPolicy.java
io.backbeam.FileUpload.java
io.backbeam.GCMBroadcastReceiver.java
io.backbeam.GCMCallback.java
io.backbeam.GCMIntentService.java
io.backbeam.IntentCallback.java
io.backbeam.JoinResult.java
io.backbeam.Json.java
io.backbeam.ListCallback.java
io.backbeam.Location.java
io.backbeam.NearFetchCallback.java
io.backbeam.ObjectCallback.java
io.backbeam.OperationCallback.java
io.backbeam.PushNotification.java
io.backbeam.Query.java
io.backbeam.RealTimeConnectionListener.java
io.backbeam.RealTimeEventListener.java
io.backbeam.RemoveCallback.java
io.backbeam.RequestCallback.java
io.backbeam.RequestDataCallback.java
io.backbeam.SignupCallback.java
io.backbeam.Utils.java