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) ;
|
}
|
}
|
}
|
|
}
|