Example usage for org.springframework.data.mongodb.core.query Query getQueryObject

List of usage examples for org.springframework.data.mongodb.core.query Query getQueryObject

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query Query getQueryObject.

Prototype

public Document getQueryObject() 

Source Link

Usage

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

/**
 * @param entityName//from  ww  w .  j a va  2 s .c om
 * @param propertyName
 * @param propertyValue
 * @return document by property value
 */
public DBObject findOneByProperty(String entityName, String propertyName, Object propertyValue) {
    final Query query;
    if (String.class.isInstance(propertyValue)) {
        propertyValue = String.class.cast(propertyValue).toLowerCase();
        query = MongoDbUtils.getQueryByStringCaseInsensitive(propertyName, (String) propertyValue);
    } else {
        query = new Query(Criteria.where(propertyName).is(propertyValue));
    }

    DBObject object = getCollection(entityName).findOne(query.getQueryObject());
    return object;
}

From source file:org.opentestsystem.authoring.testauth.persistence.BlueprintElementRepositoryImpl.java

@SuppressWarnings({ "unchecked" })
@Override/*w  w w  .  j a va 2s .com*/
public String[] findDistinctActiveLevelsByAssessmentId(final String assessmentId) {
    final Query query = new Query();
    query.addCriteria(Criteria.where("assessmentId").is(assessmentId));
    query.addCriteria(Criteria.where("active").is(true));

    return Iterables.toArray(Lists.newArrayList(
            this.mongoOperations.getCollection("blueprintElement").distinct("level", query.getQueryObject())),
            String.class);
}

From source file:org.opentestsystem.authoring.testitembank.persistence.ItemRepositoryImpl.java

@Override
public SearchResponse<Item> searchWithIrtDimensions(final ItemSearchRequest searchRequest) {
    //LOGGER.info ("Calling searchWithIrtDimensions");

    //Save the original search criteria array, it may contain IrtDimension.IrtStatDomain
    Map<String, String[]> searchCriteria = searchRequest.getSearchCriteria();

    Boolean includeAllIrtDimensions = false;
    if (searchCriteria.containsKey("IrtDimension.IrtStatDomain")) {
        //LOGGER.info ("Found IrtDimension.IrtStatDomain search criteria" );

        if (ArrayUtils.contains(searchCriteria.get("IrtDimension.IrtStatDomain"), "*") == true) {
            //LOGGER.info ("Found IrtDimension criteria with * value");
            includeAllIrtDimensions = true;
            // Note that we remove IrtDimension.IrtStatDomain from the searchRequest, but it was saved
            // for future checks in searchCriteria variable
            searchRequest.getSearchCriteria().remove("IrtDimension.IrtStatDomain");
        }/*from w w w .  jav a  2  s  .  c  o m*/
    }

    Query query = searchRequest.buildQuery();
    // SB-736 For searches that requires a criteria, nothing should be returned
    // if criteria is not present
    if (searchRequest.isSearchCriteriaRequired()) {
        if (query.getQueryObject() == null || query.getQueryObject().keySet().size() == 0) {
            return new SearchResponse<Item>(Collections.<Item>emptyList(), searchRequest, 0);
        }
    }
    long total = this.mongoTemplate.count(query, Item.class);
    List<Item> results = (total == 0) ? Collections.<Item>emptyList()
            : this.mongoTemplate.find(query, Item.class);
    //LOGGER.info ("Found " + total + " items in result set");

    // There are three options of dealing with IrtDimensions.
    // 1. original request contained IrtStatDomain criteria and its value was equal to '*'.
    //    We removed this criteria from search request and and set up 
    //    includeAllIrtDimensions variable to 'true'.
    //    It means: include all IrtDimensions into result.
    // 2. original request contained IrtStatDomain criteria and its value was not equal to '*'.
    //    It means: include only IrtDimensions with IrtStatDomain value present in the search criteria.
    // 3. original request did not contain IrtStatDomain criteria.
    //    It means: do not include IrtDimension elements in the result.

    if (includeAllIrtDimensions == true) {
        //LOGGER.info ("Including all IrtDimensions into result set");
        // Case 1. do nothing to results; they are already perfect
    }

    else if (total != 0 && searchCriteria.containsKey("IrtDimension.IrtStatDomain")) {
        // Case 2.
        String[] irtStatDomainValues = searchCriteria.get("IrtDimension.IrtStatDomain");
        for (Item item : results) {
            List<ItemScoreDimension> dimensionList = item.getItemScoreDimensionList();
            List<ItemScoreDimension> updatedDimensionList = new ArrayList<ItemScoreDimension>();
            for (ItemScoreDimension dim : dimensionList) {
                if (ArrayUtils.contains(irtStatDomainValues, dim.getIrtStatDomain()) == true) {
                    //LOGGER.info("Keeping IrtDimension for " + dim.getIrtStatDomain ());
                    updatedDimensionList.add(dim);
                } else {
                    //LOGGER.info("Skipping IrtDimension for " + dim.getIrtStatDomain ());
                }
            }
            item.setItemScoreDimensionList(updatedDimensionList);
            if (item.getAllIncludedMetatdata().containsKey("IrtDimension")) {
                //LOGGER.info ("Found IrtDimension in allIncludedMetatdata ");
                item.getAllIncludedMetatdata().put("IrtDimension", updatedDimensionList);
                //LOGGER.info("Updated IrtDimension in allIncludedMetatdata");
            }
        }
    } else if (total != 0) {
        // Case 3. Need to strip all ItemScoreDimensions
        //LOGGER.info ("Removing IrtDimension from all items and allIncludedMetatdata");
        for (Item item : results) {
            item.setItemScoreDimensionList(null);
            item.getAllIncludedMetatdata().remove("IrtDimension");
        }
    }
    return new SearchResponse<Item>(results, searchRequest, total);
}

From source file:org.opentestsystem.delivery.testreg.persistence.EligibleStudentRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//from  ww  w  .j  av  a  2 s . c  o m
public List<String> findByIdInstitutionIdAndAssessmentId(final String institutionEntityMongoId,
        final String assessmentId) {
    Query query = new Query();
    query.addCriteria(where("student.institutionEntityMongoId").is(institutionEntityMongoId)
            .and("assessments._id").is(new ObjectId(assessmentId)));
    return mongoOperations.getCollection("eligibleStudent").distinct("student.entityId",
            query.getQueryObject());

}

From source file:org.slc.sli.dal.repository.DeltaJournalTest.java

@SuppressWarnings("unchecked")
private Matcher<Query> buildQueryMatcher(final List<byte[]> ids) {
    return new BaseMatcher<Query>() {

        @Override/*from  w w w.  j  ava 2s  .  c o  m*/
        public boolean matches(Object arg0) {
            Query q = (Query) arg0;
            Map<String, Object> inClause = (Map<String, Object>) q.getQueryObject().get("_id");
            List<byte[]> idClause = (List<byte[]>) inClause.get("$in");
            for (int i = 0; i < ids.size(); i++) {
                if (!Hex.encodeHexString(ids.get(i)).equals(Hex.encodeHexString(idClause.get(i)))) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public void describeTo(Description arg0) {
            arg0.appendText("Query matching ids: " + ids);
        }
    };
}

From source file:org.slc.sli.dal.repository.MongoRepository.java

@Override
public long count(String collectionName, Query query) {
    return count(collectionName, query.getQueryObject());
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Execute a MongoDB query and iterate over the query results on a per-document basis with a
 * {@link DocumentCallbackHandler} using the provided CursorPreparer.
 * //from  w  w  w .  j a  v  a2s . c om
 * @param query the query class that specifies the criteria used to find a record and also an optional fields
 *          specification, must not be {@literal null}.
 * @param collectionName name of the collection to retrieve the objects from
 * @param dch the handler that will extract results, one document at a time
 * @param preparer allows for customization of the {@link DBCursor} used when iterating over the result set, (apply
 *          limits, skips and so on).
 */
protected void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch,
        CursorPreparer preparer) {

    Assert.notNull(query);

    DBObject queryObject = query.getQueryObject();
    DBObject sortObject = query.getSortObject();
    DBObject fieldsObject = query.getFieldsObject();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: $s",
                serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName));
    }

    this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
    if (query.getSortObject() == null) {
        return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
    } else {//w  ww.j a  v  a 2s . c o  m
        query.limit(1);
        List<T> results = find(query, entityClass, collectionName);
        return results.isEmpty() ? null : results.get(0);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> List<T> find(final Query query, Class<T> entityClass, String collectionName) {
    CursorPreparer cursorPreparer = query == null ? null : new QueryCursorPreparer(query);
    return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, cursorPreparer);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass,
        String collectionName) {//  www .  j a v  a 2s  .  com
    return doFindAndModify(collectionName, query.getQueryObject(), query.getFieldsObject(),
            query.getSortObject(), entityClass, update, options);
}