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

/**
 * Create a new test for the precise release and schema
 * /*w  w w .jav  a2 s . com*/
 * @param test
 *        a globally-unique name using
 *        {@link ConfigConstants#TEST_NAME_REGEX}
 * @param description
 *        any description
 * @param release
 *        the test definition software release
 * @param schema
 *        the schema number
 * @return <tt>true</tt> if the test was written other <tt>false</tt> if not
 */
public boolean createTest(String test, String description, String release, Integer schema) {
    if (test == null || test.length() == 0) {
        throw new IllegalArgumentException("Name length must be non-zero");
    } else if (release == null || schema == null) {
        throw new IllegalArgumentException("A release and schema number must be supplied for a test.");
    }
    Pattern pattern = Pattern.compile(TEST_NAME_REGEX);
    Matcher matcher = pattern.matcher(test);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("The test name '" + test + "' is invalid.  "
                + "Test names must start with a character and contain only characters, numbers or underscore.");
    }

    // There are no properties to start with
    DBObject writeObj = BasicDBObjectBuilder.start().add(FIELD_NAME, test)
            .add(FIELD_VERSION, Integer.valueOf(0)).add(FIELD_DESCRIPTION, description)
            .add(FIELD_RELEASE, release).add(FIELD_SCHEMA, schema).get();

    try {
        WriteResult result = tests.insert(writeObj);
        if (logger.isDebugEnabled()) {
            logger.debug("Created test: " + result + "\n" + "   Name:    " + test + "\n" + "   Descr:   "
                    + description + "\n" + "   Release: " + release + "\n" + "   Schema:  " + schema);
        }
        return true;
    } catch (DuplicateKeyException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Test exists: " + test + ".");
        }
        return false;
    }
}

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

License:Open Source License

/**
 * Update an existing test to use new test details
 * //from   w w w . jav a2  s  .  c  o m
 * @param name
 *        the name of the test (must exist)
 * @param version
 *        the version of the test for concurrency checking
 * @param newName
 *        the new test name
 * @param newDescription
 *        the new description or <tt>null</tt> ot leave it
 * @param newRelease
 *        the new software release or <tt>null</tt> to leave it
 * @param newSchema
 *        the new schema number or <tt>null</tt> to leave it
 * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if
 *         not
 */
public boolean updateTest(String name, int version, String newName, String newDescription, String newRelease,
        Integer newSchema) {
    if (name == null) {
        throw new IllegalArgumentException("Updated requires a name and version.");
    }

    // Find the test by name and version
    DBObject queryObj = QueryBuilder.start().and(FIELD_NAME).is(name).and(FIELD_VERSION).is(version).get();

    // Handle version wrap-around
    Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1;
    // Gather all the setters required
    BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start().add(FIELD_VERSION, newVersion);
    if (newName != null) {
        Pattern pattern = Pattern.compile(TEST_NAME_REGEX);
        Matcher matcher = pattern.matcher(newName);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("The test name '" + newName + "' is invalid.  "
                    + "Test names must start with a character and contain only characters, numbers or underscore.");
        }

        setObjBuilder.add(FIELD_NAME, newName);
    }
    if (newDescription != null) {
        setObjBuilder.add(FIELD_DESCRIPTION, newDescription);
    }
    if (newRelease != null) {
        setObjBuilder.add(FIELD_RELEASE, newRelease);
    }
    if (newSchema != null) {
        setObjBuilder.add(FIELD_SCHEMA, newSchema);
    }
    DBObject setObj = setObjBuilder.get();

    // Now push the values to set into the update
    DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get();

    WriteResult result = tests.update(queryObj, updateObj);
    boolean written = (result.getN() > 0);

    // Done
    if (logger.isDebugEnabled()) {
        if (written) {
            logger.debug("Updated test: \n" + "   Test:      " + name + "\n" + "   Update:    " + updateObj);
        } else {
            logger.debug("Did not update test: " + name);
        }
    }
    return written;
}

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

License:Open Source License

/**
 * Delete an existing test/* ww w  .ja  v a  2  s  .c o m*/
 * 
 * @param test
 *        the name of the test (must exist)
 * @return <tt>true</tt> if the test was deleted or <tt>false</tt> if not
 */
public boolean deleteTest(String test) {
    DBObject testObj = getTest(test, false);
    if (testObj == null) {
        // The test no longer exists, so the run effectively doesn't either
        logger.warn("Test not found: " + test);
        return false;
    }
    ObjectId testObjId = (ObjectId) testObj.get(FIELD_ID);

    // Find the test by name and version
    DBObject testDelObj = QueryBuilder.start().and(FIELD_ID).is(testObjId).get();

    WriteResult result = tests.remove(testDelObj);
    boolean written = (result.getN() > 0);

    // Clean up test-related runs
    DBObject runDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).get();
    testRuns.remove(runDelObj);

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

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

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

License:Open Source License

/**
 * Get the test run names associated with a given test
 * //  www.  jav a  2s.  com
 * @param test
 *        the name of the test
 * @return the names of all test runs associated with the given test
 */
public List<String> getTestRunNames(String test) {
    DBObject testObj = getTest(test, false);
    if (testObj == null) {
        // The test no longer exists, so the run effectively doesn't either
        logger.warn("Test not found: " + test);
        return Collections.emptyList();
    }
    ObjectId testObjId = (ObjectId) testObj.get(FIELD_ID);

    DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).get();
    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_ID, true).add(FIELD_NAME, true).get();
    DBCursor cursor = testRuns.find(queryObj, fieldsObj);
    List<String> testRunNames = new ArrayList<String>(cursor.count());
    try {
        while (cursor.hasNext()) {
            DBObject testRunObj = cursor.next();
            String testRunName = (String) testRunObj.get(FIELD_NAME);
            testRunNames.add(testRunName);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Found and returned " + testRunNames.size() + " test run names for test '" + test + "'");
    }
    return testRunNames;
}

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

License:Open Source License

/**
 * @param test/*  w w  w. ja va  2 s  . c om*/
 *        only fetch runs for this test or <tt>null</tt> to get all test
 *        runs
 * @param testRunStates
 *        optional states that the test runs must be in or empty for all
 * @return a cursor onto the test runs for the given test
 */
public DBCursor getTestRuns(String test, int skip, int count, TestRunState... testRunStates) {
    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    if (test != null) {
        ObjectId testObjId = getTestId(test);
        if (testObjId == null) {
            // The test no longer exists, so the run effectively doesn't
            // either
            logger.warn("Test not found: " + test);
            // Use a ficticious ID that will never match
            testObjId = new ObjectId();
        }
        queryObjBuilder.add(FIELD_TEST, testObjId);
    }

    // Build query for the test run states
    if (testRunStates.length > 0) {
        List<String> stateStrs = new ArrayList<String>(testRunStates.length);
        for (int i = 0; i < testRunStates.length; i++) {
            stateStrs.add(testRunStates[i].toString());
        }
        queryObjBuilder.push(FIELD_STATE);
        queryObjBuilder.add("$in", stateStrs);
    }

    DBObject queryObj = queryObjBuilder.get();

    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_NAME, true).add(FIELD_TEST, true)
            .add(FIELD_VERSION, true).add(FIELD_DESCRIPTION, true).add(FIELD_STATE, true)
            .add(FIELD_SCHEDULED, true).add(FIELD_STARTED, true).add(FIELD_STOPPED, true)
            .add(FIELD_COMPLETED, true).add(FIELD_DURATION, true).add(FIELD_PROGRESS, true)
            .add(FIELD_RESULTS_SUCCESS, true).add(FIELD_RESULTS_FAIL, true).add(FIELD_RESULTS_TOTAL, true)
            .add(FIELD_SUCCESS_RATE, true).get();

    DBCursor dbCursor = testRuns.find(queryObj, fieldsObj).skip(skip).limit(count);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "Fetched test runs: \n" + "   Test:    " + test + "\n" + "   Results: " + dbCursor.count());
    }
    return dbCursor;
}

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

License:Open Source License

/**
 * Fetch the low-level ID for a test run
 *//*from  w ww . j a  v  a 2  s  . co m*/
private ObjectId getTestRunId(ObjectId testObjId, String run) {
    DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).and(FIELD_NAME).is(run).get();
    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_ID, true).get();
    DBObject runObj = testRuns.findOne(queryObj, fieldsObj);
    ObjectId runObjId = null;
    if (runObj != null) {
        runObjId = (ObjectId) runObj.get(FIELD_ID);
    }
    // Done
    if (logger.isTraceEnabled()) {
        logger.trace("Fetched test run ID: \n" + "   Test ID: " + testObjId + "\n" + "   Run:     " + run + "\n"
                + "   Result:  " + runObjId);
    }
    return runObjId;
}

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

License:Open Source License

/**
 * Retrieve the data for given test run/*from  ww w.  j  a va 2 s.  co m*/
 * 
 * @param runObjId
 *        (ObjectId, mandatory) the ID of the test run
 * 
 * @param includeProperties
 *        <tt>true</tt> to flesh out all the properties
 * 
 * @return the test object
 */
public DBObject getTestRun(ObjectId runObjId, boolean includeProperties) throws ObjectNotFoundException {
    ArgumentCheck.checkMandatoryObject(runObjId, "runObjId");

    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runObjId).get();

    BasicDBObjectBuilder fieldsObjBuilder = BasicDBObjectBuilder.start().add(FIELD_NAME, true)
            .add(FIELD_TEST, true).add(FIELD_VERSION, true).add(FIELD_DESCRIPTION, true).add(FIELD_STATE, true)
            .add(FIELD_SCHEDULED, true).add(FIELD_STARTED, true).add(FIELD_STOPPED, true)
            .add(FIELD_COMPLETED, true).add(FIELD_DURATION, true).add(FIELD_PROGRESS, true)
            .add(FIELD_RESULTS_SUCCESS, true).add(FIELD_RESULTS_FAIL, true).add(FIELD_RESULTS_TOTAL, true)
            .add(FIELD_SUCCESS_RATE, true).add(FIELD_DRIVERS, true);
    DBObject fieldsObj = fieldsObjBuilder.get();

    DBObject runObj = testRuns.findOne(queryObj, fieldsObj);
    if (runObj == null) {
        // The test run no longer exists
        throw new ObjectNotFoundException("Test run");
    }

    if (includeProperties) {
        ObjectId testObjId = (ObjectId) runObj.get(FIELD_TEST);
        String testName = runObj.get(FIELD_TEST).toString();
        String runName = runObj.get(FIELD_NAME).toString();

        BasicDBList propsList = getTestRunProperties(testObjId, runObjId, testName, runName);
        runObj.put(FIELD_PROPERTIES, propsList);
    }

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

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

License:Open Source License

/**
 * Create a new test run//from w  w w  .  ja va2  s  .  c om
 * 
 * @param test
 *        the name of the test to which the run belongs
 * @param run
 *        a test-unique name using {@link ConfigConstants#RUN_NAME_REGEX}
 * @param description
 *        any description
 * @return <tt>true</tt> if the test run was written other <tt>false</tt> if
 *         not
 */
public boolean createTestRun(String test, String run, String description) {
    if (run == null || run.length() == 0) {
        throw new IllegalArgumentException("Name length must be non-zero");
    }
    Pattern pattern = Pattern.compile(RUN_NAME_REGEX);
    Matcher matcher = pattern.matcher(run);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("The test run name '" + run + "' is invalid.  "
                + "Test run names may contain only characters, numbers or underscore.");
    }

    DBObject testObj = getTest(test, false);
    if (testObj == null) {
        // The test no longer exists, so the run effectively doesn't either
        logger.warn("Test not found: " + test);
        return false;
    }
    ObjectId testObjId = (ObjectId) testObj.get(FIELD_ID);

    // There are no properties to start with
    DBObject writeObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_NAME, run)
            .add(FIELD_VERSION, Integer.valueOf(0)).add(FIELD_DESCRIPTION, description)
            .add(FIELD_STATE, TestRunState.NOT_SCHEDULED.toString()).add(FIELD_SCHEDULED, Long.valueOf(-1L))
            .add(FIELD_STARTED, Long.valueOf(-1L)).add(FIELD_STOPPED, Long.valueOf(-1L))
            .add(FIELD_COMPLETED, Long.valueOf(-1L)).add(FIELD_DURATION, Long.valueOf(0L))
            .add(FIELD_PROGRESS, Double.valueOf(0.0D)).add(FIELD_RESULTS_SUCCESS, Long.valueOf(0L))
            .add(FIELD_RESULTS_FAIL, Long.valueOf(0L)).add(FIELD_RESULTS_TOTAL, Long.valueOf(0L))
            .add(FIELD_SUCCESS_RATE, Double.valueOf(1.0)).add(FIELD_DRIVERS, new BasicDBList()) // Ensure we
            // have an
            // empty
            // list to
            // start
            .get();

    try {
        WriteResult result = testRuns.insert(writeObj);
        if (logger.isDebugEnabled()) {
            logger.debug("Created test run: " + result + "\n" + "   Test:    " + test + "\n" + "   Name:    "
                    + run + "\n" + "   Descr:   " + description);
        }
        return true;
    } catch (DuplicateKeyException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Test run exists: " + test + ". " + run);
        }
        return false;
    }
}

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

License:Open Source License

/**
 * Update an existing test run with new details
 *
 * @param test//from ww  w. j  av a  2s .  c  o  m
 *        the name of the test
 * @param run
 *        the name of the test run (must exist)
 * @param version
 *        the version of the test for concurrency checking
 * @param newName
 *        the new name of the test run
 * @param newDescription
 *        the new description or <tt>null</tt> ot leave it
 * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if
 *         not
 */
public boolean updateTestRun(String test, String run, int version, String newName, String newDescription) {
    if (test == null || run == null) {
        throw new IllegalArgumentException("Updated requires a name and version.");
    }

    // Get the test
    DBObject testObj = getTest(test, false);
    if (testObj == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to update test run; test not found: " + test);
        }
        return false;
    }

    // Find the test run by name and version
    DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObj.get(FIELD_ID)).and(FIELD_NAME).is(run)
            .and(FIELD_VERSION).is(version).get();

    // Handle version wrap-around
    Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1;
    // Gather all the setters required
    BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start().add(FIELD_VERSION, newVersion);
    if (newName != null) {
        Pattern pattern = Pattern.compile(RUN_NAME_REGEX);
        Matcher matcher = pattern.matcher(newName);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("The test run name '" + newName + "' is invalid.  "
                    + "Test run names may only contain characters, numbers or underscore.");
        }
        setObjBuilder.add(FIELD_NAME, newName);
    }
    if (newDescription != null) {
        setObjBuilder.add(FIELD_DESCRIPTION, newDescription);
    }
    DBObject setObj = setObjBuilder.get();

    // Now push the values to set into the update
    DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get();

    WriteResult result = testRuns.update(queryObj, updateObj);
    boolean written = (result.getN() > 0);

    // Done
    if (logger.isDebugEnabled()) {
        if (written) {
            logger.debug("Updated test run: \n" + "   Test:      " + test + "\n" + "   Run:       " + run + "\n"
                    + "   Update:    " + updateObj);
        } else {
            logger.debug("Did not update test run: " + test + "." + run);
        }
    }
    return written;
}

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

License:Open Source License

/**
 * Update the run state of a test run./*w  w  w  .  j a  va2 s .  c  om*/
 * <p/>
 * The test run {@link TestConstants#FIELD_STATE state} will be set based on
 * the values.
 * <p/>
 * Note that a test run can either be stopped or completed but not both. In
 * both cases,
 * though, the test run must have been scheduled and then started.
 *
 * @param test
 *        the name of the test
 * @param run
 *        the name of the test run (must exist)
 * @param version
 *        the version of the test for concurrency checking
 * @param testRunState
 *        the test run state to set (<null> to ignore)
 * @param scheduled
 *        the time when the test run is scheduled to start (<null> to
 *        ignore)
 * @param started
 *        the time when the test run started (<null> to ignore)
 * @param stopped
 *        the time when the test run was stopped (<null> to ignore)
 * @param completed
 *        the time when the test run was completed (<null> to ignore)
 * @param duration
 *        the time the test has been running for in ms (<null> to ignore)
 * @param progress
 *        the new progress for the test run (<null> to ignore)
 * @param resultsSuccess
 *        the number of successful results (<null> to ignore)
 * @param resultsFailure
 *        the number of failed results (<null> to ignore)
 * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if
 *         not
 */
public boolean updateTestRunState(ObjectId runId, int version, TestRunState testRunState, Long scheduled,
        Long started, Long stopped, Long completed, Long duration, Double progress, Long resultsSuccess,
        Long resultsFail) {
    // Find the test run by name and version
    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runId).and(FIELD_VERSION).is(version).get();

    // Gather all the setters required
    BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start();
    if (testRunState != null) {
        setObjBuilder.add(FIELD_STATE, testRunState.toString());
    }
    if (scheduled != null) {
        setObjBuilder.add(FIELD_SCHEDULED, scheduled);
    }
    if (started != null) {
        setObjBuilder.add(FIELD_STARTED, started);
    }
    if (stopped != null) {
        setObjBuilder.add(FIELD_STOPPED, stopped);
    }
    if (completed != null) {
        setObjBuilder.add(FIELD_COMPLETED, completed);
    }
    if (duration != null) {
        setObjBuilder.add(FIELD_DURATION, duration);
    }
    if (progress != null) {
        // Adjust accuracy of the progress
        long progressLong = Math.round(progress * 10000.0);
        if (progressLong < 0L || progressLong > 10000L) {
            throw new IllegalArgumentException("Progress must be expressed as a double in range [0.0, 1.0].");
        }
        progress = progressLong / 10000.0; // Accuracy

        setObjBuilder.add(FIELD_PROGRESS, progress);
    }
    if (resultsSuccess != null || resultsFail != null) {
        if (resultsSuccess == null || resultsFail == null) {
            throw new IllegalArgumentException("resultsSuccess and resultsFail must be updated together.");
        }
        long resultsTotal = Long.valueOf(resultsSuccess.longValue() + resultsFail.longValue());
        double successRate = (resultsTotal == 0) ? 1.0 : (resultsSuccess / (double) resultsTotal);
        setObjBuilder.add(FIELD_RESULTS_SUCCESS, resultsSuccess);
        setObjBuilder.add(FIELD_RESULTS_FAIL, resultsFail);
        setObjBuilder.add(FIELD_RESULTS_TOTAL, resultsTotal);
        setObjBuilder.add(FIELD_SUCCESS_RATE, successRate);
    }
    if (resultsFail != null) {
        setObjBuilder.add(FIELD_RESULTS_FAIL, resultsFail);
    }
    // Check that we are actually going to do something
    if (setObjBuilder.get().keySet().size() == 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("No updates provided for test run: " + runId);
        }
        return false;
    }

    // Handle version wrap-around
    Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1;
    setObjBuilder.add(FIELD_VERSION, newVersion);
    // Get the object containing the set values
    DBObject setObj = setObjBuilder.get();

    // Now push the values to set into the update
    DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get();

    WriteResult result = testRuns.update(queryObj, updateObj);
    boolean written = (result.getN() > 0);

    // Done
    if (logger.isDebugEnabled()) {
        if (written) {
            logger.debug("Updated test run state: \n" + "   Run ID:    " + runId + "\n" + "   Update:    "
                    + updateObj);
        } else {
            logger.debug("Did not update test run state: " + runId);
        }
    }
    return written;
}