Search in sources :

Example 1 with QueryConstraints

use of com.parse.ParseQuery.QueryConstraints in project Parse-SDK-Android by ParsePlatform.

the class OfflineQueryLogic method createOrMatcher.

/**
   * Handles $or queries.
   */
private <T extends ParseObject> ConstraintMatcher<T> createOrMatcher(ParseUser user, ArrayList<QueryConstraints> queries) {
    // Make a list of all the matchers to OR together.
    final ArrayList<ConstraintMatcher<T>> matchers = new ArrayList<>();
    for (QueryConstraints constraints : queries) {
        ConstraintMatcher<T> matcher = createMatcher(user, constraints);
        matchers.add(matcher);
    }
    /*
     * Now OR together the constraints for each query.
     */
    return new ConstraintMatcher<T>(user) {

        @Override
        public Task<Boolean> matchesAsync(final T object, final ParseSQLiteDatabase db) {
            Task<Boolean> task = Task.forResult(false);
            for (final ConstraintMatcher<T> matcher : matchers) {
                task = task.onSuccessTask(new Continuation<Boolean, Task<Boolean>>() {

                    @Override
                    public Task<Boolean> then(Task<Boolean> task) throws Exception {
                        if (task.getResult()) {
                            return task;
                        }
                        return matcher.matchesAsync(object, db);
                    }
                });
            }
            return task;
        }
    };
}
Also used : Continuation(bolts.Continuation) Task(bolts.Task) ArrayList(java.util.ArrayList) QueryConstraints(com.parse.ParseQuery.QueryConstraints)

Example 2 with QueryConstraints

use of com.parse.ParseQuery.QueryConstraints in project Parse-SDK-Android by ParsePlatform.

the class OfflineQueryLogic method createMatcher.

/**
   * Returns a ConstraintMatcher that return true iff the object matches QueryConstraints. This
   * takes in a SQLiteDatabase connection because SQLite is finicky about nesting connections, so we
   * want to reuse them whenever possible.
   */
private <T extends ParseObject> ConstraintMatcher<T> createMatcher(ParseUser user, QueryConstraints queryConstraints) {
    // Make a list of all the matchers to AND together.
    final ArrayList<ConstraintMatcher<T>> matchers = new ArrayList<>();
    for (final String key : queryConstraints.keySet()) {
        final Object queryConstraintValue = queryConstraints.get(key);
        if (key.equals("$or")) {
            /*
         * A set of queries to be OR-ed together.
         */
            @SuppressWarnings("unchecked") ConstraintMatcher<T> matcher = createOrMatcher(user, (ArrayList<QueryConstraints>) queryConstraintValue);
            matchers.add(matcher);
        } else if (queryConstraintValue instanceof KeyConstraints) {
            /*
         * It's a set of constraints that should be AND-ed together.
         */
            KeyConstraints keyConstraints = (KeyConstraints) queryConstraintValue;
            for (String operator : keyConstraints.keySet()) {
                final Object keyConstraintValue = keyConstraints.get(operator);
                ConstraintMatcher<T> matcher = createMatcher(user, operator, keyConstraintValue, key, keyConstraints);
                matchers.add(matcher);
            }
        } else if (queryConstraintValue instanceof RelationConstraint) {
            /*
         * It's a $relatedTo constraint.
         */
            final RelationConstraint relation = (RelationConstraint) queryConstraintValue;
            matchers.add(new ConstraintMatcher<T>(user) {

                @Override
                public Task<Boolean> matchesAsync(T object, ParseSQLiteDatabase db) {
                    return Task.forResult(relation.getRelation().hasKnownObject(object));
                }
            });
        } else {
            /*
         * It's not a set of constraints, so it's just a value to compare against.
         */
            matchers.add(new ConstraintMatcher<T>(user) {

                @Override
                public Task<Boolean> matchesAsync(T object, ParseSQLiteDatabase db) {
                    Object objectValue;
                    try {
                        objectValue = getValue(object, key);
                    } catch (ParseException e) {
                        return Task.forError(e);
                    }
                    return Task.forResult(matchesEqualConstraint(queryConstraintValue, objectValue));
                }
            });
        }
    }
    /*
     * Now AND together the constraints for each key.
     */
    return new ConstraintMatcher<T>(user) {

        @Override
        public Task<Boolean> matchesAsync(final T object, final ParseSQLiteDatabase db) {
            Task<Boolean> task = Task.forResult(true);
            for (final ConstraintMatcher<T> matcher : matchers) {
                task = task.onSuccessTask(new Continuation<Boolean, Task<Boolean>>() {

                    @Override
                    public Task<Boolean> then(Task<Boolean> task) throws Exception {
                        if (!task.getResult()) {
                            return task;
                        }
                        return matcher.matchesAsync(object, db);
                    }
                });
            }
            return task;
        }
    };
}
Also used : Task(bolts.Task) Continuation(bolts.Continuation) ArrayList(java.util.ArrayList) QueryConstraints(com.parse.ParseQuery.QueryConstraints) RelationConstraint(com.parse.ParseQuery.RelationConstraint) JSONObject(org.json.JSONObject) KeyConstraints(com.parse.ParseQuery.KeyConstraints)

Aggregations

Continuation (bolts.Continuation)2 Task (bolts.Task)2 QueryConstraints (com.parse.ParseQuery.QueryConstraints)2 ArrayList (java.util.ArrayList)2 KeyConstraints (com.parse.ParseQuery.KeyConstraints)1 RelationConstraint (com.parse.ParseQuery.RelationConstraint)1 JSONObject (org.json.JSONObject)1