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
/** * Delete an existing test run/*w w w .j a v a 2s . c o m*/ * * @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
/** * Get all test-specific overrides for properties * /* w w w . j a v a2 s . c o m*/ * @param testObjId * the ID of the test * @param runObjId * the ID of the test run or <tt>null</tt> to find generic test * properties * @return all properties for the test or test run */ private DBCursor getTestPropertiesRaw(ObjectId testObjId, ObjectId runObjId) { DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).and(FIELD_RUN).is(runObjId).get(); return testProps.find(queryObj); }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Get all test-specific overrides for properties. Note that this does not * include any/*from w w w. j a v a2s . c o m*/ * inherited fields from the property definitions. * * @param testObjId * the ID of the test * @param runObjId * the ID of the test run or <tt>null</tt> to find generic test * properties * @param propertyName * the name of the property (never <tt>null</tt>) * @return the property for the test run as a cursor */ private DBCursor getTestPropertyRaw(ObjectId testObjId, ObjectId runObjId, String propertyName) { DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).and(FIELD_RUN).is(runObjId) .and(FIELD_NAME).is(propertyName).get(); return testProps.find(queryObj); }
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 a2s. c om * 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
/** * Fetch masked property names (passwords) by test name. * /*from w ww. jav a 2s . c o m*/ * @param testName * (String, mandatory) test name * * @return (Set<String>) or exception * * @throws ObjectNotFoundException * @since 2.1.2 */ public Set<String> getMaskedProperyNames(String testName) throws ObjectNotFoundException { ArgumentCheck.checkMandatoryString(testName, "testName"); DBObject queryObj = QueryBuilder.start().and(FIELD_NAME).is(testName).get(); BasicDBObjectBuilder fieldsObjBuilder = BasicDBObjectBuilder.start(FIELD_RELEASE, true).add(FIELD_SCHEMA, true); DBObject testObj = tests.findOne(queryObj, fieldsObjBuilder.get()); ObjectNotFoundException.checkObject(testObj, testName); return getMaskedProperyNames((String) testObj.get(FIELD_RELEASE), (Integer) testObj.get(FIELD_SCHEMA)); }
From source file:org.apache.rya.indexing.geotemporal.mongo.GeoTemporalMongoDBStorageStrategy.java
License:Apache License
public DBObject getFilterQuery(final Collection<IndexingExpr> geoFilters, final Collection<IndexingExpr> temporalFilters) throws GeoTemporalIndexException { final QueryBuilder builder = QueryBuilder.start(); if (!geoFilters.isEmpty()) { final DBObject[] geo = getGeoObjs(geoFilters); if (!temporalFilters.isEmpty()) { final DBObject[] temporal = getTemporalObjs(temporalFilters); builder.and(oneOrAnd(geo), oneOrAnd(temporal)); return builder.get(); } else {//from w ww. jav a 2s. c o m return oneOrAnd(geo); } } else if (!temporalFilters.isEmpty()) { final DBObject[] temporal = getTemporalObjs(temporalFilters); return oneOrAnd(temporal); } else { return builder.get(); } }
From source file:org.apache.rya.indexing.geotemporal.mongo.GeoTemporalMongoDBStorageStrategy.java
License:Apache License
private DBObject oneOrAnd(final DBObject[] dbos) { if (dbos.length == 1) { return dbos[0]; }/*from w w w .ja v a2 s . co m*/ return QueryBuilder.start().and(dbos).get(); }
From source file:org.apache.rya.indexing.mongodb.AbstractMongoIndexer.java
License:Apache License
protected CloseableIteration<Statement, QueryEvaluationException> withConstraints( final StatementConstraints constraints, final DBObject preConstraints) { final DBObject dbo = QueryBuilder.start().and(preConstraints).and(storageStrategy.getQuery(constraints)) .get();/*from w w w . j ava 2 s . co m*/ return closableIterationFromCursor(dbo); }
From source file:org.apache.rya.indexing.mongodb.freetext.MongoFreeTextIndexer.java
License:Apache License
@Override public CloseableIteration<Statement, QueryEvaluationException> queryText(final String query, final StatementConstraints constraints) throws IOException { final QueryBuilder qb = QueryBuilder.start().text(query); return withConstraints(constraints, qb.get()); }
From source file:org.apache.rya.indexing.mongodb.IndexingMongoDBStorageStrategy.java
License:Apache License
public DBObject getQuery(final StatementConstraints contraints) { final QueryBuilder queryBuilder = QueryBuilder.start(); if (contraints.hasSubject()) { queryBuilder.and(new BasicDBObject(SUBJECT, contraints.getSubject().toString())); }// w w w.j a v a 2s .c o m if (contraints.hasPredicates()) { final Set<URI> predicates = contraints.getPredicates(); if (predicates.size() > 1) { for (final URI pred : predicates) { final DBObject currentPred = new BasicDBObject(PREDICATE, pred.toString()); queryBuilder.or(currentPred); } } else if (!predicates.isEmpty()) { queryBuilder.and(new BasicDBObject(PREDICATE, predicates.iterator().next().toString())); } } if (contraints.hasContext()) { queryBuilder.and(new BasicDBObject(CONTEXT, contraints.getContext().toString())); } return queryBuilder.get(); }