org.jongo.async.AsyncCommand.java Source code

Java tutorial

Introduction

Here is the source code for org.jongo.async.AsyncCommand.java

Source

/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jongo.async;

import com.mongodb.CommandResult;
import com.mongodb.DBObject;
import com.mongodb.async.client.MongoDatabase;
import org.jongo.ResultHandler;
import org.jongo.marshall.Unmarshaller;
import org.jongo.query.Query;
import org.jongo.query.QueryFactory;

import java.util.ArrayList;
import java.util.List;

import static org.jongo.ResultHandlerFactory.newResultHandler;

public class AsyncCommand {

    private final MongoDatabase db;
    private final Unmarshaller unmarshaller;
    private final QueryFactory queryFactory;
    private Query query;
    private boolean throwOnError;

    public AsyncCommand(MongoDatabase db, Unmarshaller unmarshaller, QueryFactory queryFactory, String query,
            Object... parameters) {
        this.db = db;
        this.unmarshaller = unmarshaller;
        this.queryFactory = queryFactory;
        this.query = this.queryFactory.createQuery(query, parameters);
    }

    public AsyncCommand throwOnError() {
        throwOnError = true;
        return this;
    }

    public ResultCommand field(String fieldName) {
        return new ResultCommand(fieldName);
    }

    public <T> void as(final Class<T> clazz, AsyncResultHandler<T> asyncResultHandler) {
        map(newResultHandler(clazz, unmarshaller), asyncResultHandler);
    }

    public <T> T map(ResultHandler<T> resultHandler, AsyncResultHandler<T> asyncResultHandler) {
        CommandResult commandResult = executeCommand(asyncResultHandler);
        return resultHandler.map(commandResult);
    }

    private CommandResult executeCommand() {
        CommandResult commandResult = db.runCommand(query.toDBObject());
        if (throwOnError) {
            commandResult.throwOnError();
        }
        return commandResult;
    }

    public class ResultCommand {

        private String fieldName;

        public ResultCommand(String fieldName) {
            this.fieldName = fieldName;
        }

        public <T> List<T> as(final Class<T> clazz) {
            return map(newResultHandler(clazz, unmarshaller));
        }

        public <T> List<T> map(ResultHandler<T> resultHandler) {
            CommandResult commandResult = executeCommand();
            List<DBObject> results = (List<DBObject>) commandResult.get(fieldName);
            if (results == null) {
                return new ArrayList<T>();
            }
            List<T> mappedResult = new ArrayList<T>(results.size());
            for (DBObject dbObject : results) {
                mappedResult.add(resultHandler.map(dbObject));
            }
            return mappedResult;
        }
    }
}