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