View Javadoc

1   /*
2    * $Id: AbstractValidator.java,v 1.5 2004/04/07 14:05:04 eelco12 Exp $
3    * $Revision: 1.5 $
4    * $Date: 2004/04/07 14:05:04 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.validation;
32  
33  import java.util.Locale;
34  
35  import nl.openedge.baritus.FormBeanContext;
36  import nl.openedge.baritus.FormBeanCtrlBase;
37  import nl.openedge.baritus.util.MessageUtils;
38  
39  /***
40   * convenience class
41   * @author Eelco Hillenius
42   */
43  public abstract class AbstractValidator implements ValidationRuleDependend
44  {
45  
46  	private ValidationActivationRule validationActivationRule = null;
47  
48  	/***
49  	 * construct
50  	 */
51  	public AbstractValidator()
52  	{
53  		// nothing here
54  	}
55  	
56  	/***
57  	 * construct with validator activation rule
58  	 * @param rule validator activation rule
59  	 */
60  	public AbstractValidator(ValidationActivationRule rule)
61  	{
62  		setValidationRule(rule);
63  	}
64  	
65  	/***
66  	 * get localized message for given key
67  	 * @param key key of message
68  	 * @return String localized message
69  	 */
70  	public String getLocalizedMessage(String key)
71  	{
72  		return getLocalizedMessage(key, Locale.getDefault());
73  	}
74  	
75  	/***
76  	 * get localized message for given key and locale. 
77  	 * If locale is null, the default locale will be used
78  	 * @param key key of message
79  	 * @param locale locale for message
80  	 * @return String localized message
81  	 */
82  	public String getLocalizedMessage(String key, Locale locale)
83  	{	
84  		return MessageUtils.getLocalizedMessage(key, locale);
85  	}
86  	
87  	/***
88  	 * Get localized message for given key and format it with the given parameters. 
89  	 * If locale is null, the default locale will be used
90  	 * @param key key of message
91  	 * @param parameters parameters for the message
92  	 * @return String localized message
93  	 */
94  	public String getLocalizedMessage(
95  			String key, Object[] parameters)
96  	{	
97  		return getLocalizedMessage(key, null, parameters);
98  	}
99  	
100 	/***
101 	 * Get localized message for given key and locale
102 	 * and format it with the given parameters. 
103 	 * If locale is null, the default locale will be used
104 	 * @param key key of message
105 	 * @param locale locale for message
106 	 * @param parameters parameters for the message
107 	 * @return String localized message
108 	 */
109 	public String getLocalizedMessage(
110 			String key, Locale locale, Object[] parameters)
111 	{	
112 		return MessageUtils.getLocalizedMessage(key, locale, parameters);
113 	}
114 
115 	/***
116 	 * @see nl.openedge.baritus.validation.ValidationRuleDependend#getValidationActivationRule()
117 	 */
118 	public ValidationActivationRule getValidationActivationRule()
119 	{
120 		return this.validationActivationRule;
121 	}
122 
123 	/***
124 	 * @see nl.openedge.baritus.validation.ValidationRuleDependend#removeValidationActivationRule()
125 	 */
126 	public void removeValidationActivationRule()
127 	{
128 		this.validationActivationRule = null;
129 	}
130 
131 	/***
132 	 * @see nl.openedge.baritus.validation.ValidationRuleDependend#setValidationRule(nl.openedge.baritus.validation.ValidationActivationRule)
133 	 */
134 	public void setValidationRule(ValidationActivationRule rule)
135 	{
136 		this.validationActivationRule = rule;
137 	}
138 	
139 	/***
140 	 * Get the - possibly translated - name of the field. If lookup in the
141 	 * resources failed, the provided name will be returned as is.
142 	 * @param formBeanContext form bean context
143 	 * @param name original name of field
144 	 * @return String the - possibly translated - name of the field.
145 	 * If lookup in the resources failed, the provided name will be returned as is.
146 	 */
147 	protected String getFieldName(FormBeanContext formBeanContext, String name)
148 	{
149 		FormBeanCtrlBase ctrl = formBeanContext.getController();
150 		String fieldName = MessageUtils.getLocalizedMessage(
151 			ctrl.getPropertyNameKey(name));
152 		return (fieldName != null) ? fieldName : name;
153 	}
154 	
155 	/***
156 	 * set error message in formBeanContext using the provided name,
157 	 * the current locale that is stored in the form bean context, the provided
158 	 * errorMessageKey and the provided arguments for parsing the localized message;
159 	 * the message will be stored in the form bean context with key that was
160 	 * provided as argument name. If no
161 	 * message was found, the provided errorMessageKey will be used.
162 	 * 
163 	 * @param formBeanContext form bean context
164 	 * @param name untransalated name of the field
165 	 * @param errorMessageKey message key for error
166 	 * @param messageArguments arguments for parsing the message
167 	 */
168 	protected void setErrorMessage(
169 		FormBeanContext formBeanContext, 
170 		String name,
171 		String errorMessageKey,
172 		Object[] messageArguments)
173 	{
174 		Locale locale = formBeanContext.getCurrentLocale();
175 		String msg = getLocalizedMessage(errorMessageKey, locale, messageArguments);
176 		if(msg == null) msg = errorMessageKey;
177 		formBeanContext.setError(name, msg);	
178 	}
179 
180 }