dangsn
2024-12-03 d0ae279ff3b83358d1c07f4481a041c4ad335026
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.vci.web.initTask;
 
import com.vci.client.mw.ClientContextVariable;
import com.vci.client.mw.LaunchModeEnum;
import com.vci.constant.FrameWorkBtmTypeConstant;
import com.vci.corba.common.PLException;
import com.vci.corba.framework.FrameworkServicePrx;
import com.vci.corba.omd.data.AttributeValue;
import com.vci.starter.web.annotation.config.VciConfigField;
import com.vci.starter.web.annotation.config.VciConfigModule;
import com.vci.starter.web.enumpck.SessionStorageTypeEnum;
import com.vci.starter.web.enumpck.UserSecretEnum;
import com.vci.starter.web.pagemodel.SessionInfo;
import com.vci.starter.web.properties.VciSessionProperties;
import com.vci.starter.web.util.ApplicationContextProvider;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
import com.vci.web.properties.VciFramworkCompatibilityProperties;
import com.vci.web.properties.WebProperties;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
 
/**
 *  服务初始化
 * @author weidy
 * @date 2019/12/13
 */
@Component
public class VciWebInitTaskComponent implements ApplicationRunner {
 
    /**
     * 日志对象
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 配置对象
     */
    @Autowired
    private WebProperties webProperties;
 
    /**
     * 平台调用客户端
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    /**
     * 会话的配置
     */
    @Autowired(required = false)
    private VciSessionProperties sessionProperties;
 
    /**
     * 引入兼容的配置
     */
    @Autowired(required = false)
    private VciFramworkCompatibilityProperties compatibilityProperties;
 
    /**
     * 执行初始化
     * @param args 启动的参数
     */
    @Override
    public void run(ApplicationArguments args) {
        initCompatibility();
        initSysadmin();
        initClearSession();
        initSetting();
    }
 
 
 
    /**
     * 清除会话的信息内容
     */
    public void initClearSession(){
        if((sessionProperties != null && sessionProperties.isClearSessionOnStart() )&&
                SessionStorageTypeEnum.DATABASE.equals(SessionStorageTypeEnum.forValue(sessionProperties.getSessionInfoStorageType()))){
            try {
                platformClientUtil.getBOFactoryService().executeUpdateSqlByParams(" delete from  VCI_SESSIONINFO ",new AttributeValue[0]);
            }catch (Throwable e) {
                if (logger.isErrorEnabled()) {
                    logger.error("清除会话信息", e);
                }
            }
        }
    }
 
 
    /**
     * 初始化兼容的内容
     */
    public void initCompatibility(){
        //设置兼容性的表格
        VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY = true;
        String userBtmType = FrameWorkBtmTypeConstant.SM_USER_BTM_TYPE.toLowerCase();
        String roleBtmType = FrameWorkBtmTypeConstant.SM_ROLE_BTM_TYPE.toLowerCase();
        String deptBtmType = FrameWorkBtmTypeConstant.ORG_DEPARTMENT_BTM_TYPE.toLowerCase();
        String functionBtmType = FrameWorkBtmTypeConstant.SM_FUNCTION_BTM_TYPE.toLowerCase();
        VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_BTM_MAP.put(userBtmType, "pluser");
        VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_BTM_MAP.put(roleBtmType, "plrole");
        VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_BTM_MAP.put(deptBtmType, "pldept");
        VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_BTM_MAP.put(functionBtmType, "plfunction");
        //读取表格映射的字段
        if (!CollectionUtils.isEmpty(compatibilityProperties.getUserTableFieldMap())) {
            compatibilityProperties.getUserTableFieldMap().forEach((k, v) -> {
                String key = userBtmType + VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_SEP + k.toLowerCase();
                VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_MAP.put(key, v);
            });
        }
        if (!CollectionUtils.isEmpty(compatibilityProperties.getRoleTableFieldMap())) {
            compatibilityProperties.getRoleTableFieldMap().forEach((k, v) -> {
                String key = roleBtmType + VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_SEP + k.toLowerCase();
                VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_MAP.put(key, v);
            });
        }
        if (!CollectionUtils.isEmpty(compatibilityProperties.getDeptTableFieldMap())) {
            compatibilityProperties.getDeptTableFieldMap().forEach((k, v) -> {
                String key = deptBtmType + VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_SEP + k.toLowerCase();
                VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_MAP.put(key, v);
            });
        }
        if (!CollectionUtils.isEmpty(compatibilityProperties.getFunctionTableFieldMap())) {
            compatibilityProperties.getFunctionTableFieldMap().forEach((k, v) -> {
                String key = functionBtmType + VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_SEP + k.toLowerCase();
                VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY_FIELD_MAP.put(key, v);
            });
        }
    }
 
    /**
     * 初始化用户
     */
    public void initSysadmin(){
        SessionInfo sessionInfo = new SessionInfo();
        sessionInfo.setUserId("sysAdmin");
        sessionInfo.setUserName("系统管理员");
        sessionInfo.setIp(WebUtil.getLocalIp());
        sessionInfo.setUserOid("193C026F-7DB8-27B4-F383-4E8BE083FB07");
        sessionInfo.setUserSecret(UserSecretEnum.PRIVACY.getValue()+"");
        WebUtil.setSessionInfo(sessionInfo);
    }
    /**
     * 初始化相关的配置信息
     */
    public void initSetting() {
        System.out.println("----初始化用于查询配置的用户----");
        ClientContextVariable.setClientLanuchMode(LaunchModeEnum.WebApp);
        System.out.println("----初始化用于查询配置的用户完成----");
        System.out.println("----开始扫描有VciConfigModule注解的类----");
        String[] namesForAnnotation = ApplicationContextProvider.getApplicationContext().getBeanNamesForAnnotation(VciConfigModule.class);
        if(namesForAnnotation!=null && namesForAnnotation.length > 0){
            for(String beanName : namesForAnnotation){
                Object bean = ApplicationContextProvider.getApplicationContext().getBean(beanName);
                List<Field> fields = WebUtil.getAllFieldForObj(bean.getClass());
                Map<String,Field> configFieldMap = new HashMap<>();
                List<Field> hasConfigFields = fields.stream().filter(s -> {
                    s.setAccessible(true);
                    return s.isAnnotationPresent(VciConfigField.class);
                }).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(hasConfigFields)){
                    hasConfigFields.stream().forEach(field -> {
                        field.setAccessible(true);
                        VciConfigField vciConfigField = field.getAnnotation(VciConfigField.class);
                        if(vciConfigField == null){
                            vciConfigField = field.getDeclaredAnnotation(VciConfigField.class);
                        }
                        configFieldMap.put(vciConfigField.value(),field);
                    });
                    FrameworkServicePrx frameworkServicePrx = platformClientUtil.getFrameworkService();
                    configFieldMap.forEach((configField,field)->{
                        String configValue = null;
                        try {
                            configValue = frameworkServicePrx.getConfigValue(configField);
                        } catch (PLException vciError) {
                           if(logger.isErrorEnabled()){
                               logger.error(vciError.code,vciError);
                           }
                        }
                        Class<?> fieldType = field.getType();
                        Method method = WebUtil.getSetmethod(bean.getClass(), field);
                        Object value = null;
                        if(fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)){
                            value = ("on".equalsIgnoreCase(configValue) || "true".equalsIgnoreCase(configValue))?true:false;
                        }else if(fieldType.equals(Double.class) || fieldType.equals(double.class)){
                            value = WebUtil.getDouble(configValue);
                        }else if(fieldType.equals(Long.class) || fieldType.equals(long.class)){
                            value = WebUtil.getLong(configValue);
                        }else if(fieldType.equals(Float.class) || fieldType.equals(float.class)){
                            value = WebUtil.getFloat(configValue);
                        }else if(fieldType.equals(Integer.class) || fieldType.equals(int.class)){
                            value = WebUtil.getInt(configValue);
                        }else{
                            value = configValue;
                        }
                        try {
                            method.invoke(bean,value);
                        } catch (IllegalAccessException e) {
                            if(logger.isErrorEnabled()){
                                logger.error(e.getMessage(),e);
                            }
                        } catch (InvocationTargetException e) {
                            if(logger.isErrorEnabled()){
                                logger.error(e.getMessage(),e);
                            }
                        }
                    });
                }
            }
        }
        //只有当数据密级控制开启的情况下,才会控制流程发起时的密级
        webProperties.setCheckSecretOnProcessStart(webProperties.isCheckClientSessionTimeout()&&webProperties.isSecretRight());
        System.out.println("---读取服务端的配置文件完成---");
    }
}