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:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public void delete(String username, String password, Resource resource) {
    DBCollection files = mongo.getDataBase().getCollection("files");
    DBCollection contents = mongo.getDataBase().getCollection("contents");

    for (Resource child : getChildren(resource)) {
        delete(username, password, child);
    }//from www  .j  a va  2 s.co m

    DBObject filter = new BasicDBObject("_id", resource.getId());

    DBObject current = files.findOne(filter);
    files.update(new BasicDBObject("_id", (ObjectId) current.get("parent")),
            new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));

    files.remove(filter);
    contents.remove(new BasicDBObject("resource", resource.getId()));

    try {
        indexService.remove(username, password, resource.getId().toString());
    } catch (IndexException ex) {
        Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.aperigeek.dropvault.web.dao.user.UsersDAO.java

License:Open Source License

public boolean login(String username, String passHash) {
    DBCollection users = mongo.getDataBase().getCollection("users");

    DBObject filter = new BasicDBObjectBuilder().add("name", username).add("password", passHash).get();

    DBObject result = users.findOne(filter);

    if (result == null) {
        return false;
    }/*from  w w w  . j av a  2  s.  co m*/

    return true;
}

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

/**
 * //from  ww  w. jav a2  s  .  c om
 * @param buildings
 * @return 
 */
public List getBuildInfoForOwner(List buildings) {
    List buildingData = new ArrayList();
    for (Object building : buildings) {
        Map data = new HashMap();
        DBObject buildingObject = (DBObject) building;
        boolean checkStatus = checkBuildingUpgrade(buildingObject.get("uid"), buildingObject.get("building_id"),
                new Date().getTime());
        if (checkStatus) {
            DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
            buildingObject = table.findOne(new BasicDBObject("building_id", buildingObject.get("building_id")));
        }
        data.put("building_id", buildingObject.get("building_id"));
        data.put("tid", buildingObject.get("tid"));
        data.put("position_x", buildingObject.get("position_x"));
        data.put("position_y", buildingObject.get("position_y"));
        data.put("level", buildingObject.get("level"));
        data.put("is_constructing", buildingObject.get("is_constructing"));
        data.put("construction_start_time", buildingObject.get("construction_start_time"));
        data.put("is_upgrading", buildingObject.get("is_upgrading"));
        data.put("upgrade_start_time", buildingObject.get("upgrade_start_time"));
        data.put("building_class", buildingObject.get("building_class"));
        data.put("resource_available", 0);
        data.put("last_collection_time", 0);
        data.put("", building);

        if (buildingObject.containsField("building_resource")) {
            // add  additional fields to buildingData object.
            // last_collection_time
            // resource_available
        }
        if (buildingObject.containsField("building_townhalls")) {
            // add  additional fields to buildingData object.
            // gold_stored
            // mithril_stored
            // remove resource_available
            // remove last_collection_time
        }
        buildingData.add(data);
    }
    return buildingData;
}

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

/**
 * //from   w ww .j ava2  s .c om
 * @param buildings
 * @return 
 */
public List getBuildInfoForAttack(List buildings) {
    List buildingData = new ArrayList();
    Map data = new HashMap();
    for (Object building : buildings) {
        DBObject buildingObject = (DBObject) building;
        boolean checkStatus = checkBuildingUpgrade(buildingObject.get("uid"), buildingObject.get("building_id"),
                new Date().getTime());
        if (checkStatus) {
            DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
            buildingObject = table.findOne(new BasicDBObject("building_id", buildingObject.get("building_id")));
        }
        data.put("building_id", buildingObject.get("building_id"));
        data.put("tid", buildingObject.get("tid"));
        data.put("position_x", buildingObject.get("position_x"));
        data.put("position_y", buildingObject.get("position_y"));
        data.put("level", buildingObject.get("level"));
        data.put("is_constructing", buildingObject.get("is_constructing"));
        data.put("construction_start_time", buildingObject.get("construction_start_time"));
        data.put("is_upgrading", buildingObject.get("is_upgrading"));
        // data.put("upgrade_start_time", buildingObject.get("upgrade_start_time"));
        data.put("building_class", buildingObject.get("building_class"));
        data.put("resource_available", 0);
        // data.put("last_collection_time", 0);

        if (buildingObject.containsField("building_resource")) {
            // add  additional fields to buildingData object.
            // resource_available
        }
        if (buildingObject.containsField("building_townhalls")) {
            // add  additional fields to buildingData object.
            // gold_stored
            // mithril_stored
            // remove resource_available
        }
        buildingData.add(data);
    }
    return buildingData;
}

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

/**
 * /*from  www  .  j  a  v  a  2 s .  c om*/
 * @param data
 * @param flag
 * @return
 * @throws JSONException 
 */
public static Map addBuilding(JSONObject data, boolean flag) throws JSONException {
    Map statusData = new HashMap();
    if (data != null && (data.has("uid") & data.has("tid") & data.has("level"))) {
        DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_configuration");
        BuildingConfiguration bc = new BuildingConfiguration();
        bc.put("level", data.get("level"));
        bc.put("tid", data.get("tid"));
        DBObject buildingConfiguration = table.findOne(bc);
        if (buildingConfiguration != null) {
            statusData = addBuildingFromConfiguration(buildingConfiguration, data, flag);
        } else {
            statusData.put("errorMessage",
                    "Building Configuration not found. Please check the tid and level and confirm it in the database.");
        }
    } else {
        statusData.put("errorMessage", "Data is missing. Please Check uid, building_id, Position X & Y.");
    }
    return statusData;
}

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

/**
 * /*w ww. java2  s.c o  m*/
 * @param tid
 * @param level
 * @return 
 */
public DBObject getBuildingConfiguration(Object tid, Object level) {
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_configuration");
    return table.findOne(new BasicDBObject().append("tid", tid).append("level", level));
}

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

/**
 * /*from w  w w.j  av  a 2 s . c o m*/
 * @param tid
 * @param uid
 * @param level
 * @return 
 */
public static boolean isAllowedBuilding(Object tid, Object uid, Object level) {
    boolean isAllowed = false;
    if (tid != null && tid.toString().equalsIgnoreCase(townHallTid)) { /**** If Town Hall comes for upgrade ***/
        isAllowed = true;
        return isAllowed;
    }
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject();
    query.put("tid", tid);
    query.put("uid", uid);
    BuildingBase buildingBase = (BuildingBase) table.findOne(query);
    if (buildingBase != null) {
        Object townHallLevel = buildingBase.get("level");

    }
    return isAllowed;
}

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

/**
 * //from   ww  w. j  av a  2 s .c  o  m
 * @param uid
 * @param building 
 */
public void checkToopsUpgrades(Long uid, DBObject building) {
    boolean success = false;
    Long time = new Date().getTime();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops_details");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBObject userTroopsDetail = table.findOne(query);
    if (userTroopsDetail != null) {

    }
    // In complete method code.....
}

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

/**
 * /*from  w w  w .  j  av a  2  s.com*/
 * @param uid
 * @param infoType
 * @return 
 */
public Map getUserVillage(long uid, String infoType) {
    Map villageInfo = new HashMap();
    Map data = new HashMap();
    DBObject user = getUserDetail(uid);
    if (user != null) {
        List userBuildings = new BuildingAction().getUserBuildings(uid);
        List troops = new TroopsAction().getUserTroopsData(uid, "other");
        List troopsOwn = null;
        if ("login".equalsIgnoreCase(infoType)) {
            troopsOwn = new TroopsAction().getUserTroopsData(uid, "self");
        }
        if (troopsOwn != null) {
            troops.addAll(troopsOwn);
        }
        List buildingData = null;
        if (userBuildings != null) {
            if ("login".equalsIgnoreCase(infoType)) {
                buildingData = new BuildingAction().getBuildInfoForOwner(userBuildings);
            } else {
                buildingData = new BuildingAction().getBuildInfoForAttack(userBuildings);
            }
        }
        if (buildingData == null)
            buildingData = new ArrayList();
        if (user.containsField("clanUsers")) {
            List<ClanUsers> clanUsers = (List<ClanUsers>) user.get("clanUsers");
            ClanUsers clanUser = clanUsers.get(0);
            villageInfo.put("clan_name", clanUser.get("name"));
            villageInfo.put("clan_flag", clanUser.get("flag"));
            villageInfo.put("clan_id", clanUser.get("clan_id"));
        }
        /// Access Token
        DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_token");
        BasicDBObject query = new BasicDBObject("uid", user.get("uid"));
        DBObject token = table.findOne(query);
        String userToken = "";
        if (token != null) {
            userToken = (String) token.get("access_token");
        } else {
            userToken = createUserAccessToken((Long) user.get("uid"));
        }
        villageInfo.put("uid", user.get("uid"));
        villageInfo.put("name", user.get("name"));
        villageInfo.put("trophies", user.get("trophies"));
        villageInfo.put("level", (int) user.get("game_level"));
        villageInfo.put("troops", troops);
        villageInfo.put("buildings", buildingData);
        villageInfo.put("access_token", userToken);
        if ("login".equalsIgnoreCase(infoType)) {
            villageInfo.put("spens", user.get("spens"));
        }

        data.put("status", true);
        data.put("message", "");
        data.put("data_set", villageInfo);
        return data;
    } else {
        data.put("status", false);
        data.put("message", "Village Not Found");
        return data;
    }
}

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

/**
 * /*from www  .  j av  a2s .  c  o m*/
 * @param uid
 * @return 
 */
public DBObject getUserDetail(long uid) {
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBObject user = table.findOne(query);
    if (user != null)
        return user;
    else
        return null;
}