wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.client.omd.attribpool.toOutside;
 
import javax.swing.JOptionPane;
 
import com.vci.client.omd.attribpool.ui.APClient;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.common.ClientLog4j;
import com.vci.client.common.datatype.VTBoolean;
import com.vci.client.common.datatype.VTDate;
import com.vci.client.common.datatype.VTDateTime;
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.common.datatype.VTTime;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.omd.dataType.VTDataType;
 
public class CheckAttribValue {
    
    private static CheckAttribValue checkAttribValue = null;
    
    private CheckAttribValue(){
        
    }
 
    public static CheckAttribValue getInstance(){
        if(checkAttribValue == null){
            checkAttribValue = new CheckAttribValue();
        }
        
        return checkAttribValue;
    }
    
    /**
     * 根据属性名 检查将要赋给的属性值是否合法。
     * @param abName
     * @param obj
     * @return
     */
    public boolean checkAttribValue(String abName, Object obj) throws VCIException{
        AttribItem abItem = null;
        try {
            abItem = APClient.getService().getAttribItemByName(abName);
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
 
        //允许为空检查
        String allowNull = getOtherValueByType(abItem.other, "allowNull");
        if(obj == null && allowNull.equals("no")){
            JOptionPane.showMessageDialog(null, "属性值不允许为空", "属性值不允许为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(obj == null){
            return true;
        }
        if(abItem.vtDataType.equals(VTDataType.VTSTRING)){
            //类型检查
            if(!(obj instanceof VTString)){
                JOptionPane.showMessageDialog(null, "VTStirng数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            
            String value_ = ((VTString)obj).getValue();
            if(value_ == null && allowNull.equals("no")){
                JOptionPane.showMessageDialog(null, "属性值不允许为空", "属性值不允许为空", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            //长度检查
            if(value_.length() > VTString.lenMax){
                JOptionPane.showMessageDialog(null, "超过VTStirng最大长度错误", "超过最大长度错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            //值域检查
            if(abItem.rage == null || abItem.rage.equals("")){
                return true;
            }
            boolean rageFlag = ((VTString)obj).checkRageValueByRage(abItem.rage);
            if(!rageFlag){
                JOptionPane.showMessageDialog(null, "不在值域范围错误", "不在值域范围错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTINTEGER)){
            //类型检查
            if(!(obj instanceof VTInteger)){
                JOptionPane.showMessageDialog(null, "VTInteger数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            Integer value_ = ((VTInteger)obj).getValue();
            if(value_ == null && allowNull.equals("no")){
                JOptionPane.showMessageDialog(null, "属性值不允许为空", "属性值不允许为空", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            //值域检查
            if(abItem.rage == null || abItem.rage.equals("")){
                return true;
            }
            boolean rageFlag = ((VTInteger)obj).checkRageValueByRage(abItem.rage);
            if(!rageFlag){
                JOptionPane.showMessageDialog(null, "不在值域范围错误", "不在值域范围错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTLONG)){
            //类型检查
            if(!(obj instanceof VTLong)){
                JOptionPane.showMessageDialog(null, "VTLong数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            
            Long value_ = ((VTLong)obj).getValue();
            if(value_ == null && allowNull.equals("no")){
                JOptionPane.showMessageDialog(null, "属性值不允许为空", "属性值不允许为空", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            
            //值域检查
            if(abItem.rage == null || abItem.rage.equals("")){
                return true;
            }
            boolean rageFlag = ((VTLong)obj).checkRageValueByRage(abItem.rage);
            
            if(!rageFlag){
                JOptionPane.showMessageDialog(null, "不在值域范围错误", "不在值域范围错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTDOUBLE)){
            //类型检查
            if(!(obj instanceof VTDouble)){
                JOptionPane.showMessageDialog(null, "VTDouble数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            Double value_ = ((VTDouble)obj).getValue();
            if(value_ == null && allowNull.equals("no")){
                JOptionPane.showMessageDialog(null, "属性值不允许为空", "属性值不允许为空", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            
            String strV = String.valueOf(value_);
            String acc = strV.substring(strV.indexOf(".") + 1);
            String len = strV.substring(0, strV.indexOf("."));
            //最大精度检查
            if(acc.length() > VTDouble.accMax){
                JOptionPane.showMessageDialog(null, "大于VTDOUBLE最大精度错误", "大于最大精度错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            //最大长度检查
            if(len.length() > VTDouble.lenMax){
                JOptionPane.showMessageDialog(null, "大于VTDOUBLE最大长度错误", "大于最大长度错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            //值域检查
            if(abItem.rage == null || abItem.rage.equals("")){
                return true;
            }
            
            boolean rageFlag = ((VTDouble)obj).checkRageValueByRage(abItem.rage);
            
            if(!rageFlag){
                JOptionPane.showMessageDialog(null, "不在值域范围错误", "不在值域范围错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTBOOLEAN)){
            //类型检查
            if(!(obj instanceof VTBoolean)){
                JOptionPane.showMessageDialog(null, "VTBoolean数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTDATE)){
            //类型检查
            if(!(obj instanceof VTDate)){
                JOptionPane.showMessageDialog(null, "VTDate数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTDATETIME)){
            //类型检查
            if(!(obj instanceof VTDateTime)){
                JOptionPane.showMessageDialog(null, "VTDateTime数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }else if(abItem.vtDataType.equals(VTDataType.VTTIME)){
            //类型检查
            if(!(obj instanceof VTTime)){
                JOptionPane.showMessageDialog(null, "VTTime数据类型错误", "数据类型错误", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        return true;
    }
    
    /**
     * 获取other中指定项目的值
     * @return
     */
    public String getOtherValueByType(String other, String type){
        String[] otherArray = other.split(";");
        for(int i = 0; i < otherArray.length; i++){
            String otherValue = otherArray[i];
            if(otherValue.contains(type)){
                return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length());
            }
        }
        return null;
        
    }
}