List of usage examples for com.mongodb DBObject containsField
boolean containsField(String s);
From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSFile.java
License:Apache License
public void validate() { if (_fs == null) throw new MongoException("no _fs"); if (_md5 == null) throw new MongoException("no _md5 stored"); DBObject cmd = new BasicDBObject("filemd5", _id); cmd.put("root", _fs._bucketName); DBObject res = _fs._db.command(cmd); if (res != null && res.containsField("md5")) { String m = res.get("md5").toString(); if (m.equals(_md5)) return; throw new MongoException("md5 differ. mine [" + _md5 + "] theirs [" + m + "]"); }/*from ww w . j ava 2 s .c o m*/ // no md5 from the server throw new MongoException("no md5 returned from server: " + res); }
From source file:co.edu.udea.runtwebapp.model.entities.Person.java
public static Person entityFromDBObject(DBObject dbObject) { Person person = null;/*ww w . j a v a 2s .c o m*/ if (dbObject != null) { person = new Person(); if (dbObject.containsField(ID)) { person.setId(dbObject.get(ID).toString().trim()); } if (dbObject.containsField(DOCUMENT_TYPE)) { person.setDocumentType((String) dbObject.get(DOCUMENT_TYPE)); } if (dbObject.containsField(ID_NUMBER)) { person.setIdNumber((String) dbObject.get(ID_NUMBER)); } if (dbObject.containsField(NAME)) { person.setName((String) dbObject.get(NAME)); } if (dbObject.containsField(LASTNAME)) { person.setLastName((String) dbObject.get(LASTNAME)); } if (dbObject.containsField(BIRTHDATE)) { person.setBirthDate((Date) dbObject.get(BIRTHDATE)); } if (dbObject.containsField(EMAIL)) { person.setEmail((String) dbObject.get(EMAIL)); } if (dbObject.containsField(LICENSE_NUMBER)) { person.setLicenseNumber((String) dbObject.get(LICENSE_NUMBER)); } if (dbObject.containsField(VEHICLES)) { BasicDBList basicDBbList = (BasicDBList) dbObject.get(VEHICLES); person.setVehicles(new ArrayList<Vehicle>()); for (Object object : basicDBbList) { person.getVehicles().add(Vehicle.entityFromDBObject((BasicDBObject) object)); } } } return person; }
From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java
License:Open Source License
public InputStream get(String username, Resource resource, char[] password) throws IOException { DBCollection col = mongo.getDataBase().getCollection("contents"); DBObject filter = new BasicDBObject(); filter.put("resource", resource.getId()); DBObject result = col.findOne(filter); if (result.containsField("file")) { String fileName = (String) result.get("file"); File dataFile = new File(fileName); return readFile(dataFile, username, password); } else {/*from w w w . ja v a 2 s . c om*/ byte[] binary = (byte[]) result.get("binary"); return new ByteArrayInputStream(binary); } }
From source file:com.aw.app.action.BattleAction.java
/** * /*from w w w .j av a 2 s .c o m*/ * @param battleId * @return */ public Map getBattleDetail(long battleId) { Map data = new HashMap(); DBObject battle = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_battle") .findOne(new BasicDBObject().append("battle_id", battleId)); if (battle != null) { if (battle.containsField("battleBuildings")) { List<BattleBuilding> battleBuildings = (List<BattleBuilding>) battle.get("battleBuildings"); List battles = new ArrayList(); for (BattleBuilding bb : battleBuildings) { Map bbData = new HashMap(); bbData.put("building_id", bb.get("building_id")); bbData.put("is_destroyed", bb.get("is_destroyed")); bbData.put("resource_lost", bb.get("resource_lost")); battles.add(bbData); } data.put("buildings", battles); } if (battle.containsField("battleTroops")) { List<BattleTroops> battleTroops = (List<BattleTroops>) battle.get("battleTroops"); List troops = new ArrayList(); for (BattleTroops bt : battleTroops) { Map tData = new HashMap(); tData.put("tid", bt.get("tid")); tData.put("count", bt.get("count")); tData.put("level", bt.get("level")); troops.add(tData); } data.put("troops", troops); } } return data; }
From source file:com.aw.app.action.BuildingAction.java
/** * //from w w w .j a v a 2 s .c o m * @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 ww w. ja v a2 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 ww w . j a va2 s. 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 . jav a 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
/** * /*from w w w . ja va 2 s. co m*/ * @param uid * @param buildingId * @param time * @return */ public Map updateBuildingCollection(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) { if (buildingBase.containsField("building_class") && buildingBase.get("building_class").toString().equalsIgnoreCase("Resource") && this.isCollectionBuilding(buildingBase.get("tid"))) { if (buildingBase.containsField("buildingResources")) { List<BuildingResource> buildingResources = (List<BuildingResource>) buildingBase .get("buildingResources"); BuildingResource resource = buildingResources.get(0); if (resource != null) { status = updateBuildingStorageResource(resource, buildingBase.get("tid"), new Date().getTime()); } else { status.put("status", false); status.put("errorMessage", "Resource not found."); } } } else { status.put("errorMessage", "Building is not a resource collector."); } } else { status.put("errorMessage", "Building Not Found."); } return status; }
From source file:com.aw.app.action.BuildingAction.java
/** * // ww w. j a va 2s .com * @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; }