List of usage examples for com.mongodb DBCursor hasNext
@Override public boolean hasNext()
From source file:com.aw.app.action.TroopsAction.java
/** * //from w w w .jav a2s. c om * @param level * @return */ public Map getTroopsTidLevelForBarrack(Object level) { DBCursor cursor = this.getTroopsForBarrackLevel(level); Map data = new HashMap(); while (cursor.hasNext()) { DBObject object = cursor.next(); data.put(object.get("tid"), object.get("level")); } return data; }
From source file:com.aw.app.action.UserAction.java
/** * // w w w . j a v a 2s. co m * @param uid * @param buildCost * @param type * @param action * @return */ public boolean updateUserResource(Object uid, Object buildCost, String type, String action) { boolean state = false; boolean townHallPass = false; DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .findOne(new BasicDBObject("uid", uid)); String tid = BuildingAction.resourceStorage[0]; String maxField = ""; String townHallStorageField = ""; long amount = 0; if (user != null) { type = type.toLowerCase(); if ("spens".equalsIgnoreCase(type)) { if ("increase".equalsIgnoreCase(action)) { long spens = (Long) user.get("spens"); long cost = (Long) buildCost; spens += cost; BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("spens", spens)); WriteResult result = MongoDbUtil.updateCollection("aw_user", new BasicDBObject("uid", uid), update); return result.getLastError().ok(); } else { long spens = (Long) user.get("spens"); long cost = (Long) buildCost; spens -= cost; BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("spens", spens)); WriteResult result = MongoDbUtil.updateCollection("aw_user", new BasicDBObject("uid", uid), update); return result.getLastError().ok(); } } else if ("gold".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[0]; maxField = "max_stored_gold"; townHallStorageField = "gold_stored"; } else if ("mithril".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[1]; maxField = "max_stored_mithril"; townHallStorageField = "mithril_stored"; } else if ("dark mithril".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[2]; maxField = "max_stored_dark_mithril"; townHallStorageField = "dark_mithril_stored"; } DBCursor buildings = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .find(new BasicDBObject().append("uid", uid).append("tid", tid)); if ("increase".equalsIgnoreCase(action)) { DBObject townHallBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid)); BuildingBase buildingBase = new BuildingBase(townHallBase.toMap()); if (buildingBase.containsField("buildingTownhalls")) { BuildingTownhall townHall = buildingBase.getBuildingTownhalls().get(0); if (townHall.getLong("townHallStorageField") < townHall.getLong("maxField")) { amount = (Long) buildCost; long temp = amount; long total = townHall.getLong("townHallStorageField") + amount; if (total > townHall.getLong("maxField")) { amount = amount - townHall.getLong("maxField") - townHall.getLong("townHallStorageField"); total = townHall.getLong("maxField"); } else { amount = 0; } townHall.put("townHallStorageField", total); // update BasicDBObject query = new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("buildingTownhalls", buildingBase.getBuildingTownhalls())); WriteResult result = MongoDbUtil.updateCollection("aw_building_base", query, update); townHallPass = result.getLastError().ok(); if (!result.getLastError().ok()) amount = temp; } } } long subAmount = amount; int resourceCount = 0; if (buildings != null) { while (buildings.hasNext()) { DBObject building = buildings.next(); BuildingBase bb = new BuildingBase(building.toMap()); List<BuildingResource> resources = (List<BuildingResource>) bb.get("buildingResources"); BuildingResource resource = new BuildingResource(resources.get(0).toMap()); if ("decrease".equalsIgnoreCase(action)) { if (resource.getLong("resource_stored") > 0 & resource.getLong("resource_stored") <= subAmount) { subAmount -= resource.getLong("resource_stored"); resource.put("resource_stored", 0); // Update here... resourceCount++; } else if (resource.getLong("resource_stored") > 0 & resource.getLong("resource_stored") > subAmount) { resource.put("resource_stored", resource.getLong("resource_stored") - subAmount); subAmount = 0; // Update here resourceCount++; } } else if ("increase".equalsIgnoreCase(action)) { if (subAmount > 0) { long total = resource.getLong("resource_stored") + subAmount; if (resource.getLong("$maxField") == total) { resource.put("resource_stored", total); // update here subAmount = 0; state = true; } else if (total > resource.getLong("$maxField") && resource.getLong("$maxField") > resource.getLong("resource_stored")) { long difference = resource.getLong("$maxField") - resource.getLong("resource_stored"); subAmount -= difference; resource.put("resource_stored", resource.getLong("$maxField")); resourceCount++; // update resouce } } } } } /**** First Fill the Town Hall for increase resource action ***/ if (subAmount > 0 && "decrease".equalsIgnoreCase(action)) { DBObject townHallBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid)); List<BuildingTownhall> townHallList = (List<BuildingTownhall>) townHallBase .get("buildingTownhalls"); if (townHallList != null) { BuildingTownhall townhall = townHallList.get(0); townhall.put("townHallStorageField", townhall.getLong("townHallStorageField") - subAmount); if (townhall.getLong("townHallStorageField") < 0) { state = false; } else { // update town hall townHallPass = true; state = true; } } } } if (townHallPass && !state) state = townHallPass; return state; }
From source file:com.aw.app.action.UserAction.java
/** * /*from w ww . ja v a 2 s.c o m*/ * @param uid * @param name * @return */ public Map updateUserName(long uid, String name) { Map state = new HashMap(); DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .findOne(new BasicDBObject().append("uid", uid)); if (user != null) { DBCursor cursor = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .find(new BasicDBObject().append("name", name)); boolean nameInUse = false; while (cursor.hasNext()) { DBObject nextUser = cursor.next(); long nextUserId = (Long) nextUser.get("uid"); if (nextUserId != uid) { nameInUse = true; break; } } if (!nameInUse) { BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("name", name)); BasicDBObject query = new BasicDBObject("uid", uid); WriteResult result = MongoDbUtil.updateCollection("aw_user", query, update); if (!result.getLastError().ok()) { state.put("errorMessage", "User update error."); } } else { state.put("errorMessage", "Name is already in use."); } } return state; }
From source file:com.aw.app.action.UserAction.java
/** * /*w w w . j a va 2s.c o m*/ * @param uid * @param type * @return */ public Map getUserBattleLogs(long uid, String type) { Map result = new HashMap(); result.put("status", false); result.put("message", ""); int count = 5; BasicDBObject query = new BasicDBObject(); if (uid > 0) { if ("attack".equalsIgnoreCase(type)) { query.put("attacker_id", uid); query.put("count", BattleAction.attackLogCount); } else if ("defense".equalsIgnoreCase(type)) { query.put("defender_id", uid); query.put("count", BattleAction.defenseLogCount); } else { query.put("attacker_id", uid); query.put("count", BattleAction.attackLogCount); } DBCursor cursor = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_battle") .find(query.append("limit", count)); List battles = new ArrayList(); while (cursor.hasNext()) { DBObject battle = cursor.next(); Map detail = new BattleAction().getBattleDetail((Long) battle.get("battled_id")); battles.add(detail); } result.put("dataset", battles); result.put("status", true); } else { result.put("errorMessage", "uid is missing."); } return result; }
From source file:com.aw.app.action.UserAction.java
/** * //from ww w . j a v a 2 s.co m * @param uid * @param type * @return */ public long getUserResource(Object uid, String type) { DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user") .findOne(new BasicDBObject().append("uid", uid)); long amount = 0; if (user != null) { String tid = ""; String maxField = ""; String townHallStorageField = ""; if ("spens".equalsIgnoreCase(type)) { return (Long) user.get("spens"); } else if ("gold".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[0]; maxField = "max_stored_gold"; townHallStorageField = "gold_stored"; } else if ("mithril".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[1]; maxField = "max_stored_mithril"; townHallStorageField = "mithril_stored"; } else if ("dark mithril".equalsIgnoreCase(type)) { tid = BuildingAction.resourceStorage[2]; maxField = "max_stored_dark_mithril"; townHallStorageField = "dark_mithril_stored"; } DBCursor cursor = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .find(new BasicDBObject().append("uid", uid).append("tid", tid)); DBObject townHallBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base") .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid)); BuildingTownhall townHall = ((List<BuildingTownhall>) townHallBase.get("buildingTownhalls")).get(0); amount = townHall.getLong(townHallStorageField); while (cursor.hasNext()) { DBObject building = cursor.next(); BuildingResource resource = ((List<BuildingResource>) building.get("buildingResources")).get(0); amount += resource.getLong("resource_stored"); } } return amount; }
From source file:com.aw.services.AncientWarServiceImpl.java
/** * //from w w w . jav a 2 s.com * @param userId * @return * @throws JSONException */ @Override public JSONObject browseUserVillage(Long userId) throws JSONException { DBCollection coll = MongoDbUtil.getCollection("ancient_war", "aw_user"); DBObject query = QueryBuilder.start("uid").is(userId).get(); DBCursor cursor = coll.find(query); JSONObject status = new JSONObject(); while (cursor.hasNext()) { DBObject user = cursor.next(); if (user != null) { Long uid = (Long) user.get("uid"); JSONObject userVillage = getUserVillage(uid, "visit"); return userVillage; } else { status.put("status", false); status.put("message", "No Village found"); return status; } } return status; }
From source file:com.aw.util.MongoDbUtil.java
public static Object findMaxValueForKey(String collection, String key) { DBCollection table = getCollection(defaultDBName, collection); BasicDBObject query = new BasicDBObject(); query.put(key, -1);/*from ww w . j av a2s.c om*/ DBCursor cursor = table.find().sort(query).limit(1); Object value = null; while (cursor.hasNext()) { DBObject object = cursor.next(); value = object.get(key); } return value; }
From source file:com.ayu.filter.DbListner.java
License:Open Source License
/** * @see ServletContextListener#contextInitialized(ServletContextEvent) *///from w w w . ja v a 2s . c o m public void contextInitialized(ServletContextEvent arg0) { MongoClient mongoClient = null; try { mongoClient = new MongoClient(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } DB db = mongoClient.getDB("test"); DBCollection coll; coll = db.getCollection("test"); DBCursor cursor = coll.find(); while (cursor.hasNext()) { String ip = (String) cursor.next().get("Ip-Address"); if (arg0.getServletContext().getAttribute(ip) == null) { arg0.getServletContext().setAttribute(ip, ip); } } }
From source file:com.badger.observationmongotest.execute.Main.java
public static void main(String[] args) throws ParseException, UnknownHostException, IOException { List<Observation> observations = new LinkedList(); Customer customer = new Customer(); customer.setName("Mr Customer"); customer.setId("cid123"); customer.setBackOfficeId("boid123"); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String dateStr = "2015/08/10"; Date date = format.parse(dateStr); CustomerObservationReport report = new CustomerObservationReport(customer); MonthlyProductOrderCountIssue productOrderCountIssue = new MonthlyProductOrderCountIssue(); productOrderCountIssue.setActionRequired(true); productOrderCountIssue.setCatagory("product issue"); productOrderCountIssue.setCurrentYear(2015); productOrderCountIssue.setCurrentYearValue(30); productOrderCountIssue.setCustomerId(customer.getId()); productOrderCountIssue.setDate(date); productOrderCountIssue.setDrop(50.0); productOrderCountIssue.setLastYearValue(60); productOrderCountIssue.setMonth("Feb"); productOrderCountIssue.setObservationId("oid123"); productOrderCountIssue.setPreviousYear(2014); productOrderCountIssue.setProductName("cool product"); productOrderCountIssue.setScore(4500); productOrderCountIssue.setType("monthy order count"); MonthlyProductOrderCountIssue monthlyProductOrderCountIssue = new MonthlyProductOrderCountIssue(); monthlyProductOrderCountIssue.setActionRequired(true); monthlyProductOrderCountIssue.setCatagory("product issue"); monthlyProductOrderCountIssue.setCurrentYear(2015); monthlyProductOrderCountIssue.setCurrentYearValue(30); monthlyProductOrderCountIssue.setCustomerId(customer.getId()); monthlyProductOrderCountIssue.setDate(date); monthlyProductOrderCountIssue.setDrop(50.0); monthlyProductOrderCountIssue.setLastYearValue(60); monthlyProductOrderCountIssue.setMonth("March"); monthlyProductOrderCountIssue.setObservationId("oid456"); monthlyProductOrderCountIssue.setPreviousYear(2014); monthlyProductOrderCountIssue.setProductName("cool product"); monthlyProductOrderCountIssue.setScore(4500); monthlyProductOrderCountIssue.setType("monthy order count"); MonthlyProductRevenueIssue monthlyProductRevenueIssue = new MonthlyProductRevenueIssue(); monthlyProductRevenueIssue.setActionRequired(true); monthlyProductRevenueIssue.setCatagory("product issue"); monthlyProductRevenueIssue.setCurrentYear(2015); monthlyProductRevenueIssue.setCurrentYearValue(3000.00); monthlyProductRevenueIssue.setCustomerId(customer.getId()); monthlyProductRevenueIssue.setDate(date); monthlyProductRevenueIssue.setDrop(50.0); monthlyProductRevenueIssue.setLastYearValue(6000.00); monthlyProductRevenueIssue.setMonth("Dec"); monthlyProductRevenueIssue.setObservationId("oid3445"); monthlyProductRevenueIssue.setPreviousYear(2014); monthlyProductRevenueIssue.setProductName("test product"); monthlyProductRevenueIssue.setScore(5667); monthlyProductRevenueIssue.setType("monthly product ervenue"); MonthlyRevenueIssue monthlyRevenueIssue = new MonthlyRevenueIssue(); monthlyRevenueIssue.setActionRequired(true); monthlyRevenueIssue.setCatagory("Reveue Issue"); monthlyRevenueIssue.setCurrentValue(3000); monthlyRevenueIssue.setCurrentYear(2015); monthlyRevenueIssue.setCustomerId(customer.getId()); monthlyRevenueIssue.setDate(date);/* ww w .j a v a 2 s . co m*/ monthlyRevenueIssue.setDrop(45.0); monthlyRevenueIssue.setMonth("Nov"); monthlyRevenueIssue.setObservationId("oid4646646"); monthlyRevenueIssue.setPreviousValue(5000); monthlyRevenueIssue.setPreviousYear(2014); monthlyRevenueIssue.setScore(5600); monthlyRevenueIssue.setType("monthly revenue issue"); observations.add(monthlyRevenueIssue); observations.add(monthlyProductRevenueIssue); observations.add(monthlyProductOrderCountIssue); observations.add(productOrderCountIssue); report.setAccountId("acid123"); report.addObservations(observations); // BasicDBObject documentDetail = new BasicDBObject(); /** * documentDetail.put("accountId", report.getAccountId()); * documentDetail.put("customerId", report.getCustomerId()); * documentDetail.put("Observations", * report.getObservations().toString()); * */ MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("ObservationReports"); DBCollection col = db.getCollection("reports"); Gson gson = new Gson(); String json = gson.toJson(report); DBObject dbObject = (DBObject) JSON.parse(json); col.insert(dbObject); //DBObject query = BasicDBObjectBuilder.start().add("_id", report.getId()).get(); DBCursor cursor = col.find(); while (cursor.hasNext()) { System.out.println("First Cursor: " + cursor.next().get("observations")); } while (cursor.hasNext()) { DBObject dbobj = cursor.next(); Map map = dbObject.toMap(); CustomerObservationReport report2 = new CustomerObservationReport(customer); //System.out.println("observations " +dbObject.get("observations")); List<Observation> dbObservations = (List<Observation>) dbObject.get("observations"); report2.setAccountId(report.getAccountId()); report2.setCustomerId(report.getCustomerName()); report2.setBackOfficeId(report.getBackOfficeId()); report2.setObservations((dbObservations)); System.out.println(report2.getClass().getTypeName()); } }
From source file:com.berkgokden.mongodb.MongoMapStore.java
License:Open Source License
public Map loadAll(Collection keys) { Map map = new HashMap(); BasicDBList dbo = new BasicDBList(); for (Object key : keys) { dbo.add(new BasicDBObject("_id", key)); }// w ww . j a va 2s . c o m BasicDBObject dbb = new BasicDBObject("$or", dbo); DBCursor cursor = coll.find(dbb); while (cursor.hasNext()) { try { DBObject obj = cursor.next(); Class clazz = Class.forName(obj.get("_class").toString()); map.put(obj.get("_id"), converter.toObject(clazz, obj)); } catch (ClassNotFoundException e) { logger.log(Level.WARNING, e.getMessage(), e); } } return map; }