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.mule.modules.morphia.MorphiaConnector.java

License:Open Source License

/**
 * Calculates aggregates values without the need for complex map-reduce operations
 *
 * <p/>// w ww  . j a va2  s .c o m
 * {@sample.xml ../../../doc/mule-module-morphia.xml.sample morphia:aggregate}
 *
 * @param collection collection name
 * @param pipeline list of pipeline operators
 * @param exception The exception that needs to be thrown if there is an error executing the aggregation query
 * @param username the username to use in case authentication is required
 * @param password the password to use in case authentication is required, null
 *                 if no authentication is desired
 * @param host     The host of the Mongo server. If the host is part of a replica set then you can specify all the hosts
 *                 separated by comma.
 * @param port     The port of the Mongo server
 * @param database The database name of the Mongo server
 * @return the aggregation result
 * @throws Exception if there is an exception while aggregating
 */
@Processor
public BasicDBList aggregate(String collection, List<Pipeline> pipeline, @Optional String exception,
        @Optional String username, @Optional @Password String password, @Optional String host,
        @Optional Integer port, @Optional String database) throws Exception {
    if (!pipeline.isEmpty()) {
        Datastore datastore = getDatastore(username, password, database, host, port);
        List<DBObject> dbObjects = new ArrayList<DBObject>();
        for (Pipeline pipelineOperator : pipeline) {
            Object dbObject = JSON.parse(pipelineOperator.toJson());
            if (dbObject == null || !(dbObject instanceof DBObject)) {
                throw new IllegalArgumentException("Illegal pipeline operator '" + pipelineOperator + "'");
            }
            dbObjects.add((DBObject) dbObject);
        }
        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("aggregate", collection);
        builder.append("pipeline", dbObjects.toArray());
        CommandResult result = datastore.getDB().command(builder.get());
        if (result.ok()) {
            return (BasicDBList) result.get("result");
        }
        if (exception != null) {
            throw getExceptionFromClassName(exception);
        }
    }
    // Return an empty list
    return new BasicDBList();
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

public static DBObject marshall(Variable variable) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", variable.getName()) //
            .add("valueType", variable.getValueType().getName()) //
            .add("entityType", variable.getEntityType()) //
            .add("mimeType", variable.getMimeType()) //
            .add("repeatable", variable.isRepeatable()) //
            .add("occurrenceGroup", variable.getOccurrenceGroup()) //
            .add("referencedEntityType", variable.getReferencedEntityType()) //
            .add("unit", variable.getUnit()).add("index", variable.getIndex());

    if (variable.hasCategories()) {
        Collection<Object> list = new BasicDBList();
        for (Category category : variable.getCategories()) {
            list.add(marshall(category));
        }/*from w w  w  . j av a 2 s  .  c om*/
        builder.add("categories", list);
    }

    if (variable.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : variable.getAttributes()) {
            list.add(marshall(attribute));
        }
        builder.add("attributes", list);
    }

    return builder.get();
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

private static DBObject marshall(Category category) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start() //
            .add("name", category.getName()).add("missing", category.isMissing());
    if (category.hasAttributes()) {
        Collection<Object> list = new BasicDBList();
        for (Attribute attribute : category.getAttributes()) {
            list.add(marshall(attribute));
        }//from   w w w .  j  a  v a2 s  .  com
        builder.add("attributes", list);
    }
    return builder.get();
}

From source file:org.obiba.magma.datasource.mongodb.converter.VariableConverter.java

License:Open Source License

private static DBObject marshall(Attribute attribute) {
    return BasicDBObjectBuilder.start() //
            .add("namespace", attribute.hasNamespace() ? attribute.getNamespace() : null) //
            .add("name", attribute.getName()) //
            .add("locale", attribute.isLocalised() ? attribute.getLocale().toString() : null) //
            .add("value", attribute.getValue().toString()).get();
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBDatasource.java

License:Open Source License

DBObject asDBObject() {
    if (dbObject == null) {
        dbObject = getDatasourceCollection().findOne(BasicDBObjectBuilder.start() //
                .add("name", getName()) //
                .get());//from w  w  w. ja  va2s.  c o  m
        if (dbObject == null) {
            dbObject = BasicDBObjectBuilder.start() //
                    .add("name", getName()) //
                    .add(TIMESTAMPS_FIELD, createTimestampsObject()).get();
            getDatasourceCollection().insert(dbObject, WriteConcern.ACKNOWLEDGED);
        }
    }
    return dbObject;
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBDatasource.java

License:Open Source License

static DBObject createTimestampsObject() {
    return BasicDBObjectBuilder.start() //
            .add(TIMESTAMPS_CREATED_FIELD, new Date()) //
            .add(TIMESTAMPS_UPDATED_FIELD, new Date()).get();
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBDatasource.java

License:Open Source License

@Override
public void drop() {
    for (String name : getValueTableNames()) {
        dropTable(name);/*from  w ww . java2s. co  m*/
    }
    getDatasourceCollection().remove(BasicDBObjectBuilder.start().add("_id", getName()).get());
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBDatasource.java

License:Open Source License

@Override
protected Set<String> getValueTableNames() {
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    try (DBCursor cursor = getValueTableCollection().find(
            BasicDBObjectBuilder.start().add("datasource", asDBObject().get("_id")).get(),
            BasicDBObjectBuilder.start().add("name", 1).get())) {
        while (cursor.hasNext()) {
            builder.add(cursor.next().get("name").toString());
        }//from w ww . j  a v a 2  s. co  m
    }
    return builder.build();
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBValueTable.java

License:Open Source License

DBObject asDBObject() {
    if (dbObject == null) {
        dbObject = getValueTableCollection().findOne(BasicDBObjectBuilder.start() //
                .add("datasource", getMongoDBDatasource().asDBObject().get("_id")) //
                .add("name", getName()) //
                .get());/*from   w  w w  . j  a  v a2 s  .  com*/
        // create DBObject if not found
        if (dbObject == null) {
            dbObject = BasicDBObjectBuilder.start() //
                    .add("datasource", getMongoDBDatasource().asDBObject().get("_id")) //
                    .add("name", getName()) //
                    .add("entityType", getEntityType()) //
                    .add(MongoDBDatasource.TIMESTAMPS_FIELD, MongoDBDatasource.createTimestampsObject()).get();
            getValueTableCollection().insert(dbObject, WriteConcern.ACKNOWLEDGED);
        }
    }
    return dbObject;
}

From source file:org.obiba.magma.datasource.mongodb.MongoDBValueTable.java

License:Open Source License

public void drop() {
    // drop associated collections
    dropFiles();/*from w ww.  j  ava 2s  .  c  o m*/
    getValueSetCollection().drop();
    getVariablesCollection().drop();
    getValueTableCollection().remove(BasicDBObjectBuilder.start().add("_id", getIdAsObjectId()).get());
    getMongoDBDatasource().setLastUpdate(new Date());
    dbObject = null;
}