List of usage examples for com.mongodb DBObject put
Object put(String key, Object v);
From source file:com.avbravo.jmoordbdianna.mongodb.internal.JavaToDocument.java
@SuppressWarnings("rawtypes") public Object toDBObjectRecursive(Object object, FieldDescriptor fieldDescriptor, List<EmbeddedBeans> embeddedBeansList, List<ReferencedBeans> referencedBeansList) { if (object == null) { return null; }//from w w w . j ava 2 s . c om if (fieldDescriptor.isArray()) { System.out.println(" isArray"); if (ReflectionUtils.isSimpleClass(fieldDescriptor.getField().getType().getComponentType())) { return fieldDescriptor.getFieldValue(object); } else { Object[] array = (Object[]) fieldDescriptor.getFieldValue(object); BasicDBList fieldObj = new BasicDBList(); for (Object el : array) { fieldObj.add(toDocument(el, embeddedBeansList, referencedBeansList)); } return fieldObj; } } else if (fieldDescriptor.isIterable()) { Iterable col = (Iterable) fieldDescriptor.getFieldValue(object); BasicDBList fieldObj = new BasicDBList(); if (col != null) { for (Object el : col) { if (ReflectionUtils.isSimpleClass(el.getClass())) { fieldObj.add(el); } else { if (isEmbedded(fieldDescriptor.getName())) { fieldObj.add(toDocument(el, embeddedBeansList, referencedBeansList)); } else { if (isReferenced(fieldDescriptor.getName())) { //aris ClassDescriptor classD = cache.get(el.getClass()); for (FieldDescriptor fieldDesc : classD.getFields()) { if (fieldDesc.getName().equals(referencedBeans.getField())) { fieldObj.add(toDocumentReferenced(el, referencedBeans.getField())); } } //aris } else { System.out.println("..........no es embebido ni referenciado"); } } } } } return fieldObj; } else if (fieldDescriptor.isObject()) { //System.out.println(" fieldDescriptor.isObject() "); if (isEmbedded(fieldDescriptor.getName())) { //Embebido Object fieldValue = fieldDescriptor.getFieldValue(object); if (fieldValue == null) { return null; } DBObject dbObject = new BasicDBObject(); for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) { dbObject.put(childDescriptor.getName(), toDBObjectRecursive(fieldValue, childDescriptor, embeddedBeansList, referencedBeansList)); } return dbObject; } else { if (isReferenced(fieldDescriptor.getName())) { //Referenciado Object fieldValue = fieldDescriptor.getFieldValue(object); if (fieldValue == null) { return null; } DBObject dbObject = new BasicDBObject(); for (FieldDescriptor childDescriptor : fieldDescriptor.getChildren()) { if (childDescriptor.getName().equals(referencedBeans.getField())) { dbObject.put(childDescriptor.getName(), toDBObjectRecursive(fieldValue, childDescriptor, embeddedBeansList, referencedBeansList)); } } return dbObject; } else { System.out.println(" [No es Referenced]"); new JmoordbException( "@Embedded or @Reference is required for this field " + fieldDescriptor.getName()); return new BasicDBObject(); } } } else if (fieldDescriptor.isMap()) { System.out.println("=========================="); System.out.println("fieldDescriptor.isMap()"); System.out.println("=========================="); DBObject dbObject = new BasicDBObject(); Map map = (Map) fieldDescriptor.getFieldValue(object); for (Object key : map.keySet()) { Object el = map.get(key); if (el == null || ReflectionUtils.isSimpleClass(el.getClass())) { dbObject.put(key.toString(), el); } else { dbObject.put(key.toString(), toDocument(el, embeddedBeansList, referencedBeansList)); } } return dbObject; } else { //valor del atributo que no es otra clase return fieldDescriptor.getFieldValue(object); } }
From source file:com.aw.app.action.BattleAction.java
/** * //from ww w . ja v a2 s .c o m * @param battle */ public void updateBattleTrophy(Battle battle) { Map battleMap = battle.toMap(); if (battleMap.containsKey("attacker_id") && battle.getLong("attacker_id") > 0) { DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .findOne(new BasicDBObject("uid", battle.get("attacker_id"))); if (user != null && battle.getLong("attacker_trophies") > 0) { if (battle.getLong("attacker_won") == 1) { long userTrophies = (Long) user.get("trophies"); userTrophies += battle.getLong("attacker_trophies"); user.put("trophies", userTrophies); } else { long userTrophies = (Long) user.get("trophies"); userTrophies -= battle.getLong("attacker_trophies"); if (userTrophies < 0) userTrophies = 0; user.put("trophies", userTrophies); } // Update User here BasicDBObject updateUser = new BasicDBObject("$set", new BasicDBObject("trophies", user.get("trophies"))); BasicDBObject query = new BasicDBObject("uid", battle.get("attacker_id")); MongoDbUtil.updateCollection("aw_user", query, updateUser); } } if (battleMap.containsKey("defender_id") && battle.getDefenderId() > 0) { DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .findOne(new BasicDBObject("uid", battle.get("defender_id"))); if (user != null && battle.getLong("defender_trophies") > 0) { if (battle.getAttackerWon() == 1) { long userTrophies = (Long) user.get("trophies"); userTrophies += battle.getLong("defender_trophies"); user.put("trophies", userTrophies); } else { long userTrophies = (Long) user.get("trophies"); userTrophies -= battle.getLong("defender_trophies"); if (userTrophies < 0) userTrophies = 0; user.put("trophies", userTrophies); } // Update User here BasicDBObject updateUser = new BasicDBObject("$set", new BasicDBObject("trophies", user.get("trophies"))); BasicDBObject query = new BasicDBObject("uid", battle.get("defender_id")); MongoDbUtil.updateCollection("aw_user", query, updateUser); } } }
From source file:com.aw.app.action.BuildingAction.java
/** * /*w w w. ja va 2 s . com*/ * @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.BuildingAction.java
/** * /* ww w .j a v a 2 s . co m*/ * @param building * @return */ public boolean updateBuildingFromConfiguration(DBObject building) { boolean state = false; if (building != null) { DBObject buildingConfig = MongoDbUtil .getCollection(MongoDbUtil.defaultDBName, "aw_building_configuration") .findOne(new BasicDBObject().append("tid", building.get("tid")).append("level", building.get("level"))); if (buildingConfig != null) { building.put("attributes", buildingConfig.get("attributes")); BasicDBObject params = new BasicDBObject().append("is_upgrading", building.get("is_upgrading")) .append("upgrade_start_time", building.get("upgrade_start_time")) .append("level", building.get("level")).append("attributes", building.get("attributes")); WriteResult result = MongoDbUtil.updateCollection("aw_building_base", new BasicDBObject("building_id", building.get("building_id")), new BasicDBObject("$set", params)); if (result.getLastError().ok()) { String buildingClass = (String) buildingConfig.get("building_class"); DBObject specialBuildObject = null; String collectionName = "aw_building_base"; if ("Defense".equalsIgnoreCase(buildingClass)) { collectionName = "aw_building_defense"; } else if ("Resource".equalsIgnoreCase(buildingClass)) { collectionName = "aw_building_resource"; } else if ("Army".equalsIgnoreCase(buildingClass)) { collectionName = "aw_building_army"; } else if ("Town Hall".equalsIgnoreCase(buildingClass)) { collectionName = "aw_building_townhall"; } if (specialBuildObject != null) { } } } } return false; }
From source file:com.aw.app.action.BuildingAction.java
/** * /* ww w .j ava 2 s.c om*/ * @param uid * @param townGoldLoot * @param townMithrilLoot * @param isDestroyed */ public void updateBattleTownHall(long uid, long townGoldLoot, long townMithrilLoot, boolean isDestroyed) { DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid) .append("building_class", "Town Hall")); if (buildingBase != null) { if (buildingBase.containsField("buildingTownhalls")) { List<BuildingTownhall> townhalls = (List<BuildingTownhall>) buildingBase.get("buildingTownhalls"); BuildingTownhall townHall = townhalls.get(0); if (townGoldLoot > 0) { townHall.put("gold_stored", townHall.getLong("gold_stored") - townGoldLoot); } if (townGoldLoot < 0) { townHall.put("gold_stored", 0); } if (townMithrilLoot > 0) { townHall.put("mithril_stored", townHall.getLong("mithril_stored") - townMithrilLoot); } if (townMithrilLoot < 0) { townHall.put("mithril_stored", 0); } // update townhall.... BasicDBObject townhallUpdate = new BasicDBObject("$set", new BasicDBObject().append("gold_stored", townHall.get("gold_stored")) .append("mithril_stored", townHall.get("mithril_stored")) .append("mithril_stored", townHall.get("mithril_stored"))); BasicDBObject townhallQuery = new BasicDBObject("building_townhall_id", townHall.get("building_townhall_id")); MongoDbUtil.updateCollection("aw_building_townhall", townhallQuery, townhallUpdate); if (isDestroyed) { buildingBase.put("is_destroyed", 1); } buildingBase.put("buildingTownhalls", townhalls); BasicDBObject updateObject = new BasicDBObject("$set", new BasicDBObject().append("is_destroyed", buildingBase.get("buildingBase")) .append("buildingTownhalls", buildingBase.get("buildingTownhalls"))); BasicDBObject query = new BasicDBObject().append("uid", uid) .append("tid", TownHallAction.townHallTid).append("building_class", "Town Hall"); MongoDbUtil.updateCollection("aw_building_base", query, updateObject); } } }
From source file:com.aw.app.action.BuildingAction.java
/** * /*w w w . ja va2 s . co m*/ * @param uid * @param buildingId * @param time * @return */ public Map instantFinishBuilding(long uid, long buildingId, long time) { Map status = new HashMap(); DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("building_id", buildingId)); if (buildingBase != null) { int updating = (Integer) buildingBase.get("is_upgrading"); int constructing = (Integer) buildingBase.get("is_constructing"); if (updating == 1 || constructing == 1) { DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"), (Long) buildingBase.get("level") + 1); if (updating == 1 && nextLevelConfig == null) { status.put("errorMessage", "Next level of building is not present."); return status; } long spenAmount = this.calculateInstantFinishAmount(buildingId, time); long userSpens = new UserAction().getUserResource(uid, "spens"); if (userSpens >= spenAmount) { UserAction action = new UserAction(); if (action.updateUserResource(uid, spenAmount, "spens", "decrease")) { buildingBase.put("is_upgrading", 0); buildingBase.put("upgrade_start_time", 0); buildingBase.put("level", (Long) buildingBase.get("level") + 1); if (AWUtil.isInArray(collectionResource, buildingBase.get("tid").toString())) { long lastCollectionTime = time; List<BuildingResource> resources = (List<BuildingResource>) buildingBase .get("buildingResources"); BuildingResource resource = resources.get(0); if (resource != null) { resource.put("last_collection_time", lastCollectionTime); // update resource here... } } // update building base here.... } } else { status.put("errorMessage", "Not enough resources in the storage to finish this building upgrade."); } } else { status.put("errorMessage", "Building is not upgrading, you have made bad request."); } } else { status.put("errorMessage", "Building Not Found."); } return status; }
From source file:com.aw.app.action.BuildingAction.java
/** * //from w w w . j a v a2s . co m * @param uid * @param buildingId * @param time * @return */ public Map upgradeBuilding(long uid, long buildingId, long time) { Map status = new HashMap(); DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("building_id", buildingId)); if (buildingBase != null) { int upgrading = (Integer) buildingBase.get(""); if (upgrading == 1) { status.put("errorMessage", "Building is already upgrading."); return status; } int constructing = (Integer) buildingBase.get(""); if (constructing == 1) { status.put("errorMessage", "Building is under construction."); return status; } DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"), (Long) buildingBase.get("level") + 1); boolean isAllowed = TownHallAction.isAllowedBuilding(buildingBase.get("tid"), uid, (Long) buildingBase.get("level") + 1); if (isAllowed) status.put("status", true); if (nextLevelConfig == null) { status.put("errorMessage", "Next level of building is not present."); } buildingBase.put("is_upgrading", 1); buildingBase.put("upgrade_start_time", time); if (new UserAction().updateUserResource(uid, nextLevelConfig.get("build_cost"), (String) nextLevelConfig.get("build_resource"), "decrease")) { if (buildingBase.get("tid").toString().equals(collectionResource[0]) || buildingBase.get("tid").toString().equals(collectionResource[1])) { long lastCollectionTime = time + ApplicationSettings.convertToTimeStamp(nextLevelConfig); if (buildingBase.containsField("buildingResources")) { List<BuildingResource> buildingResources = (List<BuildingResource>) buildingBase .get("buildingResources"); if (buildingResources != null && buildingResources.size() > 0) { BuildingResource resource = buildingResources.get(0); resource.put("last_collection_time", lastCollectionTime); // update resource object here. } } } // update building base here } else { status.put("errorMessage", "Building Not Found."); } } return status; }
From source file:com.aw.app.action.TroopsAction.java
/** * /* w ww. j a v a 2 s.co m*/ * @param data * @return */ public Map upgradeTroops(Map data) { Map state = new HashMap(); long time = new Date().getTime(); DBObject userTroopsDetail = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops_details") .findOne(new BasicDBObject("uid", data.get("uid"))); if (userTroopsDetail == null) { userTroopsDetail = new UserTroopsDetails(data); userTroopsDetail.put("titan_level", 1); MongoDbUtil.saveCollection("aw_user_troops_details", userTroopsDetail); } String tid = data.get("tid").toString(); String fildName = tid.replace("TID_", ""); long level = (Long) userTroopsDetail.get(fildName) + 1; DBObject troopsConfig = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration") .findOne(new BasicDBObject("level", level).append("tid", data.get("tid"))); if (troopsConfig != null) { long userResource = new UserAction().getUserResource(data.get("uid"), troopsConfig.get("upgrade_resource").toString()); long upgradeCost = (Long) troopsConfig.get("upgrade_cost"); DBObject userLabBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject("uid", data.get("uid")).append("tid", "TID_BUILDING_TAVERN")); BuildingArmy userLab = null; if (userLabBase == null || !userLabBase.containsField("buildingArmies")) { state.put("errorMessage", "You need research laboratary to upgrade any troop."); return state; } else { List<BuildingArmy> buildingArmies = (List<BuildingArmy>) userLabBase.get("buildingArmies"); userLab = buildingArmies.get(0); } long labLevel = (Long) troopsConfig.get("laboratory_level"); long baseLevel = (Long) userLabBase.get("level"); if (userLab != null && userLab.containsField("upgrading_character_tid") && !"".equals(userLab.getString("upgrading_character_tid")) && userLab.getLong("research_start_time") > 0) { state.put("errorMessage", "Already another troop/spell is on the research."); } else if (labLevel > baseLevel) { String errorMessage = "This troop can be upgraded in research lab, Research Lab of lv-'" + labLevel + "\" is required."; state.put("errorMessage", errorMessage); } else if (userResource >= upgradeCost) { userLab.put("upgrading_character_tid", data.get("tid")); userLab.put("research_start_time", time); //update userLab here } else if (userResource < upgradeCost) { String errorMessage = "Upgrade resource('" + troopsConfig.get("upgrade_resource") + "') is not enough in the storage."; state.put("errorMessage", errorMessage); } } else { state.put("errorMessage", "Troop Configuration is not available."); } return state; }
From source file:com.aw.app.action.UserAction.java
/** * // w w w . j a v a2 s .c o m * @param uid * @param level */ public void unlockUserTroops(Object uid, Object level) { Map tidLevel = new TroopsAction().getTroopsTidLevelForBarrack(level); DBObject userTroopsDetail = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops_details") .findOne(new BasicDBObject().append("uid", uid)); if (userTroopsDetail == null) { userTroopsDetail = this.createUserTroopDetail(uid); } if (tidLevel != null && !tidLevel.isEmpty()) { Set keys = tidLevel.keySet(); Iterator keyIterator = keys.iterator(); while (keyIterator.hasNext()) { String key = (String) keyIterator.next(); long value = (Long) tidLevel.get(key); String fieldName = key.replace("TID_", "").concat("_level").toLowerCase(); long utdValue = (Long) userTroopsDetail.get(fieldName); if (value > utdValue) userTroopsDetail.put(fieldName, value); BasicDBObject updateUser = new BasicDBObject("$set", new BasicDBObject(fieldName, userTroopsDetail.get(fieldName))); BasicDBObject query = new BasicDBObject().append("uid", uid); WriteResult result = MongoDbUtil.updateCollection("aw_user_troops_details", query, updateUser); } } }
From source file:com.aw.services.AncientWarServiceImpl.java
/** * /*w w w . ja va 2s .co m*/ * @param key * @return * @throws JSONException */ @Override public JSONObject getSystemSettingByKey(String key) throws JSONException { DBCollection collection = MongoDbUtil.getCollection("ancient_war", "aw_system_settings"); DBObject dbObject = new BasicDBObject(); dbObject.put("key", key); DBObject filteredObject = collection.findOne(dbObject); JSONObject json = new JSONObject(); if (filteredObject != null) { json.put("status", filteredObject.get("value").toString()); } else { json.put("status", ""); } return json; }