KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > ISOValidValue


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj;
66
67 import com.jcorporate.expresso.core.i18n.Messages;
68 import com.jcorporate.expresso.core.misc.StringUtil;
69 import org.apache.log4j.Logger;
70
71 import java.util.Locale JavaDoc;
72
73 /**
74  * An ISO valid value is a enumerated collection very similar to a
75  * pure Struts label and value bean with <em>internationalization</em>
76  * (i18n) support. It typically represent an item of data that is
77  * displayed in a drop-down list box or a menu selection. A valid
78  * value has a real value as known as a key and description.
79  * <p/>
80  * To support internationalisation (i18n) the <code>description</code>
81  * field also has a dual role. The description field is used a look-up
82  * key into a resource bundle during <em>canonisation</em>.
83  * </p>
84  *
85  * @author Peter A. Pilgrim, Fri Dec 27 22:33:27 GMT 2002, Created XeNoNSoFT.com
86  * @version $Id: ISOValidValue.java,v 1.7 2004/11/18 02:03:27 lhamel Exp $
87  */

88 public class ISOValidValue extends ValidValue {
89     private static Logger log = Logger.getLogger(ISOValidValue.class);
90
91
92     /**
93      * Default constructor for creating a valid value.
94      * Please note: no canonization takes place within this method.
95      */

96     public ISOValidValue() {
97         super();
98     }
99
100     /**
101      * Original constructor for creating a valid value.
102      * Please note: no canonization takes place within this method.
103      *
104      * @param newValue the real value of the enumeration
105      * @param newDescrip the description of the enumeration
106      */

107     public ISOValidValue(String JavaDoc newValue, String JavaDoc newDescrip) {
108         super(newValue, newDescrip);
109     } /* ISOValidValue(String, String) */
110
111     /**
112      * Constructor of valid value enumeration which automatically
113      * <b>canonizes</b> the description attribute if it can be found
114      * in the schema resource bundle in the default locale
115      *
116      * @param schemaClass the fully qualified class name of the schema
117      * @param prefix the optional prefix argument
118      * @param value the real value string of the enumeration
119      * @param description the description string of the enumeration
120      * that is used as look-up key in the resource bundle
121      * @see #canonize( String schemaClass, Locale locale, String prefix )
122      */

123     public ISOValidValue(String JavaDoc schemaClass, String JavaDoc prefix,
124                          String JavaDoc value, String JavaDoc description) {
125         this(schemaClass, Locale.getDefault(), prefix, value, description);
126     }
127
128     /**
129      * Constructor of valid value enumeration which automatically
130      * <b>canonizes</b> the description attribute if it can be found
131      * in the schema resource bundle
132      *
133      * @param schemaClass the fully qualified class name of the schema
134      * @param locale the locale
135      * @param prefix the optional prefix argument
136      * @param value the real value string of the enumeration
137      * @param description the description string of the enumeration
138      * that is used as look-up key in the resource bundle
139      * @see #canonize( String schemaClass, Locale locale, String prefix )
140      */

141     public ISOValidValue(String JavaDoc schemaClass, Locale JavaDoc locale, String JavaDoc prefix,
142                          String JavaDoc value, String JavaDoc description) {
143         setValue(value);
144         setDescription(description);
145         canonize(schemaClass, locale, prefix);
146     }
147
148     /**
149      * Constructor of valid value enumeration which automatically
150      * <b>canonizes</b> the description attribute if it can be found
151      * in the schema resource bundle
152      *
153      * @param schemaClass the fully qualified class name of the schema
154      * @param request the request context
155      * @param prefix the optional prefix argument
156      * @param value the real value string of the enumeration
157      * @param description the description string of the enumeration
158      * that is used as look-up key in the resource bundle
159      * @see #canonize( RequestContext request, String schemaClass, String prefix )
160      */

161     public ISOValidValue(RequestContext request, String JavaDoc schemaClass, String JavaDoc prefix,
162                          String JavaDoc value, String JavaDoc description) {
163         setValue(value);
164         setDescription(description);
165         canonize(request, schemaClass, prefix);
166     }
167
168     /**
169      * Gets the real value of the valid value
170      *
171      * @return the value string
172      * @see #setValue
173      */

174     public String JavaDoc getValue() {
175         return value;
176     } /* getValue() */
177
178     /**
179      * Sets the real value of the valid value
180      *
181      * @param value the new value string
182      * @see #getValue
183      */

184     public void setValue(String JavaDoc value) {
185         this.value = StringUtil.notNull(value);
186     } /* getValue() */
187
188     /**
189      * Gets the real value of the valid value as a cache key
190      *
191      * @return the value string as a cache key
192      * @see #getValue
193      */

194     public String JavaDoc getKey() {
195         return getValue();
196     } /* getKey() */
197
198     /**
199      * Gets the description of the valid value
200      *
201      * @return the description
202      */

203     public String JavaDoc getDescription() {
204         return description;
205     } /* getDescription() */
206
207     /**
208      * Sets the real description of the valid value
209      *
210      * @param description the new description string
211      * @see #getDescription
212      */

213     public void setDescription(String JavaDoc description) {
214         this.description = StringUtil.notNull(description);
215     } /* getDescription() */
216
217     /**
218      * This is method will attempt to convert the
219      * <code>description</code> attribute into a the default locale value
220      * string from a message resource bundle. If the
221      * <code>prefix</code> parameter is not null then the
222      * <code>description</code> attribute is prepended with a full
223      * stop "." to the original description string to make a new look
224      * up key. Otherwise if the <code>prefix</code> parameter is null
225      * then original description is the look up key. The key value is
226      * used to look up the description from a resource bundle.
227      *
228      * @param schemaClass the fully qualified class name of the schema
229      * @param prefix the optional prefix argument
230      * @return result the string if found in the resource bundle or null
231      * @see #canonize( String schemaClass, Locale locale, String prefix )
232      */

233     public String JavaDoc canonize(String JavaDoc schemaClass, String JavaDoc prefix) {
234         return canonize(schemaClass, Locale.getDefault(), prefix);
235     }
236
237     /**
238      * This is method will attempt to convert the
239      * <code>description</code> attribute into a localised value
240      * string from a message resource bundle. If the
241      * <code>prefix</code> parameter is not null then the
242      * <code>description</code> attribute is prepended with a full
243      * stop "." to the original description string to make a new look
244      * up key. Otherwise if the <code>prefix</code> parameter is null
245      * then original description is the look up key. The key value is
246      * used to look up the description from a resource bundle.
247      *
248      * @param schemaClass the fully qualified class name of the schema
249      * @param locale the locale
250      * @param prefix the optional prefix argument
251      * @return result the string if found in the resource bundle or null
252      * @see #getDescription
253      */

254     public String JavaDoc canonize(String JavaDoc schemaClass, Locale JavaDoc locale, String JavaDoc prefix) {
255         // System.out.println( "ISOValidValue.canonize("+schemaClass+","+locale+","+prefix+" )" );
256

257         String JavaDoc key = (prefix != null ? prefix + "." + description : description);
258         String JavaDoc result = null;
259         try {
260             result = Messages.getString(schemaClass, locale, key, new Object JavaDoc[0]);
261             // System.out.println( "key:`"+key+"' => result:`"+result+"'" );
262
if (result != null) {
263                 description = result;
264             }
265         } catch (IllegalArgumentException JavaDoc iae) {
266             // Silently ignore exception message "No such key
267
// 'com.jcorporate.eforum.search.LAST_DAY' in bundle at
268
// 'com/jcorporate/eforum'"
269
log.warn("No such key:`" + key + "' found in schema bundle at `" + schemaClass + "'");
270             result = null;
271         }
272
273         return result;
274     }
275
276     /**
277      * This is method will attempt to convert the
278      * <code>description</code> attribute into a localised value
279      * string from a message resource bundle. If the
280      * <code>prefix</code> parameter is not null then the
281      * <code>description</code> attribute is prepended with a full
282      * stop "." to the original description string to make a new look
283      * up key. Otherwise if the <code>prefix</code> parameter is null
284      * then original description is the look up key. The key value is
285      * used to look up the description from a resource bundle.
286      *
287      * @param schemaClass the fully qualified class name of the schema
288      * @param request the request context containing locale and dbcontext
289      * @param prefix the optional prefix argument
290      * @return result the string if found in the resource bundle or null
291      * @see com.jcorporate.expresso.core.i18n.Messages#getString(String schemaClass, Locale l, String stringCode, Object[] args)
292      * @see #canonize( String schemaClass, String prefix )
293      * @see #canonize( String schemaClass, Locale locale, String prefix )
294      */

295     public String JavaDoc canonize(RequestContext request, String JavaDoc schemaClass, String JavaDoc prefix) {
296         // System.out.println( "ISOValidValue.canonize( RequestContext request, "+schemaClass+","+prefix+" )" );
297

298         String JavaDoc key = (prefix != null ? prefix + "." + description : description);
299         String JavaDoc result = null;
300         try {
301             result = Messages.getString(schemaClass, request.getLocale(), key, new Object JavaDoc[0]);
302             // System.out.println( "key:`"+key+"' => result:`"+result+"'" );
303
if (result != null) {
304                 description = result;
305             }
306         } catch (IllegalArgumentException JavaDoc iae) {
307             // Silently ignore exception message "No such key
308
// 'com.jcorporate.eforum.search.LAST_DAY' in bundle at
309
// 'com/jcorporate/eforum'"
310
log.warn("No such key:`" + key + "' found in schema bundle at `" + schemaClass + "'");
311             result = null;
312         }
313         return result;
314     }
315
316     /**
317      * Human readable string for debugging
318      *
319      * @return java.lang.String
320      */

321     public String JavaDoc toString() {
322         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
323         buf.append("ISOValidValue@" + Integer.toHexString(this.hashCode()) + "{");
324         buf.append("value:`" + value + "' (`" + description + "')");
325         buf.append("}");
326         return buf.toString();
327     }
328
329 }
330
331 /* ISOValidValue */
332
Popular Tags