Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

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

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:TestGson.java

public static void main(String[] args) throws RemoteException {
    StaffDCO dco = new StaffDCO();
    Gson gson = new Gson();
    Session ses = utils.HibernateUtils.getSessionFactory().getCurrentSession();
    try {//  w w  w  . j  a v a  2s  . co  m
        ses.beginTransaction();
        Criteria crit = ses.createCriteria(Staff.class);
        crit.add(Restrictions.eq("id", 1));
        Staff s = (Staff) crit.uniqueResult();
        ses.getTransaction().commit();
        StaffBean bean = dco.toStaffBean(s);
        String v = gson.toJson(bean, StaffBean.class);
        System.out.println(v);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:go_to_user.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w w  w  . 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 {
    PrintWriter out = response.getWriter();
    try {

        String id = request.getParameter("id");

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        Login l = (Login) ss.get(Login.class, Integer.parseInt(id));
        System.out.println("uid is " + l.getUId());

        switch (l.getURole()) {
        case "customer": {
            Criteria cr = ss.createCriteria(CustomerDetail.class);
            cr.add(Restrictions.eq("uId", l));
            CustomerDetail cd = (CustomerDetail) cr.uniqueResult();

            request.setAttribute("fname", cd.getCFname());
            request.setAttribute("lname", cd.getCLname());

            request.setAttribute("email", cd.getCEmail());
            request.setAttribute("mobile", cd.getCNum());

            RequestDispatcher rd = request.getRequestDispatcher("contact_customer.jsp");
            rd.forward(request, response);

            break;
        }

        case "agent": {

            Criteria cr = ss.createCriteria(AgentDetail.class);
            cr.add(Restrictions.eq("uId", l));
            AgentDetail ad = (AgentDetail) cr.uniqueResult();

            Criteria cr1 = ss.createCriteria(AgentFeedback.class);
            cr1.add(Restrictions.eq("aId", ad));
            ArrayList<AgentFeedback> afl = (ArrayList<AgentFeedback>) cr1.list();

            request.setAttribute("ad", ad);

            if (!afl.isEmpty()) {
                request.setAttribute("afl", afl);
            }
            RequestDispatcher rd = request.getRequestDispatcher("agentdescription.jsp");
            rd.forward(request, response);

            break;
        }
        }

    }

    catch (HibernateException he) {
        System.out.println(he.getMessage());
    }

}

From source file:loginServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();// w w  w .j a  va2 s  .  co m
    //userpass query
    Criteria crit = session.createCriteria(SystemUser.class);
    Criterion cond1 = Restrictions.eq("username", username);
    Criterion cond2 = Restrictions.eq("password", password);
    crit.add(Restrictions.and(cond1, cond2));
    if ((SystemUser) crit.uniqueResult() != null) {
        //redirect positive
        RequestDispatcher rdp = request.getRequestDispatcher("/success.jsp");
        rdp.forward(request, response);
    } else {
        //redirect negative 
        RequestDispatcher rdp = request.getRequestDispatcher("/fail.jsp");
        rdp.forward(request, response);
    }
    ;

}

From source file:calculatefactorrisk.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// w  w w.ja va2  s.  com
 *
 * @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");
            int metervalue = 0;
            double rmetervalue = 0;
            int facindex = 0;
            int points = 0;
            String[] chartdata = new String[4];
            chartdata[0] = "5";
            chartdata[1] = "12";
            chartdata[2] = "25";
            chartdata[3] = "60";
            ArrayList<PatientfactorAnswer> finalans = new ArrayList<PatientfactorAnswer>();

            if (request.getParameter("facindex") != null && request.getParameter("ans") != null) {

                if (hs.getAttribute("metervalue") != null) {
                    metervalue = (Integer) hs.getAttribute("metervalue");
                }
                if (hs.getAttribute("finalans") != null) {
                    finalans = (ArrayList<PatientfactorAnswer>) hs.getAttribute("finalans");
                }
                if (request.getParameter("ans").equals("15")) {
                    hs.setAttribute("gender", "MALE");
                    hs.setAttribute("agefactor", null);
                }
                if (request.getParameter("ans").equals("16")) {
                    hs.setAttribute("gender", "FEMALE");
                    hs.setAttribute("agefactor", null);
                }

                facindex = Integer.parseInt(request.getParameter("facindex"));
                if (request.getParameter("next") != null) {

                    int ansid = Integer.parseInt(request.getParameter("ans"));
                    FactorOption ansfo = (FactorOption) ss.get(FactorOption.class, ansid);
                    if (ansfo.getFactorId().equals(new FactorDetails(2))) {
                        hs.setAttribute("agefactor", ansfo);
                    }

                    PatientfactorAnswer currentans = new PatientfactorAnswer();
                    currentans.setFactorId(ansfo.getFactorId());
                    currentans.setFactorOptionId(ansfo);
                    currentans.setPId(p1);

                    if (finalans.size() > 0) {
                        ArrayList<PatientfactorAnswer> finalans1 = finalans;
                        for (int i1 = 0; i1 < finalans1.size(); i1++) {
                            PatientfactorAnswer c1 = finalans.get(i1);
                            if (c1.getFactorId().equals(currentans.getFactorId())) {
                                finalans.remove(i1);
                            }
                        }
                    }
                    finalans.add(facindex, currentans);

                    //                        System.out.println(finalans.get(0).getFactorOptionId());
                    //                        System.out.println(finalans.get(1).getFactorOptionId());
                    //                        System.out.println(finalans.get(2).getFactorOptionId());
                    System.out.println("size : " + finalans.size());
                    facindex = facindex + 1;
                } else if (request.getParameter("prev") != null) {
                    if (facindex > 0) {
                        facindex = facindex - 1;
                    }
                }

                if (finalans.size() > 0) {
                    String gender = "MALE";
                    if (hs.getAttribute("gender") != null) {
                        gender = (String) hs.getAttribute("gender");
                    }
                    if (gender.equals("MALE") && hs.getAttribute("agefactor") != null) {

                        FactorOption agefac = (FactorOption) hs.getAttribute("agefactor");

                        for (PatientfactorAnswer c1 : finalans) {
                            points = points + c1.getFactorOptionId().getMen();
                        }
                        if (points < 0) {
                            points = 0;
                        }
                        if (points > 14) {
                            points = 14;
                        }
                        //                Count risk on point
                        Criteria cr2 = ss.createCriteria(AbsulateRiskMale.class);
                        cr2.add(Restrictions.eq("foptionid", agefac));
                        cr2.add(Restrictions.eq("point", points));
                        ArrayList<AbsulateRiskMale> riskdata = (ArrayList<AbsulateRiskMale>) cr2.list();
                        if (riskdata.size() > 0) {
                            AbsulateRiskMale risk = riskdata.get(0);
                            if (risk.getGreen() != null) {
                                metervalue = risk.getGreen();
                            } else if (risk.getViolet() != null) {
                                metervalue = risk.getViolet();
                            } else if (risk.getYellow() != null) {
                                metervalue = risk.getYellow();
                            } else if (risk.getRed() != null) {
                                metervalue = risk.getRed();
                            }
                            if (risk.getRGreen() != null) {
                                rmetervalue = risk.getRGreen();
                            } else if (risk.getRViolet() != null) {
                                rmetervalue = risk.getRViolet();
                            } else if (risk.getRYellow() != null) {
                                rmetervalue = risk.getRYellow();
                            } else if (risk.getRRed() != null) {
                                rmetervalue = risk.getRRed();
                            }
                            //                                System.out.println("Risk data : " + risk);
                        }
                        System.out.println("Meter value : " + metervalue);
                        System.out.println("RMeter value : " + rmetervalue);
                        //                        Set Chart Data    
                        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();
                        }
                        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();
                        }
                        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();
                        }
                        chartdata[3] = "54";
                    }
                    if (gender.equals("FEMALE") && hs.getAttribute("agefactor") != null) {

                        FactorOption agefac = (FactorOption) hs.getAttribute("agefactor");

                        for (PatientfactorAnswer c1 : finalans) {
                            points = points + c1.getFactorOptionId().getWomen();
                        }
                        if (points < 0) {
                            points = 0;
                        }
                        if (points > 17) {
                            points = 17;
                        }
                        //                Count risk on point
                        Criteria cr2 = ss.createCriteria(AbsoluteRiskFemale.class);
                        cr2.add(Restrictions.eq("foptionid", agefac));
                        cr2.add(Restrictions.eq("point", points));
                        ArrayList<AbsoluteRiskFemale> riskdata = (ArrayList<AbsoluteRiskFemale>) cr2.list();
                        if (riskdata.size() > 0) {
                            AbsoluteRiskFemale risk = riskdata.get(0);
                            if (risk.getGreen() != null) {
                                metervalue = risk.getGreen();
                            } else if (risk.getViolet() != null) {
                                metervalue = risk.getViolet();
                            } else if (risk.getYellow() != null) {
                                metervalue = risk.getYellow();
                            } else if (risk.getRed() != null) {
                                metervalue = risk.getRed();
                            }
                            if (risk.getRGreen() != null) {
                                rmetervalue = risk.getRGreen();
                            } else if (risk.getRViolet() != null) {
                                rmetervalue = risk.getRViolet();
                            } else if (risk.getRYellow() != null) {
                                rmetervalue = risk.getRYellow();
                            } else if (risk.getRRed() != null) {
                                rmetervalue = risk.getRRed();
                            }
                            //                                System.out.println("Risk data : " + risk);
                        }
                        //                            System.out.println("Meter value : " + metervalue);
                        //                        Set Chart Data    
                        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();
                        }
                        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();
                        }
                        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();
                        }
                        chartdata[3] = "30";
                    }
                }

            }
            System.out.println("sssss : " + hs.getAttribute("agefactor"));
            Criteria cr = ss.createCriteria(FactorDetails.class);
            ArrayList<FactorDetails> fdlist = (ArrayList<FactorDetails>) cr.list();
            if (fdlist.size() > facindex) {
                FactorDetails factor = fdlist.get(facindex);

                Criteria cr1 = ss.createCriteria(FactorOption.class);
                cr1.add(Restrictions.eq("factorId", factor));
                ArrayList<FactorOption> folist = (ArrayList<FactorOption>) cr1.list();
                if (folist.size() > 0) {
                    request.setAttribute("folist", folist);
                }

                //                    metervalue = metervalue + 3;
                hs.setAttribute("metervalue", metervalue);
                hs.setAttribute("rmetervalue", rmetervalue);
                hs.setAttribute("points", points);
                hs.setAttribute("finalans", finalans);
                hs.setAttribute("chartdata", chartdata);
                request.setAttribute("factor", factor);
                request.setAttribute("facindex", facindex);

            } else {
                //                    Save In database final ans
                ArrayList<PatientfactorAnswer> save = (ArrayList<PatientfactorAnswer>) hs
                        .getAttribute("finalans");
                Calculationmeteredata cm = new Calculationmeteredata();

                cm.setFinalpoints((Integer) hs.getAttribute("points"));
                cm.setMetervalue((Integer) hs.getAttribute("metervalue"));
                cm.setRmetervalue((Double) hs.getAttribute("rmetervalue"));
                cm.setDate(new Date().toString());
                cm.setPId(p1);

                ss.save(cm);
                if (cm.getCalculationmeteredataId() != null) {
                    for (PatientfactorAnswer pans : save) {
                        pans.setCalculationmeteredataId(cm);
                        ss.save(pans);
                    }
                }
                tr.commit();
                RequestDispatcher rd = request.getRequestDispatcher(
                        "systemgeneratedreport?calcid=" + cm.getCalculationmeteredataId());
                rd.forward(request, response);
            }

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

    }
}

From source file:GetAgentDetailServ.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();/*w ww .  j  a v a2s  . com*/

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

        Criteria cr = ss.createCriteria(AgentDetail.class);
        cr.add(Restrictions.eq("aId", aid));
        AgentDetail ad = (AgentDetail) cr.uniqueResult();

        Criteria cr1 = ss.createCriteria(AgentFeedback.class);
        cr1.add(Restrictions.eq("aId", ad));
        ArrayList<AgentFeedback> afl = (ArrayList<AgentFeedback>) cr1.list();

        request.setAttribute("ad", ad);

        if (!afl.isEmpty()) {
            request.setAttribute("afl", afl);
        }

        System.out.println("the aid is " + ad.getAId());
        System.out.println("the uid is " + ad.getUId().getUId());

        Criteria cr2 = ss.createCriteria(PropDetail.class);
        cr2.add(Restrictions.eq("uId", ad.getUId()));
        ArrayList<PropDetail> pdl = (ArrayList<PropDetail>) cr2.list();

        if (!pdl.isEmpty()) {
            request.setAttribute("pdl", pdl);
        }
        System.out.println("the size of proplist is " + pdl.size());

        RequestDispatcher rd = request.getRequestDispatcher("agentdescription.jsp");
        rd.forward(request, response);

    }
}

From source file:searchServ.java

public static SystemUser gameExists(String gameName) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();/*from ww  w  .  ja v  a  2s  .co  m*/
    Criteria crit = session.createCriteria(WishListItem.class);
    Criterion cond1 = Restrictions.eq("title", gameName);
    crit.add(Restrictions.and(cond1));
    return (SystemUser) crit.uniqueResult();
}

From source file:GetPropDetailServ.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  www  . ja v  a2 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 = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

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

        ArrayList<AssetRecords> a = new ArrayList<>();
        String s = "SELECT sum(Price),Year FROM the_asset_consultancy.AssetRecords where Assetid=" + pid
                + " group by Price,Year ;";

        SQLQuery query = ss.createSQLQuery(s);

        List<Object[]> lt = query.list();

        for (Object[] op : lt) {
            AssetRecords ar = new AssetRecords();
            ar.setPrice(op[0].toString());
            ar.setYear(op[1].toString());
            a.add(ar);

            out.println(a);

        }
        System.out.println("Size of array:" + a.size());
        request.setAttribute("al", a);

        Collections.sort(a, new Comparator<AssetRecords>() {

            @Override
            public int compare(AssetRecords o1, AssetRecords o2) {
                return o1.getYear().compareTo(o2.getYear());
            }
        });

        Criteria cr = ss.createCriteria(PropDetail.class);
        cr.add(Restrictions.eq("pId", pid));
        PropDetail pd = (PropDetail) cr.uniqueResult();

        System.out.println("property found:" + pd.getPId());

        Criteria cr1 = ss.createCriteria(PropFeedback.class);
        cr1.add(Restrictions.eq("pId", pd));

        ArrayList<PropFeedback> pfl = (ArrayList<PropFeedback>) cr1.list();

        request.setAttribute("pd", pd);

        if (!pfl.isEmpty()) {
            request.setAttribute("pfl", pfl);
        } else {
            System.out.println("No property feedback available");
        }

        //for the random agent display

        Criteria cr2 = ss.createCriteria(AgentDetail.class);

        ArrayList<AgentDetail> adl = (ArrayList<AgentDetail>) cr2.list();

        if (!adl.isEmpty()) {
            request.setAttribute("adl", adl);

        } else {
            System.out.println("random agent list empty");
        }

        RequestDispatcher rd = request.getRequestDispatcher("single.jsp");
        rd.forward(request, response);

    } catch (HibernateException e) {
        out.print(e.getMessage());
    }
}

From source file:systemgen_report_doc.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  www.j  av 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 {
        /* 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("doctor") != 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("doctor_view_systemreport_full.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:systemgeneratedreport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  ww  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");
    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: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;/* ww w  . j  a  va2 s  .  co m*/
}