KickJava   Java API By Example, From Geeks To Geeks.

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


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.jasperreports.domain.ReportUnit;
29 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
30 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
31 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
32 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper;
33 import com.jaspersoft.jasperserver.war.dto.ReportUnitWrapper;
34
35 /**
36  * A validator for our form backing object. Note that this validator supports
37  * individual validation of each step in the "create report flow".
38  */

39 public class ReportDetailsValidator implements Validator {
40
41     public boolean supports(Class JavaDoc clazz) {
42         return ReportUnitWrapper.class.isAssignableFrom(clazz);
43     }
44
45     public void validate(Object JavaDoc o, Errors errors) {
46         ReportUnitWrapper reportDetails = (ReportUnitWrapper) o;
47         validateNameLabelDesc(reportDetails, errors); // Step 1
48
validateJrxmlUpload(reportDetails, errors); // Step 2
49
validateReportAndControlForm(reportDetails, errors); // Step 3
50
}
51
52     public void validateNameLabelDesc(ReportUnitWrapper ruWrapper, Errors errors) {
53         ReportUnit ru = ruWrapper.getReportUnit();
54         if (ru.getLabel() == null || ru.getLabel().trim().length() == 0) {
55             errors.rejectValue("reportUnit.label", null,
56                     "Report label must not be empty");
57         } else {
58             if (ruWrapper.getReportUnit().getLabel().length() > 100) {
59                 errors
60                         .rejectValue("reportUnit.label", null,
61                                 "Report label should not be longer than 100 characters");
62             } else if (!JasperServerUtil.regExValidateLabel(ru.getLabel()))
63                 errors.rejectValue("reportUnit.label", null,
64                         "Report label contains invalid characters");
65         }
66
67         if (ru.getName() == null || ru.getName().trim().length() == 0) {
68             errors.rejectValue("reportUnit.name", null,
69                     "Report name must not be empty");
70         } else {
71             if (ru.getName().length() > 100) {
72                 errors.rejectValue("reportUnit.name", null,
73                         "Report name should not be longer than 100 characters");
74             } else if (!JasperServerUtil.regExValidateName(ru.getName()))
75                 errors.rejectValue("reportUnit.name", null,
76                         "Report name contains invalid characters");
77             else {
78                 if (ruWrapper.isNewMode()
79                         && ruWrapper.getExistingResources() != null) {
80                     List JavaDoc res = ruWrapper.getExistingResources();
81                     for (int i = 0; i < res.size(); i++) {
82                         String JavaDoc preExtName = (String JavaDoc) res.get(i);
83                         if (preExtName.equalsIgnoreCase(ru.getName().trim())) {
84                             errors
85                                     .rejectValue("reportUnit.name", null,
86                                             "A Resource with the chosen name already exists");
87                             break;
88                         }
89                     }
90                 }
91             }
92         }
93
94         if (ru.getDescription() != null && ru.getDescription().length() > 250)
95             errors
96                     .rejectValue("reportUnit.description", null,
97                             "Report description should not be longer than 250 characters");
98     }
99
100     public void validateURIString(ReportUnitWrapper reportUnit, Errors errors) {
101         if (reportUnit.getSource() == null) {
102             errors.rejectValue("source", null, "Select a valid JRXML source");
103         } else {
104             if (reportUnit.getSource().equals(
105                     JasperServerConst.FIELD_CHOICE_CONT_REPO)) {
106                 if (reportUnit.getJrxmlUri() == null
107                         || reportUnit.getJrxmlUri().length() == 0) {
108                     errors.rejectValue("jrxmlUri", null,
109                             "Select a resuable JRxml ");
110                 }
111             }
112         }
113
114     }
115
116     public void validateJrxmlUpload(ReportUnitWrapper wrapper, Errors errors) {
117         if (wrapper.getSource() == null) {
118             errors.rejectValue("source", null, "Select a valid JRXML source");
119         } else {
120             if (wrapper.getSource().equals(
121                     JasperServerConst.FIELD_CHOICE_FILE_SYSTEM)
122                     && !wrapper.isJrxmlLocated()) {
123                 // FileResource mainReport = (FileResource)
124
// wrapper.getReportUnit().getMainReport().getLocalResource();
125
if (wrapper.getJrxmlData() == null
126                         || wrapper.getJrxmlData().length == 0) {
127                     errors.rejectValue("jrxmlData", null,
128                             "Select a valid JRXML file");
129                 }
130             }
131         }
132     }
133
134     public void validateResources(ReportUnitWrapper wrapper, Errors errors) {
135         boolean allResLocated = true;
136         boolean allControlsLocated = true;
137         List JavaDoc sugRes = wrapper.getSuggestedResources();
138         if (sugRes != null && !sugRes.isEmpty()) {
139             for (int i = 0; i < sugRes.size(); i++) {
140                 FileResourceWrapper resWrap = (FileResourceWrapper) sugRes
141                         .get(i);
142                 if (!resWrap.isLocated()) {
143                     allResLocated = false;
144                     break;
145                 }
146             }
147         }
148         List JavaDoc sugContr = wrapper.getSuggestedControls();
149         if (sugContr != null && !sugContr.isEmpty()) {
150             for (int i = 0; i < sugContr.size(); i++) {
151                 InputControlWrapper icWrap = (InputControlWrapper) sugContr
152                         .get(i);
153                 if (!icWrap.isLocated()) {
154                     allControlsLocated = false;
155                     break;
156                 }
157             }
158         }
159         if (!allControlsLocated && !allResLocated)
160             errors.rejectValue("validationMessage", null,
161                     "All suggested Resources and Controls must be located");
162         else if (!allControlsLocated)
163             errors.rejectValue("validationMessage", null,
164                     "All suggested Controls must be located");
165         else if (!allResLocated)
166             errors.rejectValue("validationMessage", null,
167                     "All suggested Resources must be located");
168
169     }
170
171     public void validateReportAndControlForm(ReportUnitWrapper wrapper, Errors errors) {
172         // Both the input control and the report JSPs are optional
173
if (wrapper.getReportUnit().getInputControlRenderingView() != null &&
174                 wrapper.getReportUnit().getInputControlRenderingView().length() > 100) {
175             errors
176                     .rejectValue("reportUnit.inputControlRenderingView", null,
177                             "JSP reference for input controls should not be longer than 100 characters");
178         }
179         if (wrapper.getReportUnit().getReportRenderingView() != null &&
180                 wrapper.getReportUnit().getReportRenderingView().length() > 100) {
181             errors
182                     .rejectValue("reportUnit.reportRenderingView", null,
183                             "JSP reference for report display should not be longer than 100 characters");
184         }
185     }
186 }
Popular Tags