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.session.MongoSessionService.java

License:Open Source License

@Override
public long getCompletedSessionsCount() {
    DBObject queryObj = BasicDBObjectBuilder.start().push(FIELD_END_TIME).append("$gt", 0).pop().get();
    return collection.count(queryObj);
}

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

License:Open Source License

/**
 * Register a test driver//from  w ww.  ja v a 2  s . com
 * 
 * @param release
 *        the software release version
 * @param schema
 *        the schema number
 * @param ipAddress
 *        the IP address of the machine the application is running on
 * @param contextPath
 *        the application context path (or similar) for information
 * @param capabilities
 *        the features supported by the driver
 * @return a unique registration key
 */
public String registerDriver(String release, Integer schema, String ipAddress, String hostname,
        String contextPath, Set<String> capabilities) {
    DBObject insertObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, release).add(FIELD_SCHEMA, schema)
            .add(FIELD_IP_ADDRESS, ipAddress).add(FIELD_HOSTNAME, hostname).add(FIELD_CONTEXT_PATH, contextPath)
            .add(FIELD_CAPABILITIES, BasicDBObjectBuilder.start().add(FIELD_SYSTEM, capabilities).get())
            .add(FIELD_PING, BasicDBObjectBuilder.start().add(FIELD_TIME, new Date())
                    .add(FIELD_EXPIRES, new Date(0L)).get())
            .get();
    testDrivers.insert(insertObj);
    // Get the object ID
    ObjectId objId = (ObjectId) insertObj.get(FIELD_ID);
    String id = objId == null ? null : objId.toString();

    // Done
    if (logger.isDebugEnabled()) {
        // Retrieve the object for debug
        logger.debug("Registered test driver: \n" + "   ID:  " + id + "\n" + "   New: " + insertObj);
    }
    return id;
}

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

License:Open Source License

/**
 * Refresh the expiry time of a driver/*  w  w w  . j a v  a2  s .c om*/
 * 
 * @param id
 *        the driver id
 * @param expiryTime
 *        the new expiry time
 */
public void refreshDriver(String id, long expiryTime) {
    DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(new ObjectId(id)).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set")
            .add(FIELD_PING + "." + FIELD_EXPIRES, new Date(expiryTime)).pop().get();
    testDrivers.findAndModify(queryObj, null, null, false, updateObj, false, false);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Updated test driver expiry: \n" + "   ID:  " + id + "\n" + "   New: " + expiryTime);
    }
}

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

License:Open Source License

/**
 * Unregister a test driver//  ww w.  j av a2s .c o  m
 * 
 * @param id
 *        the ID of the registration
 */
public void unregisterDriver(String id) {
    // Find the driver by ID
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, new ObjectId(id)).get();
    testDrivers.remove(queryObj);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Unregistered test driver: " + id);
    }
}

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

License:Open Source License

/**
 * Get registered test drivers/*from  ww  w  . j  ava 2s  . c  o m*/
 * 
 * @param release
 *        the release name of the test or <tt>null</tt> for all releases
 * @param schema
 *        the schema number of the driver or <tt>null</tt> for all schemas
 * @param liveOnly
 *        <tt>true</tt> to retrieve only live instances
 */
public DBCursor getDrivers(String release, Integer schema, boolean active) {
    QueryBuilder queryBuilder = QueryBuilder.start();
    if (release != null) {
        queryBuilder.and(FIELD_RELEASE).is(release);
    }
    if (schema != null) {
        queryBuilder.and(FIELD_SCHEMA).is(schema);
    }
    if (active) {
        queryBuilder.and(FIELD_PING + "." + FIELD_EXPIRES).greaterThan(new Date());
    }
    DBObject queryObj = queryBuilder.get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, 1).add(FIELD_SCHEMA, 1).get();

    DBCursor cursor = testDrivers.find(queryObj).sort(sortObj);

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieved test driver: \n" + "   Release: " + release + "\n" + "   Schema:  " + schema
                + "\n" + "   active:  " + active + "\n" + "   Results: " + cursor.count());
    }
    return cursor;
}

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

License:Open Source License

/**
 * Write all the test application's property definitions against the release
 * and schema number//  w  w w  .  j  av  a 2 s  .  c o m
 * 
 * @param release
 *        the test release name
 * @param schema
 *        the property schema
 * @param description
 *        a description of the test definition
 * @param testProperties
 *        the property definitions
 * @return <tt>true</tt> if the properties were written or <tt>false</tt>
 *         if they already existed
 */
public boolean writeTestDef(String release, Integer schema, String description,
        List<TestProperty> testProperties) {
    // Check the schema number for any existing instance
    DBObject queryObjExistingSchema = QueryBuilder.start().and(FIELD_RELEASE).is(release).and(FIELD_SCHEMA)
            .greaterThanEquals(schema).get();
    DBObject fieldsObjExistingSchema = BasicDBObjectBuilder.start().add(FIELD_SCHEMA, true).get();
    DBObject resultsObjExistingSchema = testDefs.findOne(queryObjExistingSchema, fieldsObjExistingSchema);
    Integer existingSchema = (resultsObjExistingSchema == null) ? null
            : (Integer) resultsObjExistingSchema.get(FIELD_SCHEMA);

    if (existingSchema == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No test definition exists for " + release + ":" + schema);
        }
        // Fall through to write the test definition
    } else if (existingSchema.equals(schema)) {
        // We have an exact match. Don't do anything.
        if (logger.isDebugEnabled()) {
            logger.debug("Test definition exists for " + release + ":" + schema);
        }
        return false;
    } else {
        // The query found an instance with a larger schema number. We
        // don't run downgrades on the same release.
        throw new RuntimeException(
                "The current test is out of date and needs to be upgraded to a later version or schema "
                        + release + ":" + schema);
    }

    // Pattern for valid property names
    Pattern pattern = Pattern.compile(PROP_NAME_REGEX);

    // Build a DB-safe map for direct persistence
    Collection<Properties> testPropertiesForDb = new ArrayList<Properties>(testProperties.size());
    for (TestProperty testProperty : testProperties) {
        // Do not write properties with invalid names
        Matcher matcher = pattern.matcher(testProperty.getName());
        if (!matcher.matches()) {
            logger.warn("Property will be ignored.  The name is non-standard: " + matcher);
            continue;
        }

        // Convert the Java object to Java Properties
        Properties propValues = testProperty.toProperties();

        // That's it. Add it the properties.
        testPropertiesForDb.add(propValues);
    }

    // Attempt an insert
    DBObject newObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, release).add(FIELD_SCHEMA, schema)
            .add(FIELD_DESCRIPTION, description).add(FIELD_PROPERTIES, testPropertiesForDb).get();

    try {
        WriteResult result = testDefs.insert(newObj);
        if (logger.isDebugEnabled()) {
            logger.debug("Created test definition: " + result + "\n" + "   Release: " + release + "\n"
                    + "   Schema:  " + schema + "\n" + "   New:     " + newObj);
        }
        return true;
    } catch (DuplicateKeyException e) {
        // Already present
        if (logger.isDebugEnabled()) {
            logger.debug("Test definition exists for " + release + ":" + schema);
        }
        return false;
    }
}

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

License:Open Source License

/**
 * @param count//from   w w  w .ja  v a 2 s .  co m
 *        the number of results to retrieve (must be greater than zero)
 * @return a list of tests, active or all
 */
public DBCursor getTestDefs(boolean active, int skip, int count) {
    if (count < 1) {
        throw new IllegalArgumentException("'count' must be larger than zero.");
    }

    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, true).add(FIELD_SCHEMA, true).get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, 1).add(FIELD_SCHEMA, 1).get();

    DBCursor cursor;
    if (active) {
        DBObject queryObj = QueryBuilder.start().put(FIELD_PING + "." + FIELD_EXPIRES)
                .greaterThanEquals(new Date()).get();
        cursor = testDrivers.find(queryObj, fieldsObj).sort(sortObj).skip(skip).limit(count);
    } else {
        cursor = testDefs.find(null, fieldsObj).sort(sortObj).skip(skip).limit(count);
    }

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Fetching test definitions: \n" + "   active:  " + active + "\n" + "   skip:    " + skip
                + "\n" + "   count:   " + count + "\n" + "   Results: " + cursor.count());
    }
    return cursor;
}

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

License:Open Source License

/**
 * Get the test definition for internal use
 * /* www  . j a va  2  s  .c  o  m*/
 * @return the test definition (untouched) or <tt>null</tt>
 */
private DBObject getTestDefRaw(String release, Integer schema) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, release).add(FIELD_SCHEMA, schema)
            .get();

    DBObject testDefObj = testDefs.findOne(queryObj);
    // Done
    return testDefObj;
}

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

License:Open Source License

/**
 * Get a list of all defined tests//  w w  w  .j av a2s.  co  m
 * 
 * @param release
 *        the test definition software release or <tt>null</tt> for all test
 *        releases
 * @param schema
 *        the schema number or <tt>null</tt> for all schemas
 * @return all the currently-defined tests
 */
public DBCursor getTests(String release, Integer schema, int skip, int count) {
    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    if (release != null && release.length() > 0) {
        queryObjBuilder.add(FIELD_RELEASE, release);
    }
    if (schema != null) {
        queryObjBuilder.add(FIELD_SCHEMA, schema);
    }
    DBObject queryObj = queryObjBuilder.get();

    // We don't want everything just now
    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_NAME, true).add(FIELD_VERSION, true)
            .add(FIELD_DESCRIPTION, true).add(FIELD_RELEASE, true).add(FIELD_SCHEMA, true).get();

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

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Fetched tests: \n" + "   Release: " + release + "\n" + "   Schema:  " + schema + "\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  w  w  .j a  v  a2 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;
}