Java Code Examples for org.springframework.data.mongodb.core.query.Query#getQueryObject()

The following examples show how to use org.springframework.data.mongodb.core.query.Query#getQueryObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubDocAccessor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private DBObject getParentQuery(Map<String, Object> body) {
    Query parentQuery = new Query();
    for (Entry<String, String> entry : lookup.entrySet()) {
        parentQuery.addCriteria(new Criteria(entry.getValue()).is(body.get(entry.getKey())));
    }
    return parentQuery.getQueryObject();
}
 
Example 2
Source File: SubDocAccessor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public boolean doUpdate(Query query, Update update) {
    DBObject queryDBObject = toSubDocQuery(query, true);

    DBObject elementMatch = new BasicDBObject("$elemMatch", query.getQueryObject());
    queryDBObject.put(subField, elementMatch);

    DBObject patchUpdate = toSubDocUpdate(update);
    String updateCommand = "{findAndModify:\"" + collection + "\",query:" + queryDBObject.toString()
            + ",update:" + patchUpdate.toString() + "}";
    LOG.debug("the update date mongo command is: {}", updateCommand);
    TenantContext.setIsSystemCall(false);
    CommandResult result = template.executeCommand(updateCommand);
    return result.get("value") != null;

}
 
Example 3
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testPIISearchEquals() {
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.addCriteria(new NeutralCriteria("birthData.birthDate", "=", "1960-01-01"));

    Query query = mongoQueryConverter.convert("student", neutralQuery);
    assertNotNull("Should not be null", query);
    DBObject obj = query.getQueryObject();
    assertNotNull("Should not be null", obj);
}
 
Example 4
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the conversion of NeutralQueries (containing ORed criteria)
 * into Mongo-appropriate Query objects.
 *
 * This test uses an example similar to:
 *
 * select *
 *   from student
 *  where economicDisadvantaged = true
 *    and studentUniqueStateId = '000000054')
 *
 */
@Test
public void testOrConvert() {
    NeutralQuery mainQuery = new NeutralQuery();

    //not part of the or, so added to the main query
    mainQuery.addCriteria(new NeutralCriteria("economicDisadvantaged=true"));

    //construct a query representing all the criteria in 1 or branch
    NeutralQuery orQuery1 = new NeutralQuery();

    //construct a query representing all the criteria in a second or branch
    NeutralQuery orQuery2 = new NeutralQuery();
    orQuery2.addCriteria(new NeutralCriteria("studentUniqueStateId", "=", "000000054"));

    //add the or queries
    mainQuery.addOrQuery(orQuery1);
    mainQuery.addOrQuery(orQuery2);

    //the converter will convert the NeutralQuery into a mongo Query Object
    Query query = mongoQueryConverter.convert("student", mainQuery);

    assertNotNull("Should not be null", query);
    DBObject obj = query.getQueryObject();
    assertNotNull("Should not be null", obj);
    assertNotNull("Should not be null", obj.get("$or"));
    assertTrue(((BasicBSONList) obj.get("$or")).size() == 1);
}
 
Example 5
Source File: Denormalizer.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private DBObject getParentQuery(Map<String, Object> body) {
    Query parentQuery = new Query();

    for (Map.Entry<String, String> entry : denormalizationReferenceKeys.entrySet()) {
        String value = (String) body.get(entry.getKey());
        String queryKey = entry.getValue();

        if((value == null ) || value.isEmpty() ) {
            Entity entity = null;
            String refEntityId = (String) body.get(entry.getValue());

            if (cachedEntityRefKey != null) {
                if ((denormalizationHelperCache != null)
                        && (!denormalizationHelperCache.isEmpty())) {
                    entity = denormalizationHelperCache.get(entry.getKey()).get(refEntityId);
                } else {
                    Query refEntityQuery = new Query();
                    refEntityQuery.addCriteria(new Criteria("_id").is(refEntityId));
                    entity = template.findOne(refEntityQuery,Entity.class,entry.getKey());
                }
            }

            if(entity == null) {
                continue;
            }
            if (referencedEntityMap == null) {
                referencedEntityMap = new HashMap<String, Entity>();
            }
            referencedEntityMap.put(refEntityId,entity);
            for (Map.Entry<String,String> refEntry : cachedEntityRefKey.entrySet()) {
                String refKey = refEntry.getKey();
                if (refKey.equals("_id")) {
                    value = entity.getEntityId();
                }   else {
                    value = (String)entity.getBody().get(refKey);
                }
                queryKey = refEntry.getValue();
                addToParentQuery(parentQuery,queryKey,value);
            }
        } else {
            addToParentQuery(parentQuery,queryKey, value);
        }
    }

    return parentQuery.getQueryObject();
}
 
Example 6
Source File: ContainerDocumentAccessor.java    From secure-data-service with Apache License 2.0 3 votes vote down vote up
private DBObject getContainerDocQuery(final Entity entity) {
    final String parentKey = ContainerDocumentHelper.createParentKey(entity, containerDocumentHolder, generatorStrategy);

    final Query query = Query.query(Criteria.where("_id").is(parentKey));

    return query.getQueryObject();
}