KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > validation > EngineValidationHelper


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 28-Feb-2005, Commaro, Benjamin Papez
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41
42 package org.jahia.engines.validation;
43
44 import org.apache.commons.validator.Field;
45 import org.apache.commons.validator.Form;
46 import org.apache.commons.validator.ValidatorResources;
47 import org.apache.struts.validator.Resources;
48 import org.jahia.data.containers.JahiaContainer;
49 import org.jahia.data.containers.JahiaContainerDefinition;
50 import org.jahia.data.fields.JahiaField;
51 import org.jahia.exceptions.JahiaException;
52 import org.jahia.params.ParamBean;
53
54 import java.util.ArrayList JavaDoc;
55
56
57 /**
58  * <p>Title: EngineValidationHelper</p>
59  * <p>Description: This class is used to ease the validation process and groups the
60  * validation errors per screen. </p>
61  * <p>Copyright: Copyright (c) 2004</p>
62  * <p>Company: Jahia Ltd</p>
63  *
64  * @author not attributable
65  * @version 1.0
66  */

67 public class EngineValidationHelper {
68
69     private static final org.apache.log4j.Logger logger =
70             org.apache.log4j.Logger.getLogger(EngineValidationHelper.class);
71
72     private String JavaDoc previousScreen;
73     private String JavaDoc nextScreen;
74     private final ArrayList JavaDoc validationErrors;
75
76     public EngineValidationHelper() {
77         validationErrors = new ArrayList JavaDoc();
78     }
79
80     public String JavaDoc getPreviousScreen() {
81         return this.previousScreen;
82     }
83
84     public void setPreviousScreen(String JavaDoc screen) {
85         this.previousScreen = screen;
86     }
87
88     public String JavaDoc getNextScreen() {
89         return this.nextScreen;
90     }
91
92     public void setNextScreen(String JavaDoc screen) {
93         this.nextScreen = screen;
94     }
95
96     public void addError(ValidationError ve) {
97         if (ve != null) {
98             this.validationErrors.add(ve);
99         }
100     }
101
102     public ArrayList JavaDoc getErrors() {
103         return this.validationErrors;
104     }
105
106     public boolean hasErrors() {
107         return (this.validationErrors.size() > 0);
108     }
109
110     public String JavaDoc toString() {
111         final StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
112         buff.append(EngineValidationHelper.class.getName());
113         buff.append(": Errors: ").append(validationErrors);
114         return buff.toString();
115     }
116
117     /**
118      * Checks the Validation Rules to see if a given field is mandatory or not.
119      */

120     public static boolean isFieldMandatory(final JahiaContainer parentContainer,
121                                            final JahiaField theField,
122                                            final ParamBean jParams) throws JahiaException {
123         if (parentContainer == null || theField == null) return false;
124
125         final String JavaDoc fieldName = theField.getDefinition().getName();
126
127         final JahiaContainerDefinition def = parentContainer.getDefinition();
128         if (def == null) return false;
129
130         final String JavaDoc containerBeanName = def.getProperty("containerBeanName");
131         final String JavaDoc validatorKey = def.getProperty("validatorKey");
132
133         logger.debug("containerBeanName: " + containerBeanName +
134                 ", validatorKey: " + validatorKey);
135
136         if (containerBeanName != null
137                 && containerBeanName.length() > 0
138                 && validatorKey != null
139                 && validatorKey.length() > 0) {
140
141             final ParamBean paramBean = jParams;
142             final ValidatorResources vr = Resources.getValidatorResources(paramBean.getContext(),
143                     paramBean.getRequest());
144             if (vr == null) return false;
145             final Form form = vr.getForm(jParams.getLocale(), validatorKey);
146             logger.debug("Form: " + form);
147             if (form == null) return false;
148             final Field f = form.getField(fieldName);
149             if (f == null) return false;
150             return f.getDepends().indexOf("required") > -1 && fieldName.equals(f.getKey());
151         }
152         return false;
153     }
154 }
155
156
Popular Tags