KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.metadata.olap.domain.MondrianConnection;
30 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit;
31 import com.jaspersoft.jasperserver.api.metadata.olap.service.impl.OlapConnectionServiceImpl;
32 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
33 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
34 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
35 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper;
36 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper;
37 //import com.jaspersoft.jasperserver.war.dto.ReportUnitWrapper;
38

39 /**
40  * A validator for our form backing object. Note that this validator supports
41  * individual validation of each step in the "create OLAP flow".
42  */

43 public class OlapDetailsValidator implements Validator {
44
45     public boolean supports(Class JavaDoc clazz) {
46         return OlapUnitWrapper.class.isAssignableFrom(clazz);
47     }
48
49     public void validate(Object JavaDoc o, Errors errors) {
50         OlapUnitWrapper olapDetails = (OlapUnitWrapper) o;
51         validateNameLabelDesc(olapDetails, errors); // Step 1
52
validateSchemaUpload(olapDetails, errors); // Step 2
53
}
54
55     public void validateNameLabelDesc(OlapUnitWrapper ouWrapper, Errors errors) {
56         OlapUnit ou = ouWrapper.getOlapUnit();
57         if (ou.getLabel() == null || ou.getLabel().trim().length() == 0) {
58             errors.rejectValue("olapUnit.label", null,
59                     "OLAP label must not be empty");
60         } else {
61             if (ouWrapper.getOlapUnit().getLabel().length() > 100) {
62                 errors
63                         .rejectValue("olapUnit.label", null,
64                                 "OLAP label should not be longer than 100 characters");
65             } else if (!JasperServerUtil.regExValidateLabel(ou.getLabel()))
66                 errors.rejectValue("olapUnit.label", null,
67                         "OLAP label contains invalid characters");
68         }
69
70         if (ou.getName() == null || ou.getName().trim().length() == 0) {
71             errors.rejectValue("olapUnit.name", null,
72                     "OLAP name must not be empty");
73         } else {
74             if (ou.getName().length() > 100) {
75                 errors.rejectValue("olapUnit.name", null,
76                         "OLAP name should not be longer than 100 characters");
77             } else if (!JasperServerUtil.regExValidateName(ou.getName()))
78                 errors.rejectValue("olapUnit.name", null,
79                         "OLAP name contains invalid characters");
80             else {
81                 if (ouWrapper.isNewMode()
82                         && ouWrapper.getExistingResources() != null) {
83                     List JavaDoc res = ouWrapper.getExistingResources();
84                     for (int i = 0; i < res.size(); i++) {
85                         String JavaDoc preExtName = (String JavaDoc) res.get(i);
86                         if (preExtName.equalsIgnoreCase(ou.getName().trim())) {
87                             errors
88                                     .rejectValue("olapUnit.name", null,
89                                             "A Resource with the chosen name already exists");
90                             break;
91                         }
92                     }
93                 }
94             }
95         }
96
97         if (ou.getDescription() != null && ou.getDescription().length() > 300)
98             errors
99                     .rejectValue("olapUnit.description", null,
100                             "OLAP description should not be longer than 300 characters");
101     }
102
103     public void validateMdxQuery(OlapUnitWrapper ouWrapper, Errors errors) {
104         OlapUnit ou = ouWrapper.getOlapUnit();
105
106         if (ou.getMdxQuery() == null || ou.getMdxQuery().trim().length() == 0) {
107             errors.rejectValue("olapUnit.mdxQuery", null,
108                     "OLAP MDX Query must not be empty");
109         }
110         else if (ou.getMdxQuery() != null && ou.getMdxQuery().length() > 4096) {
111             errors.rejectValue("olapUnit.mdxQuery", null,
112                             "OLAP MDX Query should not be longer than 4096 characters");
113         }
114
115 // mondrian.olap.Connection mondrianConnection = ouWrapper.getMondrianConnection();
116
//
117
// mondrian.olap.Query mdxQuery = null;
118
//
119
// try
120
// {
121
// mdxQuery = mondrianConnection.parseQuery(ou.getMdxQuery());
122
// }
123
// catch (Exception e)
124
// {
125
// e.getMessage();e.getCause();e.getClass();
126
// errors.rejectValue("olapUnit.mdxQuery", null, e.getCause().getMessage());
127
// }
128
}
129
130     public void validateURIString(OlapUnitWrapper olapUnit, Errors errors) {
131         if (olapUnit.getSource() == null) {
132             errors.rejectValue("source", null, "Select a valid schema source");
133         } else {
134             if (olapUnit.getSource().equals(
135                     JasperServerConst.FIELD_CHOICE_CONT_REPO)) {
136                 if (olapUnit.getSchemaUri() == null
137                         || olapUnit.getSchemaUri().length() == 0) {
138                     errors.rejectValue("schemaUri", null,
139                             "Select a resuable schema ");
140                 }
141             }
142         }
143
144     }
145
146     public void validateSchemaUpload(OlapUnitWrapper wrapper, Errors errors) {
147         // TODO
148
// if (wrapper.getSource() == null) {
149
// errors.rejectValue("source", null, "Select a valid schema source");
150
// } else {
151
// if (wrapper.getSource().equals(
152
// JasperServerConst.FIELD_CHOICE_FILE_SYSTEM)
153
// && !wrapper.isSchemaLocated()) {
154
// // FileResource mainReport = (FileResource)
155
// // wrapper.getReportUnit().getMainReport().getLocalResource();
156
// if (wrapper.getSchemaData() == null
157
// || wrapper.getSchemaData().length == 0) {
158
// errors.rejectValue("schemaData", null,
159
// "Select a valid schema file");
160
// }
161
// }
162
// }
163
}
164
165     public void validateResources(OlapUnitWrapper wrapper, Errors errors) {
166         boolean allResLocated = true;
167         boolean allControlsLocated = true;
168         List JavaDoc sugRes = wrapper.getSuggestedResources();
169         if (sugRes != null && !sugRes.isEmpty()) {
170             for (int i = 0; i < sugRes.size(); i++) {
171                 FileResourceWrapper resWrap = (FileResourceWrapper) sugRes
172                         .get(i);
173                 if (!resWrap.isLocated()) {
174                     allResLocated = false;
175                     break;
176                 }
177             }
178         }
179         List JavaDoc sugContr = wrapper.getSuggestedControls();
180         if (sugContr != null && !sugContr.isEmpty()) {
181             for (int i = 0; i < sugContr.size(); i++) {
182                 InputControlWrapper icWrap = (InputControlWrapper) sugContr
183                         .get(i);
184                 if (!icWrap.isLocated()) {
185                     allControlsLocated = false;
186                     break;
187                 }
188             }
189         }
190         if (!allControlsLocated && !allResLocated)
191             errors.rejectValue("validationMessage", null,
192                     "All suggested Resources and Controls must be located");
193         else if (!allControlsLocated)
194             errors.rejectValue("validationMessage", null,
195                     "All suggested Controls must be located");
196         else if (!allResLocated)
197             errors.rejectValue("validationMessage", null,
198                     "All suggested Resources must be located");
199
200     }
201
202 }
Popular Tags