KickJava   Java API By Example, From Geeks To Geeks.

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


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.war.common.JasperServerConst;
29 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
30 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
31 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper;
32 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper;
33
34 /**
35  *
36  * OlapClientConnectionValidator provides validation methods for the
37  * olapClientConnectionFlow
38  *
39  * @author jshih
40  */

41 public class OlapClientConnectionValidator implements Validator {
42
43     public boolean supports(Class JavaDoc clazz) {
44         return OlapClientConnectionWrapper.class.isAssignableFrom(clazz);
45     }
46
47     public void validate(Object JavaDoc object, Errors errors) {
48         OlapClientConnectionWrapper details = (OlapClientConnectionWrapper) object;
49         validateNameLabelDesc(details, errors); // Step 1
50
validateSchemaUpload(details, errors); // Step 2
51
}
52
53     public void validateNameLabelDesc(OlapClientConnectionWrapper wrapper, Errors errors) {
54         if (wrapper.getConnectionLabel() == null ||
55                 wrapper.getConnectionLabel().trim().length() == 0) {
56             errors.rejectValue("connectionLabel", null,
57                     "OLAP connection label must not be empty");
58         } else {
59             if (wrapper.getConnectionLabel().length() > 100) {
60                 errors
61                         .rejectValue("connectionLabel", null,
62                                 "OLAP connection label should not be longer than 100 characters");
63             } else if (!JasperServerUtil.regExValidateLabel(wrapper.getConnectionLabel()))
64                 errors.rejectValue("connectionLabel", null,
65                         "OLAP connection label contains invalid characters");
66         }
67
68         if (wrapper.getConnectionName() == null ||
69                 wrapper.getConnectionName().trim().length() == 0) {
70             errors.rejectValue("connectionName", null,
71                     "OLAP connection name must not be empty");
72         } else {
73             if (wrapper.getConnectionName().length() > 100) {
74                 errors.rejectValue("connectionName", null,
75                         "OLAP connection name should not be longer than 100 characters");
76             } else if (!JasperServerUtil.regExValidateName(wrapper.getConnectionName()))
77                 errors.rejectValue("connectionName", null,
78                         "OLAP connection name contains invalid characters");
79             else {
80                 if (wrapper.isNewMode()
81                         && wrapper.getExistingResources() != null) {
82                     List JavaDoc res = wrapper.getExistingResources();
83                     for (int i = 0; i < res.size(); i++) {
84                         String JavaDoc preExtName = (String JavaDoc) res.get(i);
85                         if (preExtName.equalsIgnoreCase(wrapper.getConnectionName().trim())) {
86                             errors
87                                     .rejectValue("connectionName", null,
88                                             "A Resource with the chosen name already exists");
89                             break;
90                         }
91                     }
92                 }
93             }
94         }
95
96         if (wrapper.getConnectionDescription() != null &&
97                 wrapper.getConnectionDescription().length() > 300)
98             errors.rejectValue("connectionDescription", null,
99                             "OLAP description should not be longer than 300 characters");
100     }
101     
102     public void validateNameLabelDescAndXmlaSource(OlapClientConnectionWrapper wrapper, Errors errors) {
103         validateNameLabelDesc(wrapper, errors);
104         // datasource
105
if (wrapper.getXmlaDatasource() == null || wrapper.getXmlaDatasource().trim().length() == 0) {
106             errors.rejectValue("connectionDatasource", null,
107                     "OLAP datasource must not be empty");
108         }
109         // uri
110
if (wrapper.getXmlaConnectionUri() == null ||
111                 wrapper.getXmlaConnectionUri().trim().length() == 0) {
112             errors.rejectValue("xmlaConnectionUri", null,
113                     "OLAP XML/A conneciton URI must not be empty");
114         }
115         // username
116
if (wrapper.getUsername() == null || wrapper.getUsername().trim().length() == 0) {
117             errors.rejectValue("username", null,
118                     "OLAP conneciton username must not be empty");
119         }
120         // password
121
if (wrapper.getPassword() == null || wrapper.getPassword().trim().length() == 0) {
122             errors.rejectValue("password", null,
123                     "OLAP conneciton password must not be empty");
124         }
125     }
126     
127     public void validateURIString(OlapClientConnectionWrapper olapUnit, Errors errors) {
128         if (olapUnit.getSource() == null) {
129             errors.rejectValue("source", null, "Select a valid schema source");
130         } else {
131             if (olapUnit.getSource().equals(
132                     JasperServerConst.FIELD_CHOICE_CONT_REPO)) {
133                 if (olapUnit.getSchemaUri() == null
134                         || olapUnit.getSchemaUri().length() == 0) {
135                     errors.rejectValue("schemaUri", null,
136                             "Select a resuable schema ");
137                 }
138             }
139         }
140
141     }
142
143     public void validateSchemaUpload(OlapClientConnectionWrapper wrapper, Errors errors) {
144         // TODO
145
}
146
147     public void validateResources(OlapClientConnectionWrapper wrapper, Errors errors) {
148         boolean allResLocated = true;
149         boolean allControlsLocated = true;
150         List JavaDoc sugRes = wrapper.getSuggestedResources();
151         if (sugRes != null && !sugRes.isEmpty()) {
152             for (int i = 0; i < sugRes.size(); i++) {
153                 FileResourceWrapper resWrap = (FileResourceWrapper) sugRes
154                         .get(i);
155                 if (!resWrap.isLocated()) {
156                     allResLocated = false;
157                     break;
158                 }
159             }
160         }
161         List JavaDoc sugContr = wrapper.getSuggestedControls();
162         if (sugContr != null && !sugContr.isEmpty()) {
163             for (int i = 0; i < sugContr.size(); i++) {
164                 InputControlWrapper icWrap = (InputControlWrapper) sugContr
165                         .get(i);
166                 if (!icWrap.isLocated()) {
167                     allControlsLocated = false;
168                     break;
169                 }
170             }
171         }
172         if (!allControlsLocated && !allResLocated)
173             errors.rejectValue("validationMessage", null,
174                     "All suggested Resources and Controls must be located");
175         else if (!allControlsLocated)
176             errors.rejectValue("validationMessage", null,
177                     "All suggested Controls must be located");
178         else if (!allResLocated)
179             errors.rejectValue("validationMessage", null,
180                     "All suggested Resources must be located");
181     }
182
183     public void validateConnectionType(OlapClientConnectionWrapper wrapper, Errors errors){
184         if(wrapper.getType() == null)
185             errors.rejectValue("source",null,"Please select a Connection type");
186     }
187     
188     public void validateConnectionSource(OlapClientConnectionWrapper wrapper, Errors errors){
189         if(wrapper.getSource() == null)
190             errors.rejectValue("source",null,"Please select a Connection source");
191     }
192 }
Popular Tags