com.eryansky.service.sys.DictionaryTypeManager.java Source code

Java tutorial

Introduction

Here is the source code for com.eryansky.service.sys.DictionaryTypeManager.java

Source

/**
 *  Copyright (c) 2012-2014 http://www.eryansky.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.eryansky.service.sys;

import java.util.Iterator;
import java.util.List;

import com.eryansky.common.utils.collections.Collections3;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.eryansky.common.exception.DaoException;
import com.eryansky.common.exception.ServiceException;
import com.eryansky.common.exception.SystemException;
import com.eryansky.common.orm.hibernate.EntityManager;
import com.eryansky.common.orm.hibernate.HibernateDao;
import com.eryansky.entity.sys.DictionaryType;
import com.eryansky.utils.CacheConstants;

/**
 * ?.
 * 
 * @author : &Eryan eryanwcp@gmail.com
 * @date : 2013-1-24 ?3:01:27
 */
@Service
public class DictionaryTypeManager extends EntityManager<DictionaryType, Long> {

    private HibernateDao<DictionaryType, Long> dictionaryTypeDao;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        dictionaryTypeDao = new HibernateDao<DictionaryType, Long>(sessionFactory, DictionaryType.class);
    }

    @Override
    protected HibernateDao<DictionaryType, Long> getEntityDao() {
        return dictionaryTypeDao;
    }

    @CacheEvict(value = { CacheConstants.DICTIONARY_TYPE_ALL_CACHE,
            CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE }, allEntries = true)
    public void saveOrUpdate(DictionaryType entity) throws DaoException, SystemException, ServiceException {
        logger.debug(":{}",
                CacheConstants.DICTIONARY_TYPE_ALL_CACHE + "," + CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE);
        Assert.notNull(entity, "?[entity]!");
        dictionaryTypeDao.saveOrUpdate(entity);
    }

    @CacheEvict(value = { CacheConstants.DICTIONARY_TYPE_ALL_CACHE,
            CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE }, allEntries = true)
    public void deleteByIds(List<Long> ids) throws DaoException, SystemException, ServiceException {
        logger.debug(":{}",
                CacheConstants.DICTIONARY_TYPE_ALL_CACHE + "," + CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE);
        if (!Collections3.isEmpty(ids)) {
            for (Long id : ids) {
                DictionaryType dictionaryType = this.getById(id);
                List<DictionaryType> subDictionaryTypes = dictionaryType.getSubDictionaryTypes();
                if (!subDictionaryTypes.isEmpty()) {
                    dictionaryTypeDao.deleteAll(subDictionaryTypes);
                }
                dictionaryTypeDao.delete(dictionaryType);
            }
        } else {
            logger.warn("?[ids].");
        }
    }

    @CacheEvict(value = { CacheConstants.DICTIONARY_TYPE_ALL_CACHE,
            CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE }, allEntries = true)
    @Override
    public void saveEntity(DictionaryType entity) throws DaoException, SystemException, ServiceException {
        logger.debug(":{}",
                CacheConstants.DICTIONARY_TYPE_ALL_CACHE + "," + CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE);
        super.saveEntity(entity);
    }

    /**
    * ???name.
    * 
    * @param name
    *            ???
    * @return
    * @throws DaoException
    * @throws SystemException
    * @throws ServiceException
    */
    @SuppressWarnings("unchecked")
    public DictionaryType getByName(String name) throws DaoException, SystemException, ServiceException {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        name = StringUtils.strip(name);// 
        List<DictionaryType> list = dictionaryTypeDao
                .createQuery("from DictionaryType d where d.name = ?", new Object[] { name }).list();
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * ?????
     * @param groupDictionaryTypeCode ?
     * @param name   ??
     * @return
     * @throws DaoException
     * @throws SystemException
     * @throws ServiceException
     */
    public DictionaryType getByGroupCode_Name(String groupDictionaryTypeCode, String name)
            throws DaoException, SystemException, ServiceException {
        Assert.notNull(groupDictionaryTypeCode, "?[groupDictionaryTypeCode]!");
        Assert.notNull(name, "?[name]!");
        List<DictionaryType> list = dictionaryTypeDao
                .createQuery("from DictionaryType d where d.groupDictionaryType.code = ? and d.name = ?",
                        new Object[] { groupDictionaryTypeCode, name })
                .list();
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * ??code.
     * 
     * @param code
     *            ??
     * @return
     * @throws DaoException
     * @throws SystemException
     * @throws ServiceException
     */
    @SuppressWarnings("unchecked")
    public DictionaryType getByCode(String code) throws DaoException, SystemException, ServiceException {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        code = StringUtils.strip(code);// 
        List<DictionaryType> list = dictionaryTypeDao
                .createQuery("from DictionaryType d where d.code = ?", new Object[] { code }).list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Cacheable(value = { CacheConstants.DICTIONARY_TYPE_ALL_CACHE })
    public List<DictionaryType> getAll() throws DaoException, SystemException, ServiceException {
        logger.debug(":{}", CacheConstants.DICTIONARY_TYPE_ALL_CACHE);
        return dictionaryTypeDao.getAll();
    }

    /**
     * ?.
     * 
     * @return ?
     * @throws DaoException
     * @throws SystemException
     * @throws ServiceException
     */
    public Integer getMaxSort() throws DaoException, SystemException, ServiceException {
        Iterator<?> iterator = dictionaryTypeDao.createQuery("select max(d.orderNo)from DictionaryType d ")
                .iterate();
        Integer max = null;
        while (iterator.hasNext()) {
            // Object[] row = (Object[]) iterator.next();
            max = (Integer) iterator.next();
        }
        if (max == null) {
            max = 0;
        }
        return max;
    }

    /**
     * .
     * @return
     * @throws DaoException
     * @throws SystemException
     * @throws ServiceException
     */
    @Cacheable(value = { CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE })
    public List<DictionaryType> getGroupDictionaryTypes() throws DaoException, SystemException, ServiceException {
        List<DictionaryType> list = dictionaryTypeDao
                .createQuery("from DictionaryType d where d.groupDictionaryType is null").list();
        logger.debug(":{}", CacheConstants.DICTIONARY_TYPE_GROUPS_CACHE);
        return list;
    }

}