KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
24
25 import org.springframework.validation.Errors;
26 import org.springframework.validation.Validator;
27
28 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType;
29 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl;
30 import com.jaspersoft.jasperserver.api.metadata.common.domain.ListOfValues;
31 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query;
32 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
33 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
34 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
35 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
36 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
37 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper;
38 import com.jaspersoft.jasperserver.war.dto.ReportUnitWrapper;
39
40 public class InputControlsFlowValidator implements Validator {
41
42     public boolean supports(Class JavaDoc clazz) {
43         return InputControlWrapper.class.isAssignableFrom(clazz);
44     }
45
46     public void validate(Object JavaDoc arg0, Errors arg1) {
47         // TODO Auto-generated method stub
48
}
49
50     public void validateControlNaming(InputControlWrapper dto, Errors errors) {
51
52         InputControl inputControl = dto.getInputControl();
53         if(inputControl.getName()==null || inputControl.getName().trim().length()==0) {
54             errors.rejectValue("inputControl.name", null, "Control Name must not be blank");
55         } else {
56             if(!JasperServerUtil.regExValidateName(inputControl.getName())) {
57                 errors.rejectValue("inputControl.name", null, "Control name contains invalid characters");
58             }else{
59                 Object JavaDoc parentObject=dto.getParentFlowObject();
60                 if(dto.isSubNewMode() && parentObject!=null &&
61                         ReportUnitWrapper.class.isAssignableFrom(parentObject.getClass())){
62                     //Parent flow obj is a ReportUnitWrapper
63
ReportUnitWrapper ruWrapper=(ReportUnitWrapper)parentObject;
64                     List JavaDoc resources=ruWrapper.getReportUnit().getResources();
65                     if(resources!=null && !resources.isEmpty()){
66                         for(int i=0;i<resources.size();i++){
67                             ResourceReference resourceRef = (ResourceReference) resources.get(i);
68                             if(resourceRef.isLocal() && resourceRef.getLocalResource().getName().equals(inputControl.getName()))
69                                     errors.rejectValue("inputControl.name", null, "A Resource with this name is added already");
70                         }
71                     }
72                     List JavaDoc controls=ruWrapper.getReportUnit().getInputControls();
73                     if(controls!=null && !controls.isEmpty()){
74                         for(int i=0;i<controls.size();i++){
75                             ResourceReference controlRef = (ResourceReference) controls.get(i);
76                             if(controlRef.isLocal() && controlRef.getLocalResource().getName().equals(inputControl.getName()))
77                                 errors.rejectValue("inputControl.name", null, "A Control with this name is added already");
78                         }
79                     }
80                     ResourceReference dataSourceRef = ruWrapper.getReportUnit().getDataSource();
81                     if (dataSourceRef != null && dataSourceRef.isLocal()) {
82                         ReportDataSource ds = (ReportDataSource) dataSourceRef.getLocalResource();
83                         if (ds != null
84                                 && ds.getName().equals(inputControl.getName()))
85                             errors.rejectValue("inputControl.name", null,
86                                             "This name is already assigned to the DataSource");
87                     }
88                 }else{
89                     // When in stand alone new mode check name uniquenesss in folder
90
List JavaDoc allResources=dto.getAllResources();
91                     if(dto.isAloneNewMode() && allResources!=null){
92                         for(int i=0;i<allResources.size();i++){
93                             Resource res=(Resource)allResources.get(i);
94                             if(res.getName().trim().equals(inputControl.getName().trim())){
95                                 errors.rejectValue("inputControl.name",
96                                         null, "A Resource with this name already exists");
97                                 break;
98                             }
99                         }
100                     }
101                 }
102                 
103             }
104         }
105
106         if(inputControl.getLabel()==null || inputControl.getLabel().trim().length()==0) {
107             errors.rejectValue("inputControl.label", null, "Control Label must not be blank");
108         } else {
109             if(!JasperServerUtil.regExValidateLabel(inputControl.getLabel())) {
110                 errors.rejectValue("inputControl.label", null, "Control label contains invalid characters");
111             }
112         }
113
114         if(inputControl.getDescription()==null || inputControl.getDescription().trim().length() > 250)
115             errors.rejectValue("inputControl.description",null,"Description can not be more than 250 characters");
116
117     }
118
119     public void dataTypeDetails(InputControlWrapper dto, Errors errors){
120         ResourceReference dataTypeRef = dto.getInputControl().getDataType();
121         if (!dataTypeRef.isLocal()) {
122             return;
123         }
124         DataType dataType = (DataType) dataTypeRef.getLocalResource();
125         if(dto.getSource()==null || dto.getSource().equals(JasperServerConstImpl.getFieldChoiceLocal())){
126             if(dataType.getName()==null || dataType.getName().trim().length()==0) {
127                 errors.rejectValue("inputControl.dataType.name", null, "Data type Name must not be blank");
128             } else {
129                 if(!JasperServerUtil.regExValidateName(dataType.getName())) {
130                     errors.rejectValue("inputControl.dataType.name", null, "Data type Name contains invalid characters");
131                 }
132             }
133
134             if(dataType.getLabel()==null || dataType.getLabel().trim().length()==0) {
135                 errors.rejectValue("inputControl.dataType.label", null, "Data type Label must not be blank");
136             } else {
137                 if(!JasperServerUtil.regExValidateLabel(dataType.getLabel())) {
138                     errors.rejectValue("inputControl.dataType.label", null, "Data type Label contains invalid characters");
139                 }
140             }
141
142             if (dto.getDtMaxLength() != null && dto.getDtMaxLength().length() > 0)
143                 try {
144                     new Integer JavaDoc(dto.getDtMaxLength());
145                 } catch(NumberFormatException JavaDoc e) {
146                     errors.rejectValue("dtMaxLength", null, "Data type Maximum length must be an integer");
147                 }
148
149             if (dto.getDtDecimals() != null && dto.getDtDecimals().length() > 0)
150                 try {
151                     new Integer JavaDoc(dto.getDtDecimals());
152                 } catch(NumberFormatException JavaDoc e) {
153                     errors.rejectValue("dtDecimals", null, "Decimals must be an integer");
154                 }
155         } else {
156             if(dto.getExistingPath() == null || dto.getExistingPath().trim().length()==0) {
157                 errors.reject("existingPath", null, "Please select a Data Type");
158             }
159         }
160     }
161     public void queryDetails(InputControlWrapper dto, Errors errors){
162         ResourceReference queryRef = dto.getInputControl().getQuery();
163         if (!queryRef.isLocal()) {
164             return;
165         }
166         Query query = (Query) queryRef.getLocalResource();
167         if(dto.getSource()==null || dto.getSource().equals(JasperServerConstImpl.getFieldChoiceLocal())){
168
169             if(query.getName()==null || query.getName().trim().length()==0) {
170                 errors.rejectValue("inputControl.query.name", null, "Query Name must not be blank");
171             } else {
172                 if(!JasperServerUtil.regExValidateName(query.getName())) {
173                     errors.rejectValue("inputControl.query.name", null, "Query Name contains invalid characters");
174                 }
175             }
176
177             if(query.getLabel()==null || query.getLabel().trim().length()==0) {
178                 errors.rejectValue("inputControl.query.label", null, "Query Label must not be blank");
179             } else {
180                 if(!JasperServerUtil.regExValidateLabel(query.getLabel())) {
181                     errors.rejectValue("inputControl.query.label", null, "Query Label contains invalid characters");
182                 }
183             }
184
185             String JavaDoc language = query.getLanguage();
186             if(language == null || language.trim().length() == 0) {
187                 errors.rejectValue("inputControl.query.language", null, "Query language must not be blank");
188             }
189
190             if(query.getSql()==null || query.getSql().trim().length()==0)
191                 errors.rejectValue("inputControl.query.sql",null,"Query sql must not be blank");
192 //TODO: check this
193
// if(query.getVisibleColumn()==null || query.getVisibleColumn().trim().length()==0)
194
// errors.rejectValue("inputControl.query.sql",null,"Visible Columns field must not be blank");
195

196         }else{
197             if(dto.getExistingPath().trim().length()==0)
198                 errors.reject("existingPath",null,"Please select a Query");
199         }
200     }
201     public void listOfValueDetails(InputControlWrapper dto, Errors errors){
202         ResourceReference listOfValuesRef = dto.getInputControl().getListOfValues();
203         if (!listOfValuesRef.isLocal()) {
204             
205         }
206         ListOfValues listOfValues = (ListOfValues) listOfValuesRef.getLocalResource();
207         if(dto.getSource()==null || dto.getSource().equals(JasperServerConstImpl.getFieldChoiceLocal())){
208             if(listOfValues.getName()==null || listOfValues.getName().trim().length()==0) {
209                 errors.rejectValue("inputControl.listOfValues.name", null, "List of Values Name must not be blank");
210             } else {
211                 if(!JasperServerUtil.regExValidateName(listOfValues.getName())) {
212                     errors.rejectValue("inputControl.listOfValues.name", null, "List of Values Name contains invalid characters");
213                 }
214             }
215
216             if(listOfValues.getLabel()==null || listOfValues.getLabel().trim().length()==0) {
217                 errors.rejectValue("inputControl.listOfValues.label", null, "List of Values label must not be blank");
218             } else {
219                 if(!JasperServerUtil.regExValidateLabel(listOfValues.getLabel())) {
220                     errors.rejectValue("inputControl.listOfValues.label", null, "List of Values label contains invalid characters");
221                 }
222             }
223             
224             if(listOfValues.getValues()==null || listOfValues.getValues().length==0){
225                 errors.rejectValue("inputControl.listOfValues.label", null, "List of Values label contains invalid characters");
226             }
227
228         }else{
229             if(dto.getExistingPath()==null || dto.getExistingPath().trim().length()==0)
230                 errors.reject("inputControl.listOfValues.description",null,"Couldnt create a List of values with no values");
231         }
232     }
233
234
235     public void validateAddValue(InputControlWrapper dto, Errors errors){
236
237         if(dto.getListItemLabel()==null || size(dto.getListItemLabel())==0) {
238             errors.reject("listItemLabel", null, "Please enter Item label");
239         } else {
240             if(!JasperServerUtil.regExValidateLabel(dto.getListItemLabel())) {
241                 errors.reject("listItemLabel", null, "Item label contains invalid characters");
242             }
243         }
244
245         if(dto.getListItemValue()==null || size(dto.getListItemValue())==0) {
246             errors.reject("listItemValue", null, "Please enter Item value");
247         }
248     }
249
250
251     public void validateAddVisibleColumn(InputControlWrapper dto, Errors errors){
252
253         if(dto.getNewVisibleColumn()==null || size(dto.getNewVisibleColumn())==0) {
254             errors.reject("newVisibleColumn", null, "Please enter a value for new visible column");
255         }
256     }
257
258     public void validateAddValueColumn(InputControlWrapper dto, Errors errors){
259
260         InputControl inputControl = dto.getInputControl();
261         if(inputControl.getQueryValueColumn()==null || size(inputControl.getQueryValueColumn())==0) {
262             errors.reject("inputControl.queryValueColumn", null, "Please enter a value for query value column");
263         }
264     }
265
266     private int size(String JavaDoc text){
267         return text.trim().length();
268     }
269
270 }
271
Popular Tags