Example usage for org.springframework.data.mongodb.core CollectionCallback doInCollection

List of usage examples for org.springframework.data.mongodb.core CollectionCallback doInCollection

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core CollectionCallback doInCollection.

Prototype

@Nullable
T doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException;

Source Link

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T execute(String collectionName, CollectionCallback<T> callback) {

    Assert.notNull(callback);//w w  w.j a v  a  2  s.  c  o  m

    try {
        DBCollection collection = getAndPrepareCollection(getDb(), collectionName);
        return callback.doInCollection(collection);
    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Internal method using callbacks to do queries against the datastore that requires reading a single object from a
 * collection of objects. It will take the following steps
 * <ol>/*from w  ww  . j  a v  a 2 s.c o m*/
 * <li>Execute the given {@link ConnectionCallback} for a {@link DBObject}.</li>
 * <li>Apply the given {@link DbObjectCallback} to each of the {@link DBObject}s to obtain the result.</li>
 * <ol>
 * 
 * @param <T>
 * @param collectionCallback the callback to retrieve the {@link DBObject} with
 * @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type
 * @param collectionName the collection to be queried
 * @return
 */
private <T> T executeFindOneInternal(CollectionCallback<DBObject> collectionCallback,
        DbObjectCallback<T> objectCallback, String collectionName) {

    try {
        T result = objectCallback
                .doWith(collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName)));
        return result;
    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Internal method using callback to do queries against the datastore that requires reading a collection of objects.
 * It will take the following steps//from w w  w  . j av  a 2s  . c  o  m
 * <ol>
 * <li>Execute the given {@link ConnectionCallback} for a {@link DBCursor}.</li>
 * <li>Prepare that {@link DBCursor} with the given {@link CursorPreparer} (will be skipped if {@link CursorPreparer}
 * is {@literal null}</li>
 * <li>Iterate over the {@link DBCursor} and applies the given {@link DbObjectCallback} to each of the
 * {@link DBObject}s collecting the actual result {@link List}.</li>
 * <ol>
 * 
 * @param <T>
 * @param collectionCallback the callback to retrieve the {@link DBCursor} with
 * @param preparer the {@link CursorPreparer} to potentially modify the {@link DBCursor} before ireating over it
 * @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type
 * @param collectionName the collection to be queried
 * @return
 */
private <T> List<T> executeFindMultiInternal(CollectionCallback<DBCursor> collectionCallback,
        CursorPreparer preparer, DbObjectCallback<T> objectCallback, String collectionName) {

    try {
        DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));

        if (preparer != null) {
            cursor = preparer.prepare(cursor);
        }

        List<T> result = new ArrayList<T>();

        for (DBObject object : cursor) {
            result.add(objectCallback.doWith(object));
        }

        return result;
    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

private void executeQueryInternal(CollectionCallback<DBCursor> collectionCallback, CursorPreparer preparer,
        DocumentCallbackHandler callbackHandler, String collectionName) {

    try {//  w  ww.j a v  a 2 s.c  om
        DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));

        if (preparer != null) {
            cursor = preparer.prepare(cursor);
        }

        for (DBObject dbobject : cursor) {
            callbackHandler.processDocument(dbobject);
        }
    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e);
    }
}