Example usage for org.hibernate Criteria add

List of usage examples for org.hibernate Criteria add

Introduction

In this page you can find the example usage for org.hibernate Criteria add.

Prototype

public Criteria add(Criterion criterion);

Source Link

Document

Add a Criterion restriction to constrain the results to be retrieved.

Usage

From source file:admin_add_doctors.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w ww .ja  v  a 2 s  .  c  om*/
 *
 * @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");

    PrintWriter out = response.getWriter();

    try {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("admin") != null) {
            Admin a = (Admin) hs.getAttribute("admin");

            if (request.getParameter("status") != null && request.getParameter("dId") != null) {

                int apid = Integer.parseInt(request.getParameter("dId"));
                Doctor doc1 = (Doctor) ss.get(Doctor.class, apid);
                if (request.getParameter("status").equals("approve")) {

                    doc1.setStatus("ACTIVE");

                    ss.update(doc1);

                    String subject = "Your Appointment is Approved.!";
                    String content = "Hi, Dr." + doc1.getDFirstname() + " " + doc1.getDLastname() + "\n"
                            + "Your Request has been approved by Admin" + ".\n";
                    String mail = doc1.getEmailId();

                    String[] recipients = new String[] { mail };
                    //String[] bccRecipients = new String[]{"sunilkotadiya777@gmail.com"};  

                    if (new MailUtil().sendMail(recipients, subject, content)) {

                    }
                    request.setAttribute("msg", "Appointment accepted..!");

                } else if (request.getParameter("status").equals("reject")) {
                    doc1.setStatus("REJECTED");
                    ss.update(doc1);
                    String subject = "Your Request is Rejected.!";
                    String content = "Hi, " + doc1.getDFirstname() + " " + doc1.getDLastname()
                            + "Your Request to join Cardiac Countermeasure has been rejected by admin"
                            + ".\n anil ";
                    String mail = doc1.getEmailId();

                    String[] recipients = new String[] { mail };
                    //String[] bccRecipients = new String[]{"sunilkotadiya777@gmail.com"};  

                    if (new MailUtil().sendMail(recipients, subject, content)) {

                    }
                    request.setAttribute("msg", "Appointment rejected..!");
                }

            }

            Criteria cr = ss.createCriteria(Doctor.class);
            //cr.add(Restrictions.eq("dId", a));
            cr.add(Restrictions.eq("status", "not_active"));
            ArrayList<Doctor> da = (ArrayList<Doctor>) cr.list();
            if (da.size() > 0) {
                request.setAttribute("da", da);
            }

            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("admin_add_doctors.jsp");
            rd.forward(request, response);
        } else {
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }
    } catch (HibernateException he) {

        out.println(he.getMessage());
    } finally {
        out.close();
    }
}

From source file:consultarProfessor.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w  w w  .j  a va2  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()) {

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        String nome = request.getParameter("nome");
        String sobrenome = request.getParameter("sobrenome");
        String disciplina = request.getParameter("disciplina");

        Criteria c = sessao.createCriteria(Professor.class);

        if (nome != null && !nome.trim().equals("")) {
            c.add(Restrictions.eq("nome", nome));
        }
        if (sobrenome != null && !sobrenome.trim().equals("")) {
            c.add(Restrictions.eq("sobrenome", sobrenome));
        }
        if (disciplina != null && !disciplina.trim().equals("")) {
            c.add(Restrictions.eq("disciplina", disciplina));
        }

        List<Professor> result = c.list();
        if (result != null && result.size() > 0) {
            for (Professor prof : result) {
                out.println("<br><a href=\"formEditar?nome=" + prof.getNome() + "\">editar</a>  : "
                        + prof.getNome() + " " + prof.getSobrenome());
            }
        } else {
            out.println("Nenhum resultado encontrado para esse criterio de busca: " + nome + "|" + sobrenome
                    + "|" + disciplina);
        }
    }
}

From source file:systemgeneratedreport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w  ww.  j  av 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()) {
        /* TODO output your page here. You may use following sample code. */

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        if (hs.getAttribute("patient") != null) {

            Patient p1 = (Patient) hs.getAttribute("patient");
            if (request.getParameter("calcid") != null) {

                int calcid = Integer.parseInt(request.getParameter("calcid"));

                Calculationmeteredata cm1 = (Calculationmeteredata) ss.get(Calculationmeteredata.class, calcid);
                String color = "red";

                if (cm1.getCalculationmeteredataId() != null) {

                    String gender = "MALE";
                    Criteria optcr = ss.createCriteria(PatientfactorAnswer.class);
                    optcr.add(Restrictions.eq("calculationmeteredataId", cm1));
                    ArrayList<PatientfactorAnswer> anslist = (ArrayList<PatientfactorAnswer>) optcr.list();
                    if (anslist.size() > 0) {

                        FactorOption agefac = new FactorOption();
                        for (PatientfactorAnswer opp : anslist) {
                            if (opp.getFactorId().equals(new FactorDetails(1))) {
                                if (opp.getFactorOptionId().getFoptionId().equals(16)) {
                                    gender = "FEMALE";
                                }
                                System.out.println("Gender is :" + gender + opp.getFactorOptionId());
                            }
                            if (opp.getFactorId().equals(new FactorDetails(2))) {
                                agefac = opp.getFactorOptionId();
                                System.out.println("Age is :" + agefac.getFactorOption());
                            }
                        }
                        if (gender.equals("MALE")) {
                            //                        Set Chart Data   
                            String[] chartdata = new String[4];
                            String[] rchartdata = new String[4];

                            Criteria cr01 = ss.createCriteria(AbsulateRiskMale.class);
                            cr01.add(Restrictions.eq("foptionid", agefac));
                            cr01.setProjection(Projections.max("green"));
                            if (cr01.uniqueResult() != null) {
                                chartdata[0] = cr01.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[0]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "green";
                                }
                            }
                            Criteria cr02 = ss.createCriteria(AbsulateRiskMale.class);
                            cr02.add(Restrictions.eq("foptionid", agefac));
                            cr02.setProjection(Projections.max("violet"));
                            if (cr02.uniqueResult() != null) {
                                chartdata[1] = cr02.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[1]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "violet";
                                }
                            }
                            System.out.println(chartdata[1]);
                            System.out.println(color);
                            Criteria cr03 = ss.createCriteria(AbsulateRiskMale.class);
                            cr03.add(Restrictions.eq("foptionid", agefac));
                            cr03.setProjection(Projections.max("yellow"));
                            if (cr03.uniqueResult() != null) {
                                chartdata[2] = cr03.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[2]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "yellow";
                                }
                            }
                            chartdata[3] = "54";

                            //                                RchartData create
                            Criteria cr010 = ss.createCriteria(AbsulateRiskMale.class);
                            cr010.add(Restrictions.eq("foptionid", agefac));
                            cr010.setProjection(Projections.max("rGreen"));
                            if (cr010.uniqueResult() != null) {
                                rchartdata[0] = cr010.uniqueResult().toString();
                            }
                            Criteria cr020 = ss.createCriteria(AbsulateRiskMale.class);
                            cr020.add(Restrictions.eq("foptionid", agefac));
                            cr020.setProjection(Projections.max("rViolet"));
                            if (cr020.uniqueResult() != null) {
                                rchartdata[1] = cr020.uniqueResult().toString();

                            }
                            System.out.println(chartdata[1]);
                            System.out.println(color);
                            Criteria cr030 = ss.createCriteria(AbsulateRiskMale.class);
                            cr030.add(Restrictions.eq("foptionid", agefac));
                            cr030.setProjection(Projections.max("rYellow"));
                            if (cr030.uniqueResult() != null) {
                                rchartdata[2] = cr030.uniqueResult().toString();

                            }
                            rchartdata[3] = "30";

                            request.setAttribute("rchartdata", rchartdata);
                            request.setAttribute("chartdata", chartdata);

                        } else if (gender.equals("FEMALE")) {
                            //                        Set Chart Data   
                            String[] chartdata = new String[4];
                            String[] rchartdata = new String[4];

                            Criteria cr01 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr01.add(Restrictions.eq("foptionid", agefac));
                            cr01.setProjection(Projections.max("green"));
                            if (cr01.uniqueResult() != null) {
                                chartdata[0] = cr01.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[0]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "green";
                                }
                            }
                            Criteria cr02 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr02.add(Restrictions.eq("foptionid", agefac));
                            cr02.setProjection(Projections.max("violet"));
                            if (cr02.uniqueResult() != null) {
                                chartdata[1] = cr02.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[1]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "violet";
                                }
                            }
                            Criteria cr03 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr03.add(Restrictions.eq("foptionid", agefac));
                            cr03.setProjection(Projections.max("yellow"));
                            if (cr03.uniqueResult() != null) {
                                chartdata[2] = cr03.uniqueResult().toString();
                                int cdata = Integer.parseInt(chartdata[2]);
                                if (cdata >= cm1.getMetervalue()) {
                                    color = "yellow";
                                }
                            }
                            chartdata[3] = "30";

                            //                                RchartData create
                            Criteria cr010 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr010.add(Restrictions.eq("foptionid", agefac));
                            cr010.setProjection(Projections.max("rGreen"));
                            if (cr010.uniqueResult() != null) {
                                rchartdata[0] = cr010.uniqueResult().toString();
                            }
                            Criteria cr020 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr020.add(Restrictions.eq("foptionid", agefac));
                            cr020.setProjection(Projections.max("rViolet"));
                            if (cr020.uniqueResult() != null) {
                                rchartdata[1] = cr020.uniqueResult().toString();

                            }
                            System.out.println(chartdata[1]);
                            System.out.println(color);
                            Criteria cr030 = ss.createCriteria(AbsoluteRiskFemale.class);
                            cr030.add(Restrictions.eq("foptionid", agefac));
                            cr030.setProjection(Projections.max("rYellow"));
                            if (cr030.uniqueResult() != null) {
                                rchartdata[2] = cr030.uniqueResult().toString();

                            }
                            rchartdata[3] = "20";

                            request.setAttribute("rchartdata", rchartdata);
                            request.setAttribute("chartdata", chartdata);
                        }
                        Criteria testcr = ss.createCriteria(TestRecommend.class);
                        testcr.add(Restrictions.eq("color", color));
                        ArrayList<TestRecommend> tlist = (ArrayList<TestRecommend>) testcr.list();
                        if (tlist.size() > 0) {
                            request.setAttribute("testlist", tlist);
                        }

                        request.setAttribute("calcdata", cm1);
                        request.setAttribute("anslist", anslist);
                    }
                }
                tr.commit();
                RequestDispatcher rd = request.getRequestDispatcher("system_generated_report1.jsp");
                rd.forward(request, response);
            }
        }

        else {

            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }
    }
}

From source file:searchprop_byname.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w  w  w.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");
    PrintWriter out = response.getWriter();
    try {
        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        String searchedprop = request.getParameter("propname");
        Criteria cr = ss.createCriteria(PropDetail.class);
        cr.add(Restrictions.like("pName", "%" + searchedprop + "%"));

        ArrayList<PropDetail> pd = (ArrayList<PropDetail>) cr.list();

        /*   for(PropDetail p: pd)
         {
         out.print(p.getPId()+" type:"+p.getPType()+" bhk:"+p.getPBhk());
         out.println(" ");
         }
         */
        if (pd.isEmpty()) {
            out.print("no such property");
            String msg = "Sorry, No results found.";
            request.setAttribute("error", msg);
        } else {
            request.setAttribute("proplist", pd);
        }
        RequestDispatcher rd = request.getRequestDispatcher("getstate?id=9");
        rd.forward(request, response);

    } catch (Exception e) {
        out.println(e.getMessage());
    }
}

From source file:$.GenericDaoHibernate.java

protected Criteria findByAttributes(Map<String, Object> attributes) {
        Criteria criteria = getSession().createCriteria(entityClass);
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));
        }/*from  w w  w  .j a va  2  s .  c om*/
        return criteria;
    }

From source file:abid.password.springmvc.dao.UserDaoHibernateImpl.java

License:Apache License

@Transactional(readOnly = true)
public User getUser(String username) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.add(Restrictions.eq("username", username));
    User user = (User) criteria.uniqueResult();
    return user;//from w w  w .  j ava  2s.  c om
}

From source file:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
    Session session = sessionFactory.openSession();
    try {/*from w ww .  j  a  v a2  s. c o m*/
        Criteria criteria = session.createCriteria(getPersistentClass());
        criteria.setCacheable(true);
        for (Criterion c : criterion) {
            criteria.add(c);
        }
        return criteria.list();
    } finally {
        session.close();
    }
}

From source file:abid.password.swing.dao.hibernate.UserDaoImpl.java

License:Apache License

public User getUser(String username) {
    Session session = getSessionFactory().openSession();
    try {//from   w ww  .j a  v a2s .  c o m
        session.beginTransaction();
        Criteria criteria = session.createCriteria(User.class);
        criteria.add(Restrictions.eq("username", username));
        User user = (User) criteria.uniqueResult();
        // Query query = session.createQuery("from User u where u.username = ?");
        // query.setString(0, username);
        // User user = (User) query.uniqueResult();
        return user;
    } catch (HibernateException e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
    return null;
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
    Criteria criteria = session.get().createCriteria(getPersistentClass());
    criteria.setCacheable(true);//  w  w w . jav  a  2s  .co  m
    for (Criterion c : criterion) {
        criteria.add(c);
    }
    return criteria.list();
}

From source file:abid.password.wicket.dao.UserDaoHibernateImpl.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
public User getUser(String username) {
    Criteria criteria = getSession().get().createCriteria(User.class);
    criteria.add(Restrictions.eq("username", username));
    User user = (User) criteria.uniqueResult();
    // Query query = session.createQuery("from User u where u.username = ?");
    // query.setString(0, username);
    // User user = (User) query.uniqueResult();
    return user;/*from  w  ww.ja  va2 s. c o m*/
}