wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.vci.client.uif.actions.client;
 
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.vci.client.common.datatype.VTDouble;
import com.vci.client.common.datatype.VTInteger;
import com.vci.client.common.datatype.VTLong;
import com.vci.client.common.datatype.VTString;
import com.vci.client.ui.swing.components.VCIJOptionPane;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.omd.constants.BusinessConstants;
import com.vci.omd.constants.LinkConstants;
 
/**
 * 业务对象属性校验器
 * @author VCI-STGK006
 *
 */
public class BusinessObjectAttributeChecker {
    
    private Map<String, AttribItem> attrItemMap = null;
    
    private Component owner = null;
    
    /**
     * 
     */
    public BusinessObjectAttributeChecker(Component owner){
        this.owner = owner;
    }
    
    /**
     * 查询所有属性
     */
    public void getAllAttribute() {
        attrItemMap = UIFCache.getInstance().getAllAttributeMap();
    }
    
    /**
     * 校验属性值
     * @param attrMap key:属性名称 value:属性值
     * @return true:通过
     */
    public boolean validateAttr(Map<String,String> attrMap){
        if(attrMap != null && !attrMap.isEmpty()){
            Set<Entry<String, String>> set = attrMap.entrySet();
            for (Entry<String, String> entry : set) {
                String key = entry.getKey();
                if(key != null && key.indexOf(".") != -1){
                    key = key.substring(key.lastIndexOf(".") + 1);
                }
                String value = entry.getValue();
                if(!validateAttr(key, value)){
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }
    }
    
    /**
     * 校验属性值
     * @param attrName 属性名称
     * @param attrVal 属性值
     * @return true:通过
     */
    public boolean validateAttr(String attrName, String attrVal){
        getAllAttribute();
        try{
            String code = isAttrValValid(attrName, attrVal);
            if(code.equals("000")){
                return true;
            } else {
                AttribItem attrItem = attrItemMap.get(attrName);
                if(attrItem != null){
                    showMessage(attrItem.label, attrVal, code);
                } else {
                    showMessage(attrName, attrVal, code);
                }
                return false;
            }
        } catch (Exception e){
            e.printStackTrace();
            VCIJOptionPane.showMessage(owner, "属性校验错误:" + e.getMessage());
            return false;
        }
    }
    
    /**
     * 
     * @param attrName
     * @param attrVal
     * @return
     */
    private String isAttrValValid(String attrName, String attrVal){
        //XXX 判断是否系统属性 有肯能系统属性和用户属性重名的情况 导致属性没有校验
        if (BusinessConstants.BO_CONSTANTS.containsKey(attrName.toUpperCase())
                || LinkConstants.LO_CONSTANTS.containsKey(attrName.toUpperCase())) {
            return "000";
        }
        String code = "000";
        AttribItem attrItem = attrItemMap.get(attrName);
        //XXX 位置属性暂时不校验
        if(attrItem == null){
            return "000";
        }
        //得到类型参数设置
        Map<String, String> otherMap = new HashMap<String, String>();
        String other = attrItem.other;
        if(other != null){
            String[] temps = other.split(";");
            if(temps != null && temps.length > 0){
                for(String temp : temps){
                    String[] sets = temp.split("=");
                    if(sets != null && sets.length == 2){
                        otherMap.put(sets[0].trim(), sets[1].trim());
                    }
                }
            }
        }
        String attrType = attrItem.vtDataType;
        if (attrType.equals("VTString")){
            //校验字符串
            code = checkVTStringValValid(otherMap, attrItem.rage, attrName, attrVal);
        } else if (attrType.equals("VTInteger")){
            //校验整数
            code = checkVTIntegerValValid(otherMap, attrItem.rage, attrName, attrVal);
        } else if (attrType.equals("VTLong")){
            //校验长整数
            code = checkVTLongValValid(otherMap, attrItem.rage, attrName, attrVal);
        } else if (attrType.equals("VTDouble")){
            //校验双精度浮点型
            code = checkVTDoubleValValid(otherMap, attrItem.rage, attrName, attrVal);
        } else if (attrType.equals("VTBoolean")){
            //校验Boolean型
            code = checkVTBooleanValValid(otherMap, attrItem.rage, attrName, attrVal);
        } else if (attrType.equals("VTImage")){
            code = "000";
        } else if (attrType.equals("VTDate")){
            code = "000";
        } else if (attrType.equals("VTTime")){
            code = "000";
        } else if (attrType.equals("VTDateTime")) {
            code = "000";
        } else if (attrType.equals("VTNote")){
            code = "000";
        } else if (attrType.equals("VTFilePath")) {
            code = "000";
        } else {
            code = "000";
        }
        
        return code;
    }
    
    /**
     * 判断VTString类型的属性值是否符合校验要求
     * @param setMap  
     *   VTString型设置参数
     *     btm = ; 当设置为业务对象时此属性不为空
     *     allowNull = yes; 是否允许为空
     *     length = 100; 长度
     *     enumName = deptFileClsf 当设置为枚举时此属性不为空
     * @param attrName 属性名称
     * @param attrVal 属性值
     * @return code
     */
    private String checkVTStringValValid(Map<String,String> setMap, String rage, String attrName, String attrVal) {
        //校验是否可以为空
        if(setMap.get("allowNull") != null && setMap.get("allowNull").equalsIgnoreCase("no")){
            if(attrVal == null || attrVal.trim().equals("")){
                return "999";
            }
        }
        if(attrVal != null && !attrVal.trim().equals("")){
            VTString obj = new VTString(attrVal);
            //校验长度
            if (obj.beyondMaxLength(attrName)) {
                return "001";
            }
            //校验是否为业务对象
            if(setMap.get("btm") != null && !setMap.get("btm").equals("")){
                //XXX 如果是业务类型或枚举类型都应该是通过选择得到的 还有必要验证吗?
            }
            //校验是否为枚举
            if(setMap.get("enumName") != null && !setMap.get("enumName").equals("")){
                if(!obj.checkRageValueByRage(rage)){
                    return "002";
                }
            }
        }
        return "000";
    }
 
    /**
     * 判断VTInteger类型的属性值是否符合校验要求
     * @param setMap 
     *      VTInteger类型参数设置
     *      allowNull = yes;
     *      enumName = docseclevel;
     * @param rage 取值范围
     * @param attrName 属性名称
     * @param attrVal 属性值
     * @return code
     */
    private String checkVTIntegerValValid(Map<String,String> setMap, String rage, String attrName, String attrVal) {
        //校验是否可以为空
        if(setMap.get("allowNull") != null && setMap.get("allowNull").equalsIgnoreCase("no")){
            if(attrVal == null || attrVal.trim().equals("")){
                return "999";
            }
        }
        //attrVla不为空继续校验
        if(attrVal != null && !attrVal.trim().equals("")){
            //校验输入是否为整数
            Pattern p = Pattern.compile("^[-]?(([1-9]+[\\d]*)|(0?))$");
            Matcher m = p.matcher(attrVal);
            if(!m.matches()){
                return "004";
            }
            //验证取值范围、
            try {
                VTInteger obj = new VTInteger(Integer.valueOf(attrVal).intValue());
                if (!obj.checkRageValueByRage(rage)) {
                    return "002";
                }
            } catch (Exception e) {
                return "005";
            }
        }
        return "000";
    }
    
    /**
     * 判断VTLong类型的属性值是否符合校验要求
     * @param setMap
     *      VTLong类型参数设置
     *      allowNull = yes;
     *      enumName = docseclevel;
     * @param rage 取值范围
     * @param attrName 属性名
     * @param attrVal 属性值
     * @return code
     */
    private String checkVTLongValValid(Map<String,String> setMap, String rage, String attrName, String attrVal) {
        //校验是否可以为空
        if(setMap.get("allowNull") != null && setMap.get("allowNull").equalsIgnoreCase("no")){
            if(attrVal == null || attrVal.trim().equals("")){
                return "999";
            }
        }
        if(attrVal != null && !attrVal.trim().equals("")){
            //验证是否为长整数
            Pattern p = Pattern.compile("^[-]?(([1-9]+[\\d]*)|(0?))$");
            Matcher m = p.matcher(attrVal);
            if(!m.matches()){
                return "004";
            }
            VTLong obj = new VTLong(Long.valueOf(attrVal).longValue());
            //验证取值范围
            if (!obj.checkRageValueByRage(rage)) {
                return "002";
            }
        }
        return "000";
    }
    
    /**
     * 
     * @param attrName
     * @param attrVal
     * @return
     */
    /**
     * 判断VTDouble类型的属性值是否符合校验要求
     * @param setMap
     *   VTDouble类型属性验证
     *     allowNull = yes; 是否为空
     *     accuracy = 2; 精度
     *     length = 20 长度
     * @param rage 取值范围
     * @param attrName 属性名
     * @param attrVal 属性值
     * @return code
     */
    private String checkVTDoubleValValid(Map<String,String> setMap, String rage, String attrName, String attrVal) {
        //校验是否可以为空
        if(setMap.get("allowNull") != null && setMap.get("allowNull").equalsIgnoreCase("no")){
            if(attrVal == null || attrVal.trim().equals("")){
                return "999";
            }
        }
        //校验是否超出最大取值范围 超出取值范围
        if(attrVal != null && !attrVal.trim().equals("")){
            //校验是否为小数数字
            Pattern p = Pattern.compile("^[-]?(([1-9]+[\\d]*[\\.][\\d]+)|([0][\\.][\\d]+)|([1-9]+[\\d]*)|(0?))$");
            Matcher m = p.matcher(attrVal);
            if(!m.matches()){
                return "004";
            }
            
            VTDouble obj = new VTDouble(Double.valueOf(attrVal));
            if (obj.beyondMaxLength(attrName)) {
                return "003";
            } else if (!obj.checkRageValueByRage(rage)) {
                return "002";
            }
            //XXX 还有个精度没有校验
        }
        return "000";
    }
    
    /**
     * 判断TVBoolean类型属性是否符合校验规则
     * @param setMap
     *   TVBoolean类型属性设置:
     *     allowNull = yes
     * @param rage 取值范围
     * @param attrName 属性名
     * @param attrVal 属性值
     * @return code
     */
    private String checkVTBooleanValValid(Map<String,String> setMap, String rage, String attrName, String attrVal) {
        //校验是否可以为空
        if(setMap.get("allowNull") != null && setMap.get("allowNull").equalsIgnoreCase("no")){
            if(attrVal == null || attrVal.trim().equals("")){
                return "999";
            }
        }
        return "000";
    }
    
    /**
     * 显示检查信息
     * @param attrName
     * @param attrValue
     * @param code
     */
    private void showMessage(String attrName, String attrValue, String code){
        code = "uifmodel.plm.uif.actions.validate." + code;
        UIFUtils.showMessage(owner, code, attrName, attrValue);
    }
}