Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
public Model save(final Model model) {

    final URI schemaUri = model.getSchemaUri();
    final String collectionName = convertToCollectionName(schemaUri);

    final Keys keys = model.getKeys();
    DBObject mongoKeys = createMongoKeys(keys);

    if (!_Mongo.collectionExists(collectionName)) {
        final DBCollection mongoCollection = _Mongo.getCollection(collectionName);

        final DBObject collectionIndex = new BasicDBObject();
        final Set<String> indexKeySet = mongoKeys.keySet();
        for (final String indexKey : indexKeySet) {
            collectionIndex.put(indexKey, 1);
        }// w  w  w .  j a  v a  2s. c  o m

        final DBObject options = new BasicDBObject();
        options.put("background", true);

        mongoCollection.ensureIndex(collectionIndex, options);
    }

    final DBObject mongoObject;
    try {
        mongoObject = convertToMongoObject(model);
    } catch (ModelWritingException e) {
        throw new ServiceException("Failed to convert WRML model instance to a mongoDB object.", e, this);
    }

    final DBCollection mongoCollection = _Mongo.getCollection(collectionName);
    if (mongoCollection == null) {
        // Should not happen
        final String logMessage = getConfiguration().getName() + " - Collection should exist. Name:\n"
                + collectionName;

        LOG.error(logMessage);
        throw new ServiceException(logMessage, null, this);
    }

    final DBObject existingMongoObject = mongoCollection.findOne(mongoKeys);
    if (existingMongoObject != null) {
        mongoObject.put("_id", existingMongoObject.get("_id"));
    }

    String errorMessage = null;
    Throwable throwable = null;
    try {
        final WriteResult mongoWriteResult = mongoCollection.save(mongoObject);
        errorMessage = mongoWriteResult.getError();
    } catch (Throwable t) {
        errorMessage = t.getMessage();
        throwable = t;
    }

    if (errorMessage != null || throwable != null) {
        final String logMessage = getConfiguration().getName() + " - Error saving model (" + errorMessage
                + ").";

        LOG.error(logMessage);
        throw new ServiceException(logMessage, throwable, this);
    }

    // TODO: Should this return the saved model instead (using get?)?
    return model;

}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
public Model get(final Keys keys, final Dimensions dimensions) {

    final URI schemaUri = dimensions.getSchemaUri();
    final String collectionName = convertToCollectionName(schemaUri);
    if (!_Mongo.collectionExists(collectionName)) {
        LOG.debug(getConfiguration().getName() + " - Collection does not exist. Name:\n" + collectionName);
        return null;
    }//from  ww  w .  j a v  a  2s . c om

    final DBCollection mongoCollection = _Mongo.getCollection(collectionName);
    if (mongoCollection == null) {
        // Should not happen
        LOG.error(getConfiguration().getName() + " - Collection should exist. Name:\n" + collectionName);
        return null;
    }

    final DBObject mongoKeys = createMongoKeys(keys);
    final DBObject mongoObject = mongoCollection.findOne(mongoKeys);
    if (mongoObject == null) {
        LOG.debug(getConfiguration().getName() + " - Failed to find model. Keys:\n" + keys);
        return null;
    }

    Model model = null;

    try {
        model = convertToModel(mongoObject, keys, dimensions);
    } catch (ModelReadingException e) {
        LOG.error(e.getMessage(), e);
    }

    return model;
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
public void delete(final Keys keys, final Dimensions dimensions) {

    final DBObject mongoKeys = createMongoKeys(keys);

    for (final URI schemaUri : keys.getKeyedSchemaUris()) {

        final String collectionName = convertToCollectionName(schemaUri);
        if (!_Mongo.collectionExists(collectionName)) {
            continue;
        }//from w ww .  ja v  a2 s. c  om

        final DBCollection mongoCollection = _Mongo.getCollection(collectionName);
        if (mongoCollection == null) {
            continue;
        }

        final DBObject mongoObject = mongoCollection.findOne(mongoKeys);

        if (mongoObject != null) {
            final WriteResult mongoWriteResult = mongoCollection.remove(mongoObject);

            final String errorMessage = mongoWriteResult.getError();
            if (errorMessage != null) {
                LOG.error(getConfiguration().getName() + " - Error deleting model (" + errorMessage
                        + "). Keys:\n" + keys);
            }
        }
    }
}

From source file:org.xtext.mongobeans.lib.MongoExtensions.java

License:Open Source License

public <T extends IMongoBean> T findOneBean(final DBCollection collection, final T wrapper) {
    IMongoBean _wrap = null;//from   w w w. j  av  a  2s . com
    DBObject _dbObject = wrapper.getDbObject();
    DBObject _findOne = collection.findOne(_dbObject);
    if (_findOne != null) {
        _wrap = WrappingUtil.wrap(_findOne);
    }
    return ((T) _wrap);
}

From source file:pubsub.broker.model.Login.java

public Publisher getPublisher() {
    DBCollection collection = db.getCollection(DBConstants.PUBLISHER_COLLECTION);
    DBObject dbo = collection.findOne(this);
    if (dbo != null) {
        Publisher pub = Converter.toObject(Publisher.class, dbo);
        return pub;
    } else/*  w  w  w  . j  ava2  s.c o m*/
        return null;
}

From source file:pubsub.broker.model.Publisher.java

public boolean alreadyRegistered(Messages msg) {

    this.put(DBConstants.PUBLISHER_EMAIL, this.email);
    DBCollection coll = db.getCollection(DBConstants.PUBLISHER_COLLECTION);
    if (coll.findOne(this) != null)
        return true;
    else//from   w  ww .j  a  va 2s. co  m
        return false;

}

From source file:pubsub.broker.model.Topics.java

public void addSubscriberByHostAddress(String topics, String hostAddress) {
    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);
    //BasicDBObject query = new BasicDBObject(DBConstants.TOPIC_TOPIC, topics);

    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, topics);

    DBObject dbo = collection.findOne(query);

    List<String> hostList = new ArrayList<String>();
    hostList.addAll((List<String>) dbo.get(DBConstants.TOPIC_HOST_LIST));
    hostList.add(hostAddress);/* w w w .j av  a  2 s.co m*/

    BasicDBObject newDocument = new BasicDBObject();
    newDocument.put(DBConstants.TOPIC_HOST_LIST, hostList);

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    collection.update(query, updateObj);
}

From source file:pubsub.broker.model.Topics.java

public void addSubscriberByEmail(String topics, String email) {
    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);

    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, topics);

    DBObject dbo = collection.findOne(query);

    List<String> emailList = new ArrayList<String>();
    emailList.addAll((List<String>) dbo.get(DBConstants.TOPIC_EMAIL_LIST));
    emailList.add(email);/*from w w w. j  a  v a  2  s.c o  m*/

    BasicDBObject newDocument = new BasicDBObject();
    newDocument.put(DBConstants.TOPIC_EMAIL_LIST, emailList);

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    collection.update(query, updateObj);

}

From source file:pubsub.broker.model.Topics.java

public void removeSubscriberEmail(String topics, String email) {
    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);
    //BasicDBObject query = new BasicDBObject(DBConstants.TOPIC_TOPIC, topics);

    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, topics);

    DBObject dbo = collection.findOne(query);

    List<String> emailList = new ArrayList<String>();
    emailList.addAll((List<String>) dbo.get(DBConstants.TOPIC_EMAIL_LIST));
    emailList.remove(email);//from   w ww. j a v  a 2 s  . c  om

    BasicDBObject newDocument = new BasicDBObject();
    newDocument.put(DBConstants.TOPIC_EMAIL_LIST, emailList);

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    collection.update(query, updateObj);
}

From source file:pubsub.broker.model.Topics.java

public void removeSubscriberHostAddress(String topics, String hostAddress) {
    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);

    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, topics);

    DBObject dbo = collection.findOne(query);

    List<String> hostList = new ArrayList<String>();
    hostList.addAll((List<String>) dbo.get(DBConstants.TOPIC_HOST_LIST));
    hostList.remove(hostAddress);/*from  w  w w  .  j  av a2s . co m*/

    BasicDBObject newDocument = new BasicDBObject();
    newDocument.put(DBConstants.TOPIC_HOST_LIST, hostList);

    BasicDBObject updateObj = new BasicDBObject();
    updateObj.put("$set", newDocument);

    collection.update(query, updateObj);
}