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