Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

In this page you can find the example usage for com.mongodb MongoClient MongoClient.

Prototype

public MongoClient() 

Source Link

Document

Creates an instance based on a (single) mongodb node (localhost, default port).

Usage

From source file:com.AlertMailerWebPage.servlet.GetMonthlyOpenMailServlet.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, 1, 0, 0, 0);//from   ww w  .j  av a2 s  . c  o  m
    min_13.set(Calendar.MILLISECOND, 0);
    current.set(2015, 4, 1, 0, 0, 0);
    current.add(Calendar.DATE, 29);
    current.set(Calendar.MILLISECOND, 0);
    /* min_13.add(Calendar.DATE,-29);
    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();
    //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()))

    ), 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.GetMonthlyServlet.java

public AggregateIterable<Document> getCounts(String alertType, String database, String collection,
        String serverAddress) {//from  ww  w  . j a v a 2  s . c o  m

    Calendar current = Calendar.getInstance();
    Calendar min_13 = Calendar.getInstance();

    min_13.set(2016, 6, 1, 0, 0, 0);
    current.set(2016, 6, 1, 23, 59, 59);
    current.add(Calendar.DATE, 29);
    //  min_13.add(Calendar.DATE,-29);
    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("sentDate",
                                    new Document("$gte", min_13.getTimeInMillis()).append("$lte",
                                            current.getTimeInMillis()))
                                                    //.append("isMailSent","Y")                
                                                    .append("isMailSent", "Y")),
                    new Document("$group",
                            new Document("_id", new Document("date", "$sentDate").append("type", "$alertType"))
                                    .append("count", new Document("$sum", 1)))));

    return iterable_6_9_p;
}

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 ava 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) {//w  ww .  jav a 2 s. co  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);/*from   w ww. j a  v a  2  s  .c om*/
    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.  j a  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");

        /* 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 String getMCRMData(String process, String database, String collection, String serverAddress,
        String email) {//w ww. ja  v a  2s  .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./*ww w.  j  a v a 2  s  . co 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.arquivolivre.mongocom.management.CollectionManagerFactory.java

License:Apache License

private static CollectionManager createBaseCollectionManager(String host, int port, String dbName, String user,
        String password) {/* w w  w .j a v  a2s. c  om*/
    try {
        if ("".equals(host)) {
            client = new MongoClient();
        } else {
            if (port == 0) {
                client = new MongoClient(host);
            } else {
                client = new MongoClient(host, port);
            }
        }
        LOG.log(Level.INFO, "Connected to {0}", client.getAddress());
        if ("".equals(user)) {
            return new CollectionManager(client, dbName);
        }
        return new CollectionManager(client, dbName, user, password);
    } catch (MongoException | UnknownHostException ex) {
        LOG.log(Level.SEVERE,
                "Unable to connect to a mongoDB instance, maybe it is not running or you do not have the right permission: ",
                ex);
    }
    return null;
}

From source file:com.avbravo.jgmongodesktopexample.ejb.PaisesFacade.java

@Override
protected MongoClient getMongoClient() {
    MongoClient mongoClient = new MongoClient();
    return mongoClient;
}