1 21 package com.jaspersoft.jasperserver.war.validation; 22 23 import java.math.BigDecimal ; 24 import java.util.regex.Pattern ; 25 26 import org.springframework.validation.Validator; 27 import org.springframework.validation.Errors; 28 import com.jaspersoft.jasperserver.war.common.JasperServerUtil; 29 import com.jaspersoft.jasperserver.war.dto.DataTypeWrapper; 30 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit; 31 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType; 32 33 37 public class DataTypeValidator implements Validator 38 { 39 public boolean supports(Class klass) 40 { 41 return DataTypeWrapper.class.isAssignableFrom(klass); 42 } 43 44 public void validate(Object object, Errors errors) 45 { 46 DataTypeWrapper wrapper = (DataTypeWrapper) object; 47 DataType dataType = wrapper.getDataType(); 48 if (dataType.getName() == null || dataType.getName().trim().length() == 0) { 49 errors.rejectValue("dataType.name", "error.not.empty"); 50 } else { 51 if(!JasperServerUtil.regExValidateName(dataType.getName())) { 52 errors.rejectValue("dataType.name", null, "Name contains invalid characters"); 53 } 54 if (dataType.getName().length() > 100) { 55 errors.rejectValue("dataType.name", null, "Name is longer than 100 characters"); 56 } 57 } 58 59 if (dataType.getLabel() == null || dataType.getLabel().trim().length() == 0) { 60 errors.rejectValue("dataType.label", "error.not.empty"); 61 } else { 62 if(!JasperServerUtil.regExValidateLabel(dataType.getLabel())) { 63 errors.rejectValue("dataType.label", null, "Label contains invalid characters"); 64 } 65 if (dataType.getLabel().length() > 100) { 66 errors.rejectValue("dataType.label", null, "Label is longer than 100 characters"); 67 } 68 } 69 70 if (dataType.getDescription() != null && dataType.getDescription().length() > 250) { 71 errors.rejectValue("dataType.description", null, "Description is longer than 250 characters"); 72 } 73 74 String strMinValue = (String ) dataType.getMinValue(); 75 String strMaxValue = (String ) dataType.getMaxValue(); 76 77 if (strMinValue != null && strMinValue.length() == 0) 78 dataType.setMinValue(null); 79 if (strMaxValue != null && strMaxValue.length() == 0) 80 dataType.setMaxValue(null); 81 82 if (strMinValue != null && strMinValue.length() > 100) { 83 errors.rejectValue("dataType.minValue", null, "Minimum value is longer than 100 characters"); 84 } 85 if (strMaxValue != null && strMaxValue.length() > 100) { 86 errors.rejectValue("dataType.maxValue", null, "Maximum value is longer than 100 characters"); 87 } 88 if (dataType.getRegularExpr() != null && dataType.getRegularExpr().length() > 100) { 89 errors.rejectValue("dataType.regularExpr", null, "Pattern is longer than 100 characters"); 90 } 91 92 if (dataType.getType() == DataType.TYPE_NUMBER) { 93 BigDecimal minValue = null; 94 BigDecimal maxValue = null; 95 try { 96 97 minValue = new BigDecimal (strMinValue); 98 } catch(NumberFormatException e) { 99 if (strMinValue.length() > 0) 100 errors.rejectValue("dataType.minValue", null, "Invalid number"); 101 } 102 103 try { 104 105 maxValue = new BigDecimal (strMaxValue); 106 } catch(NumberFormatException e) { 107 if (strMaxValue.length() > 0) 108 errors.rejectValue("dataType.maxValue", null, "Invalid number"); 109 } 110 111 if (minValue != null && maxValue != null) 112 if (minValue.compareTo(maxValue) >= 0) 113 errors.rejectValue("dataType.minValue", null, "Minimum value must be smaller than maximum value"); 114 } 115 116 if (dataType.getType() == DataType.TYPE_TEXT && dataType.getRegularExpr() != null && dataType.getRegularExpr().trim().length() > 0) { 117 if ( 118 strMinValue != null 119 && strMinValue.trim().length() > 0 120 && !Pattern.matches(dataType.getRegularExpr(), strMinValue) 121 ) 122 { 123 errors.rejectValue("dataType.minValue", null, "Minimum value does not match pattern"); 124 } 125 if ( 126 strMaxValue != null 127 && strMaxValue.trim().length() > 0 128 && !Pattern.matches(dataType.getRegularExpr(), strMaxValue) 129 ) 130 { 131 errors.rejectValue("dataType.maxValue", null, "Maximum value does not match pattern"); 132 } 133 } 134 } 135 } 136 | Popular Tags |