Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:net.atos.entng.wiki.service.WikiServiceMongoImpl.java

License:Open Source License

@Override
public void deleteRevisions(String wikiId, String pageId, Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("wikiId").is(wikiId);
    if (pageId != null && !pageId.trim().isEmpty()) {
        query.put("pageId").is(pageId);
    }//  ww  w.j  ava  2 s .co m
    mongo.delete(REVISIONS_COLLECTION, MongoQueryBuilder.build(query),
            MongoDbResult.validResultHandler(handler));
}

From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java

License:Open Source License

/**
 * Retrieve the data for given test//w ww  . ja v  a  2s . com
 * 
 * @param testObjId
 *        the ID of the test
 * @param includeProperties
 *        <tt>true</tt> to flesh out the properties
 * @return the test object or <tt>null</tt> if not found
 */
public DBObject getTest(ObjectId testObjId, boolean includeProperties) {
    DBObject queryObj = QueryBuilder.start(FIELD_ID).is(testObjId).get();

    BasicDBObjectBuilder fieldsObjBuilder = BasicDBObjectBuilder.start(FIELD_NAME, 1).add(FIELD_VERSION, true)
            .add(FIELD_DESCRIPTION, true).add(FIELD_RELEASE, true).add(FIELD_SCHEMA, true);
    DBObject fieldsObj = fieldsObjBuilder.get();

    DBObject testObj = tests.findOne(queryObj, fieldsObj);
    if (testObj == null) {
        // The test run no longer exists
        logger.warn("Test not found.  Returning null test: " + testObjId);
        return null;
    }

    BasicDBList propsList = new BasicDBList();
    if (includeProperties) {
        // Get the associated test definition
        String test = (String) testObj.get(FIELD_NAME);
        String release = (String) testObj.get(FIELD_RELEASE);
        Integer schema = (Integer) testObj.get(FIELD_SCHEMA);
        TestDefEntry testDefEntry = getTestDefCached(release, schema);
        if (testDefEntry == null) {
            // Again, we don't bother trying to resolve this
            logger.warn("Test definition not found for test: " + testObj);
            logger.warn("Deleting test without a test definition: " + testObj);
            this.deleteTest(test);
            return null;
        } else {
            // Start with the properties from the test definition
            Map<String, DBObject> propsMap = new HashMap<String, DBObject>(testDefEntry.testDefPropsMap);

            // Fetch the properties for the test
            DBCursor testPropsCursor = getTestPropertiesRaw(testObjId, null);
            // Combine
            MongoTestDAO.mergeProperties(propsMap, testPropsCursor);

            // Turn into a map and add back into the object
            propsList = MongoTestDAO.getPropertyList(propsMap);
            testObj.put(FIELD_PROPERTIES, propsList);
        }
    }

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Found test: " + testObj);
    }
    return testObj;
}

From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java

License:Open Source License

public DBObject importTestRun(String testName, String runName, DBObject importObj) {
    // create return object
    DBObject resultObj = new BasicDBObject();
    String message = "Import succeeded.";
    ImportResult result = ImportResult.OK;

    try {//  w  w  w  .  jav  a2s  .  c  o  m
        ArgumentCheck.checkMandatoryString(testName, "testName");
        ArgumentCheck.checkMandatoryString(runName, "runName");
        ArgumentCheck.checkMandatoryObject(importObj, "importObj");

        // get object IDs
        ObjectId testObjId = getTestId(testName);
        ObjectId runObjId = getTestRunId(testObjId, runName);
        if (null == testObjId) {
            throw new ObjectNotFoundException(testName + "." + runName);
        }

        // get test definition
        DBObject queryObj = QueryBuilder.start(FIELD_ID).is(testObjId).get();
        BasicDBObjectBuilder fieldsObjBuilder = BasicDBObjectBuilder.start(FIELD_NAME, 1)
                .add(FIELD_RELEASE, true).add(FIELD_SCHEMA, true);
        DBObject fieldsObj = fieldsObjBuilder.get();

        DBObject testObj = tests.findOne(queryObj, fieldsObj);
        if (testObj == null) {
            throw new ObjectNotFoundException(testName + "." + runName);
        }

        // get values from test
        String release = (String) testObj.get(FIELD_RELEASE);
        Object tmp = testObj.get(FIELD_SCHEMA);
        Integer schema = null == tmp ? 0 : Integer.valueOf(tmp.toString());

        // get properties
        Map<String, DBObject> mapProps = getTestRunPropertiesMap(testObjId, runObjId, testName, runName);

        // get values from the import object
        Object relObj = importObj.get(FIELD_RELEASE);
        Object schemaObj = importObj.get(FIELD_SCHEMA);
        if (null != relObj && !relObj.toString().equals(release)) {
            result = ImportResult.WARN;
            message += "\r\nWARN: Release '" + release + "' from test to import doesn't match import release '"
                    + relObj.toString() + "'!";
        }
        if (null != schemaObj && !schemaObj.toString().equals(schema.toString())) {
            result = ImportResult.WARN;
            message += "\r\nWARN: Schema '" + schema + "' from test to import doesn't match import schema '"
                    + schemaObj.toString() + "'!";
        }

        // decrypt all values in the properties 
        // separate from set value - might throw exception and nothing should be changed if
        BasicDBList propsListEnc = (BasicDBList) importObj.get(FIELD_PROPERTIES);
        BasicDBList propsListDec = new BasicDBList();
        for (final Object obj : propsListEnc) {
            final DBObject dbObj = (DBObject) obj;
            String propName = (String) dbObj.get(FIELD_NAME);

            // decrypt
            DBObject prop = decryptPropertyValue(dbObj, propName);
            propsListDec.add(prop);
        }

        // again a loop and update the values 
        for (final Object objProp : propsListDec) {
            // get property
            final DBObject dbObj = (DBObject) objProp;
            String propName = (String) dbObj.get(FIELD_NAME);

            // get oldProperty
            final DBObject oldProp = mapProps.get(propName);
            if (null == oldProp) {
                result = ImportResult.WARN;
                message += "\r\nWARN: Ignored property '" + propName + "' not found";
            } else {
                // see if the value differs
                String oldValue = getPropValueAsString(oldProp);
                String newValue = getPropValueAsString(dbObj);
                if (!oldValue.equals(newValue)) {
                    // update property
                    updateProperty(testName, runName, propName, newValue, oldProp);
                }
            }
        }
    } catch (ObjectNotFoundException onfe) {
        message = "Test or test run not found: '" + testName + "." + runName + "'!";
        result = ImportResult.ERROR;
        logger.error(message, onfe);

        message += "\r\n\r\n" + onfe.toString();
    } catch (CipherException ce) {
        message = "Error during decryption while import properties of test run: '" + testName + "." + runName
                + "'! No value imported";
        result = ImportResult.ERROR;
        logger.error(message, ce);

        message += "\r\n\r\n" + ce.toString();
    }

    // put return values
    resultObj.put(FIELD_RESULT, result.toString());
    resultObj.put(FIELD_MESSAGE, message);

    return resultObj;
}

From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java

License:Open Source License

/**
 * Exports a test run with encrypted password properties by test and run
 * name.//w  w w . j  a v a2  s.co  m
 * 
 * @param testName
 *        (String, mandatory) test name
 * @param runName
 *        (String, mandatory) run name
 * 
 * @return (DBObject) or exception
 * 
 * @throws ObjectNotFoundException
 * @throws CipherException
 * 
 * @since 2.1.2
 */
public DBObject exportTestRun(String testName, String runName) throws ObjectNotFoundException, CipherException {
    ArgumentCheck.checkMandatoryString(testName, "testName");
    ArgumentCheck.checkMandatoryString(runName, "runName");

    // get object IDs
    ObjectId testObjId = getTestId(testName);
    ObjectId runObjId = getTestRunId(testObjId, runName);
    if (null == testObjId) {
        throw new ObjectNotFoundException(testName + "." + runName);
    }

    // get test definition
    DBObject queryObj = QueryBuilder.start(FIELD_ID).is(testObjId).get();
    BasicDBObjectBuilder fieldsObjBuilder = BasicDBObjectBuilder.start(FIELD_NAME, 1).add(FIELD_VERSION, true)
            .add(FIELD_RELEASE, true).add(FIELD_SCHEMA, true);
    DBObject fieldsObj = fieldsObjBuilder.get();

    DBObject testObj = tests.findOne(queryObj, fieldsObj);
    if (testObj == null) {
        throw new ObjectNotFoundException(testName + "." + runName);
    }

    // get values from test
    String release = (String) testObj.get(FIELD_RELEASE);
    Object tmp = testObj.get(FIELD_SCHEMA);
    Integer schema = null == tmp ? 0 : Integer.valueOf(tmp.toString());
    tmp = testObj.get(FIELD_VERSION);
    Integer version = null == tmp ? 0 : Integer.valueOf(tmp.toString());

    // get properties
    Set<String> maskedProps = getMaskedProperyNames(testName);
    Map<String, DBObject> mapProps = getTestRunPropertiesMap(testObjId, runObjId, testName, runName);

    // encrypt passwords
    for (final String propName : maskedProps) {
        DBObject propDbObj = mapProps.get(propName);
        if (null != propDbObj) {
            // encrypt
            propDbObj = encryptPropertyValue(propDbObj, propName);
            mapProps.put(propName, propDbObj);
        }
    }

    // prepare return object
    DBObject exportObj = new BasicDBObject();
    exportObj.put(FIELD_TEST, testName);
    exportObj.put(FIELD_RUN, runName);
    exportObj.put(FIELD_RELEASE, release);
    exportObj.put(FIELD_SCHEMA, schema);
    exportObj.put(FIELD_VERSION, version);

    // Turn into a map and add
    BasicDBList propsList = MongoTestDAO.getPropertyList(mapProps);
    exportObj.put(FIELD_PROPERTIES, propsList);

    return exportObj;
}

From source file:org.alfresco.cacheserver.dao.mongo.MongoWebSocketDAO.java

License:Open Source License

@Override
public Registration getByUsername(String username) {
    QueryBuilder queryBuilder = QueryBuilder.start("u").is(username);
    DBObject query = queryBuilder.get();

    DBObject dbObject = registrationData.findOne(query);
    Registration registration = toRegistration(dbObject);
    return registration;
}

From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java

License:Open Source License

@Override
public NodeInfo getByNodeId(String nodeId, String nodeVersion, boolean isPrimary) {
    QueryBuilder queryBuilder = QueryBuilder.start("e").is(serverIdentity.getId()).and("n").is(nodeId).and("v")
            .is(nodeVersion).and("pri").is(isPrimary);
    DBObject query = queryBuilder.get();

    DBObject dbObject = contentData.findOne(query);
    NodeInfo nodeInfo = toNodeInfo(dbObject);
    return nodeInfo;
}

From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java

License:Open Source License

@Override
public NodeInfo getByNodeId(String nodeId, long nodeVersion, boolean isPrimary) {
    QueryBuilder queryBuilder = QueryBuilder.start("e").is(serverIdentity.getId()).and("n").is(nodeId).and("v")
            .is(nodeVersion).and("pri").is(isPrimary);
    DBObject query = queryBuilder.get();

    DBObject dbObject = contentData.findOne(query);
    NodeInfo nodeInfo = toNodeInfo(dbObject);
    if (nodeInfo == null) {
        throw new InvalidNodeException(nodeId, nodeVersion);
    }//  w  ww.ja v a 2  s .c  o  m
    return nodeInfo;
}

From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java

License:Open Source License

@Override
public NodeInfo getByNodeId(String nodeId, long nodeVersion, MimeType mimeType) {
    QueryBuilder queryBuilder = QueryBuilder.start("e").is(serverIdentity.getId()).and("n").is(nodeId).and("v")
            .is(nodeVersion).and("pri").is(false).and("mimeType").is(mimeType.getMimetype());
    DBObject query = queryBuilder.get();

    DBObject dbObject = contentData.findOne(query);
    NodeInfo nodeInfo = toNodeInfo(dbObject);
    if (nodeInfo == null) {
        throw new InvalidNodeException(nodeId, nodeVersion);
    }/*from  w w  w.j  a va 2 s  . co m*/
    return nodeInfo;
}

From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java

License:Open Source License

@Override
public NodeInfo getByNodeId(long nodeInternalId, String mimeType) {
    QueryBuilder queryBuilder = QueryBuilder.start("e").is(serverIdentity.getId()).and("ni").is(nodeInternalId)
            .and("m").is(mimeType);
    DBObject query = queryBuilder.get();

    DBObject dbObject = contentData.findOne(query);
    NodeInfo nodeInfo = toNodeInfo(dbObject);
    return nodeInfo;
}

From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java

License:Open Source License

@Override
public void updateNode(NodeInfo nodeInfo) {
    String cacheServerId = serverIdentity.getId();
    String nodeId = nodeInfo.getNode().getNodeId();
    // String versionLabel = nodeInfo.getNode().getVersionLabel();
    Long nodeInternalId = nodeInfo.getNode().getNodeInternalId();
    Long nodeVersion = nodeInfo.getNode().getNodeVersion();
    MimeType mimeType = nodeInfo.getMimeType();
    Long size = nodeInfo.getSize();
    String nodePath = nodeInfo.getNode().getNodePath();
    String contentPath = nodeInfo.getContentPath();
    boolean isPrimary = nodeInfo.isPrimary();

    QueryBuilder queryBuilder = QueryBuilder.start("e").is(cacheServerId).and("n").is(nodeId);
    if (nodeVersion != null) {
        queryBuilder.and("v").is(nodeVersion);
    }//  www  .j  a v  a 2  s  .  c om
    if (nodeInternalId != null) {
        queryBuilder.and("ni").is(nodeInternalId);
    }
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("$set",
            BasicDBObjectBuilder.start("m", mimeType.getMimetype()).add("s", size).add("ni", nodeInternalId)
                    .add("c", contentPath).add("p", nodePath).add("pri", isPrimary).get());
    DBObject update = builder.get();

    WriteResult result = contentData.update(query, update, true, false);
    checkResult(result);
}