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.cm.FileFolderService.java

License:Open Source License

/**
 * Delete a folder entry based on the folder ID
 * // ww  w  .  j a  va  2 s .  c  o m
 * @param id                the folder ID
 */
public int deleteFolder(String context, String path, boolean cascade) {
    int deleted = 0;
    // Delete primary
    {
        DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path)
                .get();
        WriteResult wr = collection.remove(queryObj);
        deleted += wr.getN();
    }
    // Cascade
    if (cascade) {
        DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).push(FIELD_PATH)
                .add("$regex", "^" + path + "/").pop().get();
        WriteResult wr = collection.remove(queryObj);
        deleted += wr.getN();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Deleted folder: \n" + "   Context:        " + context + "\n" + "   Path:           "
                + path + "\n" + "   Cascade:        " + cascade + "\n" + "   Deleted:        " + deleted);
    }
    return deleted;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Retrieve a folder by the ID/*from w  w  w.  j  a  va  2 s.  c o  m*/
 * 
 * @param id                the folder id
 * @return                  the folder data or <tt>null</tt> if it does not exist
 */
public FolderData getFolder(String id) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, id).get();
    DBObject folderDataObj = collection.findOne(queryObj);
    FolderData folderData = fromDBObject(folderDataObj);
    return folderData;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Retrieve a folder by the path/*from  w  ww. ja  v a  2 s.  c  o m*/
 * 
 * @param context           the context in which the folder path is valid (mandatory)
 * @param path              the folder path relative to the given context
 * @return                  the folder data or <tt>null</tt> if it does not exist
 */
public FolderData getFolder(String context, String path) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path).get();
    DBObject folderDataObj = collection.findOne(queryObj);
    FolderData folderData = fromDBObject(folderDataObj);
    return folderData;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Increment the count of the subfolders in a folder.
 * /*from  w w w. j a  va  2 s  .c om*/
 * @param context           the context in which the folder path is valid (mandatory)
 * @param path              the folder path relative to the given context
 * @param fileCountInc      the file count increment (can be negative)
 */
public void incrementFolderCount(String context, String path, long folderCountInc) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$inc").add(FIELD_FOLDER_COUNT, folderCountInc).pop()
            .get();
    WriteResult result = collection.update(queryObj, updateObj);
    if (result.getN() != 1) {
        throw new RuntimeException("Failed to update folder's subfolder count: \n" + "   Context:  " + context
                + "\n" + "   Path:     " + path + "\n" + "   Result:   " + result);
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Incremented the subfolder count on " + context + "/" + path + " by " + folderCountInc);
    }
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Increment the count of the files in a folder.
 * /*from  ww  w .j av  a 2  s . com*/
 * @param context           the context in which the folder path is valid (mandatory)
 * @param path              the folder path relative to the given context
 * @param fileCountInc      the file count increment (can be negative)
 */
public void incrementFileCount(String context, String path, long fileCountInc) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$inc").add(FIELD_FILE_COUNT, fileCountInc).pop()
            .get();
    WriteResult result = collection.update(queryObj, updateObj);
    if (result.getN() != 1) {
        throw new RuntimeException("Failed to update folder's file count: \n" + "   Context:  " + context + "\n"
                + "   Path:     " + path + "\n" + "   Result:   " + result);
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Incremented the file count on " + context + "/" + path + " by " + fileCountInc);
    }
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Produces a count of the folders that have the given context and path as a <b>parent</b> folder.
 * <p/>//from  w  ww.j a  va  2s .c o m
 * If the following files exist:
 * <pre>
 *      /home/tests/a
 *      /home/tests/b
 * </pre>
 * then the counts for:
 * <pre>
 *      /home/tests
 * </pre>
 * will be <tt>2</tt>
 * 
 * @return      the folder count
 */
public long countChildFolders(String context, String path) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PARENT_PATH, path)
            .get();
    long count = collection.count(queryObj);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Count of children for " + context + path + ": " + count);
    }
    return count;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Count folders without either subfolders or files
 * //w ww.  j  av a 2  s  . com
 * @return                  count of folders without subfolders or files
 */
public long countEmptyFolders(String context) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_FOLDER_COUNT, 0L)
            .add(FIELD_FILE_COUNT, 0L).get();
    long count = collection.count(queryObj);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("There are " + count + " empty folders in context '" + context + "'.");
    }
    return count;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Get a list of folders that have the given context and path as a <b>parent</b> folder.
 * <p/>/* w  w w .j  a  va2 s .  c o  m*/
 * If the path given is:
 * <pre>
 *      /home/tests
 * </pre>
 * then the following results can be returned:
 * <pre>
 *      /home/tests/a
 *      /home/tests/b
 * </pre>
 * 
 * @param context           the context in which the folder path is valid (mandatory)
 * @param path              the path that will be the parent of all child folders returned
 * @param skip              the number of entries to skip
 * @param limit             the number of entries to return
 * @return                  the child folders
 */
public List<FolderData> getChildFolders(String context, String path, int skip, int limit) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PARENT_PATH, path)
            .get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, 1).add(FIELD_PARENT_PATH, 1).get();
    DBCursor cursor = collection.find(queryObj).sort(sortObj).skip(skip).limit(limit);
    List<FolderData> results = fromDBCursor(cursor);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Found " + results.size() + " results in folder " + context + path);
    }
    return results;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Get a list of folders filtered by the number of child files and/or folders, returning
 * results sorted according to the parameters supplied.
 * <p/>//from ww  w  .ja  v  a2s .  com
 * Apart from the context, all parametes are optional.  However, for best performance,
 * do not mix the file and folder levels; the underlying query performance will be OK
 * but the sorting will not be ideal.
 * <p/>
 * The sort precedence is <b>folderCount-fileCount</b>.
 * 
 * @param context           the context in which the folder path is valid (mandatory)
 * @param minLevel          the minimum folder level to consider (inclusive, optional)
 * @param maxLevel          the maximum folder level to consider (inclusive, optional)
 * @param minFiles          the minimum number of files in the folder (inclusive, optional)
 * @param maxFiles          the maximum number of files in the folder (inclusive, optional)
 * @param skip              the number of entries to skip
 * @param limit             the number of entries to return
 * @return                  the folders with the correct number of children
 */
public List<FolderData> getFoldersByCounts(String context, Long minLevel, Long maxLevel, Long minFolders,
        Long maxFolders, Long minFiles, Long maxFiles, int skip, int limit) {
    if (context == null) {
        throw new IllegalArgumentException();
    }

    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    BasicDBObjectBuilder sortObjBuilder = BasicDBObjectBuilder.start();

    queryObjBuilder.add(FIELD_CONTEXT, context);
    if (minLevel != null || maxLevel != null) {
        queryObjBuilder.push(FIELD_LEVEL);
        {
            if (minLevel != null) {
                queryObjBuilder.add("$gte", minLevel);
            }
            if (maxLevel != null) {
                queryObjBuilder.add("$lte", maxLevel);
            }
            // No sorting by level!
        }
        queryObjBuilder.pop();
    }
    if (minFolders != null || maxFolders != null) {
        queryObjBuilder.push(FIELD_FOLDER_COUNT);
        {
            if (minFolders != null) {
                queryObjBuilder.add("$gte", minFolders);
            }
            if (maxFolders != null) {
                queryObjBuilder.add("$lte", maxFolders);
            }
            // We have to sort by the counts
            sortObjBuilder.add(FIELD_FOLDER_COUNT, 1);
        }
        queryObjBuilder.pop();
    }
    if (minFiles != null || maxFiles != null) {
        queryObjBuilder.push(FIELD_FILE_COUNT);
        {
            if (minFiles != null) {
                queryObjBuilder.add("$gte", minFiles);
            }
            if (maxFiles != null) {
                queryObjBuilder.add("$lte", maxFiles);
            }
            // We have to sort by the counts
            sortObjBuilder.add(FIELD_FILE_COUNT, 1);
        }
        queryObjBuilder.pop();
    }
    DBObject queryObj = queryObjBuilder.get();
    DBObject sortObj = sortObjBuilder.get();

    DBCursor cursor = collection.find(queryObj).sort(sortObj).skip(skip).limit(limit);
    List<FolderData> results = fromDBCursor(cursor);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Found " + results.size() + " results for file counts: \n" + "   context:    " + context
                + "\n" + "   minLevel:   " + minLevel + "\n" + "   maxLevel:   " + maxLevel + "\n"
                + "   minFiles:   " + minFiles + "\n" + "   maxFiles:   " + maxFiles + "\n" + "   skip:       "
                + skip + "\n" + "   limit:      " + limit);
    }
    return results;
}

From source file:org.alfresco.bm.cmis.AbstractCMISEventProcessor.java

License:Open Source License

public final EventResult processEvent(Event event) throws Exception {
    try {/*from  w  w  w  . j  a  v a  2s .c om*/
        return processCMISEvent(event);
    } catch (CmisRuntimeException e) {
        String error = e.getMessage();
        String stack = ExceptionUtils.getStackTrace(e);
        // Grab the CMIS information
        DBObject data = BasicDBObjectBuilder.start().append("msg", error).append("stack", stack)
                .push("cmisFault").append("code", "" + e.getCode()) // BigInteger is not Serializable
                .append("errorContent", e.getErrorContent()).pop().get();

        // Build failure result
        return new EventResult(data, false);
    } catch (Exception genEx) {
        if (logger.isDebugEnabled()) {
            logger.debug("General exception in CMIS benchmark.", genEx);
        }
        throw genEx;
    }
}