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: /**
066: * Messages.java
067: *
068: * Copyright 2001 Jcorporate Ltd.
069: */package com.jcorporate.expresso.core.i18n;
070:
071: import com.jcorporate.expresso.core.db.DBException;
072: import com.jcorporate.expresso.core.jsdkapi.GenericSession;
073: import com.jcorporate.expresso.core.misc.ConfigContext;
074: import com.jcorporate.expresso.core.misc.ConfigManager;
075: import com.jcorporate.expresso.core.misc.ConfigurationException;
076: import com.jcorporate.expresso.core.misc.CurrentLogin;
077: import com.jcorporate.expresso.core.misc.StringUtil;
078: import com.jcorporate.expresso.core.security.User;
079: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
080: import com.jcorporate.expresso.services.dbobj.UserPreference;
081: import com.jcorporate.expresso.services.dbobj.UserPreferenceDef;
082: import org.apache.log4j.Logger;
083: import org.apache.log4j.Priority;
084:
085: import javax.servlet.ServletException;
086: import javax.servlet.http.HttpServletRequest;
087: import java.util.HashMap;
088: import java.util.ListIterator;
089: import java.util.Locale;
090: import java.util.Map;
091: import java.util.Stack;
092:
093: /**
094: * Messages.java
095: * The Messages object provides a way to gain access to the appropriate
096: * local-language version of a message, and to provide substitution of
097: * parameters in the message string if necessary.
098: * The appropriate local language is determined by means of the following:
099: * <p/>
100: * Created on January 10, 2001, 8:35 AM
101: *
102: * @author Michael Nash
103: */
104: public class Messages extends java.lang.Object {
105:
106: /**
107: * Schema to use by default if none is specified
108: */
109: private static String defaultSchema = "com.jcorporate.expresso.core.ExpressoSchema";
110:
111: private static Logger log = Logger.getLogger(Messages.class);
112:
113: private static Map messageBundlesBySchema = new HashMap();
114: private static Map messageBundlesByPath = new HashMap();
115:
116: public final static String LOCALE_KEY = "expresso.LOCALE";
117:
118: /**
119: * Creates new Class
120: */
121: public Messages() {
122: } /* Messages() */
123:
124: /**
125: * The getBundleBySchema method fetches the MessageBundle object
126: * for the specified schema name, language and country. It tries
127: * the cache of MessageBundle objects first, and adds the object
128: * to the cache if it must be created
129: *
130: * @param schemaName
131: * @param l locale to use
132: * @return
133: */
134: private static synchronized MessageBundle getBundleBySchema(
135: String schemaName, Locale l) {
136: if (l == null) {
137: l = Locale.getDefault();
138: }
139:
140: String language = l.getLanguage();
141: String country = l.getCountry();
142: FastStringBuffer bundleKey = new FastStringBuffer(schemaName);
143: bundleKey.append("|");
144: bundleKey.append(language);
145: bundleKey.append("|");
146: bundleKey.append(country);
147:
148: MessageBundle myBundle = (MessageBundle) messageBundlesBySchema
149: .get(bundleKey.toString());
150:
151: if (myBundle == null) {
152: System.out.println("Loading Messagebundle: " + schemaName);
153: myBundle = new MessageBundle();
154: myBundle.setSchema(schemaName);
155: myBundle.setLanguage(language);
156: myBundle.setCountry(country);
157: messageBundlesBySchema.put(bundleKey.toString(), myBundle);
158:
159: FastStringBuffer pathKey = new FastStringBuffer(myBundle
160: .getBundlePath());
161: pathKey.append("|");
162: pathKey.append(language);
163: pathKey.append("|");
164: pathKey.append(country);
165: messageBundlesByPath.put(pathKey.toString(), myBundle);
166: }
167:
168: return myBundle;
169: } /* getBundleBySchema(String, String, String) */
170:
171: /**
172: * Like the getBundleBySchema object, this method retrieves a MessageBundle
173: * object, but in this case for a specified bundle path.
174: *
175: * @param bundlePath
176: * @param language
177: * @param country
178: * @return
179: */
180: private static synchronized MessageBundle getBundleByPath(
181: String bundlePath, String language, String country) {
182: FastStringBuffer bundleKey = new FastStringBuffer(bundlePath);
183: bundleKey.append("|");
184: bundleKey.append(language);
185: bundleKey.append("|");
186: bundleKey.append(country);
187:
188: MessageBundle myBundle = (MessageBundle) messageBundlesByPath
189: .get(bundleKey.toString());
190:
191: if (myBundle == null) {
192: System.out.println("Loading Messagebundle: " + bundlePath);
193: myBundle = new MessageBundle();
194: myBundle.setBundlePath(bundlePath);
195: myBundle.setLanguage(language);
196: myBundle.setCountry(country);
197: messageBundlesByPath.put(myBundle.getBundlePath() + "|"
198: + language + "|" + country, myBundle);
199: }
200:
201: return myBundle;
202: } /* getBundleByPath(String, String, String) */
203:
204: /**
205: * The "full" form of getString must specify a schema, language and
206: * country. If we don't have one of these things, we use the convenience
207: * methods to determine them
208: *
209: * @param schemaClass the schema class
210: * @param l the Locale
211: * @param stringCode the string code
212: * @param args any arguments.
213: * @return translated string, or if not found, the stringCode
214: */
215: public static String getString(String schemaClass, Locale l,
216: String stringCode, Object[] args) {
217:
218: if (l == null) {
219: l = Locale.getDefault();
220: }
221:
222: MessageBundle mb = getBundleBySchema(schemaClass, l);
223:
224: return mb.getString(stringCode, args);
225: } /* getString(String, String, String, String, Object[] */
226:
227: /**
228: * Return a string out of our message bundle associated with the specified schemaClass,
229: * throwing an exception if it's not found
230: *
231: * @param schemaClass the schema class
232: * @param l the Locale
233: * @param stringCode the string code
234: * @param args any arguments.
235: * @return java.lang.String
236: * @throws IllegalArgumentException if the given string code doesn't exist in the given bundle.
237: */
238: public static String getStringRequired(String schemaClass,
239: Locale l, String stringCode, Object[] args)
240: throws IllegalArgumentException {
241:
242: if (l == null) {
243: l = Locale.getDefault();
244: }
245:
246: MessageBundle mb = getBundleBySchema(schemaClass, l);
247:
248: return mb.getStringRequired(stringCode, args);
249: } /* getString(String, String, String, String, Object[] */
250:
251: /**
252: * Return a string out of our message bundle associated with the specified schemaClass,
253: * throwing an exception if it's not found
254: *
255: * @param schemaClass the schema class
256: * @param stringCode the string code
257: * @return java.lang.String
258: * @throws IllegalArgumentException if the given string code doesn't exist in the given bundle.
259: */
260: public static String getStringRequired(String schemaClass,
261: String stringCode) {
262: MessageBundle mb = getBundleBySchema(schemaClass, Locale
263: .getDefault());
264: return mb.getStringRequired(stringCode, null);
265: }
266:
267: /**
268: * Convenience method that gets a string using the schemaStack, looping
269: * through it until the key is found
270: *
271: * @param schemaStack A Stack of schemas
272: * @param ourLocale The specified Locale Object
273: * @param stringCode The string code to retrieve
274: * @param args the formatting arguments for the String
275: * @return The properly formatted string as read from the messages bundle or the key if not found
276: * @throws IllegalArgumentException if the given string code doesn't exist in any schema in stack.
277: */
278: public static String getStringRequired(Stack schemaStack,
279: Locale ourLocale, String stringCode, Object[] args)
280: throws IllegalArgumentException {
281: String result = null;
282:
283: for (ListIterator li = schemaStack.listIterator(schemaStack
284: .size()); li.hasPrevious();) {
285: String schema = (String) li.previous();
286: result = getStringOrNull(schema, ourLocale, stringCode,
287: args);
288: if (result != null) {
289: break;
290: }
291: }
292:
293: // stack might have been empty, or not included default
294: if (result == null) {
295: // last chance; will throw
296: result = getStringRequired(defaultSchema, ourLocale,
297: stringCode, args);
298: }
299:
300: return result;
301: }
302:
303: /**
304: * Convenience method that gets a string using the schemaStack, looping
305: * through it until the key is found
306: *
307: * @param schemaStack A Stack of schemas
308: * @param ourLocale The specified Locale Object
309: * @param stringCode The string code to retrieve
310: * @param args the formatting arguments for the String
311: * @return The properly formatted string as read from the messages bundle or the key if not found
312: */
313: public static String getStringUnrequired(Stack schemaStack,
314: Locale ourLocale, String stringCode, Object[] args)
315: throws IllegalArgumentException {
316: String result = null;
317:
318: for (ListIterator li = schemaStack.listIterator(schemaStack
319: .size()); li.hasPrevious();) {
320: String schema = (String) li.previous();
321: result = getStringOrNull(schema, ourLocale, stringCode,
322: args);
323: if (result != null) {
324: break;
325: }
326: }
327:
328: // stack might have been empty, or not included default
329: if (result == null) {
330: // last chance; will throw
331: result = getStringOrNull(defaultSchema, ourLocale,
332: stringCode, args);
333: }
334:
335: if (result == null) {
336: result = stringCode;
337: }
338:
339: return result;
340: }
341:
342: /**
343: * Variation on the above gets a string from a specified message bundle
344: * path
345: *
346: * @param bundlePath
347: * @param language
348: * @param country
349: * @param stringCode
350: * @param args
351: * @return
352: */
353: public static String getStringByPath(String bundlePath,
354: String language, String country, String stringCode,
355: Object[] args) {
356: MessageBundle mb = getBundleByPath(bundlePath, language,
357: country);
358:
359: return mb.getString(stringCode, args);
360: } /* getStringByPath(String, String, String, String, Object[]) */
361:
362: /**
363: * Set the default schema to a class name of a Schema object
364: *
365: * @param newDefault
366: */
367: public static void setDefaultSchema(String newDefault) {
368: defaultSchema = newDefault;
369: } /* setDefaultSchema(String) */
370:
371: /**
372: * Convenience method to return a specified string from the default
373: * schema, with no arguments
374: *
375: * @param stringCode
376: * @return
377: */
378: public static String getString(String stringCode) {
379: Object[] args = {};
380:
381: return getString(defaultSchema, stringCode, args);
382: } /* getString(String) */
383:
384: /**
385: * Convenience method that gets a string with arguments
386: * using the default schema (e.g. no specific schema specified)
387: * but with the specified arguments
388: *
389: * @param stringCode
390: * @param args
391: * @return
392: */
393: public static String getString(String stringCode, Object[] args) {
394: return getString(defaultSchema, stringCode, args);
395: } /* getString(String, Object[]) */
396:
397: /**
398: * get string translation
399: *
400: * @param schemaClass name of schema
401: * @param stringCode The string code to retrieve
402: * @return translated string, or if not found, the stringCode
403: */
404: public static String getString(String schemaClass, String stringCode) {
405: return getString(schemaClass, stringCode, null);
406: } /* getString(String, String) */
407:
408: /**
409: * Convenience method that gets a string using the schemaStack, looping
410: * through stack until the key is found
411: *
412: * @param schemaStack A Stack of schemas
413: * @param ourLocale The specified Locale Object
414: * @param stringCode The string code to retrieve
415: * @param args the formatting arguments for the String
416: * @return The properly formatted string as read from the messages bundle or the key if not found
417: */
418: public static String getString(Stack schemaStack, Locale ourLocale,
419: String stringCode, Object[] args) {
420: String result = null;
421: for (ListIterator li = schemaStack.listIterator(schemaStack
422: .size()); li.hasPrevious();) {
423: String schema = (String) li.previous();
424: result = getStringOrNull(schema, ourLocale, stringCode,
425: args);
426: if (result != null) {
427: break;
428: }
429: }
430:
431: if (result == null) {
432: result = stringCode;
433: // log warning
434: if (log.isEnabledFor(Priority.WARN)) {
435: String msg = "No such key '" + stringCode
436: + "' in bundle for schemas: '"
437: + schemaStack.toString() + "'";
438: if (log.isDebugEnabled()) {
439: msg += " with language '" + ourLocale.getLanguage()
440: + "', " + "Country '"
441: + ourLocale.getCountry() + "'";
442: }
443: log.warn(msg);
444: }
445: }
446:
447: return result;
448: } /* getString(String, String) */
449:
450: /**
451: * @return translation string, or null if not found
452: */
453: private static String getStringOrNull(String schema,
454: Locale ourLocale, String stringCode, Object[] args) {
455: if (ourLocale == null) {
456: ourLocale = Locale.getDefault();
457: }
458:
459: MessageBundle mb = getBundleBySchema(schema, ourLocale);
460: return mb.getStringOrNull(stringCode, args);
461: }
462:
463: /**
464: * Convenience method to use the current default schema
465: *
466: * @param schemaClass
467: * @param req
468: * @param stringCode
469: * @param args
470: * @return translated string, or if not found, the stringCode
471: */
472: public static String getString(String schemaClass,
473: HttpServletRequest req, String stringCode, Object[] args)
474: throws ServletException {
475: Locale l = getLocale(req);
476:
477: return getString(schemaClass, l, stringCode, args);
478: } /* getString(String, HttpServletRequest, String, Object[]) */
479:
480: public static String getString(Locale l, String stringCode) {
481: Object[] args = {};
482:
483: return getString(defaultSchema, l, stringCode, args);
484: }
485:
486: public static String getString(Locale l, String stringCode,
487: Object[] args) {
488: return getString(defaultSchema, l, stringCode, args);
489: }
490:
491: /**
492: * getString(schema, locale, stringCode)
493: *
494: * @return translated string, or if not found, the stringCode
495: */
496: public static String getString(String schemaClass, Locale l,
497: String stringCode) {
498: Object[] args = {};
499: return getString(schemaClass, l, stringCode, args);
500: } /* getString(String, String, String) */
501:
502: /**
503: * Figure out a Locale by reading the current request.
504: * If there is a currently logged-in user, use that users UserPreference
505: * settings to determine the language.
506: * If there is no logged-in user, use the browser settings if available.
507: * If no user and no browser settings, use the context's default language.
508: * If there is no context default setting, use english.
509: * At the end of calling this method, the session will contain a locale
510: * object for this user, one way or another :-)
511: */
512: public static void establishLocale(HttpServletRequest req)
513: throws ServletException {
514: if (req == null) {
515: throw new IllegalArgumentException(
516: "Request may not be null here");
517: }
518: if (GenericSession.getAttribute(req, LOCALE_KEY) != null) {
519: return;
520: }
521:
522: Locale newLocale = null;
523:
524: /* If there is a currently logged-in user, use that users UserPreference */
525: User myUser = getLoggedInUser(req);
526: String db = GenericSession.getAttributeString(req, "db");
527:
528: if (myUser != null) {
529: newLocale = setLocaleFromPreferences(myUser, db);
530: }
531: /* If we haven't found a preference (or there was no user) */
532: if (newLocale == null) {
533: newLocale = req.getLocale();
534: }
535:
536: GenericSession.setAttribute(req, LOCALE_KEY, newLocale);
537: } /* establishLocale(HttpServletRequest) */
538:
539: /**
540: * Return a user object if there is a currently logged-in user
541: * defined in the session, else return 'null'
542: */
543: private static User getLoggedInUser(HttpServletRequest req)
544: throws ServletException {
545: String db = GenericSession.getAttributeString(req, "db");
546:
547: if (db.equals("")) {
548: db = "default";
549: }
550:
551: // String userName = GenericSession.getAttributeString(req, "UserName");
552: CurrentLogin login = (CurrentLogin) GenericSession
553: .getAttribute(req, "CurrentLogin");
554: String userName = "";
555: if (login != null) {
556: userName = login.getUserName();
557: }
558:
559: if (userName.equals("")) {
560: userName = User.UNKNOWN_USER;
561: }
562: if (userName.equals(User.UNKNOWN_USER)) {
563: return null;
564: }
565: try {
566: User myUser = new User();
567: myUser.setDataContext(db);
568:
569: //Clyde - Fixed bug
570: //before - myUser.setField("LoginName", userName);
571: myUser.setLoginName(userName);
572: myUser.find();
573:
574: //Clyde - Fixed bug
575: return myUser;
576: } catch (DBException de) {
577: return null;
578: }
579: } /* getLoggedInUser */
580:
581: /**
582: * Return the local the user specified in his/her UserPreferences,
583: * or the default if one is set. If none, or if "browser" is set,
584: * return null
585: */
586: private static Locale setLocaleFromPreferences(User myUser,
587: String dbcontext) throws ServletException {
588: String language = ("");
589: String country = ("");
590:
591: try {
592:
593: /* try the preferences for this user */
594: UserPreference up = new UserPreference();
595: up.setDataContext(dbcontext);
596:
597: //Clyde - Fixed the following bug
598: //before - up.setField("UserName", myUser.getUserName());
599: up.setField("ExpUid", myUser.getUid());
600: up.setField("ClassName",
601: "com.jcorporate.expresso.core.servlet.CheckLogin");
602: up.setField("PrefCode", "language");
603:
604: if (up.find()) {
605: language = up.getField("PrefValue");
606:
607: if (log.isDebugEnabled()) {
608: log.debug("Language preference for user '"
609: + myUser.getDisplayName() + "' was '"
610: + language + "'");
611: }
612: if (language.equals("Browser")) {
613: return null;
614: }
615:
616: up.clear();
617:
618: //Clyde - Fixed the following bug
619: //before - up.setField("UserName", myUser.getField("LoginName"));
620: up.setField("ExpUid", myUser.getUid());
621: up
622: .setField("ClassName",
623: "com.jcorporate.expresso.core.servlet.CheckLogin");
624: up.setField("PrefCode", "country");
625:
626: if (up.find()) {
627: country = up.getField("PrefValue");
628:
629: if (log.isDebugEnabled()) {
630: log.debug("Country preference for user '"
631: + myUser.getDisplayName() + "' was '"
632: + country + "'");
633: }
634: } else {
635: if (log.isDebugEnabled()) {
636: log.debug("No country preference found for '"
637: + myUser.getDisplayName() + "'");
638: }
639: }
640:
641: return new Locale(language, country);
642: }
643:
644: UserPreferenceDef upd = new UserPreferenceDef();
645: upd.setDataContext(dbcontext);
646: upd.setField("ClassName",
647: "com.jcorporate.expresso.core.servlet.CheckLogin");
648: upd.setField("PrefCode", "language");
649:
650: if (!upd.find()) {
651: return null;
652: }
653: if (upd.getField("DefaultVal").equals("Browser")) {
654: return null;
655: } else {
656: language = upd.getField("DefaultVal");
657: upd.clear();
658: upd
659: .setField("ClassName",
660: "com.jcorporate.expresso.core.servlet.CheckLogin");
661: upd.setField("PrefCode", "language");
662:
663: if (upd.find()) {
664: country = upd.getField("DefaultVal");
665: }
666: }
667: } catch (DBException de) {
668: log.error(de);
669: throw new ServletException(
670: "Unable to retrive language preferences");
671: }
672:
673: return new Locale(language, country);
674: } /* establishLocale */
675:
676: public static Locale getLocale(HttpServletRequest req)
677: throws ServletException {
678: if (req == null) {
679: throw new IllegalArgumentException(
680: "Request may not be null here");
681: }
682:
683: establishLocale(req);
684:
685: Object o = GenericSession.getAttribute(req, LOCALE_KEY);
686:
687: if (o != null) {
688: return (Locale) o;
689: } else {
690: return Locale.getDefault();
691: }
692: } /* getLocale(HttpServletRequest) */
693:
694: /**
695: * If we have no information about what language we should be using, we
696: * use the defaults out of the properties file if present. If there are no
697: * defaults in the property file, we use english
698: *
699: * @param schemaClass
700: * @param stringCode
701: * @param args
702: * @return translated string, or if not found, the stringCode
703: */
704: public static String getString(String schemaClass,
705: String stringCode, Object[] args) {
706: Locale theLocale = getDefaultLocale();
707:
708: return getString(schemaClass, theLocale, stringCode, args);
709: }
710:
711: /**
712: * get default locale (much better to get it from user's request!!)
713: *
714: * @return locale based on 'default' config context
715: * @see #getLocale(javax.servlet.http.HttpServletRequest)
716: */
717: public static Locale getDefaultLocale() {
718: String language = "en";
719: String country = "US";
720:
721: try {
722: ConfigContext myContext = ConfigManager
723: .getContext("default");
724: language = StringUtil.notNull(myContext.getLanguage());
725: country = StringUtil.notNull(myContext.getCountry());
726: } catch (ConfigurationException ce) {
727: log.error(ce);
728: }
729:
730: Locale theLocale = new Locale(language, country);
731: return theLocale;
732: }
733:
734: /**
735: * use locale based on user ID, which is set as user preference;
736: * use with discrimination--it may be better to get locale from request
737: *
738: * @param uid
739: * @param dbName
740: * @param schemaClass
741: * @param stringCode
742: * @param args
743: * @return translated string, or if not found, the stringCode
744: * @see #getString(java.util.Locale, String)
745: */
746: public static String getStringForUser(int uid, String dbName,
747: String schemaClass, String stringCode, Object[] args) {
748: if (log.isDebugEnabled()) {
749: log.debug("Getting string for user '" + uid + "', db '"
750: + dbName + "', key '" + stringCode + "'");
751: }
752:
753: StringUtil.assertNotBlank(schemaClass,
754: "Schema class name may not be blank here");
755: StringUtil.assertNotBlank(stringCode,
756: "String code may not be blank here");
757: StringUtil.assertNotBlank(dbName,
758: "DB name/context must not be blank here");
759:
760: String language = "";
761: String country = "";
762:
763: /* try the preferences for this user */
764: try {
765: UserPreference up = new UserPreference();
766: up.setDataContext(dbName);
767: up.setField("ExpUid", uid);
768: up.setField("PrefCode", "language");
769:
770: if (up.find()) {
771: language = up.getField("PrefValue");
772: up.setField("ExpUid", uid);
773: up.setField("PrefCode", "country");
774:
775: if (up.find()) {
776: country = up.getField("PrefValue");
777: }
778: } else {
779:
780: /* If we have no specific settings for the user, then use the */
781:
782: /* defaults out of the property file */
783: try {
784: ConfigContext myContext = ConfigManager
785: .getContext(dbName);
786: language = StringUtil.notNull(myContext
787: .getLanguage());
788: country = StringUtil
789: .notNull(myContext.getCountry());
790: } catch (ConfigurationException ce) {
791: log.error(ce);
792: }
793: }
794: } catch (DBException de) {
795: log.error(de);
796: language = "";
797: }
798: /* If we *still* have no language, then use english */
799: if (language.equals("")) {
800: language = "en";
801: country = "US";
802: }
803:
804: return getString(schemaClass, new Locale(language, country),
805: stringCode, args);
806: } /* getStringForUser(String, String, String, String, Object[]) */
807:
808: /**
809: * @param req
810: * @param stringCode
811: * @return translated string, or if not found, the stringCode
812: */
813: public static String getString(HttpServletRequest req,
814: String stringCode) throws ServletException {
815: Object[] args = {};
816:
817: return getString(defaultSchema, req, stringCode, args);
818: } /* getString(HttpServletRequest, String) */
819:
820: /**
821: * Return a string out of our message bundle
822: *
823: * @param req
824: * @param stringCode
825: * @param firstReplace
826: * @return
827: */
828: public static String getString(HttpServletRequest req,
829: String stringCode, String firstReplace)
830: throws ServletException {
831: Object[] args = { firstReplace };
832:
833: return getString(defaultSchema, req, stringCode, args);
834: } /* getString(HttpServletRequest, String, String) */
835:
836: /**
837: * Return a string out of our message bundle
838: *
839: * @param req
840: * @param stringCode
841: * @param firstReplace
842: * @param secondReplace
843: * @return
844: */
845: public String getString(HttpServletRequest req, String stringCode,
846: String firstReplace, String secondReplace)
847: throws ServletException {
848: Object[] args = { firstReplace, secondReplace };
849:
850: return getString(defaultSchema, req, stringCode, args);
851: } /* getString(HttpServletRequest, String, String, String) */
852:
853: /**
854: * Convenience method to return a string out of our message bundle, using the thres
855: * string arguments as the replacement arguments
856: *
857: * @param req
858: * @param stringCode
859: * @param firstReplace
860: * @param secondReplace
861: * @param thirdReplace
862: * @return
863: */
864: public String getString(HttpServletRequest req, String stringCode,
865: String firstReplace, String secondReplace,
866: String thirdReplace) throws ServletException {
867: Object[] args = { firstReplace, secondReplace, thirdReplace };
868:
869: return getString(defaultSchema, req, stringCode, args);
870: } /* getString(HttpServletRequest, String, String, String, String) */
871:
872: } /* Messages */
|