001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dbobj;
066:
067: import com.jcorporate.expresso.core.i18n.Messages;
068: import com.jcorporate.expresso.core.misc.StringUtil;
069: import org.apache.log4j.Logger;
070:
071: import java.util.Locale;
072:
073: /**
074: * An ISO valid value is a enumerated collection very similar to a
075: * pure Struts label and value bean with <em>internationalization</em>
076: * (i18n) support. It typically represent an item of data that is
077: * displayed in a drop-down list box or a menu selection. A valid
078: * value has a real value as known as a key and description.
079: * <p/>
080: * To support internationalisation (i18n) the <code>description</code>
081: * field also has a dual role. The description field is used a look-up
082: * key into a resource bundle during <em>canonisation</em>.
083: * </p>
084: *
085: * @author Peter A. Pilgrim, Fri Dec 27 22:33:27 GMT 2002, Created XeNoNSoFT.com
086: * @version $Id: ISOValidValue.java,v 1.7 2004/11/18 02:03:27 lhamel Exp $
087: */
088: public class ISOValidValue extends ValidValue {
089: private static Logger log = Logger.getLogger(ISOValidValue.class);
090:
091: /**
092: * Default constructor for creating a valid value.
093: * Please note: no canonization takes place within this method.
094: */
095: public ISOValidValue() {
096: super ();
097: }
098:
099: /**
100: * Original constructor for creating a valid value.
101: * Please note: no canonization takes place within this method.
102: *
103: * @param newValue the real value of the enumeration
104: * @param newDescrip the description of the enumeration
105: */
106: public ISOValidValue(String newValue, String newDescrip) {
107: super (newValue, newDescrip);
108: } /* ISOValidValue(String, String) */
109:
110: /**
111: * Constructor of valid value enumeration which automatically
112: * <b>canonizes</b> the description attribute if it can be found
113: * in the schema resource bundle in the default locale
114: *
115: * @param schemaClass the fully qualified class name of the schema
116: * @param prefix the optional prefix argument
117: * @param value the real value string of the enumeration
118: * @param description the description string of the enumeration
119: * that is used as look-up key in the resource bundle
120: * @see #canonize( String schemaClass, Locale locale, String prefix )
121: */
122: public ISOValidValue(String schemaClass, String prefix,
123: String value, String description) {
124: this (schemaClass, Locale.getDefault(), prefix, value,
125: 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 schemaClass, Locale locale,
142: String prefix, String value, String 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 schemaClass,
162: String prefix, String value, String 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 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 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 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 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 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 canonize(String schemaClass, String 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 canonize(String schemaClass, Locale locale,
255: String prefix) {
256: // System.out.println( "ISOValidValue.canonize("+schemaClass+","+locale+","+prefix+" )" );
257:
258: String key = (prefix != null ? prefix + "." + description
259: : description);
260: String result = null;
261: try {
262: result = Messages.getString(schemaClass, locale, key,
263: new Object[0]);
264: // System.out.println( "key:`"+key+"' => result:`"+result+"'" );
265: if (result != null) {
266: description = result;
267: }
268: } catch (IllegalArgumentException iae) {
269: // Silently ignore exception message "No such key
270: // 'com.jcorporate.eforum.search.LAST_DAY' in bundle at
271: // 'com/jcorporate/eforum'"
272: log.warn("No such key:`" + key
273: + "' found in schema bundle at `" + schemaClass
274: + "'");
275: result = null;
276: }
277:
278: return result;
279: }
280:
281: /**
282: * This is method will attempt to convert the
283: * <code>description</code> attribute into a localised value
284: * string from a message resource bundle. If the
285: * <code>prefix</code> parameter is not null then the
286: * <code>description</code> attribute is prepended with a full
287: * stop "." to the original description string to make a new look
288: * up key. Otherwise if the <code>prefix</code> parameter is null
289: * then original description is the look up key. The key value is
290: * used to look up the description from a resource bundle.
291: *
292: * @param schemaClass the fully qualified class name of the schema
293: * @param request the request context containing locale and dbcontext
294: * @param prefix the optional prefix argument
295: * @return result the string if found in the resource bundle or null
296: * @see com.jcorporate.expresso.core.i18n.Messages#getString(String schemaClass, Locale l, String stringCode, Object[] args)
297: * @see #canonize( String schemaClass, String prefix )
298: * @see #canonize( String schemaClass, Locale locale, String prefix )
299: */
300: public String canonize(RequestContext request, String schemaClass,
301: String prefix) {
302: // System.out.println( "ISOValidValue.canonize( RequestContext request, "+schemaClass+","+prefix+" )" );
303:
304: String key = (prefix != null ? prefix + "." + description
305: : description);
306: String result = null;
307: try {
308: result = Messages.getString(schemaClass, request
309: .getLocale(), key, new Object[0]);
310: // System.out.println( "key:`"+key+"' => result:`"+result+"'" );
311: if (result != null) {
312: description = result;
313: }
314: } catch (IllegalArgumentException iae) {
315: // Silently ignore exception message "No such key
316: // 'com.jcorporate.eforum.search.LAST_DAY' in bundle at
317: // 'com/jcorporate/eforum'"
318: log.warn("No such key:`" + key
319: + "' found in schema bundle at `" + schemaClass
320: + "'");
321: result = null;
322: }
323: return result;
324: }
325:
326: /**
327: * Human readable string for debugging
328: *
329: * @return java.lang.String
330: */
331: public String toString() {
332: StringBuffer buf = new StringBuffer();
333: buf.append("ISOValidValue@"
334: + Integer.toHexString(this .hashCode()) + "{");
335: buf.append("value:`" + value + "' (`" + description + "')");
336: buf.append("}");
337: return buf.toString();
338: }
339:
340: }
341:
342: /* ISOValidValue */
|