Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.modules.sys.service; import com.eryansky.common.exception.DaoException; import com.eryansky.common.exception.ServiceException; import com.eryansky.common.exception.SystemException; import com.eryansky.common.model.TreeNode; import com.eryansky.common.orm.entity.StatusState; import com.eryansky.common.orm.hibernate.EntityManager; import com.eryansky.common.orm.hibernate.HibernateDao; import com.eryansky.common.utils.StringUtils; import com.eryansky.common.utils.collections.Collections3; import com.eryansky.modules.sys._enum.OrganType; import com.eryansky.modules.sys.entity.Organ; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.collections.ListUtils; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Organ? Service. * @author &Eryan eryanwcp@gmail.com * @date 2013-09-09 ?21:26:46 */ @Service public class OrganManager extends EntityManager<Organ, Long> { private HibernateDao<Organ, Long> organDao;// DAO???. /** * sessionFactory?DAO???. */ @Autowired public void setSessionFactory(final SessionFactory sessionFactory) { organDao = new HibernateDao<Organ, Long>(sessionFactory, Organ.class); } @Override protected HibernateDao<Organ, Long> getEntityDao() { return organDao; } /** * ?. */ public void saveOrUpdate(Organ entity) throws DaoException, SystemException, ServiceException { logger.debug(":{}"); Assert.notNull(entity, "?[entity]!"); organDao.saveOrUpdate(entity); } /** * ?. */ public void merge(Organ entity) throws DaoException, SystemException, ServiceException { Assert.notNull(entity, "?[entity]!"); organDao.merge(entity); } @Override public void saveEntity(Organ entity) throws DaoException, SystemException, ServiceException { super.saveEntity(entity); } /** * ?. * <br/>?? ?? * @param entity * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public void saveOrgan(Organ entity) throws DaoException, SystemException, ServiceException { Assert.notNull(entity, "?[entity]!"); this.saveEntity(entity); if (entity.getType() != null && OrganType.department.getValue().equals(entity.getType())) { List<Organ> subOrgans = entity.getSubOrgans(); while (!Collections3.isEmpty(subOrgans)) { Iterator<Organ> iterator = subOrgans.iterator(); while (iterator.hasNext()) { Organ subOrgan = iterator.next(); subOrgan.setType(OrganType.department.getValue()); iterator.remove(); subOrgans = ListUtils.union(subOrgans, subOrgan.getSubOrgans()); super.update(subOrgan); } } } } /** * . */ public void deleteByIds(List<Long> ids) throws DaoException, SystemException, ServiceException { super.deleteByIds(ids); } /** * OrganTreeNode * @param organ * @param organType * @param isCascade ?? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ private TreeNode organToTreeNode(Organ organ, Integer organType, boolean isCascade) throws DaoException, SystemException, ServiceException { if (organType != null) { if (!organType.equals(organ.getType())) { return null; } } TreeNode treeNode = new TreeNode(organ.getId().toString(), organ.getName()); // url Map<String, Object> attributes = Maps.newHashMap(); attributes.put("code", organ.getCode()); attributes.put("type", organ.getType()); treeNode.setAttributes(attributes); if (isCascade) { List<TreeNode> childrenTreeNodes = Lists.newArrayList(); for (Organ subOrgan : organ.getSubOrgans()) { TreeNode node = organToTreeNode(subOrgan, organType, isCascade); if (node != null) { childrenTreeNodes.add(node); } } treeNode.setChildren(childrenTreeNodes); } return treeNode; } /** * * @param entity * @param parentId * @param isCascade ?? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public TreeNode getTreeNode(Organ entity, Long parentId, boolean isCascade) throws DaoException, SystemException, ServiceException { TreeNode node = this.organToTreeNode(entity, null, true); List<Organ> subOrgans = this.getByParentId(entity.getId(), StatusState.normal.getValue()); if (subOrgans.size() > 0) { if (isCascade) {// ? List<TreeNode> children = Lists.newArrayList(); for (Organ d : subOrgans) { boolean isInclude = true;// ?? TreeNode treeNode = null; treeNode = getTreeNode(d, parentId, true); // if (parentId != null) { if (!d.getId().equals(parentId)) { treeNode = getTreeNode(d, parentId, true); } else { isInclude = false; } } else { treeNode = getTreeNode(d, parentId, true); } if (isInclude) { children.add(treeNode); node.setState(TreeNode.STATE_CLOASED); } else { node.setState(TreeNode.STATE_OPEN); } } node.setChildren(children); } } return node; } /** * ????. * @param excludeOrganId ?ID ? * @param isCascade ?? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public List<TreeNode> getOrganTree(Long excludeOrganId, boolean isCascade) throws DaoException, SystemException, ServiceException { List<TreeNode> treeNodes = Lists.newArrayList(); // List<Organ> organs = getByParentId(null, StatusState.normal.getValue()); for (Organ rs : organs) { TreeNode rootNode = getTreeNode(rs, excludeOrganId, isCascade); treeNodes.add(rootNode); } return treeNodes; } /** * * ?nameOrgan. * * @param name * ?? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public Organ getByName(String name) throws DaoException, SystemException, ServiceException { if (StringUtils.isBlank(name)) { return null; } name = StringUtils.strip(name);// return organDao.findUniqueBy("name", name); } /** * * ??Organ. * * @param sysCode * ? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public Organ getBySysCode(String sysCode) throws DaoException, SystemException, ServiceException { if (StringUtils.isBlank(sysCode)) { return null; } return organDao.findUniqueBy("sysCode", sysCode); } /** * * ??Organ. * * @param code * ? * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ public Organ getByCode(String code) throws DaoException, SystemException, ServiceException { if (StringUtils.isBlank(code)) { return null; } return organDao.findUniqueBy("code", code); } /** * * ?ID Organ. <br> * orderNo asc,id asc?. * * @param parentId * ID(?null) * @param status * ?? @see com.eryansky.common.orm.entity.StatusState * <br>statusnull :StatusState.normal.getValue() * @return * @throws com.eryansky.common.exception.DaoException * @throws com.eryansky.common.exception.SystemException * @throws com.eryansky.common.exception.ServiceException */ @SuppressWarnings("unchecked") public List<Organ> getByParentId(Long parentId, Integer status) throws DaoException, SystemException, ServiceException { // if (status == null) { status = StatusState.normal.getValue(); } StringBuilder sb = new StringBuilder(); Object[] objs; sb.append("from Organ o where o.status = ? "); sb.append(" and o.parentOrgan.id "); if (parentId == null) { sb.append(" is null "); objs = new Object[] { status }; } else { sb.append(" = ? "); objs = new Object[] { status, parentId }; } sb.append(" order by o.orderNo asc,o.id asc"); List<Organ> list = organDao.createQuery(sb.toString(), objs).list(); return list; } /** * ?. * * @return ? */ public Integer getMaxSort() throws DaoException, SystemException, ServiceException { Iterator<?> iterator = organDao.createQuery("select max(o.orderNo)from Organ o ").iterate(); Integer max = 0; while (iterator.hasNext()) { max = (Integer) iterator.next(); if (max == null) { max = 0; } } return max; } }