Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start() 

Source Link

Document

Returns a new QueryBuilder.

Usage

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventRecord> getResults(long startTime, long endTime, boolean chartOnly, int skip, int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start().and(EventRecord.FIELD_START_TIME)
            .greaterThanEquals(new Date(startTime)).and(EventRecord.FIELD_START_TIME)
            .lessThan(new Date(endTime));
    if (chartOnly) {
        queryBuilder.and(EventRecord.FIELD_CHART).is(true);
    }//w ww .j  a  v a  2s.com
    DBObject queryObj = queryBuilder.get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(1)).get();

    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventRecord> results = new ArrayList<EventRecord>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventRecord eventRecord = convertToEventRecord(obj);
            results.add(eventRecord);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "\n" + "Found results: \n" + "   Query:          " + queryObj + "\n" + "   Skip:           "
                        + skip + "\n" + "   Limit:          " + limit + "\n" + "   Results:        " + size);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public long countResultsByEventName(String name) {
    DBObject queryObj = QueryBuilder.start().and(EventRecord.FIELD_EVENT_NAME).is(name).get();

    long count = collection.count(queryObj);

    // Done/*from   w ww .jav  a  2s .  c  o m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Counted " + count + " results for event name: " + name);
    }
    return count;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public long countResultsBySuccess() {
    DBObject queryObj = QueryBuilder.start().and(EventRecord.FIELD_SUCCESS).is(true).get();

    long count = collection.count(queryObj);

    // Done/* w  w  w .jav a2s .c o  m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Counted " + count + " results for success: " + true);
    }
    return count;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public long countResultsByFailure() {
    DBObject queryObj = QueryBuilder.start().and(EventRecord.FIELD_SUCCESS).is(false).get();

    long count = collection.count(queryObj);

    // Done//from  w ww.  j a  v a 2s  .  c  om
    if (logger.isDebugEnabled()) {
        logger.debug("Counted " + count + " results for success: " + false);
    }
    return count;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventDetails> getEventDetails(EventResultFilter filter, String filterEventName, int skip,
        int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start();

    // apply filter
    switch (filter) {
    case Failed:/*from   w ww .j  ava  2 s  . c  o  m*/
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(false);
        break;

    case Success:
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(true);
        break;
    default:
        break;
    }

    //apply event name filter
    if (null != filterEventName && !filterEventName.isEmpty()) {
        queryBuilder.and(EventRecord.FIELD_EVENT_NAME).is(filterEventName);
    }

    DBObject queryObj = queryBuilder.get();
    // sort descending to get the newest values first
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(-1))
            .get();
    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventDetails> results = new ArrayList<EventDetails>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventDetails eventDetails = convertToEventDetails(obj);
            results.add(eventDetails);
        }
    } finally {
        cursor.close();
    }

    return results;
}

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

License:Open Source License

/**
 * Refresh the expiry time of a driver/*from  w ww.  j a  va2s .co m*/
 * 
 * @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

/**
 * Get registered test drivers/*  w  ww .j  av a 2 s .  com*/
 * 
 * @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

/**
 * Count registered drivers// w  w w  .j  av  a 2s.  c om
 * 
 * @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
 * @return a count of the number of drivers matching the criteria
 */
public long countDrivers(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();

    long count = testDrivers.count(queryObj);

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

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/*from w w w  . j  a v  a 2 s .co 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/*w  w w. j  a va  2s . c o 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;
}