KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.validation.Errors;
24 import org.springframework.validation.Validator;
25
26 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.BeanReportDataSource;
27 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
28 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
29 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
30 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
31 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
32 import com.jaspersoft.jasperserver.war.dto.ReportDataSourceWrapper;
33
34 public class ReportDataSourceValidator implements Validator {
35     private JasperServerConstImpl constants=new JasperServerConstImpl();
36     public boolean supports(Class JavaDoc clazz) {
37         return ReportDataSourceWrapper.class.isAssignableFrom(clazz);
38     }
39
40     public void validate(Object JavaDoc arg0, Errors arg1) {
41         // TODO Auto-generated method stub
42
}
43
44     public void chooseType(ReportDataSourceWrapper wrapper, Errors errors){
45         if(wrapper.getSource()==null)
46             errors.rejectValue("source",null,"Please select a Datasource type");
47     }
48
49     public void jndiPropsForm(ReportDataSourceWrapper wrapper, Errors errors){
50         JndiJdbcReportDataSource jndiSource=(JndiJdbcReportDataSource)wrapper.getReportDataSource();
51         if(jndiSource.getJndiName()==null || jndiSource.getJndiName().trim().length()==0) {
52             errors.rejectValue("reportDataSource.jndiName",null,"Please enter the JNDI name");
53         } else {
54             if(!JasperServerUtil.regExValidateJndiServiceName(jndiSource.getJndiName())) {
55                 errors.rejectValue("reportDataSource.jndiName", null, "JNDI Name contains invalid characters");
56             }
57         }
58         namingForm(wrapper, errors);
59     }
60
61     public void namingForm(ReportDataSourceWrapper wrapper, Errors errors){
62         ReportDataSource ds=wrapper.getReportDataSource();
63         if(ds.getName()==null || ds.getName().trim().length()==0) {
64             errors.rejectValue("reportDataSource.name",null,"Please enter the datasource Name");
65         } else {
66             if(!JasperServerUtil.regExValidateName(ds.getName())) {
67                 errors.rejectValue("reportDataSource.name", null, "Datasource Name contains invalid characters");
68             }
69         }
70
71         if(ds.getLabel()==null || ds.getLabel().trim().length()==0) {
72             errors.rejectValue("reportDataSource.label",null,"Please enter the datasource Label");
73         } else {
74             if(!JasperServerUtil.regExValidateLabel(ds.getLabel())) {
75                 errors.rejectValue("reportDataSource.label",null,"Datasource Label contains invalid characters");
76             }
77         }
78
79         if(ds.getDescription()==null || ds.getDescription().trim().length()>250)
80             errors.rejectValue("reportDataSource.description",null,"Datasource description can not be more than 250 characters");
81     }
82
83     public void jdbcPropsForm(ReportDataSourceWrapper wrapper, Errors errors){
84         JdbcReportDataSource ds=(JdbcReportDataSource)wrapper.getReportDataSource();
85         if(ds.getDriverClass()==null || ds.getDriverClass().trim().length()==0) {
86             errors.rejectValue("reportDataSource.driverClass",null,"Please enter the driver class name");
87         } else {
88             ds.setDriverClass(ds.getDriverClass().trim());
89             if(!JasperServerUtil.regExValidateDbDriver(ds.getDriverClass())) {
90                 errors.rejectValue("reportDataSource.driverClass", null, "driver contains invalid characters");
91             }
92         }
93
94         if(ds.getConnectionUrl()==null || ds.getConnectionUrl().trim().length()==0) {
95             errors.rejectValue("reportDataSource.connectionUrl", null, "Please enter the JDBC URL");
96         } else {
97             if(!JasperServerUtil.regExValidateJdbcURL(ds.getConnectionUrl())) {
98                 errors.rejectValue("reportDataSource.connectionUrl", null, "Jdbc Url contains invalid characters");
99             }
100         }
101
102         if(ds.getUsername()==null || ds.getUsername().trim().length()==0) {
103             errors.rejectValue("reportDataSource.username",null,"Please enter an authorised username");
104         } else {
105             if(!JasperServerUtil.regExValidateName(ds.getUsername())) {
106                 errors.rejectValue("reportDataSource.username", null, "username contains invalid characters");
107             }
108         }
109
110         if(ds.getPassword()==null || ds.getPassword().trim().length()==0)
111             errors.rejectValue("reportDataSource.password",null,"Please enter the password");
112
113         namingForm(wrapper, errors);
114     }
115
116     public void beanPropsForm(ReportDataSourceWrapper wrapper, Errors errors){
117         BeanReportDataSource beanSource=(BeanReportDataSource)wrapper.getReportDataSource();
118         if(beanSource.getBeanName()==null || beanSource.getBeanName().trim().length()==0) {
119             errors.rejectValue("reportDataSource.beanName",null,"Please enter the bean name");
120         } else {
121             // TODO Try and find the bean in the application context
122
// If found, see if it has the given method
123
// If it has the given method, does that method return the right thing
124
}
125         namingForm(wrapper, errors);
126     }
127
128     public void validateSource(ReportDataSourceWrapper wrapper, Errors errors){
129         if(constants.getFieldChoiceRepo().equals(wrapper.getSource())
130                 &&( wrapper.getSelectedUri()==null || wrapper.getSelectedUri().trim().length()==0))
131             errors.rejectValue("selectedUri",null,"Please select a Datasource");
132     }
133 }
134
Popular Tags