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() 

Source Link

Document

Creates a builder intialized with an empty document.

Usage

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

License:Open Source License

/**
 * Register a driver with a test run/*from www.  j  av a2 s. c  o m*/
 * 
 * @param runObjId
 *        the ID of the test run
 * @param driverId
 *        the ID of the driver to include
 */
public void addTestRunDriver(ObjectId runObjId, String driverId) {
    // Find the test run
    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runObjId).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$addToSet").add(FIELD_DRIVERS, driverId).pop()
            .get();
    DBObject runObj = testRuns.findAndModify(queryObj, null, null, false, updateObj, true, false);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Added driver ID to run drivers: \n" + "   Run ID:     " + runObjId + "\n"
                + "   Driver:     " + driverId + "\n" + "   Drivers:    " + runObj.get(FIELD_DRIVERS));
    }
}

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

License:Open Source License

/**
 * Derigister a driver from a test run//  w  w  w  .  j  av  a2s. com
 * 
 * @param runObjId
 *        the ID of the test run
 * @param driverId
 *        the ID of the driver to remove
 */
public void removeTestRunDriver(ObjectId runObjId, String driverId) {
    // Find the test run
    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runObjId).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$pull").add(FIELD_DRIVERS, driverId).pop().get();
    DBObject runObj = testRuns.findAndModify(queryObj, null, null, false, updateObj, true, false);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Removed driver ID from run drivers: \n" + "   Run ID:     " + runObjId + "\n"
                + "   Driver:     " + driverId + "\n" + "   Drivers:    " + runObj.get(FIELD_DRIVERS));
    }
}

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

License:Open Source License

/**
 * Delete an existing test run//  w  ww.j a  v a2s .c  om
 * 
 * @param runObjId
 *        the ID of the test run
 * @return <tt>true</tt> if the test run was deleted or <tt>false</tt> if
 *         not
 */
public boolean deleteTestRun(ObjectId runObjId) {
    // Get the test run
    DBObject runObj;
    try {
        runObj = getTestRun(runObjId, false);
    } catch (ObjectNotFoundException e) {
        logger.warn("Unable to delete test run as it does not exist: " + runObjId, e);
        return false;
    }
    ObjectId testObjId = (ObjectId) runObj.get(FIELD_TEST);

    // Find the test run
    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runObjId).get();

    WriteResult result = testRuns.remove(queryObj);
    boolean written = (result.getN() > 0);

    // Clean up properties
    DBObject propDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId)
            .get();
    testProps.remove(propDelObj);

    // Done
    if (logger.isDebugEnabled()) {
        if (written) {
            logger.debug("Deleted test run: " + queryObj);
        } else {
            logger.debug("Did not delete test run: " + runObjId);
        }
    }
    return written;
}

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

License:Open Source License

/**
 * Utility method to copy a DBObject/* www .ja v  a  2s  . c  o  m*/
 */
private static DBObject copyDBObject(DBObject input) {
    // Copy the property to a new instance
    BasicDBObjectBuilder newPropObjBuilder = BasicDBObjectBuilder.start();
    for (String fieldName : input.keySet()) {
        Object fieldValue = input.get(fieldName);
        newPropObjBuilder.add(fieldName, fieldValue);
    }
    return newPropObjBuilder.get();
}

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

License:Open Source License

/**
 * Override a specific test property value.
 * <p/>/*from   w w  w . j a  v  a  2 s . c  o  m*/
 * A version number of zero indicates that there is no existing override
 * defined.<br/>
 * A value of <tt>null</tt> indicates that the existing override should be
 * removed.
 * 
 * @param test
 *        the name of the test
 * @param run
 *        the name of the test run (<tt>null</tt> to reference the test
 *        alone)
 * @param propertyName
 *        the name of the property
 * @param version
 *        the current version of the property
 * @param value
 *        the new value to set or <tt>null</tt> to remove any override
 * @throws IllegalStateException
 *         if the test has started
 */
public boolean setPropertyOverride(String test, String run, String propertyName, int version, String value) {
    // Handle version wrap-around
    int newVersion = (version >= Short.MAX_VALUE) ? 1 : version + 1;

    // We need to keep the IDs
    ObjectId runObjId = null;
    ObjectId testObjId = null;
    String origin = null;

    if (run == null) {
        origin = TestPropertyOrigin.TEST.name();
        // Get the test
        DBObject testObj = getTest(test, false);
        if (testObj == null) {
            logger.warn("Unable to set property override for test as it was not found: " + test);
            return false;
        }
        // Get the ID
        testObjId = (ObjectId) testObj.get(FIELD_ID);
    } else {
        origin = TestPropertyOrigin.RUN.name();
        // Get the test run
        DBObject runObj;
        try {
            runObj = getTestRun(test, run, false);
        } catch (ObjectNotFoundException e1) {
            logger.warn("Test run not found: " + test + "." + run, e1);
            return false;
        }
        // Check the state of the run
        try {
            TestRunState runState = TestRunState.valueOf((String) runObj.get(FIELD_STATE));
            if (runState != TestRunState.NOT_SCHEDULED && runState != TestRunState.SCHEDULED) {
                throw new IllegalStateException(
                        "Property overrides can only be set for test runs that have not started: \n"
                                + "   Run:      " + runObj + "\n" + "   Property: " + propertyName);
            }
        } catch (IllegalArgumentException e) {
            logger.error("Test run state is unknown: " + runObj);
            this.deleteTestRun(runObjId);
            return false;
        }
        // Get the ID
        runObjId = (ObjectId) runObj.get(FIELD_ID);
        testObjId = (ObjectId) runObj.get(FIELD_TEST);
    }

    DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).and(FIELD_RUN).is(runObjId)
            .and(FIELD_NAME).is(propertyName).and(FIELD_VERSION).is(Integer.valueOf(version)).get();

    DBObject updateObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId)
            .add(FIELD_NAME, propertyName).add(FIELD_VERSION, Integer.valueOf(newVersion))
            .add(FIELD_VALUE, value).add(FIELD_ORIGIN, origin).get();

    WriteResult result = null;
    boolean written = false;
    try {
        if (value == null) {
            // remove property
            result = testProps.remove(queryObj);
            written = (result.getN() > 0);
        } else {
            // A value was provided, so either INSERT or UPDATE
            if (version == 0) {
                // This indicates that no override should exist, yet
                result = testProps.insert(updateObj);
                written = true;
            } else {
                // There must an update
                result = testProps.update(queryObj, updateObj);
                written = result.getN() > 0;
            }
        }
    } catch (DuplicateKeyException e) {
        written = false;
    }

    // Done
    if (logger.isDebugEnabled()) {
        if (written) {
            logger.debug("Wrote property override: \n" + "   Test:      " + test + "\n" + "   Run:       " + run
                    + "\n" + "   Property:  " + propertyName + "\n" + "   Version:   " + version);
        } else {
            logger.debug(
                    "Did not update property override: \n" + "   Test:      " + test + "\n" + "   Run:       "
                            + run + "\n" + "   Property:  " + propertyName + "\n" + "   Version:   " + version);
        }
    }
    return written;
}

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

License:Open Source License

/**
 * Record a final set of locked properties for a test run. The properties
 * written will not be updatable./*from   w ww. ja v  a  2  s  .  c o  m*/
 * 
 * @param testObjId
 *        the ID of the test
 * @param runObjId
 *        the ID of the test run
 * 
 * @throws ObjectNotFoundException
 */
public void lockProperties(ObjectId testObjId, ObjectId runObjId) throws ObjectNotFoundException {
    // Get all the test run overrides
    DBObject runObj = getTestRun(runObjId, true);
    BasicDBList propObjs = (BasicDBList) runObj.get(FIELD_PROPERTIES);

    // First remove all current property overrides for the run
    DBObject propDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId)
            .get();
    testProps.remove(propDelObj);

    // Now add them all back
    int version = Short.MAX_VALUE + 1; // This puts it out of
                                       // the normal range of
                                       // editing
    List<DBObject> insertObjList = new ArrayList<DBObject>(propObjs.size());
    for (Object obj : propObjs) {
        DBObject propObj = (DBObject) obj;
        String propName = (String) propObj.get(FIELD_NAME);
        String propOrigin = (String) propObj.get(FIELD_ORIGIN);
        Object defValue = propObj.get(FIELD_DEFAULT);
        Object value = propObj.get(FIELD_VALUE);
        if (value == null) {
            // We override ALL values
            value = defValue;
        }

        DBObject insertObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId)
                .add(FIELD_NAME, propName).add(FIELD_VERSION, Integer.valueOf(version))
                .add(FIELD_ORIGIN, propOrigin).add(FIELD_VALUE, value).get();
        insertObjList.add(insertObj);
    }

    // Do the bulk insert
    try {
        testProps.insert(insertObjList, WriteConcern.SAFE);
    } catch (MongoException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("Lost test run property overrides: \n" + "   Error: " + e.getMessage() + "\n" + "   Props:");
        for (Object propObj : propObjs) {
            sb.append("\n").append("      ").append(propObj);
        }
        String msg = sb.toString();
        logger.error(msg);
        throw new RuntimeException(msg, e);
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Successfully fixed property overrides for run: " + runObjId);
    }
}

From source file:org.alfresco.bm.user.UserDataServiceImpl.java

License:Open Source License

/**
 * {@inheritDoc}//from   www  .  j a v  a  2s .  co m
 */
@Override
public void createNewUser(UserData data) {
    BasicDBObjectBuilder insertObjBuilder = BasicDBObjectBuilder.start()
            .add(FIELD_RANDOMIZER, data.getRandomizer()).add(FIELD_USERNAME, data.getUsername())
            .add(FIELD_PASSWORD, data.getPassword())
            .add(FIELD_CREATION_STATE, data.getCreationState().toString())
            .add(FIELD_FIRST_NAME, data.getFirstName()).add(FIELD_LAST_NAME, data.getLastName())
            .add(FIELD_EMAIL, data.getEmail()).add(FIELD_DOMAIN, data.getDomain())
            .add(FIELD_GROUPS, data.getGroups());
    DBObject insertObj = insertObjBuilder.get();

    try {
        collection.insert(insertObj);
    } catch (DuplicateKeyException e) {
        // We just rethrow as per the API
        throw e;
    }
}

From source file:org.alfresco.bm.user.UserDataServiceImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww  w. j  av a  2s  .  c o  m*/
 */
@Override
public void setUserPassword(String username, String password) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_USERNAME, username).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set").add(FIELD_PASSWORD, password).pop().get();
    WriteResult result = collection.update(queryObj, updateObj);
    if (result.getN() != 1) {
        throw new RuntimeException("Failed to update user ticket: \n" + "   Username: " + username + "\n"
                + "   Password: " + password + "\n" + "   Result:   " + result);
    }
}

From source file:org.alfresco.bm.user.UserDataServiceImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*from w  w w  .  ja v a2  s .c  o m*/
 */
@Override
public void setUserCreationState(String username, DataCreationState creationState) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_USERNAME, username).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set")
            .add(FIELD_CREATION_STATE, creationState.toString()).pop().get();
    WriteResult result = collection.update(queryObj, updateObj);
    if (result.getN() != 1) {
        throw new RuntimeException("Failed to update user creation state: \n" + "   Username:       " + username
                + "\n" + "   Creation State: " + creationState + "\n" + "   Result:         " + result);
    }
}

From source file:org.alfresco.bm.user.UserDataServiceImpl.java

License:Open Source License

@Override
public long countUsers(String domain, DataCreationState creationState) {
    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    if (domain != null) {
        queryObjBuilder.add(FIELD_DOMAIN, domain);
    }/*from   ww  w.  j  av a2  s  . com*/
    if (creationState != null) {
        queryObjBuilder.add(FIELD_CREATION_STATE, creationState.toString());
    }
    DBObject queryObj = queryObjBuilder.get();

    return collection.count(queryObj);
}