KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jahia.engines.validation;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46
47 import org.apache.commons.beanutils.PropertyUtils;
48 import org.apache.commons.validator.Arg;
49 import org.apache.commons.validator.Field;
50 import org.apache.commons.validator.ValidatorAction;
51 import org.apache.log4j.Logger;
52 import org.apache.struts.action.ActionMessages;
53 import org.apache.struts.validator.FieldChecks;
54 import org.apache.struts.validator.Resources;
55 import org.jahia.bin.Jahia;
56 import org.jahia.engines.shared.JahiaPageEngineTempBean;
57 import org.jahia.engines.shared.Page_Field;
58 import org.jahia.exceptions.JahiaSessionExpirationException;
59 import org.jahia.params.ParamBean;
60 import org.jahia.services.sites.SiteLanguageSettings;
61
62 /**
63 *
64 * <p>Title: JahiaFieldChecks</p>
65 * <p>Description: This class implements the Jahia specific user input validation methods.</p>
66 * <p>Copyright: Copyright (c) 2004</p>
67 * <p>Company: Jahia Ltd</p>
68 * @author not attributable
69 * @version 1.0
70 */

71 public class JahiaFieldChecks extends FieldChecks {
72
73     private static final Logger logger =
74         Logger.getLogger(JahiaFieldChecks.class);
75
76     /**
77      *
78      */

79     public JahiaFieldChecks() {
80         super();
81     }
82
83     /**
84      * Checks if texts for all languages are provided.
85      *
86      * @param bean
87      * @param va
88      * @param field
89      * @param errors
90      * @param request
91      * @return true if text in all languages is given, false if not
92      */

93     public static boolean validateMandatoryMLText(
94         Object JavaDoc bean,
95         ValidatorAction va,
96         Field field,
97         ActionMessages errors,
98         HttpServletRequest JavaDoc request) {
99             
100         Arg mltProperty = field.getArg("mltProperty", 0);
101         Object JavaDoc value = getPropertyValue(bean,
102             mltProperty != null && "mltProperty".equals(mltProperty.getName())?
103                 mltProperty.getKey() : field.getProperty());
104
105         if (!(value instanceof JahiaMltHelper)) {
106             if (value != null) {
107                 logger.error(
108                     "property value must be an JahiaMltHelper "
109                         + field.getProperty());
110             }
111             return false;
112         } else {
113             JahiaMltHelper mltHelper = (JahiaMltHelper)value;
114             if (!isAllMandatoryLanguagesSet(mltHelper)) {
115                 errors.add(
116                     field.getKey(),
117                     Resources.getActionMessage(request, va, field));
118                 return false;
119             }
120             return true;
121         }
122     }
123
124     /**
125      * Checks if texts for all languages are provided, if a text in at least
126      * one language was already set.
127      *
128      * @param bean
129      * @param va
130      * @param field
131      * @param errors
132      * @param request
133      * @return true if text in all languages is given, false if not
134      */

135     public static boolean validateMandatoryMLTextIfSet(
136         Object JavaDoc bean,
137         ValidatorAction va,
138         Field field,
139         ActionMessages errors,
140         HttpServletRequest JavaDoc request) {
141             
142         Arg mltProperty = field.getArg("mltProperty", 0);
143         Object JavaDoc value = getPropertyValue(bean,
144             mltProperty != null && "mltProperty".equals(mltProperty.getName())?
145                 mltProperty.getKey() : field.getProperty());
146
147         if (!(value instanceof JahiaMltHelper)) {
148             if (value != null) {
149                 logger.error(
150                     "property value must be an JahiaMltHelper "
151                         + field.getProperty());
152             }
153             return false;
154         } else {
155             JahiaMltHelper mltHelper = (JahiaMltHelper)value;
156             String JavaDoc[] text = mltHelper.getText();
157             boolean allEmpty = true;
158             if (text != null) {
159                 for (int i = 0; i < text.length && allEmpty; i++) {
160                     if (text[i] != null && text[i].length() > 0) {
161                         allEmpty = false;
162                     }
163                 }
164             }
165             if (!allEmpty && !isAllMandatoryLanguagesSet(mltHelper)) {
166                 errors.add(
167                     field.getKey(),
168                     Resources.getActionMessage(request, va, field));
169                 return false;
170             }
171             return true;
172         }
173     }
174
175     /**
176      * Checks if texts for all languages are provided, if a text in at least
177      * one language was already set.
178      *
179      * @param bean
180      * @param va
181      * @param field
182      * @param errors
183      * @param request
184      * @return true if text in all languages is given, false if not
185      */

186     public static boolean validateMandatoryTitleIfLinkValid(Object JavaDoc bean,
187             ValidatorAction va, Field field, ActionMessages errors,
188             HttpServletRequest JavaDoc request) {
189         boolean fieldValid = false;
190         String JavaDoc value = (String JavaDoc) getPropertyValue(bean, field.getProperty());
191         if (value != null) {
192             fieldValid = true;
193         } else {
194             ParamBean jParams = Jahia.getThreadParamBean();
195             HashMap JavaDoc pageBeans = null;
196             if (jParams != null) {
197                 try {
198                     pageBeans = (HashMap JavaDoc) jParams.getSession().getAttribute(
199                         "Page_Field.PageBeans");
200                 } catch (JahiaSessionExpirationException e) {
201                     logger.warn("Session expired - cannot validate", e);
202                 }
203             }
204             if (pageBeans == null) {
205                 pageBeans = new HashMap JavaDoc();
206             }
207
208             JahiaPageEngineTempBean pageBean = (JahiaPageEngineTempBean) pageBeans
209                 .get(field.getProperty());
210
211             if (pageBean == null || Page_Field.RESET_LINK.equals(pageBean.getOperation())) {
212                 fieldValid = true;
213             }
214         }
215         return fieldValid;
216     }
217     
218     /**
219      * Checks if texts for all languages do not exceed a specified maximum length.
220      *
221      * @param bean
222      * @param va
223      * @param field
224      * @param errors
225      * @param request
226      * @return true if text in all languages are less than maxlength, false if not
227      */

228     public static boolean validateMaxLengthMLText(
229         Object JavaDoc bean,
230         ValidatorAction va,
231         Field field,
232         ActionMessages errors,
233         HttpServletRequest JavaDoc request) {
234             
235         Arg mltProperty = field.getArg("mltProperty", 0);
236         Object JavaDoc value = getPropertyValue(bean,
237             mltProperty != null && "mltProperty".equals(mltProperty.getName())?
238                 mltProperty.getKey() : field.getProperty());
239
240         if (!(value instanceof JahiaMltHelper)) {
241             if (value != null) {
242                 logger.error(
243                     "property value must be an JahiaMltHelper "
244                         + field.getProperty());
245             }
246             return false;
247         } else {
248             JahiaMltHelper mltHelper = (JahiaMltHelper)value;
249
250             int maxLength = Integer.parseInt(field.getVarValue("maxlength"));
251             String JavaDoc[] texts = mltHelper.getText();
252             for (int i = 0; i < texts.length; i++) {
253                 if (texts[i] != null && texts[i].length() > maxLength) {
254                     errors.add(
255                         field.getKey(),
256                         Resources.getActionMessage(request, va, field));
257                     return false;
258                 }
259             }
260             return true;
261         }
262     }
263
264     /**
265      * Checks if texts for all languages isn't less than a specified minimum length.
266      *
267      * @param bean
268      * @param va
269      * @param field
270      * @param errors
271      * @param request
272      * @return true if text in all languages are not less than minlength, false if they are
273      */

274     public static boolean validateMinLengthMLText(
275         Object JavaDoc bean,
276         ValidatorAction va,
277         Field field,
278         ActionMessages errors,
279         HttpServletRequest JavaDoc request) {
280             
281         Arg mltProperty = field.getArg("mltProperty", 0);
282         Object JavaDoc value = getPropertyValue(bean,
283             mltProperty != null && "mltProperty".equals(mltProperty.getName())?
284                 mltProperty.getKey() : field.getProperty());
285
286         if (!(value instanceof JahiaMltHelper)) {
287             if (value != null) {
288                 logger.error(
289                     "property value must be an JahiaMltHelper "
290                         + field.getProperty());
291             }
292             return false;
293         } else {
294             JahiaMltHelper mltHelper = (JahiaMltHelper)value;
295
296             int minLength = Integer.parseInt(field.getVarValue("minlength"));
297             String JavaDoc[] texts = mltHelper.getText();
298             for (int i = 0; i < texts.length; i++) {
299                 if (texts[i] != null && texts[i].length() < minLength) {
300                     errors.add(
301                         field.getKey(),
302                         Resources.getActionMessage(request, va, field));
303                     return false;
304                 }
305             }
306             return true;
307         }
308     }
309
310     /**
311      * Check if all mandatory languages are set within the passed JahiaMltHelper Object
312      *
313      * @param mltHelper
314      * @return true if text in all languages is given, false if not
315      */

316     public static boolean isAllMandatoryLanguagesSet(JahiaMltHelper mltHelper) {
317         String JavaDoc[] texts = mltHelper.getText();
318         String JavaDoc[] lang = mltHelper.getLanguage();
319
320         for (Iterator JavaDoc languageIterator = mltHelper.getLanguageSettings()
321                 .listIterator(); languageIterator.hasNext();) {
322             SiteLanguageSettings siteLanguageSettings = (SiteLanguageSettings) languageIterator
323                     .next();
324             if (siteLanguageSettings.isMandatory()) {
325                 boolean found = false;
326                 for (int i = 0; texts != null && i < texts.length && !found; i++) {
327                     if (lang[i].equals("shared")
328                             || lang[i].equals(siteLanguageSettings.getCode())) {
329                         found = true;
330                         if (texts[i] == null || texts[i].length() == 0) {
331                             return false;
332                         }
333                     }
334                 }
335                 if (!found)
336                     return false;
337             }
338         }
339         return true;
340     }
341
342     /**
343      * Uses PropertyUtils to get property from bean.
344      * If PropertyUtils.getProperty() throws an exception, the exception is logged and null is returned.
345      * @param bean
346      * @param property
347      * @return the bean property or null
348      */

349     private static Object JavaDoc getPropertyValue(Object JavaDoc bean, String JavaDoc property) {
350         Object JavaDoc value = null;
351         try {
352             value = PropertyUtils.getProperty(bean, property);
353         } catch (Exception JavaDoc ex) {
354             logger.error("unable to get property value " + property, ex);
355         }
356         return value;
357     }
358
359 }
360
Popular Tags