Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:com.dz.module.vehicle.VehicleAction.java

/**
 * ????/*  ww  w .j  av a  2 s  . c om*/
 */
public String addServiceRight() {
    Vehicle v = vehicleService.selectById(vehicle);
    if (v == null) {
        request.setAttribute("msgStr", "???");
        return SUCCESS;
    }

    ServiceRightInfo i = new ServiceRightInfo();
    BeanUtils.copyProperties(vehicle, i, new String[] { "state" });
    i.setState(0);
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);
        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleAction.java

public String relookServiceRight() {
    Session s = null;
    Transaction tx = null;/*from  ww  w.j  a  v a2  s .com*/
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Query query = s.createQuery("from ServiceRightInfo where state=0");
        List<ServiceRightInfo> is = query.list();
        for (ServiceRightInfo i : is) {
            Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());
            BeanUtils.copyProperties(i, v, new String[] { "state" });
            s.update(v);
            i.setState(1);
            s.update(i);

            Query q_c = s.createQuery(
                    "select c from Contract c where c.state in (2,3) and c.idNum=:idNum and c.carframeNum=:carframeNum ");
            q_c.setString("idNum", v.getDriverId());
            q_c.setString("carframeNum", v.getCarframeNum());
            q_c.setMaxResults(1);
            Contract c = (Contract) q_c.uniqueResult();

            if (c != null) {
                Query q_va = s.createQuery("from VehicleApproval c where c.contractId=:cid and c.checkType=0 ");
                q_va.setInteger("cid", c.getId());
                q_va.setMaxResults(1);
                VehicleApproval approval = (VehicleApproval) q_va.uniqueResult();
                approval.setOperateCardDate(v.getOperateCardTime());
                s.saveOrUpdate(approval);
            }

        }
        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleAction.java

public String addTrade() {
    Vehicle v = vehicleService.selectById(vehicle);
    if (v == null) {
        request.setAttribute("msgStr", "???");
        return SUCCESS;
    }//w  ww . j  a v a2 s .c  om

    Trade i = new Trade();
    BeanUtils.copyProperties(businessLicense, i, new String[] { "state" });
    i.setCarframeNum(vehicle.getCarframeNum());
    i.setBusinessLicenseComment(vehicle.getBusinessLicenseComment());

    i.setState(0);

    String str = "";
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);

        BusinessLicense bl = (BusinessLicense) s.get(BusinessLicense.class, i.getLicenseNum());
        if (bl != null) {
            str = "?????";
        }

        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??" + str);
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleAction.java

public String relookTrade() {
    Session s = null;
    Transaction tx = null;/* w w  w. java2  s.c  om*/
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Query query = s.createQuery("from Trade where state=0");
        List<Trade> is = query.list();
        for (Trade i : is) {
            Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());
            BusinessLicense ls = new BusinessLicense();
            BeanUtils.copyProperties(i, ls, new String[] { "state" });
            s.saveOrUpdate(ls);

            v.setBusinessLicenseComment(i.getBusinessLicenseComment());
            v.setBusinessLicenseId(i.getLicenseNum());
            s.update(v);
            i.setState(1);
            s.update(i);
        }
        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleAction.java

public String revokeTrade() {
    Session s = null;
    Transaction tx = null;//from   ww w . ja v  a  2  s .  co m
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Trade i = (Trade) s.get(Trade.class, vehicle.getCarframeNum());

        if (i == null || i.getState() != 1) {
            request.setAttribute("msgStr", "");
            return SUCCESS;
        }

        Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());

        BusinessLicense ls = (BusinessLicense) s.get(BusinessLicense.class, i.getLicenseNum());
        if (ls != null) {
            s.delete(ls);
        }

        v.setBusinessLicenseComment(null);
        v.setBusinessLicenseId(null);

        i.setState(0);
        s.saveOrUpdate(i);
        s.saveOrUpdate(v);

        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleApprovalAction.java

public String vehicleApprovalInterrupt() {
    Session s = null;
    Transaction tx = null;//from  w w  w . ja  v a  2 s .co m
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();

        Message msg = new Message();

        msg.setFromUser(vehicleApproval.getInterruptPerson());
        msg.setTime(new Date());

        String reason = vehicleApproval.getInterruptReason();

        User u = (User) s.get(User.class, vehicleApproval.getInterruptPerson());
        vehicleApproval = (VehicleApproval) s.get(VehicleApproval.class, vehicleApproval.getId());
        Contract c = (Contract) s.get(Contract.class, vehicleApproval.getContractId());
        c.setState((short) -2);
        s.saveOrUpdate(c);

        vehicleApproval.setState(-vehicleApproval.getState());
        vehicleApproval.setInterruptTime(new Date());
        vehicleApproval.setInterruptPerson(u.getUid());
        vehicleApproval.setInterruptReason(reason);
        s.saveOrUpdate(vehicleApproval);

        msg.setCarframeNum(c.getCarframeNum());
        msg.setIdNum(c.getIdNum());

        if (vehicleApproval.getCheckType() == 0) {
            msg.setType("");
            if (StringUtils.isNotBlank(c.getCarNumOld())
                    && (c.getContractFrom() == null || c.getContractFrom() == 0)) {
                Query q_v = s.createQuery("from Vehicle where licenseNum = :carNum ");
                q_v.setString("carNum", c.getCarNumOld().trim());
                q_v.setMaxResults(1);
                Vehicle v = (Vehicle) q_v.uniqueResult();
                if (v != null) {
                    v.setReused(false);
                    s.saveOrUpdate(v);
                }
            }
        } else {
            msg.setType("");
        }

        Driver d = (Driver) s.get(Driver.class, c.getIdNum());

        msg.setMsg(String.format(
                "%tF %s?\n" + "%s(%s)  %s(%s) %s ",
                msg.getTime(), u.getUname(), c.getCarNum(), c.getCarframeNum(), d.getName(), c.getIdNum(),
                reason));

        s.saveOrUpdate(msg);

        Query q_us = s.createQuery(String.format(
                "from RelationUr where rid in (select rid from Role where rname in ('%s','%s','%s','%s','%s','%s','%s') ) ",
                "??", "????", "??", "?", "",
                "??", ""));
        List<RelationUr> users = q_us.list();

        for (RelationUr relationUr : users) {
            MessageToUser mu = new MessageToUser();
            mu.setUid(relationUr.getUid());
            mu.setMid(msg.getId());
            mu.setAlreadyRead(false);
            s.saveOrUpdate(mu);
        }

        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleApprovalService.java

/**
 * ?//from   w w w .ja v  a 2  s  .c o  m
 * @param _vehicleApproval
 * @return
 */
public boolean updateVehicleApproval(VehicleApproval _vehicleApproval) {
    HttpServletRequest request = null;
    int uName = 1;

    VehicleApproval vehicleApproval = vehicleApprovalDao.queryVehicleApprovalById(_vehicleApproval.getId());
    int state = vehicleApproval.getState();

    Contract contract = (Contract) ObjectAccess.getObject("com.dz.module.contract.Contract",
            vehicleApproval.getContractId());

    //      if(BooleanUtils.isTrue(contract.getGeneByImport())){
    //         request = new HttpServletRequest(){
    //            @Override
    //            public AsyncContext getAsyncContext() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Object getAttribute(String name) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Enumeration<String> getAttributeNames() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getCharacterEncoding() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public int getContentLength() {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public long getContentLengthLong() {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public String getContentType() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public DispatcherType getDispatcherType() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public ServletInputStream getInputStream() throws IOException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getLocalAddr() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getLocalName() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public int getLocalPort() {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public Locale getLocale() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Enumeration<Locale> getLocales() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getParameter(String name) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Map<String, String[]> getParameterMap() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Enumeration<String> getParameterNames() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String[] getParameterValues(String name) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getProtocol() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public BufferedReader getReader() throws IOException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRealPath(String path) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRemoteAddr() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRemoteHost() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public int getRemotePort() {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public RequestDispatcher getRequestDispatcher(String path) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getScheme() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getServerName() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public int getServerPort() {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public ServletContext getServletContext() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public boolean isAsyncStarted() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isAsyncSupported() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isSecure() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public void removeAttribute(String name) {
    //               // TODO Auto-generated method stub
    //
    //            }
    //
    //            @Override
    //            public void setAttribute(String name, Object o) {
    //               // TODO Auto-generated method stub
    //
    //            }
    //
    //            @Override
    //            public void setCharacterEncoding(String env)
    //                  throws UnsupportedEncodingException {
    //               // TODO Auto-generated method stub
    //
    //            }
    //
    //            @Override
    //            public AsyncContext startAsync() throws IllegalStateException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public AsyncContext startAsync(ServletRequest servletRequest,
    //                  ServletResponse servletResponse)
    //                  throws IllegalStateException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public boolean authenticate(HttpServletResponse response)
    //                  throws IOException, ServletException {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public String changeSessionId() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getAuthType() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getContextPath() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Cookie[] getCookies() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public long getDateHeader(String name) {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public String getHeader(String name) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Enumeration<String> getHeaderNames() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Enumeration<String> getHeaders(String name) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public int getIntHeader(String name) {
    //               // TODO Auto-generated method stub
    //               return 0;
    //            }
    //
    //            @Override
    //            public String getMethod() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Part getPart(String name) throws IOException,
    //                  ServletException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Collection<Part> getParts() throws IOException,
    //                  ServletException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getPathInfo() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getPathTranslated() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getQueryString() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRemoteUser() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRequestURI() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public StringBuffer getRequestURL() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getRequestedSessionId() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public String getServletPath() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public HttpSession getSession() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public HttpSession getSession(boolean create) {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public Principal getUserPrincipal() {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //            @Override
    //            public boolean isRequestedSessionIdFromCookie() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isRequestedSessionIdFromURL() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isRequestedSessionIdFromUrl() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isRequestedSessionIdValid() {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public boolean isUserInRole(String role) {
    //               // TODO Auto-generated method stub
    //               return false;
    //            }
    //
    //            @Override
    //            public void login(String username, String password)
    //                  throws ServletException {
    //               // TODO Auto-generated method stub
    //
    //            }
    //
    //            @Override
    //            public void logout() throws ServletException {
    //               // TODO Auto-generated method stub
    //
    //            }
    //
    //            @Override
    //            public <T extends HttpUpgradeHandler> T upgrade(
    //                  Class<T> handlerClass) throws IOException,
    //                  ServletException {
    //               // TODO Auto-generated method stub
    //               return null;
    //            }
    //
    //         };
    //      }else{
    //         request =  ServletActionContext.getRequest();
    //         uName = ((User)request.getSession().getAttribute("user")).getUid();
    //      }

    if (vehicleApproval.getCheckType() == 0) {
        if (state == 6)//?
        {
            vehicleApproval.setApprovalOfficeDate(new Date());
            vehicleApproval.setState(8);
            vehicleApproval.setOfficeName(uName);
            vehicleApproval.setOfficeRemark(_vehicleApproval.getOfficeRemark());
            vehicleApproval.setIsapprovalOffice(_vehicleApproval.getIsapprovalOffice());

            //            contractDao.changeState(_vehicleApproval.getContractId(),3);

            Session session = null;
            Transaction tx = null;
            try {
                session = HibernateSessionFactory.getSession();
                tx = session.beginTransaction();
                Contract cx = (Contract) session.get(Contract.class, _vehicleApproval.getContractId());
                cx.setState((short) 3);
                session.update(cx);

                Message msg = new Message();
                User u = (User) session.get(User.class, uName);
                msg.setFromUser(uName);
                msg.setTime(new Date());

                msg.setCarframeNum(contract.getCarframeNum());
                msg.setIdNum(contract.getIdNum());
                msg.setType("?");

                Driver d = (Driver) session.get(Driver.class, contract.getIdNum());

                msg.setMsg(String.format("%tF %s?\n"
                        + "%s(%s)  %s(%s) ????",
                        msg.getTime(), u.getUname(), contract.getCarNum(), contract.getCarframeNum(),
                        d.getName(), contract.getIdNum()));

                session.save(msg);

                Query q_u = session.createQuery(
                        "from RelationUr where " + "rid in (select rid from Role where rname = :rname )");

                q_u.setString("rname", "");
                List<RelationUr> users = q_u.list();
                for (RelationUr relationUr : users) {
                    MessageToUser mu = new MessageToUser();
                    mu.setUid(relationUr.getUid());
                    mu.setMid(msg.getId());
                    mu.setAlreadyRead(false);
                    session.saveOrUpdate(mu);
                }
                session.update(vehicleApproval);

                tx.commit();
            } catch (HibernateException e) {
                e.printStackTrace();
                if (tx != null) {
                    tx.rollback();
                }
                return false;
            } finally {
                HibernateSessionFactory.closeSession();
            }

            return true;
        } else if (state == 3)//
        {
            vehicleApproval.setApprovalFinanceDate(new Date());
            vehicleApproval.setState(4);
            vehicleApproval.setFinanceName(uName);
            vehicleApproval.setFinanceRemark(_vehicleApproval.getFinanceRemark());
            vehicleApproval.setIsapprovalFinance(_vehicleApproval.getIsapprovalFinance());
            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        } else if (state == 4)//
        {
            if (!BooleanUtils.isTrue(contract.getGeneByImport())) {
                String rentStr = (String) request.getParameter("contract.rent");
                if (StringUtils.isNotBlank(rentStr)) {
                    double rent = NumberUtils.toDouble(rentStr);
                    contract.setRent(rent);
                    ObjectAccess.saveOrUpdate(contract);
                }
            }

            vehicleApproval.setFinanceManagerApprovalDate(new Date());
            vehicleApproval.setState(5);
            vehicleApproval.setFinanceManagerName(uName);
            vehicleApproval.setFinanceManagerRemark(_vehicleApproval.getFinanceManagerRemark());
            vehicleApproval.setIsapprovalFinanceManager(_vehicleApproval.getIsapprovalFinanceManager());
            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        }
        //         else if(state==4)//???-??
        //         {
        //            vehicleApproval.setBranchManagerApprovalDate(new java.util.Date());
        //            vehicleApproval.setState(5);
        //            vehicleApproval.setBranchManagerName(uName);
        //            vehicleApproval.setBranchManagerRemark(_vehicleApproval.getBranchManagerRemark());
        //            vehicleApproval.setIsapprovalBranchManager(_vehicleApproval.getIsapprovalBranchManager());
        //            return vehicleApprovalDao.executeUpdate(vehicleApproval);      
        //         }
        else if (state == 1)//???-??
        {
            String carframeNum = contract.getCarframeNum();
            if (StringUtils.isEmpty(carframeNum)) {
                request.setAttribute("msgStr",
                        "?--?");
                return false;
            }

            Vehicle ve = ObjectAccess.getObject(Vehicle.class, carframeNum);
            if (ve != null && ve.getLicensePurseNum().matches("^\\w{6}")) {
                Vehicle ov = ObjectAccess.getObject(Vehicle.class, ve.getLicensePurseNum());
                if (ov != null && ov.getState() < 2) {
                    request.setAttribute("msgStr", "???");
                    return false;
                }
            }

            List<Insurance> ilist = ObjectAccess.query(Insurance.class,
                    " carframeNum='" + contract.getCarframeNum() + "'");
            boolean hasJQX = false, hasSX = false;
            for (Insurance insurance : ilist) {
                if (StringUtils.contains(insurance.getInsuranceClass(), "")) {
                    if (new Date().before(insurance.getEndDate())) {
                        hasJQX = true;
                    }
                } else if (StringUtils.contains(insurance.getInsuranceClass(), "?")) {
                    if (new Date().before(insurance.getEndDate())) {
                        hasSX = true;
                    }
                }
            }

            if (!hasJQX) {
                request.setAttribute("msgStr", "");
                return false;
            }

            if (!hasSX) {
                request.setAttribute("msgStr", "");
                return false;
            }

            vehicleApproval.setAssurerApprovalDate(new Date());
            vehicleApproval.setState(2);
            vehicleApproval.setAssurerName(uName);
            vehicleApproval.setAssurerRemark(_vehicleApproval.getAssurerRemark());
            vehicleApproval.setIsapprovalAssurer(_vehicleApproval.getIsapprovalAssurer());

            vehicleApproval.setDamageInsurance(_vehicleApproval.getDamageInsurance());
            vehicleApproval.setOnetimeAfterpay(_vehicleApproval.getOnetimeAfterpay());
            vehicleApproval.setPayBeginDate(_vehicleApproval.getPayBeginDate());
            vehicleApproval.setPayEndDate(_vehicleApproval.getPayEndDate());

            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        } else if (state == 2)//???-??
        {
            //            String carframeNum = contract.getCarframeNum();
            //            if(StringUtils.isEmpty(carframeNum)){
            //               request.setAttribute("msgStr", "?");
            //               return false;
            //            }
            //            
            //            List<Insurance> ilist = ObjectAccess.query(Insurance.class, " carframeNum='"+contract.getCarframeNum()+"'");
            //            boolean hasJQX=false,hasSX = false;
            //            for(Insurance insurance:ilist){
            //               if(StringUtils.equalsIgnoreCase(insurance.getInsuranceClass(),"")){
            //                  if(new Date().before(insurance.getEndDate())){
            //                     hasJQX = true;
            //                  }
            //               }else if (StringUtils.equalsIgnoreCase(insurance.getInsuranceClass(), "??")) {
            //                  if(new Date().before(insurance.getEndDate())){
            //                     hasSX = true;
            //                  }
            //               }
            //            }
            //            
            //            if(!hasJQX){
            //               request.setAttribute("msgStr", "");
            //               return false;
            //            }
            //            
            //            if(!hasSX){
            //               request.setAttribute("msgStr", "");
            //               return false;
            //            }
            //            
            //            if(vehicleApproval.getOperateCardDate()==null){
            //               request.setAttribute("msgStr", "???");
            //               return false;
            //            }

            vehicleApproval.setManagerApprovalDate(new Date());
            vehicleApproval.setState(3);
            vehicleApproval.setManagerName(uName);
            vehicleApproval.setManagerRemark(_vehicleApproval.getManagerRemark());
            vehicleApproval.setIsapprovalManager(_vehicleApproval.getIsapprovalManager());
            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        } else if (state == 5)//??
        {
            String carframeNum = contract.getCarframeNum();
            if (StringUtils.isEmpty(carframeNum)) {
                request.setAttribute("msgStr", "?");
                return false;
            }

            List<Insurance> ilist = ObjectAccess.query(Insurance.class,
                    " carframeNum='" + contract.getCarframeNum() + "'");
            boolean hasJQX = false, hasSX = false;
            for (Insurance insurance : ilist) {
                if (StringUtils.contains(insurance.getInsuranceClass(), "")) {
                    if (new Date().before(insurance.getEndDate())) {
                        hasJQX = true;
                    }
                } else if (StringUtils.contains(insurance.getInsuranceClass(), "?")) {
                    if (new Date().before(insurance.getEndDate())) {
                        hasSX = true;
                    }
                }
            }

            if (!hasJQX) {
                request.setAttribute("msgStr", "");
                return false;
            }

            if (!hasSX) {
                request.setAttribute("msgStr", "");
                return false;
            }

            //            if(vehicleApproval.getOperateCardDate()==null){
            //               request.setAttribute("msgStr", "???");
            //               return false;
            //            }

            vehicleApproval.setApprovalDirectorDate(new Date());
            vehicleApproval.setState(6);
            vehicleApproval.setDirectorName(uName);
            vehicleApproval.setDirectorRemark(_vehicleApproval.getDirectorRemark());
            vehicleApproval.setIsapprovalDirector(_vehicleApproval.getIsapprovalDirector());
            vehicleApproval.setDiscountDays(_vehicleApproval.getDiscountDays());

            contract.setDiscountDays(vehicleApproval.getDiscountDays());
            ObjectAccess.saveOrUpdate(contract);

            //            contractDao.changeState(_vehicleApproval.getContractId(),3);

            return vehicleApprovalDao.executeUpdate(vehicleApproval);
        } else
            return false;
    } else if (vehicleApproval.getCheckType() == 1) {

        if (state == 1) {
            //contract.setAbandonedTime(contract.getAbandonedTime() == null?new Date():contract.getAbandonedTime());
            ObjectAccess.saveOrUpdate(contract);
            vehicleApproval.setAssurerRemark(_vehicleApproval.getAssurerRemark());
            vehicleApproval.setAssurerName(uName);
            vehicleApproval.setAssurerApprovalDate(new Date());
            vehicleApproval.setIsapprovalAssurer(_vehicleApproval.getIsapprovalAssurer());
            vehicleApproval.setState(3);//
        } else if (state == 2) {
            vehicleApproval.setCashierRemark(_vehicleApproval.getCashierRemark());
            vehicleApproval.setCashierName(uName);
            vehicleApproval.setCashierApprovalDate(new Date());
            vehicleApproval.setState(3);
        } else if (state == 3) {
            String carframeNum = contract.getCarframeNum();

            long count = ObjectAccess.execute(String
                    .format("select count(*) from Accident where carId='%s' and status!=3 ", carframeNum));
            if (count > 0) {
                request.setAttribute("msgStr", "?");
                return false;
            }

            vehicleApproval.setManagerRemark(_vehicleApproval.getManagerRemark());
            vehicleApproval.setManagerName(uName);
            vehicleApproval.setManagerApprovalDate(new Date());
            vehicleApproval.setState(4);
            vehicleApproval.setIsapprovalManager(_vehicleApproval.getIsapprovalManager());
        } else if (state == 4) {
            vehicleApproval.setOfficeRemark(_vehicleApproval.getOfficeRemark());
            vehicleApproval.setOfficeName(uName);
            vehicleApproval.setApprovalOfficeDate(new Date());
            vehicleApproval.setState(5);
            vehicleApproval.setIsapprovalOffice(_vehicleApproval.getIsapprovalOffice());
        } else if (state == 5) {
            vehicleApproval.setFinanceRemark(_vehicleApproval.getFinanceRemark());
            vehicleApproval.setFinanceName(uName);
            vehicleApproval.setApprovalFinanceDate(new Date());
            vehicleApproval.setState(6);
            vehicleApproval.setIsapprovalFinance(_vehicleApproval.getIsapprovalFinance());
        } else if (state == 6) {
            String rentStr = (String) request.getParameter("contract.rent");
            if (StringUtils.isNotBlank(rentStr)) {
                double rent = NumberUtils.toDouble(rentStr);
                contract.setRent(rent);
                ObjectAccess.saveOrUpdate(contract);
            }

            vehicleApproval.setFinanceManagerRemark(_vehicleApproval.getFinanceManagerRemark());
            vehicleApproval.setFinanceManagerName(uName);
            vehicleApproval.setFinanceManagerApprovalDate(new Date());
            vehicleApproval.setState(7);
            vehicleApproval.setIsapprovalFinanceManager(_vehicleApproval.getIsapprovalFinanceManager());
        } else if (state == 7) {
            vehicleApproval.setDirectorRemark(_vehicleApproval.getDirectorRemark());
            vehicleApproval.setDirectorName(uName);
            vehicleApproval.setApprovalDirectorDate(new Date());
            vehicleApproval.setState(8);
            vehicleApproval.setIsapprovalDirector(_vehicleApproval.getIsapprovalDirector());

            Vehicle vehicle = ObjectAccess.getObject(Vehicle.class, contract.getCarframeNum());

            String strT = (String) request.getParameter("contract.abandonedChargeTime");
            if (StringUtils.isNotBlank(strT)) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                try {
                    Date abandonedChargeTime = df.parse(strT);
                    contract.setAbandonedChargeTime(abandonedChargeTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            Session session = HibernateSessionFactory.getSession();
            Transaction tx = null;

            try {
                tx = session.beginTransaction();

                vehicle.setDriverId(null);

                if (vehicleApproval.getHandleMatter()) {
                    vehicle.setState(3);
                } else {
                    vehicle.setState(2);
                    if (contract.getAbandonedTime() == null) {
                        contract.setAbandonedTime(contract.getContractEndDate());
                    }

                    //???? -305-25
                    Calendar sp = Calendar.getInstance();
                    sp.setTime(contract.getAbandonedTime());

                    Calendar endTime = Calendar.getInstance();
                    endTime.setTime(contract.getContractEndDate());

                    sp.add(Calendar.MONTH, 1);

                    int amount = 0;

                    while (sp.before(endTime)) {
                        sp.add(Calendar.MONTH, 1);
                        if (sp.after(endTime)) {
                            if (endTime.get(Calendar.DATE) >= 20) {
                                amount += 306 + 25;
                            }
                            break;
                        } else {
                            amount += 306 + 25;
                        }
                    }

                    sp.setTime(contract.getAbandonedTime());
                    if (sp.get(Calendar.DATE) > 26) {
                        sp.add(Calendar.MONTH, 1);
                    }
                    sp.set(Calendar.DATE, 1);

                    ChargePlan tplan = new ChargePlan();
                    tplan.setContractId(contract.getId());
                    tplan.setFee(BigDecimal.valueOf(amount));
                    tplan.setFeeType("plan_sub_contract");
                    tplan.setInTime(new Date());
                    tplan.setComment("???306,25");
                    tplan.setTime(sp.getTime());
                    tplan.setIsClear(false);
                    session.save(tplan);

                    chargeDao.planTransfer(contract.getId(), DateUtil.getNextMonth(contract.getAbandonedTime()),
                            contract.getId(), contract.getAbandonedTime());
                    List<String> dl = Arrays.<String>asList(vehicle.getFirstDriver(), vehicle.getSecondDriver(),
                            vehicle.getThirdDriver(), vehicle.getTempDriver());

                    for (int i = 0; i < dl.size(); i++) {
                        String idNum = dl.get(i);
                        if (!StringUtils.isEmpty(idNum)) {
                            Driver d = (Driver) session.get(Driver.class, idNum);
                            if (d != null && d.getDriverClass() != null) {
                                if (d.getDriverClass().equals("")) {
                                    vehicle.setFirstDriver(null);
                                } else if (d.getDriverClass().equals("")) {
                                    vehicle.setSecondDriver(null);
                                } else if (d.getDriverClass().equals("")) {
                                    vehicle.setThirdDriver(null);
                                } else if (d.getDriverClass().equals("")) {
                                    vehicle.setTempDriver(null);
                                }

                                Driverincar record = new Driverincar(d.getCarframeNum(), d.getIdNum(), "",
                                        new Date());
                                record.setFinished(true);
                                session.saveOrUpdate(record);
                                //driverService.addDriverInCarRecord(record);

                                d.setIsInCar(false);

                                d.setRestTime(null);
                                d.setCarframeNum(null);
                                d.setDriverClass(null);
                                d.setBusinessApplyTime(null);
                                d.setBusinessApplyRegistrant(null);
                                d.setBusinessApplyRegistTime(null);

                                d.setBusinessReciveTime(null);
                                d.setBusinessReciveRegistrant(null);
                                d.setBusinessReciveRegistTime(null);

                                d.setBusinessApplyCancelTime(null);
                                d.setBusinessApplyCancelRegistrant(null);
                                d.setBusinessApplyCancelRegistTime(null);

                                d.setDept(null);
                                d.setStatus(4);

                                session.saveOrUpdate(d);
                            }
                        }
                    }

                    //               contract.setAbandonedFinalTime(new Date());
                }

                //            switch(contract.getAbandonReason()){
                //            case "??":
                //               vehicle.setState(2);
                //               break;
                //            case "":
                //            case "":
                //               vehicle.setState(3);
                //               break;
                //            }

                contract.setState((short) 1);

                session.saveOrUpdate(vehicle);
                session.saveOrUpdate(contract);

                session.saveOrUpdate(vehicleApproval);

                //contractDao.changeState(_vehicleApproval.getContractId(),1);
                Message msg = new Message();
                User u = (User) session.get(User.class, uName);
                msg.setFromUser(uName);
                msg.setTime(new Date());

                msg.setCarframeNum(contract.getCarframeNum());
                msg.setIdNum(contract.getIdNum());
                msg.setType("?");

                Driver d = (Driver) session.get(Driver.class, contract.getIdNum());

                msg.setMsg(String.format("%tF %s?\n"
                        + "%s(%s)  %s(%s) ??",
                        msg.getTime(), u.getUname(), contract.getCarNum(), contract.getCarframeNum(),
                        d.getName(), contract.getIdNum()));

                session.save(msg);

                Query q_u = session.createQuery(
                        "from RelationUr where " + "rid in (select rid from Role where rname = :rname )"
                                + "and uid in (select uid from User where position like :position)");

                q_u.setString("rname", "??");
                q_u.setString("position", "%" + contract.getBranchFirm().trim().charAt(0) + "%");
                List<RelationUr> users = q_u.list();
                for (RelationUr relationUr : users) {
                    MessageToUser mu = new MessageToUser();
                    mu.setUid(relationUr.getUid());
                    mu.setMid(msg.getId());
                    mu.setAlreadyRead(false);
                    session.saveOrUpdate(mu);
                }

                tx.commit();
                return true;
            } catch (HibernateException ex) {
                ex.printStackTrace();
                if (tx != null)
                    tx.rollback();

            } finally {
                session.flush();
                HibernateSessionFactory.closeSession();
            }
            return false;
        }

        return vehicleApprovalDao.executeUpdate(vehicleApproval);
    } else {
        return false;
    }
}

From source file:com.dz.module.vehicle.VehicleDaoImpl.java

@Override
public boolean addVehicle(Vehicle vehicle) throws HibernateException {
    // TODO Auto-generated method stub
    boolean flag = false;
    Session session = null;
    Transaction tx = null;/*from   www .  j  a  v  a2 s.com*/
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();

        session.saveOrUpdate(vehicle);
        tx.commit();
        flag = true;
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return flag;
}

From source file:com.eby.orm.dao.GenericDAO.java

/**
 *
 *//*  ww  w .j av  a  2  s  .c  o m*/
public void saveOrUpdate(final Object o) {
    Session session = sessionFactory.getCurrentSession();
    try {
        session.getTransaction().begin();
        Object object1 = session.merge(o);
        session.saveOrUpdate(object1);
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
    }
}

From source file:com.enonic.cms.store.resource.FileResourceServiceImpl.java

License:Open Source License

private boolean doCreateFolder(Session session, FileResourceName name) {
    if (name == null) {
        return false;
    }/*from w  w w.j  a  v  a  2  s.c o  m*/

    doCreateFolder(session, name.getParent());

    String key = createKey(name);
    VirtualFileEntity entity = findEntity(session, key);
    if (entity != null) {
        return false;
    }

    entity = new VirtualFileEntity();
    entity.setKey(key);
    entity.setBlobKey(null);
    entity.setParentKey(createKey(name.getParent()));
    entity.setLength(-1);
    entity.setName(name.getName());
    entity.setLastModified(System.currentTimeMillis());
    session.saveOrUpdate(entity);
    return true;
}