List of usage examples for com.mongodb QueryBuilder start
public static QueryBuilder start()
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Fetch the low-level ID for a test run * //from ww w.ja v a 2 s . co m * @return the test ID or <tt>null</tt> if not found */ private ObjectId getTestId(String test) { DBObject queryObj = QueryBuilder.start().and(FIELD_NAME).is(test).get(); DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_ID, true).get(); DBObject testObj = tests.findOne(queryObj, fieldsObj); ObjectId testObjId = null; if (testObj != null) { testObjId = (ObjectId) testObj.get(FIELD_ID); } // Done if (logger.isTraceEnabled()) { logger.trace("Fetched test ID: \n" + " Test: " + test + "\n" + " Result: " + testObjId); } return testObjId; }
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 ww . j a va 2 s .c om * @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//from w w w. j a va2 s . com * * @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 * /*from w ww . j a v a 2 s . c o m*/ * @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
/** * Fetch the low-level ID for a test run */// w w w .j a v a 2 s .c o 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 w ww. ja va 2 s .c o 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
/** * Update an existing test run with new details * * @param test// w w w . j a v a 2 s .co 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.//from w w w . j a va 2s. 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; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Register a driver with a test run/*from w w w. j a va 2 s . com*/ * * @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//from w ww .j a v a 2 s . 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)); } }