Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

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   www  .  j av  a  2 s  .co 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);
        }//  ww w .j  a va  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.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<AccessToken> getAccessTokenByUserIdAndClientApp(String userId, String clientId) {
    List<AccessToken> accessTokens = new ArrayList<AccessToken>();
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_USER_ID, userId);
    dbObject.put(CLIENTS_CLIENTID, clientId);
    dbObject.put(ACCESS_TOKEN_VALID, true);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    for (DBObject object : list) {
        Map<String, Object> mapLoaded = object.toMap();
        // convert details list to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }/*  www.j ava2  s. c  o m*/
        AccessToken loadedAccessToken = AccessToken.loadFromMap(mapLoaded);
        accessTokens.add(loadedAccessToken);
    }
    return accessTokens;
}

From source file:com.avanza.ymer.MirrorEnvironment.java

License:Apache License

public void removeFormatVersion(Class<?> dataType, Object id) {
    String collectionName = dataType.getSimpleName().substring(0, 1).toLowerCase()
            + dataType.getSimpleName().substring(1);
    DBCollection collection = getMongoDb().getCollection(collectionName);
    DBObject idQuery = BasicDBObjectBuilder.start("_id", id).get();
    DBCursor cursor = collection.find(idQuery);

    cursor.next();/*  w  ww  . j  a  v a 2 s . c  o m*/
    DBObject obj = cursor.curr();
    cursor.close();

    obj.removeField("_formatVersion");
    collection.update(idQuery, obj);
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /*from w w  w.  j av  a2 s  . c om*/
 * @param uid
 * @return 
 */
public List getUserBuildings(long uid) {
    List buildings = new ArrayList();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBCursor cursor = table.find(query);
    while (cursor.hasNext()) {
        DBObject object = cursor.next();
        buildings.add(object);
    }
    return buildings;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /*from  w ww .  j a v a2s  .c  o  m*/
 * @param uid
 * @return 
 */
public long getTotalHousingSpace(long uid) {
    long campSpace = 0;
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject();
    query.put("tid", HOUSING_TID);
    query.put("uid", uid);
    DBCursor cursor = table.find(query);
    while (cursor.hasNext()) {
        DBObject building = cursor.next();
        if (building.containsField("buildingArmies")) {
            /// In complete Code
        }
    }
    return campSpace;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /* ww w.j  av a2  s. co  m*/
 * @param uid
 * @param buildingId
 * @param time
 * @return 
 */
public boolean checkBuildingUpgrade(Object uid, Object buildingId, long time) {
    boolean state = false;
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject();
    query.put("uid", uid);
    query.put("building_id", buildingId);
    DBCursor cursor = table.find(query);
    {
        while (cursor.hasNext()) {
            DBObject buildingBase = cursor.next();
            if (time == 0)
                time = new Date().getTime();
            boolean isConstructing = (Boolean) buildingBase.get("is_constructing");
            if (isConstructing) {
                long startTime = (Long) buildingBase.get("construction_start_time");
                long endTime = startTime + ApplicationSettings.convertToTimeStamp(buildingBase);
                if (time > endTime) {
                    BasicDBObject queryUpdate = new BasicDBObject();
                    queryUpdate.put("uid", buildingBase.get("uid"));
                    queryUpdate.put("building_id", buildingBase.get("building_id"));
                    BasicDBObject toUpdate = new BasicDBObject();
                    toUpdate.put("is_constructing", 0);
                    toUpdate.put("construction_start_time", 0);
                    BasicDBObject update = new BasicDBObject("$set", toUpdate);
                    MongoDbUtil.updateCollection("aw_building_base", queryUpdate, update);
                    return true;
                }
            }
            boolean upgrading = (Boolean) buildingBase.get("is_upgrading");
            if (upgrading) {
                long startTime = (Long) buildingBase.get("upgrade_start_time");
                DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"),
                        ((Long) buildingBase.get("level") + 1));
                long endTime = startTime + ApplicationSettings.convertToTimeStamp(nextLevelConfig);
                if (time > endTime) {
                    buildingBase.put("is_upgrading", 0);
                    buildingBase.put("upgrade_start_time", 0);
                    buildingBase.put("level", (Long) buildingBase.get("level") + 1);
                    state = this.updateBuildingFromConfiguration(buildingBase);
                }
            }
        }
    }
    return state;
}

From source file:com.aw.app.action.StrategyAction.java

/**
 * /*from ww w.j a v a2 s.  c o m*/
 * @param uid 
 */
public void bootStrapAction(Long uid) {
    /********** Perform User Building Checking ***************/
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBCursor userBuildings = table.find(query);
    int count = 0;
    while (userBuildings.hasNext()) {
        DBObject building = userBuildings.next();
        if (building.containsField("tid")) {
            String tid = (String) building.get("tid");
            TroopsAction troopsAction = new TroopsAction();
            if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_TAVERN")) {
                troopsAction.checkToopsUpgrades(uid, building);
                count++;
            } else if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_BARRACK")) {
                int buildingId = (Integer) building.get("building_id");
                troopsAction.checkToopsInBarrack(uid, buildingId);
                count++;
            }

        }
    }
}

From source file:com.aw.app.action.TroopsAction.java

/**
 * /*from  w  w w.  j a va2s  . co  m*/
 * @param uid
 * @param buildingId
 * @return 
 */
public boolean checkToopsInBarrack(Long uid, long buildingId) {
    boolean state = false;
    Long time = new Date().getTime();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_barrack_troops");

    BasicDBObject query = new BasicDBObject("uid", uid);
    query.put("building_id", buildingId);
    DBCursor barrackTroops = table.find(query);
    long nextTroopsTrainingTime = 0;
    if (barrackTroops != null && barrackTroops.size() > 0) {
        while (barrackTroops.hasNext()) {
            DBObject barrackTroop = barrackTroops.next();
            if (nextTroopsTrainingTime > 0 && state) {
                BasicDBObject updateQuery = new BasicDBObject();
                updateQuery.put("bc_id", barrackTroop.get("bc_id"));
                BasicDBObject updateBt = new BasicDBObject();
                Map data = new HashMap();
                data.put("is_first_in_queue", 1);
                data.put("training_start_time", nextTroopsTrainingTime);
                BasicDBObject object = new BasicDBObject(data);
                updateBt.put("$set", object);
                MongoDbUtil.updateCollection("aw_barrack_troops", updateQuery, updateBt);
            }
            Map result = processTroopTraining(barrackTroop, time);
            boolean breakState = (boolean) result.get("breakState");
            state = (boolean) result.get("status");
            long nextTrainingTime = (long) result.get("nextTrainingTime");
            if (breakState) {
                // Error Condition
            } else if (state && result.containsKey("nextTrainingTime") && nextTrainingTime > 0) {
                nextTroopsTrainingTime = nextTrainingTime;
            } else {
                nextTroopsTrainingTime = 0;
            }
        }
    }
    return state;
}

From source file:com.aw.app.action.TroopsAction.java

/**
 * //from  w  w  w  .j  ava  2  s  . c  o m
 * @param troopSet
 * @param time
 * @return 
 */
public Map processTroopTraining(DBObject troopSet, Long time) {
    Map result = new HashMap();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration");
    BasicDBObject query = new BasicDBObject();
    query.put("tid", troopSet.get("tid"));
    query.put("level", troopSet.get("level"));
    DBCursor resultSet = table.find(query);
    while (resultSet.hasNext()) {
        DBObject config = resultSet.next();
        long remainingSpace = this.getRemainingTroopSpace((Long) troopSet.get("uid")); // Get from Method Later.
        if (config != null) {
            long trainingTime = (Long) config.get("training_time");
            long count = (Long) troopSet.get("count");
            long totalElapseTimeRequired = trainingTime * count;
            long trainingStartTime = (Long) troopSet.get("training_start_time");
            long endTime = trainingStartTime + totalElapseTimeRequired;
            long housingSpace = (Long) config.get("housing_space");
            long spaceRequired = count * housingSpace;
            if (endTime < new Date().getTime()) {
                if (remainingSpace >= spaceRequired) {
                    boolean isAdded = addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"),
                            (String) config.get("tid"), (Long) troopSet.get("count"));
                    if (isAdded) {
                        BasicDBObject deleteQuery = new BasicDBObject();
                        deleteQuery.put("bc_id", troopSet.get("bc_id"));
                        MongoDbUtil.removeCollection("aw_barrack_troops", deleteQuery);
                        result.put("status", true);
                        result.put("nextTrainingTime", endTime);
                    }
                } else if (remainingSpace == 0) {
                    BasicDBObject queryObject = new BasicDBObject("bc_id", troopSet.get("bc_id"));
                    BasicDBObject updateData = new BasicDBObject("training_start_time", new Date().getTime());
                    BasicDBObject updateQuery = new BasicDBObject("$set", updateData);
                    MongoDbUtil.updateCollection(MongoDbUtil.defaultDBName, queryObject, updateQuery);

                    result.put("status", false);
                    result.put("breakStatus", true);
                    result.put("reason", "Training Fields are full.");
                } else {
                    long troopCount = (long) Math.floor(remainingSpace / housingSpace);
                    this.addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"),
                            (String) config.get("tid"), troopCount);
                    long intialCount = (Long) troopSet.get("count");
                    long resultCount = intialCount - count;

                    Map queryMap = new HashMap();
                    queryMap.put("bc_id", troopSet.get("bc_id"));
                    Map updateData = new HashMap();
                    updateData.put("count", resultCount);
                    updateData.put("training_start_time", trainingStartTime + (trainingTime * troopCount));
                    Map dataMap = new HashMap();
                    dataMap.put("query", queryMap);
                    dataMap.put("update", updateData);
                    MongoDbUtil.updateCollection("aw_barrack_troops", updateData);
                    result.put("status", true);
                    result.put("breakStatus", true);
                    result.put("reason", "Training Fields are full.");
                }
            }
        } else {
            result.put("reason", "Configuration not found");
        }
    }
    return result;
}