KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > validation > DataTypeValidator


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.validation;
22
23 import java.math.BigDecimal JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
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 /**
34  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
35  * @version $Id: DataTypeValidator.java 4008 2006-07-18 16:56:26Z inedelcu $
36  */

37 public class DataTypeValidator implements Validator
38 {
39     public boolean supports(Class JavaDoc klass)
40     {
41         return DataTypeWrapper.class.isAssignableFrom(klass);
42     }
43
44     public void validate(Object JavaDoc 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 JavaDoc strMinValue = (String JavaDoc) dataType.getMinValue();
75         String JavaDoc strMaxValue = (String JavaDoc) 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 JavaDoc minValue = null;
94             BigDecimal JavaDoc maxValue = null;
95             try {
96
97                 minValue = new BigDecimal JavaDoc(strMinValue);
98             } catch(NumberFormatException JavaDoc e) {
99                 if (strMinValue.length() > 0)
100                     errors.rejectValue("dataType.minValue", null, "Invalid number");
101             }
102
103             try {
104
105                 maxValue = new BigDecimal JavaDoc(strMaxValue);
106             } catch(NumberFormatException JavaDoc 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