Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

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

Prototype

public static BasicDBObjectBuilder start(final String key, final Object val) 

Source Link

Document

Creates a builder initialized with the given key/value.

Usage

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 {//from  w w  w .j a  va 2 s . c om
        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./*www. j  a  v a2  s.c o  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.bm.user.UserDataServiceImpl.java

License:Open Source License

/**
 * Ensure that the MongoDB collection has the required indexes associated with
 * this user bean./*from   www .  j  av a  2 s .  c o  m*/
 */
private void checkIndexes() {
    collection.setWriteConcern(WriteConcern.SAFE);

    DBObject uidxUserName = BasicDBObjectBuilder.start(FIELD_USERNAME, 1).get();
    DBObject optUserName = BasicDBObjectBuilder.start("name", "uidxUserName").add("unique", Boolean.TRUE).get();
    collection.createIndex(uidxUserName, optUserName);

    DBObject uidxEmail = BasicDBObjectBuilder.start(FIELD_EMAIL, 1).get();
    DBObject optEmail = BasicDBObjectBuilder.start("name", "uidxEmail").add("unique", Boolean.TRUE).get();
    collection.createIndex(uidxEmail, optEmail);

    DBObject idxDomainRand = BasicDBObjectBuilder.start(FIELD_DOMAIN, 1).add(FIELD_RANDOMIZER, 2).get();
    DBObject optDomainRand = BasicDBObjectBuilder.start("name", "idxDomainRand").add("unique", Boolean.FALSE)
            .get();
    collection.createIndex(idxDomainRand, optDomainRand);

    DBObject idxCreationStateRand = BasicDBObjectBuilder.start(FIELD_CREATION_STATE, 1).add(FIELD_RANDOMIZER, 2)
            .get();
    DBObject optCreationStateRand = BasicDBObjectBuilder.start("name", "idxCreationStateRand")
            .add("unique", Boolean.FALSE).get();
    collection.createIndex(idxCreationStateRand, optCreationStateRand);
}

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

License:Open Source License

private void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }/*  w  w  w .j av a2 s. com*/

    this.registrationData = getCollection(db, registrationDataCollectionName, WriteConcern.ACKNOWLEDGED);

    {
        DBObject keys = BasicDBObjectBuilder.start("u", 1).get();
        this.registrationData.ensureIndex(keys, "byUserName", false);
    }
}

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

License:Open Source License

private DBObject fromRegistration(Registration registration) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("u", registration.getUsername()).add("i",
            registration.getIpAddress());
    return builder.get();
}

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

License:Open Source License

private void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }/*from w  ww.  j a  v a  2 s. c om*/

    this.contentData = getCollection(db, contentCollectionName, WriteConcern.ACKNOWLEDGED);

    {
        DBObject keys = BasicDBObjectBuilder.start("e", 1).add("n", 1).add("v", 1).add("pri", 1).get();
        this.contentData.ensureIndex(keys, "byNodeId", false);
    }

    {
        DBObject keys = BasicDBObjectBuilder.start("e", 1).add("n", 1).add("p", 1).get();
        this.contentData.ensureIndex(keys, "byNodePath", false);
    }
}

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);
    }/*from  w ww . j a v  a  2  s .  com*/
    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);
}

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

License:Open Source License

private void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }/*from  ww w.j a va 2s  . c  o  m*/

    this.nodeUsageData = getCollection(db, nodeUsageCollectionName, WriteConcern.ACKNOWLEDGED);
    DBObject keys = BasicDBObjectBuilder.start("e", 1).add("n", 1).add("m", 1).get();
    this.nodeUsageData.ensureIndex(keys, "byMimeType", false);
}

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

License:Open Source License

private DBObject fromNodeUsageInfo(NodeUsage nodeUsage) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("e", serverIdentity.getId())
            .add("n", nodeUsage.getNodeId()).add("v", nodeUsage.getNodeVersion())
            .add("t", nodeUsage.getTimestamp()).add("u", nodeUsage.getUsername());
    return builder.get();
}

From source file:org.alfresco.events.serialize.PropertySerializer.java

License:Open Source License

private List<DBObject> serializePath(Path path) {
    Iterator<Path.Element> elements = path.iterator();
    List<DBObject> elementsList = new ArrayList<DBObject>();
    while (elements.hasNext()) {
        Element element = elements.next();

        PathElementType elementType = getElementType(element);
        DBObject elementValue = null;//from w ww .  jav  a  2 s. c  o  m

        if (elementType == PathElementType.ChildAssoc) {
            ChildAssocElement childAssocElement = (ChildAssocElement) element;
            ChildAssociationRef childAssocRef = childAssocElement.getRef();
            elementValue = serializeChildAssocRef(childAssocRef);
        } else {
            throw new IllegalArgumentException();
        }

        DBObject dbObject = BasicDBObjectBuilder.start("element", elementValue)
                .add("elementType", elementType.toString()).get();
        elementsList.add(dbObject);
    }

    return elementsList;
}