KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.domain.ValidationDetail;
29 import com.jaspersoft.jasperserver.api.common.domain.ValidationResult;
30
31 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
32 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
33 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper;
34
35 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
36
37 /**
38  *
39  * OlapUnitValidator provides validation methods for the
40  * olapUnitFlow
41  *
42  * @author jshih
43  * @revision $Id$
44  */

45 public class OlapUnitValidator implements Validator {
46
47     public boolean supports(Class JavaDoc clazz) {
48     return OlapUnitWrapper.class.isAssignableFrom(clazz);
49     }
50
51     public void validate(Object JavaDoc o, Errors errors) {
52     OlapUnitWrapper olapDetails = (OlapUnitWrapper) o;
53     validateNameLabelDesc(olapDetails, errors);
54     }
55
56     public void validateNameLabelDesc(OlapUnitWrapper ouWrapper, Errors errors) {
57     if (ouWrapper.getOlapUnitLabel() == null ||
58         ouWrapper.getOlapUnitLabel().trim().length() == 0) {
59         errors.rejectValue("olapUnitLabel", null,
60                    "OLAP label must not be empty");
61     } else {
62         if (ouWrapper.getOlapUnitLabel().length() > 100) {
63         errors
64             .rejectValue("olapUnitLabel", null,
65                  "OLAP label should not be longer than 100 characters");
66         } else if (!JasperServerUtil.regExValidateLabel(ouWrapper.getOlapUnitLabel()))
67         errors.rejectValue("olapUnitLabel", null,
68                    "OLAP label contains invalid characters");
69     }
70
71     if (ouWrapper.getOlapUnitName() == null ||
72         ouWrapper.getOlapUnitName().trim().length() == 0) {
73         errors.rejectValue("olapUnitName", null,
74                    "OLAP name must not be empty");
75     } else {
76         if (ouWrapper.getOlapUnitName().length() > 100) {
77         errors.rejectValue("olapUnitName", null,
78                    "OLAP name should not be longer than 100 characters");
79         } else if (!JasperServerUtil.regExValidateName(ouWrapper.getOlapUnitName()))
80         errors.rejectValue("olapUnitName", null,
81                    "OLAP name contains invalid characters");
82         else {
83         if (ouWrapper.isNewMode()
84             && ouWrapper.getExistingResources() != null) {
85             List JavaDoc res = ouWrapper.getExistingResources();
86             for (int i = 0; i < res.size(); i++) {
87             String JavaDoc preExtName = (String JavaDoc) res.get(i);
88             if (preExtName.equalsIgnoreCase(ouWrapper.getOlapUnitName().trim())) {
89                 errors.rejectValue("olapUnitName", null,
90                            "A Resource with the chosen name already exists");
91                 break;
92             }
93             }
94         }
95         }
96     }
97
98     if (ouWrapper.getOlapUnitDescription() !=
99         null && ouWrapper.getOlapUnitDescription().length() > 300)
100         errors.rejectValue("olapUnitDescription", null,
101                    "OLAP description should not be longer than 300 characters");
102     }
103
104     public void validateMdxQuery(OlapUnitWrapper ouWrapper, Errors errors) {
105     // at this point the unit and connection have not been saved.
106
// (apparently the action named saveOlapClientConnection does not
107
// actually save anything... that is left for saveOlapUnit).
108
// this works out ok if we are editing an existing olap unit,
109
// but if we are making a new one, its connection must be a
110
// local reference if it is to be retrieved...
111
ouWrapper.getOlapUnit().
112         setMdxQuery(ouWrapper.getOlapUnitMdxQuery());
113     if (ouWrapper.isNewMode()) {
114         ouWrapper.getOlapUnit().
115         setOlapClientConnection(ouWrapper.getOlapClientConnection());
116     }
117
118     if (ouWrapper.getOlapUnitMdxQuery() == null ||
119         ouWrapper.getOlapUnitMdxQuery().trim().length() == 0) {
120         errors.rejectValue("olapUnitMdxQuery", null,
121                    "OLAP MDX Query must not be empty");
122     }
123     else if (ouWrapper.getOlapUnitMdxQuery() !=
124          null && ouWrapper.getOlapUnitMdxQuery().length() > 4096) {
125         errors.rejectValue("olapUnitMdxQuery", null,
126                    "OLAP MDX Query should not be longer than 4096 characters");
127     }
128     else {
129         ValidationResult result =
130         getConnectionService().validate(null, ouWrapper.getOlapUnit());
131         if (result.getValidationState().equals(ValidationResult.STATE_ERROR)) {
132         List JavaDoc details = result.getResults();
133         String JavaDoc msg = "That is not a valid MDX Query: ";
134         String JavaDoc causeMsg = null;
135         for( int i = 0; i < details.size(); i++ ) {
136             ValidationDetail detail = (ValidationDetail)details.get(i);
137             Throwable JavaDoc e = detail.getException();
138             while (e != null) {
139             causeMsg = e.getMessage();
140             e = e.getCause();
141             }
142         }
143         errors.rejectValue("olapUnitMdxQuery", null, msg + causeMsg);
144         }
145     }
146     }
147
148     public void validateURIString(OlapUnitWrapper ouWrapper, Errors errors) {
149     if (ouWrapper.getSource() == null) {
150         errors.rejectValue("source", null, "Select a valid schema source");
151     } else {
152         if (ouWrapper.getSource().equals(
153                          JasperServerConst.FIELD_CHOICE_CONT_REPO)) {
154         if (ouWrapper.getSchemaUri() == null
155             || ouWrapper.getSchemaUri().length() == 0) {
156             errors.rejectValue("schemaUri", null,
157                        "Select a resuable schema ");
158         }
159         }
160     }
161
162     }
163
164     public void validateConnectionType(OlapUnitWrapper ouWrapper, Errors errors){
165     if(ouWrapper.getSource() == null)
166         errors.rejectValue("source",null,"Please select a Connection type");
167     }
168     
169     public void validateConnectionSource(OlapUnitWrapper wrapper, Errors errors){
170     if(wrapper.getType() == null)
171         errors.rejectValue("source",null,"Please select a Connection source");
172     }
173
174     /**
175      * property: olapConnectionService
176      */

177     private OlapConnectionService mConnectionService;
178     public OlapConnectionService getConnectionService() {
179         return mConnectionService;
180     }
181     public void setConnectionService( OlapConnectionService cs ) {
182         mConnectionService = cs;
183     }
184
185 }
Popular Tags