List of usage examples for com.mongodb DBCursor hasNext
@Override public boolean hasNext()
From source file:com.deafgoat.ml.prognosticator.Mongo2CSV.java
License:Apache License
/** * Writes the collectionection to a CSV file using the MongoExport command. * //from w w w.j a v a 2s . c o m * @param filename * Th name of the file to write the collectionection to * @throws IOException * If unable to write to file * @throws FileNotFoundException * If the file is not found */ public void writeCSV(String filename) throws IOException, FileNotFoundException { BufferedWriter out = new BufferedWriter(new FileWriter(filename)); DBCursor cursor = _collection.find(); out.write(Mongo2CSV.join(Arrays.asList(_fields), _delimeter) + "\n"); ArrayList<String> entry = new ArrayList<String>(); DBObject obj; while (cursor.hasNext()) { obj = cursor.next(); for (String field : _fields) { entry.add((String) obj.get(field)); } out.write(join(entry, _delimeter) + "\n"); entry.clear(); } cursor.close(); out.close(); }
From source file:com.deafgoat.ml.prognosticator.MongoImport.java
License:Apache License
/** * Returns all JSON configurations found in the collection. * //from ww w . j a v a 2 s . c o m * @return The set of all JSON configurations found. Null if none is found. * @throws JSONException * If the configuration can not be converted to a JSONObject */ public ArrayList<JSONObject> getAllConfigurations() throws JSONException { DBCursor cursor = _collection.find(); ArrayList<JSONObject> configFiles = new ArrayList<JSONObject>(); try { while (cursor.hasNext()) { configFiles.add(new JSONObject(cursor.next().toString())); } } finally { cursor.close(); } return configFiles; }
From source file:com.deftlabs.lock.mongo.impl.LockDao.java
License:Apache License
/** * Check for expired/inactive/dead locks and unlock. *///from w ww . j a v a2s .co m static void expireInactiveLocks(final MongoClient pMongo, final DistributedLockSvcOptions pSvcOptions) { // Adjust the time buffer to make sure we do not have small time issues. final long queryServerTime = getServerTime(pMongo, pSvcOptions); final BasicDBObject query = new BasicDBObject(LockDef.STATE.field, LockState.LOCKED.code()); query.put(LockDef.LOCK_TIMEOUT_TIME.field, new BasicDBObject(LT, new Date(queryServerTime))); final DBCursor cur = getDbCollection(pMongo, pSvcOptions).find(query).batchSize(10); try { while (cur.hasNext()) { final BasicDBObject lockDoc = (BasicDBObject) cur.next(); final ObjectId lockId = (ObjectId) lockDoc.get(LockDef.LOCK_ID.field); final String lockName = lockDoc.getString(LockDef.ID.field); final long serverTime = getServerTime(pMongo, pSvcOptions); final BasicDBObject toSet = new BasicDBObject(); toSet.put(LockDef.LIBRARY_VERSION.field, null); toSet.put(LockDef.UPDATED.field, new Date(serverTime)); toSet.put(LockDef.LOCK_ACQUIRED_TIME.field, null); toSet.put(LockDef.LOCK_TIMEOUT_TIME.field, null); toSet.put(LockDef.LOCK_ID.field, null); toSet.put(LockDef.STATE.field, LockState.UNLOCKED.code()); toSet.put(LockDef.OWNER_APP_NAME.field, null); toSet.put(LockDef.OWNER_ADDRESS.field, null); toSet.put(LockDef.OWNER_HOSTNAME.field, null); toSet.put(LockDef.OWNER_THREAD_ID.field, null); toSet.put(LockDef.OWNER_THREAD_NAME.field, null); toSet.put(LockDef.OWNER_THREAD_GROUP_NAME.field, null); toSet.put(LockDef.LOCK_ATTEMPT_COUNT.field, 0); toSet.put(LockDef.INACTIVE_LOCK_TIMEOUT.field, null); final BasicDBObject timeoutQuery = new BasicDBObject(LockDef.ID.field, lockName); timeoutQuery.put(LockDef.LOCK_ID.field, lockId); timeoutQuery.put(LockDef.STATE.field, LockState.LOCKED.code()); timeoutQuery.put(LockDef.LOCK_TIMEOUT_TIME.field, new BasicDBObject(LT, new Date(serverTime))); final BasicDBObject previousLockDoc = (BasicDBObject) getDbCollection(pMongo, pSvcOptions) .findAndModify(timeoutQuery, new BasicDBObject(LockDef.INACTIVE_LOCK_TIMEOUT.field, 1), null, false, new BasicDBObject(SET, toSet), false, false); if (previousLockDoc == null) continue; if (!pSvcOptions.getEnableHistory()) continue; // Insert the history state. LockHistoryDao.insert(pMongo, lockName, pSvcOptions, previousLockDoc.getInt(LockDef.INACTIVE_LOCK_TIMEOUT.field), serverTime, LockState.LOCKED, lockId, true); } } finally { if (cur != null) cur.close(); } }
From source file:com.deliveronthego.CommonAPI.java
@POST @Path("/findDrivers") @Consumes(MediaType.APPLICATION_JSON)//from ww w .j a va2s. c om @Produces(MediaType.APPLICATION_JSON) public Response doDeliveryDetails(String deliver) { System.out.println("inside delivery details"); String message; GraphTraversal g = new GraphTraversal(); try { System.out.println("inside delivery details"); JSONObject deliverJson = new JSONObject(deliver); boolean var = new DbConnection().deliver(deliverJson.getString("emailId"), Double.valueOf(deliverJson.getString("pickupLatitude")), Double.valueOf(deliverJson.getString("pickupLongitude")), Double.valueOf(deliverJson.getString("dropOffLatitude")), Double.valueOf(deliverJson.getString("dropOffLongitude")), deliverJson.getInt("length"), deliverJson.getInt("breadth"), deliverJson.getInt("width")); System.out.println("after delivery details"); if (var) { System.out.println("in success"); message = "Delivery Details Inserted successfully"; JSONObject statusMessage = new JSONObject(); statusMessage.put("message", message); String res = g.findDriver(deliverJson.getString("emailId"), deliverJson.getDouble("pickupLatitude"), deliverJson.getDouble("pickupLongitude"), deliverJson.getDouble("dropOffLatitude"), deliverJson.getDouble("dropOffLongitude")); System.out.println(res); JSONArray jarr = new JSONArray(res); for (int j = 0; j < jarr.length(); j++) { JSONObject json = new JSONObject(jarr.get(j).toString()); String driverId = json.get("driverId").toString(); System.out.println(driverId); mongoclient = getConnection(); DB db = mongoclient.getDB("deliveronthego"); DBCollection driverSourceDetails = db.getCollection("login"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("emailId", driverId); whereQuery.put("userType", "Driver"); DBCursor cursor = driverSourceDetails.find(whereQuery); if (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); String regId = obj.getString("regId"); String[] gcmIds = regId.split("\\="); System.out.println(gcmIds[1]); String apiKey = "AIzaSyDzXEIMFY3EGbJ4mjc9xBYyeakjggxuTC0"; Content content = createContent(gcmIds[1], deliverJson.getString("emailId")); DotgGCMPost.post(apiKey, content); DBCollection selection = db.getCollection("selection"); BasicDBObject sel = new BasicDBObject() .append("userEmailId", deliverJson.getString("emailId")) .append("driverEmailId", driverId).append("Accepted", "No"); selection.insert(sel); throw new JSONException("gi"); } else { //System.out.println("cursor=="+cursor.toString()); throw new JSONException("No email found"); } } return Response.status(200).entity(statusMessage).build(); } else { System.out.println("in fail"); message = "Delivery Details Insertion Failed"; JSONObject statusMessage = new JSONObject(); statusMessage.put("message", message); return Response.status(404).entity(statusMessage).build(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } message = "Delivery Details Insertion Failed"; return Response.status(404).entity(message).build(); }
From source file:com.deliveronthego.DbConnection.java
public String customerSignup(String firstName, String lastName, String emailId, String password, int phoneNumber, String regId) { mongoclient = getConnection();//from www . j a va 2 s .c om @SuppressWarnings("deprecation") DB database = mongoclient.getDB("deliveronthego"); DBCollection customerSignUpInfo = database.getCollection("login"); if (emailId.contains("@") && (password.length() <= 8) && (String.valueOf(phoneNumber).length() == 10)) { BasicDBObject customerSignUpInfoObj = new BasicDBObject("emailId", emailId).append("password", password); DBCursor customerSignUpInfoCur = customerSignUpInfo.find(customerSignUpInfoObj); if (customerSignUpInfoCur.hasNext()) { return "Customer Sign Up Info Already Exists"; } else { customerSignUpInfoObj.append("firstName", firstName).append("lastName", lastName) .append("phoneNumber", phoneNumber).append("userType", "User").append("regId", regId); customerSignUpInfo.insert(customerSignUpInfoObj); return "Customer Signup Info inserted successfully"; } } else { return "Customer SignUp Info failed to insert"; } }
From source file:com.deliveronthego.DbConnection.java
public String driverSignup(String firstName, String lastName, String driverLicense, String emailId, String password, int phoneNumber, String regId) { mongoclient = getConnection();//w w w .j a v a 2s. c om DB db = mongoclient.getDB("deliveronthego"); DBCollection driverSignUpInfo = db.getCollection("login"); if (emailId.contains("@") && (password.length() <= 8) && (String.valueOf(phoneNumber).length() == 10) && !driverLicense.isEmpty()) { BasicDBObject driverSignUpInfoObj = new BasicDBObject("driverLicense", driverLicense) .append("emailId", emailId).append("password", password); DBCursor driverSignUpInfoCur = driverSignUpInfo.find(driverSignUpInfoObj); if (driverSignUpInfoCur.hasNext()) { return "Driver Sign Up Info Already Exists"; } else { driverSignUpInfoObj.append("firstName", firstName).append("lastName", lastName) .append("phoneNumber", phoneNumber).append("userType", "Driver").append("regId", regId); driverSignUpInfo.insert(driverSignUpInfoObj); return "Driver Sign Up Info Inserted Successfully"; } } else { return "Driver SignUp Info failed to insert"; } }
From source file:com.deliveronthego.DbConnection.java
public boolean login(String emailId, String password, String userType) { mongoclient = getConnection();// ww w .j a va 2 s. c o m @SuppressWarnings("deprecation") DB db = mongoclient.getDB("deliveronthego"); DBCollection login = db.getCollection("login"); if (emailId.contains("@")) { BasicDBObject logInObj = new BasicDBObject(); logInObj.put("emailId", emailId); DBCursor logInCursor = login.find(logInObj); while (logInCursor.hasNext()) { logInCursor.next(); DBObject userDetailObj = logInCursor.curr(); if (userDetailObj != null) { String logInPassword = userDetailObj.get("password").toString(); String loginUserType = userDetailObj.get("userType").toString(); System.out.println(logInPassword); if ((logInPassword != null) && (logInPassword.equalsIgnoreCase(password)) && (loginUserType != null) && (loginUserType.equalsIgnoreCase(userType))) { return true; } else { return false; } } else { return false; } } return true; } else { return false; } }
From source file:com.deliveronthego.DbConnection.java
public String location(String date, double transitionLatitude, double transitionLongitude, double stopLatitude, double stopLongitude, String driverId) { mongoclient = getConnection();//w w w.j a v a2 s .c o m @SuppressWarnings("deprecation") DB db = mongoclient.getDB("deliveronthego"); DBCollection location = db.getCollection("location"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("driverID", driverId); DBCursor locationCursor = location.find(whereQuery); boolean updateFlag = false; while (locationCursor.hasNext()) { locationCursor.next(); DBObject locDB = locationCursor.curr(); System.out.println(locDB); String dateStr = locDB.get("Date").toString(); if (dateStr.equals(date)) { location.update(new BasicDBObject("driverID", driverId), new BasicDBObject("$set", new BasicDBObject("transitionLatitude", transitionLatitude) .append("transitionLongitude", transitionLongitude))); Double previousStopLatitude = (Double) locDB.get("stopLatitude"); Double previousStopLongitude = (Double) locDB.get("stopLongitude"); System.out.println("previousStopLatitude: " + previousStopLatitude); System.out.println("previousStopLongitude: " + previousStopLongitude); /*if((previousStopLatitude==0.0) && (previousStopLongitude==0.0)) { location.update(new BasicDBObject("driverID", driverId), new BasicDBObject("$set", new BasicDBObject("stopLatitude", stopLatitude).append("stopLongitude", stopLongitude))); }*/ } updateFlag = true; } if (!updateFlag) { BasicDBObject locationObj = new BasicDBObject("Date", date.toString()) .append("transitionLatitude", transitionLatitude) .append("transitionLongitude", transitionLongitude).append("stopLatitude", stopLatitude) .append("stopLongitude", stopLongitude).append("driverID", driverId); location.insert(locationObj); return "Location Details Inserted Sucessfully"; } else { return "Location Details Updated"; } }
From source file:com.deliveronthego.DbConnection.java
public String transcationNotification(String driverID, Boolean pickedUp, Boolean delivered) { mongoclient = getConnection();/*from ww w. j av a 2 s . c o m*/ @SuppressWarnings("deprecation") DB db = mongoclient.getDB("deliveronthego"); DBCollection notification = db.getCollection("notification"); BasicDBObject notificationObj = new BasicDBObject(); notificationObj.append("driverID", driverID); Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); DBCursor notificationCursor = notification.find(notificationObj); if (pickedUp && !delivered) { notificationObj.append("pickedUp", pickedUp.toString()).append("delivered", delivered.toString()) .append("date", cal.toString()); notification.insert(notificationObj); return "New Transaction Data inserted"; } else { if (notificationCursor.hasNext()) { notificationCursor.next(); DBObject notifyObj = notificationCursor.curr(); Date currentDateInDatabase = (Date) notifyObj.get("date"); if (!(boolean) notifyObj.get("delivered") && currentDateInDatabase.before(date)) { notification.update(new BasicDBObject("driverID", driverID), new BasicDBObject("$set", new BasicDBObject("delivered", delivered.toString()))); return "Transaction Completed"; } else { return "Transaction failed to update"; } } else { return "Transaction failed"; } } }
From source file:com.dhamacher.addressbook.DBController.java
License:Open Source License
/** * This methods provides the data that is then published in the JList * component of the user view [Main.class] * * @return An object array for the JList component in the user view *//*from w w w. j a va 2s .com*/ protected Object[] getContacts() { if (collection.count() == 0) { Object[] data = { "" }; return data; } else { /* Use the database cursor to iterate trough the collection */ DBCursor cursor = collection.find(); /* New array based on the size of the collection */ Object[] data = new Object[(int) collection.count()]; int i = 0; while (cursor.hasNext()) { DBObject document = cursor.next(); data[i] = document.get("first_name") + " " + document.get("last_name"); i++; } cursor.close(); return data; } }