Android Open Source - Android-SDK Cloud Query






From Project

Back to project page Android-SDK.

License

The source code is released under:

MIT License

If you think the Android project Android-SDK 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 net.getcloudengine;
//from  ww  w  . j a  v  a2 s . c  om

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.methods.HttpGet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

/**
 * CloudQuery class allows the user to query for CloudObjects
 * satisfying a particular condition. The query is typically constructed
 * by making multiple where* method calls on a new CloudQuery object 
 * to set the appropriate condition. Then you can call a get or a find
 * method to actually run the query.
 * 
 */
public class CloudQuery {
  
  private static final String TAG = "CloudQuery";
  private static final int ASCENDING = 1;
  private static final int DESCENDING = -1;
  private String collection = null;
  private JSONObject queryObj = new JSONObject();
  private String orderby = null;
  private int order = ASCENDING;
  private String nextPage = null;
  private List<CloudObject> objList = new ArrayList<CloudObject>();
  
  CloudEngineUtils utils = CloudEngineUtils.getInstance();
  
  /**
   * Constructs a new CloudQuery 
   * 
   * @param objName name of the CloudObject type that the query is 
   * being constructed for. This is the same name that was passed 
   * while constructing that particular CloudObject  
   * 
   */
  public CloudQuery(String objName){
    this.collection = objName;
  }
  
  private class GetResult{
    public CloudException exception = null;
    public CloudObject obj = null;
  }
  
  private class FindResult{
    public CloudException exception = null;
    public List<CloudObject> objects = null;
  }

  private CloudObject gettask(String id) throws CloudAuthException,
                          CloudException
  {
    String response, address = null;
    CloudObject obj = null;
    address = CloudEndPoints.CLASSES + collection + "/" +  id + "/";
    try{
      
      response = utils.httpRequest(address, new HttpGet());
      Log.d(TAG, "CloudQuery " + collection + " response received: " + response);
      JSONObject myobj = new JSONObject(response);
      obj = new CloudObject(collection, myobj);
    }
    
    catch (JSONException e)
    {
      throw new CloudException("Invalid CloudObject received");
    }
    return obj;
  }
  
  private class GetTask extends AsyncTask<String, Void, GetResult> {
    
    GetObjectCallback callback = null;
    
    public void setCallback(GetObjectCallback cbk){
      callback = cbk;
    }
    
    
    @Override
    protected GetResult doInBackground(String... ids) {
      GetResult result = new GetResult();
      String id = ids[0];
      try{
        Log.d(TAG, "CloudQuery Async task  retrieving object id " + id);
        result.obj = gettask(id);
      }
      catch (CloudAuthException e){
         result.exception = e;
      }
      catch (CloudException e){
        result.exception = e;
      }
      
      return result;

    }
    
    protected void onPostExecute(GetResult result) {
      
           if(callback != null){
             callback.done(result.obj, result.exception);
           }
       }
    
  }
  
  private List<CloudObject> findtask() throws CloudAuthException, 
                      CloudException
  {
    String response= null, address = null;
    JSONObject curr_obj;
    CloudObject curr_cloud_obj;
    
    
    address = CloudEndPoints.CLASSES + collection + "/";
    Log.i(TAG, "Running query on server: " + queryObj.toString());
    
    // Construct query parameters
    try{
      address += "?query=" + URLEncoder.encode(queryObj.toString(), "UTF-8") +
          "&page_size=0";
    }
    catch(Exception e){
      throw new CloudException("Unable to encode query parameters");
    }
    
    // Construct orderby preference
    if(this.orderby != null){
      
      try{
        JSONObject orderObj = new JSONObject();
        orderObj.put(this.orderby, this.order);
        address += "&orderby=" + URLEncoder.encode(orderObj.toString(), "UTF-8");
      }
      catch(Exception e){
        throw new CloudException("Unable to encode query parameters");
      }
    }
    
  /*  if(nextPage != null){
      
      Pattern pattern = Pattern.compile("\\d+");
      Matcher matcher = pattern.matcher(nextPage);
      if(matcher.find()){
        address += "&page=" + matcher.group();
      }
    }
    */
    
    try{
      
      Log.i(TAG, "connecting to endpoint : " + address);
      response = utils.httpRequest(address, new HttpGet());
      Log.i(TAG, "query response received: "+ response);
      JSONArray query_response = new JSONArray(response);
      //JSONArray arr = query_response.getJSONArray("results");
      for(int i = 0; i < query_response.length(); i++)
      {
        curr_obj = query_response.getJSONObject(i);
        curr_cloud_obj = new CloudObject(collection, curr_obj);
        objList.add(curr_cloud_obj);
      }
      
    /*  nextPage = query_response.getString("next");
      Log.i(TAG, "next page: " + nextPage);
      if(nextPage != "null"){
        findtask();
      }
      */
    }
    catch (JSONException e){
      throw new CloudException("Invalid CloudObject received");
    }
    return objList;
  }
  
  
  private class FindTask extends AsyncTask<Void, Void, FindResult> {
    
    FindObjectsCallback callback = null;
    
    public void setCallback(FindObjectsCallback cbk){
      callback = cbk;
    }
    

    @Override
    protected FindResult doInBackground(Void... args) {
      
      FindResult result = new FindResult();
      try{
        result.objects = findtask();
      }
      catch (CloudAuthException e){
         result.exception = e;
      }
      catch (CloudException e){
        result.exception = e;
      }
      return result;
    }
    
    protected void onPostExecute(FindResult result) {
      
           if(callback != null){
             callback.done(result.objects, result.exception);
           }
       }
    
  }
  
  public boolean moreResults(){
    
    if(nextPage != null){
      return true;
    }
    else{
      return false;
    }
  }
  
  /**
   * Run a query to retrieve a particular object whose objectId is already known.
   * This method fetches the object in the current thread.
   * 
   * @param objId Unique Id of the object assigned when the object was saved
   * 
   * @param callback The callback function that will be called after the
   * object has been retrieved. Exception information, if any, that occured 
   * during the operation will be passed to the callback function.
   * 
   */
  public CloudObject get(String objId, GetObjectCallback callback)
              throws CloudAuthException, CloudException
  {
    return gettask(objId);
  }
  
  /**
   * Run a query to retrieve a particular object whose objectId is already known.
   * This method fetches the object in a background thread.
   * 
   * @param objId Unique Id of the object assigned when the object was saved
   * 
   * @param callback The callback function that will be called after the
   * object has been retrieved. Exception information, if any, that occured 
   * during the operation will be passed to the callback function.
   * 
   */
  public void getInBackground(String objId, GetObjectCallback callback)
  {
    GetTask task = new GetTask();
    task.setCallback(callback);
    task.execute(objId);
  }
  
  /**
   * Run a query to retrieve one or more CloudObjects satisfying all the
   * condition set in this query object. This method fetches the objects
   * in a background thread.
   * 
   * 
   * @param callback The callback function that will be called after the
   * objects have been retrieved. Exception information, if any, that occurred 
   * during the operation will be passed to the callback function.
   * 
   */
  public void findInBackground(FindObjectsCallback callback)
  {
    FindTask task = new FindTask();
    task.setCallback(callback);
    task.execute();
  }
  
  /**
   * Run a query to retrieve one or more CloudObjects satisfying all the
   * condition set in this query object. This method fetches the objects
   * in the current thread.
   * 
   * 
   * @param callback The callback function that will be called after the
   * objects have been retrieved. Exception information, if any, that occurred 
   * during the operation will be passed to the callback function.
   * 
   */
  public List<CloudObject> find() throws CloudAuthException, 
                    CloudException
  {
    return findtask();
  }
  
  /**
   * Sets a query condition. The condition checks if the value of a property of 
   * a CloudObject matches one of the objects in the given Collection. The
   * CloudObject will be selected if the match is found. 
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param values Collection containing a set of desirable values that
   * a CloudObject should have. 
   * 
   * * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereContainedIn(String key, Collection<? extends Object> values)
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$in", values);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
    
  /**
   * Sets a query condition. The condition checks if the CloudObject
   * has some value set for a particular property. The object is not selected
   * if the property is set.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereDoesNotExist(String key) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$exists", false);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is equal to the provided value.
   * The object is selected if the value matches.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereEqualTo(String key, Object value) 
  {
    
    try {
      
      queryObj.put(key, value);
    
    } catch (JSONException e) {
    
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  
  /**
   * Sets a query condition. The condition checks if the CloudObject
   * has some value set for a particular property. The object is selected
   * if the property is set.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereExists(String key) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$exists", true);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is greater than the provided value.
   * The object is selected if the object's property value is greater.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereGreaterThan(String key, Object value) 
  {
    JSONObject condition;
    try {      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$gt", value);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is greater than or equal to 
   * the provided value. The object is selected if the object's property 
   * value is greater than or equal to the given value.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereGreaterThanOrEqualTo(String key, Object value) 
  {
    JSONObject condition;
    try {
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }      
      condition.put("$gte", value);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is less than the provided value. 
   * The object is selected if the object's property value is less 
   * than the given value.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereLessThan(String key, Object value) 
  {
    JSONObject condition;
    try {      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$lt", value);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is less than or equal to 
   * the provided value. The object is selected if the object's property 
   * value is less than or equal to the given value.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereLessThanOrEqualTo(String key, Object value) 
  {
    JSONObject condition;
    try {
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      
      condition.put("$lte", value);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given string property of a CloudObject matches the given regular 
   * expression. The object is selected if there is a match.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param regex the regular expression pattern to match with.
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereMatches(String key, String regex) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$regex", regex);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given string property of a CloudObject matches the given regular 
   * expression. The object is selected if there is a match.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param regex the regular expression pattern to match with.
   * 
   * @param modifiers the following PCRE modifiers can be provided
   *   i - Case insensitive regex matching
   *  m - regex matching across multiple lines
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereMatches(String key, String regex, String modifiers) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$regex", regex);
      condition.put("$options", modifiers);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks if the value of a property of 
   * a CloudObject matches any of the objects in the given Collection. The
   * CloudObject will not be selected if the match is found. 
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param values Collection containing a set of desirable values that
   * a CloudObject should have. 
   * 
   * * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereNotContainedIn(String key, Collection<? extends Object> values) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$nin", values);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  /**
   * Sets a query condition. The condition checks whether the value of
   * a given property of a CloudObject is equal to the provided value.
   * The object is selected if the value doesn't match.
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @param value the desired value of the property
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery whereNotEqualTo(String key, Object value) 
  {
    JSONObject condition;
    try {
      
      try{
        condition = (JSONObject) queryObj.get(key);
      }
      catch (JSONException e){
        condition = new JSONObject();
      }
      condition.put("$ne", value);
      queryObj.put(key, condition);
      
    } catch (JSONException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in adding query condition", e);
      throw new CloudException("Invalid query");  
    }
    return this;
  }
  
  
  /**
   * Sets the ordering of query results. The results returned 
   * from the query will be arranged in ascending order of the 
   * values of the given key
   * 
   * @param key the name of the property of the CloudObject
   * 
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery orderByAscending(String key){
    this.order =  ASCENDING;
    this.orderby = key;
    return this;
  }
  
  
  /**
   * Sets the ordering of query results. The results returned 
   * from the query will be arranged in descending order of the 
   * values of the given key
   * 
   * @param key the name of the property of the CloudObject
   * 
   * @return the current query object. You can set more condition on the same
   *   object
   * 
   */
  public CloudQuery orderByDescending(String key){
    this.order = DESCENDING;
    this.orderby = key;
    return this;
  }
  
}




Java Source Code List

net.getcloudengine.CloudAuthException.java
net.getcloudengine.CloudEndPoints.java
net.getcloudengine.CloudEngineReceiver.java
net.getcloudengine.CloudEngineSdkVersion.java
net.getcloudengine.CloudEngineUtils.java
net.getcloudengine.CloudEngine.java
net.getcloudengine.CloudException.java
net.getcloudengine.CloudFile.java
net.getcloudengine.CloudObjectException.java
net.getcloudengine.CloudObject.java
net.getcloudengine.CloudPushService.java
net.getcloudengine.CloudQuery.java
net.getcloudengine.CloudUser.java
net.getcloudengine.DeleteObjectCallback.java
net.getcloudengine.FetchFileCallback.java
net.getcloudengine.FetchObjectCallback.java
net.getcloudengine.FindObjectsCallback.java
net.getcloudengine.GetObjectCallback.java
net.getcloudengine.LoginCallback.java
net.getcloudengine.LogoutCallback.java
net.getcloudengine.PasswordResetCallback.java
net.getcloudengine.PushCallback.java
net.getcloudengine.SaveFileCallback.java
net.getcloudengine.SaveObjectCallback.java
net.getcloudengine.SignupCallback.java