List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:com.AlertMailerWebPage.servlet.GetProcessSpeed.java
public AggregateIterable<Document> getProcessSpeed(String database, String collection, String serverAddress, String req) {//from w w w .j a v a 2 s. c o m Calendar current_min1 = Calendar.getInstance(); Calendar current = Calendar.getInstance(); if (req.equals("count")) { current = Calendar.getInstance(); int currYear = current.get(Calendar.YEAR); int currMonth = current.get(Calendar.MONTH); int currDay = current.get(Calendar.DAY_OF_MONTH); current_min1.set(currYear, 6, 4, 0, 0, 0); current.set(currYear, 6, 4, 23, 59, 59); } else if (req.equals("speed")) { int currYear = current.get(Calendar.YEAR); int currMonth = current.get(Calendar.MONTH); int currDay = current.get(Calendar.DAY_OF_MONTH); current_min1.set(currYear, 6, 4, 0, 0, 0); current.set(currYear, 6, 4, 23, 59, 59); } // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017)); MongoClient mongoClient = new MongoClient(); AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection) .aggregate(asList( new Document("$match", new Document("isMailSent", "Y") .append("$or", asList(new Document("alertType", "PRS"), new Document("alertType", "PSM"), new Document("alertType", "RENTAL"))) .append("recordTime", new Document("$gte", current_min1.getTimeInMillis()).append("$lte", current.getTimeInMillis()))), new Document("$group", new Document("_id", "$alertType").append("count", new Document("$sum", 1))))); return iterable_6_9_p; }
From source file:com.AlertMailerWebPage.servlet.GetPRSCompilation.java
public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress, String time) {/*from w ww . j ava2 s . c o m*/ // list.clear(); String groupBy = "$vmfilename"; Calendar previousTime = Calendar.getInstance(); previousTime.set(Calendar.HOUR_OF_DAY, 0); previousTime.set(Calendar.MINUTE, 0); previousTime.set(Calendar.SECOND, 0); previousTime.set(Calendar.MILLISECOND, 0); previousTime.set(2016, 6, 7, 0, 0, 0); previousTime.set(Calendar.MILLISECOND, 0); Calendar todayCalendar = Calendar.getInstance(); todayCalendar.set(2016, 6, 7, 23, 59, 59); todayCalendar.set(Calendar.MILLISECOND, 0); if (time.equals("weekly")) { previousTime.set(Calendar.DATE, previousTime.get(Calendar.DATE) - 6); todayCalendar.set(2016, 6, 7, 0, 0, 0); todayCalendar.set(Calendar.MILLISECOND, 0); } else if (time.equals("monthly")) { previousTime.set(Calendar.DATE, previousTime.get(Calendar.DATE) - 29); todayCalendar.set(2016, 6, 7, 0, 0, 0); todayCalendar.set(Calendar.MILLISECOND, 0); } else if (time.equals("daily_total")) { groupBy = null; previousTime.set(2016, 6, 4, 0, 0, 0); previousTime.set(Calendar.MILLISECOND, 0); todayCalendar.set(2016, 6, 4, 23, 59, 59); } else if (time.equals("weekly_total")) { groupBy = null; previousTime.set(2016, 6, 4, 0, 0, 0); previousTime.set(Calendar.MILLISECOND, 0); previousTime.set(Calendar.DATE, previousTime.get(Calendar.DATE) - 6); todayCalendar.set(2016, 6, 4, 0, 0, 0); todayCalendar.set(Calendar.MILLISECOND, 0); } else if (time.equals("monthly_total")) { groupBy = null; previousTime.set(2016, 6, 4, 0, 0, 0); previousTime.set(Calendar.MILLISECOND, 0); previousTime.set(Calendar.DATE, previousTime.get(Calendar.DATE) - 29); todayCalendar.set(2016, 6, 4, 0, 0, 0); todayCalendar.set(Calendar.MILLISECOND, 0); } AggregateIterable<Document> iterable_6_9_p; if (collection.equals("requirementLog")) { MongoClient mongoClient = new MongoClient(); iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection) .aggregate(asList(new Document("$match", new Document("sentDate", new Document("$gte", previousTime.getTimeInMillis()).append("$lte", todayCalendar.getTimeInMillis())).append("isMailSent", "Y") .append("alertType", "PRS") //.append("date", new Document("$gte",Long.toString(curr_6AM.getTimeInMillis())).append("$lt", Long.toString(curr_24PM.getTimeInMillis()))) ), new Document("$group", new Document("_id", groupBy).append("count", new Document("$sum", 1))))); } else { MongoClient mongoClient = new MongoClient(); iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection) .aggregate(asList(new Document("$match", new Document("date", new Document("$gte", Long.toString(previousTime.getTimeInMillis())) .append("$lte", Long.toString(todayCalendar.getTimeInMillis()))) // .append("alertType", alertType) //.append("isMailSent", isMailSent) //.append("date", new Document("$gte",Long.toString(curr_6AM.getTimeInMillis())).append("$lt", Long.toString(curr_24PM.getTimeInMillis()))) ), new Document("$group", new Document("_id", groupBy).append("count", new Document("$sum", 1))))); } return iterable_6_9_p; }
From source file:com.AlertMailerWebPage.servlet.GetWeeklyOpenMail.java
public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress) { Calendar current = Calendar.getInstance(); Calendar min_13 = Calendar.getInstance(); min_13.set(2015, 4, 12, 0, 0, 0);/* www. j a va 2s . c o m*/ min_13.set(Calendar.MILLISECOND, 0); current.set(2015, 4, 12, 23, 59, 59); current.add(Calendar.DATE, 13); /*min_13.add(Calendar.DATE,-13); int y=min_13.get(Calendar.YEAR); int m=min_13.get(Calendar.MONTH); int d=min_13.get(Calendar.DAY_OF_MONTH); min_13.set(y,m,d,0,0,0); min_13.set(Calendar.MILLISECOND,0);*/ //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017)); MongoClient mongoClient = new MongoClient(); AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection) .aggregate(asList(new Document("$match", new Document("openDate", new Document("$gte", min_13.getTimeInMillis()).append("$lte", current.getTimeInMillis())) //.append("alertType", alertType) //.append("openDate", new Document("$gte",min_13.getTimeInMillis()).append("$lte", current.getTimeInMillis())) ), new Document("$group", new Document("_id", new Document("date", "$openDate").append("type", "$alertType")).append("count", new Document("$sum", 1))))); return iterable_6_9_p; }
From source file:com.AlertMailerWebPage.servlet.Login.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww. jav a2 s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { Properties defaultProps = new Properties(); InputStream in = GetProcessSpeed.class.getResourceAsStream("configuration.properties"); defaultProps.load(in); in.close(); String database = (String) defaultProps.get("database_mailerDetails"); String collection = (String) defaultProps.get("collection_mailerDetails"); String serverAddress = (String) defaultProps.get("IP_AMDBoard"); /* TODO output your page here. You may use following sample code. */ final String username = request.getParameter("username"); final String pass = request.getParameter("password"); flag = 0; // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017)); MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase(database); Document query = new Document("login", "login"); Cookie ck = new Cookie("login", null); if (username == null || pass == null) { ck.setValue("failed"); response.addCookie(ck);//adding cookie in the response RequestDispatcher rd = request.getRequestDispatcher("index.html"); ; rd.forward(request, response); } FindIterable<Document> iterable = db.getCollection(collection).find(query); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { if (username.equals((String) document.get("username")) && pass.equals((String) document.get("password"))) { flag = 1; } } }); if (flag == 1) { ck.setValue("success"); response.addCookie(ck);//adding cookie in the response response.sendRedirect("Page1.html"); } else { ck.setValue("failed"); response.addCookie(ck);//adding cookie in the response RequestDispatcher rd = request.getRequestDispatcher("index.html"); ; rd.forward(request, response); } } }
From source file:com.AlertMailerWebPage.servlet.MCRMData.java
public Document getMCRMObject(String database, String collection, String serverAddress, String email) { MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017)); MongoDatabase db = mongoClient.getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email)) .projection(new Document("email", 0).append("_id", 0)); iterable.forEach(new Block<Document>() { @Override// w ww.j av a 2 s . com public void apply(final Document document) { MCRMData.document = document; } }); return document; }
From source file:com.AlertMailerWebPage.servlet.MCRMData.java
public void getSL_VM(String database, String collection, String serverAddress, String email) { MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017)); MongoDatabase db = mongoClient.getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email)) .sort(new Document("_id", -1)).limit(1); iterable.forEach(new Block<Document>() { @Override//from w ww. ja v a 2s. co m public void apply(final Document document) { SL = (String) document.get("subject"); VM = (String) document.get("vmfilename"); } }); }
From source file:com.AlertMailerWebPage.servlet.MCRMData.java
public String getMCRMData(String process, String database, String collection, String serverAddress, String email) {//w ww . ja v a2 s . c om process1 = process; user = "no data"; //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017)); MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email)); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { if (document.containsKey("" + process1 + "_TOTAL_OPENED") || document.containsKey("" + process1 + "_TOTAL_CONTACTS")) { String values = null; if (document.containsKey("" + process1 + "_TOTAL_OPENED") && document.containsKey("" + process1 + "_TOTAL_CONTACTS")) { values = "both"; } else if (document.containsKey("" + process1 + "_TOTAL_OPENED") && !(document.containsKey("" + process1 + "_TOTAL_CONTACTS"))) { values = "mail"; } else if (!(document.containsKey("" + process1 + "_TOTAL_OPENED")) && document.containsKey("" + process1 + "_TOTAL_CONTACTS")) { values = "contact"; } if (values.equals("both")) { if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 10 || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 5) { user = "MostActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 5 || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 2) { user = "ActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 2 || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 1) { user = "SlightlyActiveUser"; } } else if (values.equals("mail")) { if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 10) { user = "MostActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 5) { user = "ActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 2) { user = "SlightlyActiveUser"; } } else if (values.equals("contact")) { if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 5) { user = "MostActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 2) { user = "ActiveUser"; } else if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 1) { user = "SlightlyActiveUser"; } } } if (user.equals("no data")) { if (document.containsKey("" + process1 + "1WEEK_OPENED") || document.containsKey("" + process1 + "1WEEK_CONTACTS")) { user = "OtherContact1Week"; } } if (user.equals("no data")) { if (document.containsKey("" + process1 + "2WEEK_OPENED") || document.containsKey("" + process1 + "2WEEK_CONTACTS") || document.containsKey("" + process1 + "3WEEK_OPENED") || document.containsKey("" + process1 + "3WEEK_CONTACTS")) { user = "OtherContact2n3Week"; } } if (user.equals("no data")) { if (document.containsKey("WEEK1_TOTAL_TRAFFIC")) { user = "PotentialLead1Week"; } } if (user.equals("no data")) { if (document.containsKey("WEEK2_TOTAL_TRAFFIC") || document.containsKey("WEEK3_TOTAL_TRAFFIC")) { user = "PotentialLead2n3Week"; } } if (user.equals("no data")) { if (document.containsKey("" + process1 + "1WEEK_OPENED") || document.containsKey("" + process1 + "1WEEK_CONTACTS") || document.containsKey("" + process1 + "2WEEK_OPENED") || document.containsKey("" + process1 + "2WEEK_CONTACTS") || document.containsKey("" + process1 + "3WEEK_OPENED") || document.containsKey("" + process1 + "3WEEK_CONTACTS") || document.containsKey("" + process1 + "4WEEK_OPENED") || document.containsKey("" + process1 + "4WEEK_CONTACTS")) { user = "RecentInactive"; } } if (user.equals("no data")) { user = "Inactive"; } } }); return user; }
From source file:com.AlertMailerWebPage.servlet.Save.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w ww . ja v a 2 s. c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { Properties defaultProps = new Properties(); InputStream in = GetProcessSpeed.class.getResourceAsStream("configuration.properties"); defaultProps.load(in); in.close(); String database = (String) defaultProps.get("database_mailerDetails"); String collection = (String) defaultProps.get("collection_mailerDetails"); String serverAddress = (String) defaultProps.get("IP_AMDBoard"); String relaxation = request.getParameter("relaxation2"); String alertmailer = request.getParameter("alertmailer"); String SUBJECT_OWNER_NoNMultiStory = request.getParameter("SUBJECT_OWNER_NoNMultiStory2"); String SUBJECT_NoNOWNER_NoNMultiStory = request.getParameter("SUBJECT_NoNOWNER_NoNMultiStory2"); //Mongo mongo = new Mongo("localhost", 27017); try { org.json.JSONArray jj = new JSONArray(relaxation); int j = jj.length(); out.println(j); BasicDBList ll1 = new BasicDBList(); for (int i = 0; i < jj.length(); i++) { JSONObject jsonobject = jj.getJSONObject(i); BasicDBObject n = (BasicDBObject) JSON.parse(jsonobject.toString()); ll1.add(n); out.println(jsonobject.toString()); } out.println("ok"); // Document m= Document.parse(relaxation); //String o=(String)m; String SUBJECT_OWNER_MULTISTORY_RENT = request.getParameter("SUBJECT_OWNER_MULTISTORY_RENT2"); String vm = request.getParameter("vm2"); String SUBJECT_NoNOWNER_MULTISTORY_RENT = request.getParameter("SUBJECT_NoNOWNER_MULTISTORY_RENT2"); String SUBJECT_NoNOWNER_MULTISTORY_RENT_PREHEADER = request .getParameter("SUBJECT_NoNOWNER_MULTISTORY_RENT_PREHEADER2"); String SUBJECT_OWNER_MULTISTORY_SALE = request.getParameter("SUBJECT_OWNER_MULTISTORY_SALE2"); String PREHEADER_NoNOWNER_RENT = request.getParameter("PREHEADER_NoNOWNER_RENT2"); String SUBJECT_NoNOWNER_MULTISTORY_SALE = request.getParameter("SUBJECT_NoNOWNER_MULTISTORY_SALE2"); String SUBJECT_NoNOWNER_MULTISTORY_SALE_PREHEADER = request .getParameter("SUBJECT_NoNOWNER_MULTISTORY_SALE_PREHEADER2"); String PREHEADER_NoNOWNER_SALE = request.getParameter("PREHEADER_NoNOWNER_SALE2"); String noofprops = request.getParameter("noofprops"); // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017)); MongoClient mongoClient = new MongoClient(); mongoClient.getDatabase(database).getCollection(collection).insertOne(new Document( //mongoClient.getDatabase("dashboard").getCollection("users").insertOne( new Document( "AlertMailer", alertmailer) .append("InsertTime", Calendar.getInstance().getTimeInMillis()) .append("SUBJECT_OWNER_NoNMultiStory", Arrays.asList(SUBJECT_OWNER_NoNMultiStory.split(","))) .append("SUBJECT_NoNOWNER_NoNMultiStory", Arrays.asList(SUBJECT_NoNOWNER_NoNMultiStory.split(","))) .append("SUBJECT_OWNER_MULTISTORY_RENT", Arrays.asList(SUBJECT_OWNER_MULTISTORY_RENT.split(","))) .append("SUBJECT_NoNOWNER_MULTISTORY_RENT", Arrays.asList(SUBJECT_NoNOWNER_MULTISTORY_RENT.split(","))) .append("SUBJECT_NoNOWNER_MULTISTORY_RENT_PREHEADER", Arrays.asList(SUBJECT_NoNOWNER_MULTISTORY_RENT_PREHEADER.split(","))) .append("PREHEADER_NoNOWNER_RENT", Arrays.asList(PREHEADER_NoNOWNER_RENT.split(","))) .append("SUBJECT_OWNER_MULTISTORY_SALE", Arrays.asList(SUBJECT_OWNER_MULTISTORY_SALE.split(","))) .append("SUBJECT_NoNOWNER_MULTISTORY_SALE", Arrays.asList(SUBJECT_NoNOWNER_MULTISTORY_SALE.split(","))) .append("SUBJECT_NoNOWNER_MULTISTORY_SALE_PREHEADER", Arrays.asList(SUBJECT_NoNOWNER_MULTISTORY_SALE_PREHEADER.split(","))) .append("PREHEADER_NoNOWNER_SALE", Arrays.asList(PREHEADER_NoNOWNER_SALE.split(","))) .append("VM", Arrays.asList(vm.split(","))).append("Relaxation", ll1) .append("NoOfProperties", noofprops)); } catch (Exception e) { } RequestDispatcher rd = request.getRequestDispatcher("Page1.html"); rd.forward(request, response); } }
From source file:com.andersen.backendjaxrsproject.EmployeeServices.java
public EmployeeServices() { MongoClient mongo = new MongoClient("localhost", 27017); employeeDB = mongo.getDatabase("Employees"); }
From source file:com.avbravo.jgmongodesktopexample.Start.java
/** * @param args the command line arguments *//*from www. ja va2 s . c o m*/ public static void main(String[] args) { // TODO code application logic here try { PaisesFacade paisesFacade = new PaisesFacade(); SedesFacade sedesFacade = new SedesFacade(); ProvinciasFacade provinciasFacade = new ProvinciasFacade(); MongoClient client = new MongoClient("localhost", 27017); MongoDatabase database = client.getDatabase("fantasy"); MongoCollection<Document> collection = database.getCollection("paises"); List<Document> documents = (List<Document>) collection.find().into(new ArrayList<Document>()); //Document document = new Document("name","Cuba") // .append("poblacion", 7777); // // collection.insertOne(document); // DBCollection coll = database.getCollection("mycol"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a"); Date startDate = simpleDateFormat.parse(simpleDateFormat.format(new Date())); MongoCollection<BasicDBObject> coll = database.getCollection("paises", BasicDBObject.class); BasicDBObject doc = new BasicDBObject("title", "OrientDB").append("Fecha", startDate) .append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point"); coll.insertOne(doc); for (Document document : documents) { System.out.println(document); System.out.println("Siglas" + document.get("Siglas")); System.out.println("Pais" + document.get("Pais")); } // // List<Provincias> list = provinciasFacade.findAll(); // list.forEach((p) -> { // System.out.println(" "+p.toString()); // }); // // Provincias provincias = new Provincias(); // provincias.setIdprovincia("6"); // Provincias pr = provinciasFacade.findById(provincias); // // if(pr.getIdprovincia() == null){ // System.out.println("---> no hay provincia con ese id"); // }else{ // System.out.println("---> "+pr.toString()); // } // Paises paises = new Paises(); // paises.setSiglas("pa"); // Paises p = paisesFacade.findById(paises); // // if(p.getSiglas() == null){ // System.out.println("============================"); // System.out.println("no hay paises con siglas "+paises.getSiglas()); // }else{ // System.out.println("paises: "+p.toString()); // } // Provincias provincias = new Provincias(); // provincias.setIdprovincia("2"); // provincias.setProvincia("Cocle"); // provincias.setPaises(p); // // provincias.setHola("Hola"); // // if(provinciasFacade.save(provincias)){ // System.out.println("guardado"); // }else{ // System.out.println("no se guardo"); // } // // List<Paises> list = paisesFacade.findAll(); // Sedes sedes = new Sedes(); // sedes.setIdsede("s-2"); // sedes.setSede("Sede 2"); // sedes.setPaises(list); // sedesFacade.save(sedes); // } catch (Exception e) { System.out.println("error " + e.getLocalizedMessage()); } }