Example usage for com.mongodb QueryOperators EXISTS

List of usage examples for com.mongodb QueryOperators EXISTS

Introduction

In this page you can find the example usage for com.mongodb QueryOperators EXISTS.

Prototype

String EXISTS

To view the source code for com.mongodb QueryOperators EXISTS.

Click Source Link

Usage

From source file:org.slc.sli.dal.convert.ContainerDocumentAccessor.java

License:Apache License

/**
 * Generate query criteria for the container-embedded doc (e.g. attendanceEvent) based on natural key fields
 * specified in schema for the 'embeddedDocType'.
 *
 * @param embeddedDocType// w w w  .java 2 s .c o m
 *        Container document type
 * @param doc
 *        Container-embedded document (e.g. single attendanceEvent)
 *
 * @return
 *        Query criteria for the container-embedded doc based on the natural key fields.
 */
private Map<String, Object> filterByNaturalKeys(String embeddedDocType, Map<String, Object> doc) {
    Map<String, Object> filteredDoc = new HashMap<String, Object>();
    List<Map<String, Object>> fieldCriteria = new ArrayList<Map<String, Object>>();

    // get natural key fields from schema
    NeutralSchema schema = schemaRepo.getSchema(embeddedDocType);

    // don't filter if the natural keys are unknown
    if (schema == null) {
        return doc;
    }

    // loop over natural key fields
    Map<String, NeutralSchema> fieldSchemas = schema.getFields();
    for (Map.Entry<String, NeutralSchema> fieldSchema : fieldSchemas.entrySet()) {
        AppInfo appInfo = (fieldSchema.getValue() == null) ? null : fieldSchema.getValue().getAppInfo();
        if (appInfo != null && appInfo.isNaturalKey()) {
            Map<String, Object> naturalKeyCriteria = new HashMap<String, Object>();
            if (doc.containsKey(fieldSchema.getKey())) {
                // add it to the update criteria
                naturalKeyCriteria.put(fieldSchema.getKey(), doc.get(fieldSchema.getKey()));
                fieldCriteria.add(naturalKeyCriteria);
            } else {
                Map<String, Object> nonExistCriteria = new HashMap<String, Object>();
                nonExistCriteria.put(QueryOperators.EXISTS, false);
                // explicitly exclude missing natural key fields
                naturalKeyCriteria.put(fieldSchema.getKey(), nonExistCriteria);
                fieldCriteria.add(naturalKeyCriteria);
            }
        }
    }

    filteredDoc.put(QueryOperators.AND, fieldCriteria);
    return filteredDoc;
}