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.event.mongo.MongoResultService.java

License:Open Source License

@Override
public EventRecord getLastResult() {
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(-1))
            .get();/*from w  ww .  ja v a  2 s .  c  om*/

    DBObject resultObj = collection.findOne(null, null, sortObj);
    EventRecord result = convertToEventRecord(resultObj);

    if (logger.isDebugEnabled()) {
        logger.debug("Found last result: " + result);
    }
    return result;
}

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

License:Open Source License

@Override
public List<EventRecord> getResults(String eventName, int skip, int limit) {
    DBObject queryObj = QueryBuilder.start().get();
    if (eventName != null) {
        queryObj.put(EventRecord.FIELD_EVENT_NAME, eventName);
    }//from www.j av  a2 s .co m
    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 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);
    }//from   w  w w  .ja v  a 2  s . c  o m
    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 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 a  v  a 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.file.FileDataServiceImpl.java

License:Open Source License

@Override
public void createNewFileData(FileData fileData) {
    DBObject fileDataObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileData.getFileset())
            .add(FIELD_REMOTE_NAME, fileData.getRemoteName()).add(FIELD_LOCAL_NAME, fileData.getLocalName())
            .add(FIELD_EXTENSION, fileData.getExtension()).add(FIELD_ENCODING, fileData.getEncoding())
            .add(FIELD_LOCALE, fileData.getLocale()).add(FIELD_SIZE, fileData.getSize())
            .add(FIELD_RANDOMIZER, fileData.getRandomizer()).get();
    WriteResult result = collection.insert(fileDataObj);

    if (logger.isDebugEnabled()) {
        logger.debug("Wrote FileData to collection: \n" + "   " + fileData + "\n" + "   Result: " + result);
    }// w  w  w  . j  a v a 2  s  .  c  o  m
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public long fileCount(String fileset) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset).get();
    return collection.count(queryObj);
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

/**
 * Count the number of files of the given type in a fileset
 *//*from w  w  w  . j av a  2  s.c om*/
private long fileCount(String fileset, String extension) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset).add(FIELD_EXTENSION, extension)
            .get();
    return collection.count(queryObj);
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public FileData findFile(String fileset, String remoteName) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset)
            .add(FIELD_REMOTE_NAME, remoteName).get();
    DBObject resultObj = collection.findOne(queryObj);
    if (resultObj == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Did not find file '" + remoteName + "' in " + fileset);
        }//from  w  w w .j  av a  2  s  . c  o  m
        return null;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Found file '" + remoteName + "' in " + fileset + ": " + resultObj);
        }
        return fromDBObject(resultObj);
    }
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public void removeFile(String fileset, String remoteName) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset)
            .add(FIELD_REMOTE_NAME, remoteName).get();
    WriteResult result = collection.remove(queryObj);

    if (logger.isDebugEnabled()) {
        logger.debug("Removed " + fileset + "." + remoteName + " and hit " + result.getN() + " rows");
    }/* w  w w .  j a v a  2 s  .c  o m*/
}

From source file:org.alfresco.bm.file.FileDataServiceImpl.java

License:Open Source License

@Override
public FileData getRandomFile(String fileset) {
    long count = fileCount(fileset);
    if (count == 0L) {
        // There is nothing to choose from
        return null;
    }// w  ww. j av a2 s  .co m
    // Use a random number from 0 (inclusive) to 'count' (exclusive)
    int skip = (int) (Math.random() * (double) count);

    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset).get();
    DBCursor results = collection.find(queryObj).skip(skip).limit(1);
    if (results.size() == 0) {
        // No results
        return null;
    } else {
        DBObject fileDataObj = results.next();
        return fromDBObject(fileDataObj);
    }
}