KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.springframework.validation.Errors;
27 import org.springframework.validation.Validator;
28
29 import com.jaspersoft.jasperserver.api.common.domain.ValidationError;
30 import com.jaspersoft.jasperserver.api.common.domain.ValidationErrors;
31 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob;
32 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportSchedulingService;
33
34 /**
35  * @author Lucian Chirita (lucianc@users.sourceforge.net)
36  * @version $Id: ReportJobValidator.java 3550 2006-06-06 18:47:26Z lucian $
37  */

38 public class ReportJobValidator implements Validator {
39
40     private ReportSchedulingService schedulingService;
41     
42     public ReportSchedulingService getSchedulingService() {
43         return schedulingService;
44     }
45
46     public void setSchedulingService(ReportSchedulingService schedulingService) {
47         this.schedulingService = schedulingService;
48     }
49
50     public boolean supports(Class JavaDoc clazz) {
51         return ReportJob.class.isAssignableFrom(clazz);
52     }
53
54     public void validateJobDetails(ReportJob job, Errors errors) {
55         validate(job, errors, new String JavaDoc[]{"label", "description"});
56     }
57
58     public void validateJobTrigger(ReportJob job, Errors errors) {
59         validate(job, errors, new String JavaDoc[]{"trigger"});
60     }
61
62     public void validateJobOutput(ReportJob job, Errors errors) {
63         validate(job, errors, new String JavaDoc[]{"baseOutputFilename", "outputFormats", "contentRepositoryDestination", "mailNotification"});
64     }
65
66     public void validate(Object JavaDoc obj, Errors errors) {
67         validate((ReportJob) obj, errors, null);
68     }
69     
70     protected void validate(ReportJob job, Errors errors, String JavaDoc[] fieldPrefixes) {
71         ValidationErrors validationErrors = schedulingService.validateJob(null, job);//TODO context
72
setErrors(errors, validationErrors, fieldPrefixes);
73     }
74
75     protected void setErrors(Errors errors, ValidationErrors validationErrors, String JavaDoc[] fieldPrefixes) {
76         if (validationErrors.isError()) {
77             List JavaDoc errorList = validationErrors.getErrors();
78             for (Iterator JavaDoc it = errorList.iterator(); it.hasNext();) {
79                 ValidationError error = (ValidationError) it.next();
80                 if (matches(error, fieldPrefixes)) {
81                     setError(errors, error);
82                 }
83             }
84         }
85     }
86
87     protected boolean matches(ValidationError error, String JavaDoc[] fieldPrefixes) {
88         String JavaDoc field = error.getField();
89         if (fieldPrefixes == null || field == null) {
90             return true;
91         }
92         boolean match = false;
93         for (int i = 0; !match && i < fieldPrefixes.length; i++) {
94             String JavaDoc prefix = fieldPrefixes[i];
95             match |= field.startsWith(prefix);
96         }
97         return match;
98     }
99
100     protected void setError(Errors errors, ValidationError error) {
101         if (error.getField() == null) {
102             errors.reject(error.getErrorCode(), error.getErrorArguments(), error.getDefaultMessage());
103         } else {
104             errors.rejectValue(error.getField(), error.getErrorCode(), error.getErrorArguments(), error.getDefaultMessage());
105         }
106     }
107
108 }
109
Popular Tags