com.hihsoft.sso.business.service.impl.TsysOrgServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.hihsoft.sso.business.service.impl.TsysOrgServiceImpl.java

Source

/**
 * Copyright (c) 2013-2015 www.javahih.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hihsoft.sso.business.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.hihframework.core.utils.BeanUtils;
import com.hihframework.core.utils.StringHelpers;
import com.hihframework.exception.ServiceException;
import com.hihsoft.baseclass.service.impl.BaseServiceImpl;
import com.hihsoft.sso.business.model.TsysOrg;
import com.hihsoft.sso.business.service.TsysOrgService;
import com.hihsoft.sso.systempublic.constants.Constant;

@Service(value = "tsysOrgService")
public class TsysOrgServiceImpl extends BaseServiceImpl implements TsysOrgService {
    /************ TSYSORG ******************/
    private static final String ALLTSYSORG_HQL = " from TsysOrg";

    /************ TSYSORG ******************/
    private static final String TSYSORGById_HQL = " from TsysOrg tsysOrg where tsysOrg.orgid=?";

    /************ ????Hibernate HQLTSYSORG ******************/
    private static final StringBuffer QUERY_TSYSORG_HQL = new StringBuffer(" from TsysOrg tsysOrg where 1=1");

    /************ ????JDBC SQLTSYSORG ******************/
    private static final StringBuffer QUERY_TSYSORG_SQL = new StringBuffer("select * from T_SYS_ORG t where 1=1");

    /**
     * ?TsysOrg?
     * 
     * @param tsysOrg
     * @throws DataAccessException
     */
    public void saveOrUpdateTsysOrg(final TsysOrg tsysOrg) throws ServiceException {
        baseDAO.doInHibernate(new HibernateCallback<Void>() {
            public Void doInHibernate(Session session) throws HibernateException, SQLException {
                Transaction tx = session.beginTransaction();
                if (StringHelpers.notNull(tsysOrg.getOrgid())) {
                    //??? ? ??? 
                    List<TsysOrg> tsysOrgs = (List<TsysOrg>) session.createQuery(" from TsysOrg where orgno = '"
                            + tsysOrg.getOrgno() + "' and orgid != '" + tsysOrg.getOrgid() + "'").list();
                    if (tsysOrgs.size() > 0) {
                        throw new ServiceException("??!");
                    }
                    TsysOrg old = (TsysOrg) session.get(TsysOrg.class, tsysOrg.getOrgid());
                    TsysOrg parent = (TsysOrg) session.get(TsysOrg.class, tsysOrg.getParentorgid());
                    BeanUtils.bean2Bean(tsysOrg, old);
                    old.getTsysOrg().getTsysOrgs().remove(old);
                    parent.getTsysOrgs().add(old);
                    old.setTsysOrg(parent);
                    session.saveOrUpdate(parent);
                    session.saveOrUpdate(old);
                    String regionId = null;
                    if (Integer.valueOf(old.getOrgClass()) <= 2 && old.getOrgSort().equals("1")) {
                        regionId = old.getOrgid();
                    } else
                        while (parent != null) {
                            if (Integer.valueOf(parent.getOrgClass()) <= 2 && parent.getOrgSort().equals("1")) {
                                regionId = parent.getOrgid();
                                break;
                            }
                            parent = parent.getTsysOrg();
                        }
                    if (StringHelpers.notNull(regionId)) {
                        old.setOrgRegion(regionId);
                    }
                    session.saveOrUpdate(old);
                } else {
                    //????
                    List<TsysOrg> tsysOrgs = (List<TsysOrg>) session
                            .createQuery(" from TsysOrg where orgno = '" + tsysOrg.getOrgno() + "'").list();
                    if (tsysOrgs.size() > 0) {
                        throw new ServiceException("??!");
                    }
                    session.saveOrUpdate(tsysOrg);
                }
                tx.commit();
                return null;
            }

        });
    }

    /**
     * TsysOrg?
     * 
     * @param id
     * @throws DataAccessException
     * @author Xiaojf
     * @since 20110624
     */
    public String deleteTsysOrg(final String id) throws ServiceException {
        return baseDAO.doInHibernate(new HibernateCallback<String>() {
            public String doInHibernate(Session session) throws HibernateException, SQLException {
                TsysOrg org = (TsysOrg) session.get(TsysOrg.class, id);
                Set<TsysOrg> subOrgs = org.getTsysOrgs();
                if (subOrgs.size() > 0) {
                    for (TsysOrg o : subOrgs) {
                        //                  if ("2".equals(o.getOrgSort())) return "hasDept";
                        if ("01".equals(o.getOrgstate()))
                            return "hasOrg";
                    }
                }
                int num = getDataTotalNum("from TaclUserinfo where orgid=?", id);
                if (num > 0)
                    return "hasUser";
                String hql = "delete from TsysDataprivilege where orgid in (";
                StringBuffer sb = new StringBuffer();
                eachOrg(org, sb);
                hql += sb.substring(1) + ")";
                session.createQuery(hql).executeUpdate();
                org.setOrgstate(Constant.ORG_STATUS_STOPED);
                session.saveOrUpdate(org);
                hql = "delete from TsysTreeprivilege where orgid in (" + sb.substring(1) + ")";
                session.createQuery(hql).executeUpdate();

                return "success";
            }
        });
    }

    /**
     * HQL????TsysOrg?
     * 
     * @param hql
     * @return List
     * @throws DataAccessException
     */
    public List<?> getTsysOrgByHQL(String hql) throws ServiceException {
        return baseDAO.getValueObjectsByHQL(hql);

    }

    /**
     * TsysOrg?
     * 
     * @param hql
     * @return List
     * @throws DataAccessException
     */
    public List<?> getAllTsysOrg() throws ServiceException {
        return baseDAO.getValueObjectsByHQL(ALLTSYSORG_HQL);
    }

    /**
     * ?TsysOrg?
     * 
     * @param id
     * @throws DataAccessException
     */
    public TsysOrg getTsysOrgById(String id) throws ServiceException {
        TsysOrg tsysOrg = null;
        List<?> list = baseDAO.getValueObjectsByHQL(TSYSORGById_HQL, new Object[] { id });
        if (!list.isEmpty() && list.size() > 0) {
            tsysOrg = (TsysOrg) list.get(0);
        }
        return tsysOrg;
    }

    /**
     * ???TsysOrg?
     * 
     * @param Object
     *            [] object
     * @return List
     * @throws DataAccessException
     */
    public List<?> getTsysOrgByArray(Object[] filter) throws ServiceException {
        return baseDAO.getValueObjectsByHQL(QUERY_TSYSORG_HQL.toString(), filter);
    }

    /**
     * ?
     * 
     * @param hql
     * @param object
     * @return
     * @throws DataAccessException
     */

    public int getTsysOrgDataTotalNum(Object[] filter) throws ServiceException {
        return baseDAO.getDataTotalNum(QUERY_TSYSORG_HQL.toString(), filter);
    }

    /**
     * ???TsysOrg?
     * 
     * @param Map
     *            filter
     * @return List
     * @throws DataAccessException
     */
    public List<?> getTsysOrgByMap(Map<String, Object> filter) throws ServiceException {
        return baseDAO.getPageDataByHQL(QUERY_TSYSORG_HQL.toString(), filter);
    }

    /**
     * 
     * 
     * @param hql
     * @param object
     * @param page_size
     * @param pageNo
     * @return
     * @throws DataAccessException
     */

    public List<?> getTsysOrgPageDataByArray(Object[] filter, int page_size, int pageNo) throws ServiceException {
        return baseDAO.getPageDataByHQL(QUERY_TSYSORG_HQL.toString(), filter, page_size, pageNo);
    }

    /**
     * 
     * 
     * @param hql
     * @param obj
     *            MAP?
     * @param page_size
     * @param pageNo
     * @return
     * @throws DataAccessException
     */
    public List<?> getTsysOrgPageDataByMap(Map<String, Object> filter, int page_size, int pageNo)
            throws ServiceException {
        return baseDAO.getPageDataByHQL(QUERY_TSYSORG_HQL.toString(), filter, page_size, pageNo);
    }

    /**
     * SQL??
     * 
     * @param sql
     * @param object
     * @return
     * @throws DataAccessException
     */
    public List<?> getTsysOrgValueObjectWithSQLByArray(Object[] filter) throws ServiceException {
        return baseDAO.getValueObjectBySQL(QUERY_TSYSORG_SQL.toString(), filter);
    }

    /**
     * ?SQL?? SQL??
     * 
     * @param queryName
     * @param object
     * @return
     * @throws DataAccessException
     */
    public List<?> getTsysOrgValueObjectByNameQuery(String queryName, Object[] filter) throws ServiceException {
        return baseDAO.getValueObjectByNameQuery(queryName, filter);
    }

    /**
     * ?HQL?
     * 
     * @param detachedCriteria
     * @return
     * @throws ServiceException
     */
    public List<?> getTsysOrgValueObjectByDetachedCriteria(DetachedCriteria detachedCriteria)
            throws ServiceException {
        return baseDAO.getValueObjectByDetachedCriteria(detachedCriteria);
    }

    /**
     * ?HQL?
     * 
     * @param detachedCriteria
     * @return
     * @throws ServiceException
     */
    public List<?> getTsysOrgValueObjectByDetachedCriterias(DetachedCriteria detachedCriteria, int arg1, int arg2)
            throws ServiceException {
        return baseDAO.getValueObjectByDetachedCriterias(detachedCriteria, arg1, arg2);
    }
}