Example usage for com.mongodb DBCollection findAndModify

List of usage examples for com.mongodb DBCollection findAndModify

Introduction

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

Prototype

public DBObject findAndModify(final DBObject query, final DBCollectionFindAndModifyOptions options) 

Source Link

Document

Atomically modify and return a single document.

Usage

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void updateAccessTokenValidStatus(String accessToken, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("token", accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/*ww w.ja  v  a  2 s . c  o  m*/
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void updateAuthCodeValidStatus(String authCode, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("code", authCode);
    DBCollection coll = db.getCollection(AUTH_CODE_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/* w ww. ja  v  a 2s.c  o  m*/
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public boolean updateClientApp(String clientId, String scope, String description, Integer status,
        Map<String, String> applicationDetails) {
    boolean updated = false;
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(ID_NAME, clientId);
    List<DBObject> list = coll.find(query).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        if (scope != null && scope.length() > 0) {
            newObject.put("scope", scope);
        }//from w  w w .  j av a  2s .c o m
        if (description != null && description.length() > 0) {
            newObject.put("descr", description);
        }
        if (status != null) {
            newObject.put("status", status);
        }
        if (applicationDetails != null && applicationDetails.size() > 0) {
            newObject.put("applicationDetails", applicationDetails);
        }
        coll.findAndModify(query, newObject);
        updated = true;
    }
    return updated;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void removeAccessToken(String accessToken) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_ID_NAME, accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        coll.findAndModify(dbObject, newObject);
        coll.remove(dbObject);/*from  w  w  w . j  a v a 2 s.  c  o  m*/
    }
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public void updateAccessTokenValidStatus(String accessToken, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("token", accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    DBObject newObject = coll.find(dbObject).next();
    if (newObject != null) {
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/*from w w  w.  j a v a2s .c  o m*/
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public void updateAuthCodeValidStatus(String authCode, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("code", authCode);
    DBCollection coll = db.getCollection(AUTH_CODE_COLLECTION_NAME);
    DBObject newObject = coll.find(dbObject).next();
    if (newObject != null) {
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/*from   ww w  .j a  v  a2s  . c o m*/
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public boolean updateClientApp(String clientId, String scope, String description, Integer status,
        Map<String, String> applicationDetails) {
    boolean updated = false;
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(CLIENTS_ID, clientId);
    List<DBObject> list = coll.find(query).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        if (scope != null && scope.length() > 0) {
            newObject.put("scope", scope);
        }/*from   w  w  w .ja  v a  2  s. c o  m*/
        if (description != null && description.length() > 0) {
            newObject.put("descr", description);
        }
        if (status != null) {
            newObject.put("status", status);
        }
        if (applicationDetails != null && applicationDetails.size() > 0) {
            newObject.put("applicationDetails", applicationDetails);
        }
        coll.findAndModify(query, newObject);
        updated = true;
    }
    return updated;
}

From source file:com.continuent.tungsten.replicator.applier.MongoApplier.java

License:Open Source License

/**
 * Applies row updates to MongoDB. Statements are discarded. {@inheritDoc}
 * //from w ww . ja va  2s .  c o m
 * @see com.continuent.tungsten.replicator.applier.RawApplier#apply(com.continuent.tungsten.replicator.event.DBMSEvent,
 *      com.continuent.tungsten.replicator.event.ReplDBMSHeader, boolean,
 *      boolean)
 */
@Override
public void apply(DBMSEvent event, ReplDBMSHeader header, boolean doCommit, boolean doRollback)
        throws ReplicatorException, ConsistencyException, InterruptedException {
    ArrayList<DBMSData> dbmsDataValues = event.getData();

    // Iterate through values inferring the database name.
    for (DBMSData dbmsData : dbmsDataValues) {
        if (dbmsData instanceof StatementData) {
            if (logger.isDebugEnabled())
                logger.debug("Ignoring statement");
        } else if (dbmsData instanceof RowChangeData) {
            RowChangeData rd = (RowChangeData) dbmsData;
            for (OneRowChange orc : rd.getRowChanges()) {
                // Get the action as well as the schema & table name.
                ActionType action = orc.getAction();
                String schema = orc.getSchemaName();
                String table = orc.getTableName();
                if (logger.isDebugEnabled()) {
                    logger.debug("Processing row update: action=" + action + " schema=" + schema + " table="
                            + table);
                }

                // Process the action.
                if (action.equals(ActionType.INSERT)) {
                    // Connect to the schema and collection.
                    DB db = m.getDB(schema);
                    DBCollection coll = db.getCollection(table);

                    // Fetch column names.
                    List<ColumnSpec> colSpecs = orc.getColumnSpec();

                    // Make a document and insert for each row.
                    Iterator<ArrayList<ColumnVal>> colValues = orc.getColumnValues().iterator();
                    while (colValues.hasNext()) {
                        BasicDBObject doc = new BasicDBObject();
                        ArrayList<ColumnVal> row = colValues.next();
                        for (int i = 0; i < row.size(); i++) {
                            Object value = row.get(i).getValue();
                            setValue(doc, colSpecs.get(i), value);
                        }
                        if (logger.isDebugEnabled())
                            logger.debug("Adding document: doc=" + doc.toString());
                        coll.insert(doc);
                    }
                } else if (action.equals(ActionType.UPDATE)) {
                    // Connect to the schema and collection.
                    DB db = m.getDB(schema);
                    DBCollection coll = db.getCollection(table);

                    // Ensure required indexes are present.
                    ensureIndexes(coll, orc);

                    // Fetch key and column names.
                    List<ColumnSpec> keySpecs = orc.getKeySpec();
                    List<ColumnSpec> colSpecs = orc.getColumnSpec();
                    ArrayList<ArrayList<OneRowChange.ColumnVal>> keyValues = orc.getKeyValues();
                    ArrayList<ArrayList<OneRowChange.ColumnVal>> columnValues = orc.getColumnValues();

                    // Iterate across the rows.
                    for (int row = 0; row < columnValues.size() || row < keyValues.size(); row++) {
                        List<ColumnVal> keyValuesOfRow = keyValues.get(row);
                        List<ColumnVal> colValuesOfRow = columnValues.get(row);

                        // Prepare key values query to search for rows.
                        DBObject query = new BasicDBObject();
                        for (int i = 0; i < keyValuesOfRow.size(); i++) {
                            setValue(query, keySpecs.get(i), keyValuesOfRow.get(i).getValue());
                        }

                        BasicDBObject doc = new BasicDBObject();
                        for (int i = 0; i < colValuesOfRow.size(); i++) {
                            setValue(doc, colSpecs.get(i), colValuesOfRow.get(i).getValue());
                        }
                        if (logger.isDebugEnabled()) {
                            logger.debug("Updating document: query=" + query + " doc=" + doc);
                        }
                        DBObject updatedRow = coll.findAndModify(query, doc);
                        if (logger.isDebugEnabled()) {
                            if (updatedRow == null)
                                logger.debug("Unable to find document for update: query=" + query);
                            else
                                logger.debug("Documented updated: doc=" + doc);
                        }
                    }
                } else if (action.equals(ActionType.DELETE)) {
                    // Connect to the schema and collection.
                    DB db = m.getDB(schema);
                    DBCollection coll = db.getCollection(table);

                    // Ensure required indexes are present.
                    ensureIndexes(coll, orc);

                    List<ColumnSpec> keySpecs = orc.getKeySpec();
                    ArrayList<ArrayList<OneRowChange.ColumnVal>> keyValues = orc.getKeyValues();
                    ArrayList<ArrayList<OneRowChange.ColumnVal>> columnValues = orc.getColumnValues();

                    // Iterate across the rows.
                    for (int row = 0; row < columnValues.size() || row < keyValues.size(); row++) {
                        List<ColumnVal> keyValuesOfRow = keyValues.get(row);

                        // Prepare key values query to search for rows.
                        DBObject query = new BasicDBObject();
                        for (int i = 0; i < keyValuesOfRow.size(); i++) {
                            setValue(query, keySpecs.get(i), keyValuesOfRow.get(i).getValue());
                        }

                        if (logger.isDebugEnabled()) {
                            logger.debug("Deleting document: query=" + query);
                        }
                        DBObject deletedRow = coll.findAndRemove(query);
                        if (logger.isDebugEnabled()) {
                            if (deletedRow == null)
                                logger.debug("Unable to find document for delete");
                            else
                                logger.debug("Documented deleted: doc=" + deletedRow);
                        }
                    }
                } else {
                    logger.warn("Unrecognized action type: " + action);
                    return;
                }
            }
        } else if (dbmsData instanceof LoadDataFileFragment) {
            if (logger.isDebugEnabled())
                logger.debug("Ignoring load data file fragment");
        } else if (dbmsData instanceof RowIdData) {
            if (logger.isDebugEnabled())
                logger.debug("Ignoring row ID data");
        } else {
            logger.warn("Unsupported DbmsData class: " + dbmsData.getClass().getName());
        }
    }

    // Mark the current header and commit position if requested.
    this.latestHeader = header;
    if (doCommit)
        commit();
}

From source file:com.norconex.collector.core.data.store.impl.mongo.BaseMongoSerializer.java

License:Apache License

@Override
public DBObject getNextQueued(DBCollection collRefs) {
    BasicDBObject whereQuery = new BasicDBObject(FIELD_STAGE, Stage.QUEUED.name());
    BasicDBObject newDocument = new BasicDBObject("$set", new BasicDBObject(FIELD_STAGE, Stage.ACTIVE.name()));
    return collRefs.findAndModify(whereQuery, newDocument);
}

From source file:com.sube.daos.mongodb.CardMongoDaoImpl.java

License:Apache License

private void markAs(SubeCard subeCard, String status) {
    DBCollection collection = getCardCollection();
    BasicDBObject query = getCardQuery(subeCard);
    BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("status", status));
    collection.findAndModify(query, update);
}