liuxm
2024-06-13 f823fa4a9283debfa7cb6d79fc1f3c7099f9b3ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.dy.pmsBase.util;
 
import com.dy.common.util.ConfigXml;
import com.dy.common.util.MD5;
import com.dy.common.util.NumUtil;
import com.dy.common.webListener.ConfigListener;
import com.dy.pmsGlobal.daoBa.BaPrivilegeMapper;
import com.dy.pmsGlobal.daoBa.BaPrivilegeMapper;
import com.dy.pmsGlobal.pojoBa.BaPrivilege;
import com.dy.pmsGlobal.pojoBa.BaUser;
import org.jdom2.Document;
import org.jdom2.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
 
import java.net.URL;
import java.util.List;
 
/**
 * 监听器,实现功能:在系统启动时初始化,向数据库中插入数据
 * 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后
 * Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即privilegeDao。
 * 所以采用了Spring事件监听器来实现
 */
@Component
public class PrivilegeListener implements ApplicationListener<ApplicationReadyEvent> {
 
    private BaPrivilegeMapper privilegeDao ;
 
    @Autowired
    public void setprivilegeDao(BaPrivilegeMapper privilegeDao){
        this.privilegeDao = privilegeDao ;
    }
 
 
    /**
     * SpringBoot容器已经准备好了
     * @param event 事件
     */
    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        try {
            //等1秒,等待com.alibaba.druid.pool.DruidDataSource实始化完成
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(!this.existPrivileges()){
                this.init(event);
            }
        }
    }
 
    /**
     * 实始化
     */
    @SuppressWarnings("unused ")
    private void init(ApplicationReadyEvent event){
        try {
            URL configFileURL = ConfigListener.class.getResource("/privileges-config.xml" );
            ConfigXml configXml = new ConfigXml() ;
            Document doc = configXml.createDom(configFileURL) ;
            this.doInit(configXml, doc);
        } catch (Exception e) {
            System.out.println("系统启动时,初始化配置出错 !");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    private void doInit(ConfigXml configXml, Document doc) throws Exception{
        if(configXml != null && doc != null){
            Element prs = configXml.getElement(doc, "config.privileges") ;
            if(prs != null){
                List<Element> list = prs.getChildren() ;
                if(list != null){
                    for(Element ele : list){
                        String num = ele.getAttributeValue("num") ;
                        String name = ele.getAttributeValue("name") ;
                        String type = ele.getAttributeValue("type") ;
                        String typeName = ele.getAttributeValue("type_name") ;
                        this.savePrivilege(num, name, type,typeName);
                    }
                }
            }
        }
    }
 
 
    /**
     * 数据库中是否存在行政区划数据
     * @return 存在否
     */
    private boolean existPrivileges(){
        Long total = this.privilegeDao.selectCount() ;
        return (total != null && total > 0) ;
    }
 
 
    /**
     * 保存权限
     * @param name 编码
     * @param name 名称
     * @param type 类型
     */
    private void savePrivilege(String num, String name, String type,String typeName) throws Exception{
        if((num != null && !num.trim().equals("")) &&
                (name != null && !name.trim().equals("")) &&
                (type != null && !type.trim().equals(""))){
            if(NumUtil.isPlusIntNumber(num) && NumUtil.isPlusIntNumber(type)){
                BaPrivilege po = new BaPrivilege() ;
                po.num = Integer.parseInt(num) ;
                po.name = name ;
                po.type = Integer.parseInt(type) ;
                po.typeName = typeName ;
                this.privilegeDao.insertSelective(po) ;
            }
        }
    }
 
}