Example usage for com.mongodb Block Block

List of usage examples for com.mongodb Block Block

Introduction

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

Prototype

Block

Source Link

Usage

From source file:com.AlertMailerWebPage.servlet.Login.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w  w w  . j a  v 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//from  w  w w.  ja  v a  2 s  .  c o m
        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  w w.j  a  v  a  2  s  .c o  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) {/*from w w  w.ja  v a 2s . c o m*/
    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.avbravo.ejbjmoordb.mongodb.repository.Repository.java

@Override
public Optional<T> find(String key, Object value) {
    try {//from  w ww. j a  v a 2  s.  c o m

        //   Object t = entityClass.newInstance();
        MongoDatabase db = getMongoClient().getDatabase(database);

        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));

        haveElements = false;
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    haveElements = true;
                    tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e);
                    exception = new Exception("find() ", e);
                }

            }
        });
        if (haveElements) {

            return Optional.of(tlocal);
        }
        return Optional.empty();

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("find() ", e);

    }

    return Optional.empty();
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, Object value) {
    try {/*from ww w.  j a v a  2s.c  o  m*/

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        haveElements = false;
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    haveElements = true;
                    tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, Integer value) {
    try {//from  www.  java 2s.com

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        haveElements = false;
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    haveElements = true;
                    tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, String value) {
    try {/*from   w  w w.j  a va 2  s. co m*/

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        haveElements = false;
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    haveElements = true;
                    tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 * Internamente recorre el iterable//  w  w  w. j ava 2 s .  c  om
 *
 * @param iterable
 * @return
 */
private T iterableSimple(FindIterable<Document> iterable) {
    try {
        //      //Test.msg("$$$$$$$iterable simple");
        haveElements = false;

        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    haveElements = true;
                    t1 = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e);
                    exception = new Exception("find() ", e);
                }

            }
        });

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("iterableSimple() ", e);

    }
    if (haveElements) {
        return (T) t1;
    }
    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

private List<T> iterableList(FindIterable<Document> iterable) {
    List<T> l = new ArrayList<>();
    try {/*from   ww  w . j a  v a2s  .  co  m*/
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                try {
                    t1 = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList,
                            referencedBeansList);
                    l.add(t1);
                } catch (Exception e) {
                    Logger.getLogger(Repository.class.getName() + "find()").log(Level.SEVERE, null, e);
                    exception = new Exception("find() ", e);
                }

            }
        });

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("iterableSimple() ", e);

    }

    return l;
}