zoukankan      html  css  js  c++  java
  • Java-Code-Tree:TreeService.java

    ylbtech-Java-Code-Tree:TreeService.java
    1.返回顶部
    1、
    package com.sp.manager.util.common.service;
    
    import java.util.List;
    
    import org.springframework.transaction.annotation.Transactional;
    
    import com.sp.manager.util.common.persistence.TreeDao;
    import com.sp.manager.util.common.persistence.TreeEntity;
    import com.sp.manager.util.common.utils.Reflections;
    import com.sp.manager.util.common.utils.StringUtils;
    
    /**
     * Service基类
     * @author
     * @version 2014-05-16
     */
    @Transactional(readOnly = true)
    public abstract class TreeService<D extends TreeDao<T>, T extends TreeEntity<T>> extends CrudService<D, T> {
        
        @Transactional(readOnly = false)
        public void save(T entity) {
            
            @SuppressWarnings("unchecked")
            Class<T> entityClass = Reflections.getClassGenricType(getClass(), 1);
            
            // 如果没有设置父节点,则代表为跟节点,有则获取父节点实体
            if (entity.getParent() == null || StringUtils.isBlank(entity.getParentId()) 
                    || "0".equals(entity.getParentId())){
                entity.setParent(null);
            }else{
                entity.setParent(super.get(entity.getParentId()));
            }
            if (entity.getParent() == null){
                T parentEntity = null;
                try {
                    parentEntity = entityClass.getConstructor(String.class).newInstance("0");
                } catch (Exception e) {
                    throw new ServiceException(e);
                }
                entity.setParent(parentEntity);
                entity.getParent().setParentIds(StringUtils.EMPTY);
            }
            
            // 获取修改前的parentIds,用于更新子节点的parentIds
            String oldParentIds = entity.getParentIds(); 
            
            // 设置新的父节点串
            entity.setParentIds(entity.getParent().getParentIds()+entity.getParent().getId()+",");
            
            // 保存或更新实体
            super.save(entity);
            
            // 更新子节点 parentIds
            T o = null;
            try {
                o = entityClass.newInstance();
            } catch (Exception e) {
                throw new ServiceException(e);
            }
            o.setParentIds("%,"+entity.getId()+",%");
            List<T> list = dao.findByParentIdsLike(o);
            for (T e : list){
                if (e.getParentIds() != null && oldParentIds != null){
                    e.setParentIds(e.getParentIds().replace(oldParentIds, entity.getParentIds()));
                    preUpdateChild(entity, e);
                    dao.updateParentIds(e);
                }
            }
            
        }
        
        /**
         * 预留接口,用户更新子节前调用
         * @param childEntity
         */
        protected void preUpdateChild(T entity, T childEntity) {
            
        }
    
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Java 最常见的 208 道面试题(第九模块和第十模块答案)
    更灵活的边缘云原生运维:OpenYurt 单元化部署新增 Patch 特性
    高德 Serverless 平台建设及实践
    官宣:恭喜 ChaosBlade 项目进入 CNCF Sandbox
    工商银行分布式服务 C10K 场景解决方案
    云原生的进一步具象化
    阿里云入选 2021 Gartner APM 魔力象限,国内唯一入选云厂商
    云原生下的灰度体系建设
    seata-golang 一周年回顾
    WebAssembly + Dapr = 下一代云原生运行时?
  • 原文地址:https://www.cnblogs.com/storebook/p/9605628.html
Copyright © 2011-2022 走看看