1 64 65 70 package com.jcorporate.expresso.core.i18n; 71 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 74 import com.jcorporate.expresso.core.misc.ConfigContext; 75 import com.jcorporate.expresso.core.misc.ConfigManager; 76 import com.jcorporate.expresso.core.misc.ConfigurationException; 77 import com.jcorporate.expresso.core.misc.CurrentLogin; 78 import com.jcorporate.expresso.core.misc.StringUtil; 79 import com.jcorporate.expresso.core.security.User; 80 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 81 import com.jcorporate.expresso.services.dbobj.UserPreference; 82 import com.jcorporate.expresso.services.dbobj.UserPreferenceDef; 83 import org.apache.log4j.Logger; 84 import org.apache.log4j.Priority; 85 86 import javax.servlet.ServletException ; 87 import javax.servlet.http.HttpServletRequest ; 88 import java.util.HashMap ; 89 import java.util.ListIterator ; 90 import java.util.Locale ; 91 import java.util.Map ; 92 import java.util.Stack ; 93 94 95 106 public class Messages 107 extends java.lang.Object { 108 109 112 private static String defaultSchema = "com.jcorporate.expresso.core.ExpressoSchema"; 113 114 private static Logger log = Logger.getLogger(Messages.class); 115 116 private static Map messageBundlesBySchema = new HashMap (); 117 private static Map messageBundlesByPath = new HashMap (); 118 119 public final static String LOCALE_KEY = "expresso.LOCALE"; 120 121 124 public Messages() { 125 } 126 127 137 private static synchronized MessageBundle getBundleBySchema(String schemaName, 138 Locale l) { 139 if (l == null) { 140 l = Locale.getDefault(); 141 } 142 143 String language = l.getLanguage(); 144 String country = l.getCountry(); 145 FastStringBuffer bundleKey = new FastStringBuffer(schemaName); 146 bundleKey.append("|"); 147 bundleKey.append(language); 148 bundleKey.append("|"); 149 bundleKey.append(country); 150 151 MessageBundle myBundle = (MessageBundle) messageBundlesBySchema.get(bundleKey.toString()); 152 153 if (myBundle == null) { 154 System.out.println("Loading Messagebundle: " + schemaName); 155 myBundle = new MessageBundle(); 156 myBundle.setSchema(schemaName); 157 myBundle.setLanguage(language); 158 myBundle.setCountry(country); 159 messageBundlesBySchema.put(bundleKey.toString(), myBundle); 160 161 FastStringBuffer pathKey = new FastStringBuffer(myBundle.getBundlePath()); 162 pathKey.append("|"); 163 pathKey.append(language); 164 pathKey.append("|"); 165 pathKey.append(country); 166 messageBundlesByPath.put(pathKey.toString(), myBundle); 167 } 168 169 return myBundle; 170 } 171 172 181 private static synchronized MessageBundle getBundleByPath(String bundlePath, 182 String language, 183 String country) { 184 FastStringBuffer bundleKey = new FastStringBuffer(bundlePath); 185 bundleKey.append("|"); 186 bundleKey.append(language); 187 bundleKey.append("|"); 188 bundleKey.append(country); 189 190 MessageBundle myBundle = (MessageBundle) messageBundlesByPath.get(bundleKey.toString()); 191 192 if (myBundle == null) { 193 System.out.println("Loading Messagebundle: " + bundlePath); 194 myBundle = new MessageBundle(); 195 myBundle.setBundlePath(bundlePath); 196 myBundle.setLanguage(language); 197 myBundle.setCountry(country); 198 messageBundlesByPath.put(myBundle.getBundlePath() + "|" + language + "|" + country, 199 myBundle); 200 } 201 202 return myBundle; 203 } 204 205 216 public static String getString(String schemaClass, Locale l, 217 String stringCode, Object [] args) { 218 219 if (l == null) { 220 l = Locale.getDefault(); 221 } 222 223 MessageBundle mb = getBundleBySchema(schemaClass, l); 224 225 return mb.getString(stringCode, args); 226 } 227 228 239 public static String getStringRequired(String schemaClass, Locale l, 240 String stringCode, Object [] args) 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 } 250 251 260 public static String getStringRequired(String schemaClass, String stringCode) { 261 MessageBundle mb = getBundleBySchema(schemaClass, Locale.getDefault()); 262 return mb.getStringRequired(stringCode, null); 263 } 264 265 276 public static String getStringRequired(Stack schemaStack, Locale ourLocale, String stringCode, Object [] args) throws IllegalArgumentException { 277 String result = null; 278 279 for (ListIterator li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) { 280 String schema = (String ) li.previous(); 281 result = getStringOrNull(schema, ourLocale, stringCode, args); 282 if (result != null) { 283 break; 284 } 285 } 286 287 if (result == null) { 289 result = getStringRequired(defaultSchema, ourLocale, stringCode, args); 291 } 292 293 return result; 294 } 295 296 306 public static String getStringUnrequired(Stack schemaStack, Locale ourLocale, String stringCode, Object [] args) throws IllegalArgumentException { 307 String result = null; 308 309 for (ListIterator li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) { 310 String schema = (String ) li.previous(); 311 result = getStringOrNull(schema, ourLocale, stringCode, args); 312 if (result != null) { 313 break; 314 } 315 } 316 317 if (result == null) { 319 result = getStringOrNull(defaultSchema, ourLocale, stringCode, args); 321 } 322 323 if (result == null) { 324 result = stringCode; 325 } 326 327 return result; 328 } 329 330 341 public static String getStringByPath(String bundlePath, String language, 342 String country, String stringCode, 343 Object [] args) { 344 MessageBundle mb = getBundleByPath(bundlePath, language, country); 345 346 return mb.getString(stringCode, args); 347 } 348 349 354 public static void setDefaultSchema(String newDefault) { 355 defaultSchema = newDefault; 356 } 357 358 365 public static String getString(String stringCode) { 366 Object [] args = {}; 367 368 return getString(defaultSchema, stringCode, args); 369 } 370 371 380 public static String getString(String stringCode, Object [] args) { 381 return getString(defaultSchema, stringCode, args); 382 } 383 384 391 public static String getString(String schemaClass, String stringCode) { 392 return getString(schemaClass, stringCode, null); 393 } 394 395 405 public static String getString(Stack schemaStack, Locale ourLocale, String stringCode, Object [] args) { 406 String result = null; 407 for (ListIterator li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) { 408 String schema = (String ) li.previous(); 409 result = getStringOrNull(schema, ourLocale, stringCode, args); 410 if (result != null) { 411 break; 412 } 413 } 414 415 if (result == null) { 416 result = stringCode; 417 if (log.isEnabledFor(Priority.WARN)) { 419 String msg = "No such key '" + 420 stringCode + "' in bundle for schemas: '" + schemaStack.toString() + "'"; 421 if (log.isDebugEnabled()) { 422 msg += " with language '" + ourLocale.getLanguage() + "', " + "Country '" + 423 ourLocale.getCountry() + "'"; 424 } 425 log.warn(msg); 426 } 427 } 428 429 return result; 430 } 431 432 435 private static String getStringOrNull(String schema, Locale ourLocale, String stringCode, Object [] args) { 436 if (ourLocale == null) { 437 ourLocale = Locale.getDefault(); 438 } 439 440 MessageBundle mb = getBundleBySchema(schema, ourLocale); 441 return mb.getStringOrNull(stringCode, args); 442 } 443 444 453 public static String getString(String schemaClass, HttpServletRequest req, 454 String stringCode, Object [] args) 455 throws ServletException { 456 Locale l = getLocale(req); 457 458 return getString(schemaClass, l, stringCode, args); 459 } 460 461 462 public static String getString(Locale l, String stringCode) { 463 Object [] args = {}; 464 465 return getString(defaultSchema, l, stringCode, args); 466 } 467 468 public static String getString(Locale l, String stringCode, Object [] args) { 469 return getString(defaultSchema, l, stringCode, args); 470 } 471 472 477 public static String getString(String schemaClass, Locale l, String stringCode) { 478 Object [] args = {}; 479 return getString(schemaClass, l, stringCode, args); 480 } 481 482 492 public static void establishLocale(HttpServletRequest req) 493 throws ServletException { 494 if (req == null) { 495 throw new IllegalArgumentException ("Request may not be null here"); 496 } 497 if (GenericSession.getAttribute(req, LOCALE_KEY) != null) { 498 return; 499 } 500 501 Locale newLocale = null; 502 503 504 User myUser = getLoggedInUser(req); 505 String db = GenericSession.getAttributeString(req, "db"); 506 507 508 if (myUser != null) { 509 newLocale = setLocaleFromPreferences(myUser, db); 510 } 511 512 if (newLocale == null) { 513 newLocale = req.getLocale(); 514 } 515 516 GenericSession.setAttribute(req, LOCALE_KEY, newLocale); 517 } 518 519 520 524 private static User getLoggedInUser(HttpServletRequest req) 525 throws ServletException { 526 String db = GenericSession.getAttributeString(req, "db"); 527 528 if (db.equals("")) { 529 db = "default"; 530 } 531 532 CurrentLogin login = (CurrentLogin) GenericSession.getAttribute(req, "CurrentLogin"); 534 String userName = ""; 535 if (login != null) { 536 userName = login.getUserName(); 537 } 538 539 if (userName.equals("")) { 540 userName = User.UNKNOWN_USER; 541 } 542 if (userName.equals(User.UNKNOWN_USER)) { 543 return null; 544 } 545 try { 546 User myUser = new User(); 547 myUser.setDataContext(db); 548 549 myUser.setLoginName(userName); 552 myUser.find(); 553 554 return myUser; 556 } catch (DBException de) { 557 return null; 558 } 559 } 560 561 562 567 private static Locale setLocaleFromPreferences(User myUser, String dbcontext) 568 throws ServletException { 569 String language = (""); 570 String country = (""); 571 572 try { 573 574 575 UserPreference up = new UserPreference(); 576 up.setDataContext(dbcontext); 577 578 up.setField("ExpUid", myUser.getUid()); 581 up.setField("ClassName", 582 "com.jcorporate.expresso.core.servlet.CheckLogin"); 583 up.setField("PrefCode", "language"); 584 585 if (up.find()) { 586 language = up.getField("PrefValue"); 587 588 if (log.isDebugEnabled()) { 589 log.debug("Language preference for user '" + 590 myUser.getDisplayName() + "' was '" + language + 591 "'"); 592 } 593 if (language.equals("Browser")) { 594 return null; 595 } 596 597 up.clear(); 598 599 up.setField("ExpUid", myUser.getUid()); 602 up.setField("ClassName", 603 "com.jcorporate.expresso.core.servlet.CheckLogin"); 604 up.setField("PrefCode", "country"); 605 606 if (up.find()) { 607 country = up.getField("PrefValue"); 608 609 if (log.isDebugEnabled()) { 610 log.debug("Country preference for user '" + 611 myUser.getDisplayName() + "' was '" + country + 612 "'"); 613 } 614 } else { 615 if (log.isDebugEnabled()) { 616 log.debug("No country preference found for '" + 617 myUser.getDisplayName() + "'"); 618 } 619 } 620 621 return new Locale (language, country); 622 } 623 624 UserPreferenceDef upd = new UserPreferenceDef(); 625 upd.setDataContext(dbcontext); 626 upd.setField("ClassName", 627 "com.jcorporate.expresso.core.servlet.CheckLogin"); 628 upd.setField("PrefCode", "language"); 629 630 if (!upd.find()) { 631 return null; 632 } 633 if (upd.getField("DefaultVal").equals("Browser")) { 634 return null; 635 } else { 636 language = upd.getField("DefaultVal"); 637 upd.clear(); 638 upd.setField("ClassName", 639 "com.jcorporate.expresso.core.servlet.CheckLogin"); 640 upd.setField("PrefCode", "language"); 641 642 if (upd.find()) { 643 country = upd.getField("DefaultVal"); 644 } 645 } 646 } catch (DBException de) { 647 log.error(de); 648 throw new ServletException ("Unable to retrive language preferences"); 649 } 650 651 return new Locale (language, country); 652 } 653 654 655 public static Locale getLocale(HttpServletRequest req) 656 throws ServletException { 657 if (req == null) { 658 throw new IllegalArgumentException ("Request may not be null here"); 659 } 660 661 establishLocale(req); 662 663 Object o = GenericSession.getAttribute(req, LOCALE_KEY); 664 665 if (o != null) { 666 return (Locale ) o; 667 } else { 668 return Locale.getDefault(); 669 } 670 } 671 672 673 683 public static String getString(String schemaClass, String stringCode, 684 Object [] args) { 685 Locale theLocale = getDefaultLocale(); 686 687 return getString(schemaClass, theLocale, 688 stringCode, args); 689 } 690 691 697 public static Locale getDefaultLocale() { 698 String language = "en"; 699 String country = "US"; 700 701 try { 702 ConfigContext myContext = ConfigManager.getContext("default"); 703 language = StringUtil.notNull(myContext.getLanguage()); 704 country = StringUtil.notNull(myContext.getCountry()); 705 } catch (ConfigurationException ce) { 706 log.error(ce); 707 } 708 709 Locale theLocale = new Locale (language, country); 710 return theLocale; 711 } 712 713 725 public static String getStringForUser(int uid, String dbName, 726 String schemaClass, 727 String stringCode, Object [] args) { 728 if (log.isDebugEnabled()) { 729 log.debug("Getting string for user '" + uid + "', db '" + dbName + 730 "', key '" + stringCode + "'"); 731 } 732 733 StringUtil.assertNotBlank(schemaClass, 734 "Schema class name may not be blank here"); 735 StringUtil.assertNotBlank(stringCode, 736 "String code may not be blank here"); 737 StringUtil.assertNotBlank(dbName, 738 "DB name/context must not be blank here"); 739 740 String language = ""; 741 String country = ""; 742 743 744 try { 745 UserPreference up = new UserPreference(); 746 up.setDataContext(dbName); 747 up.setField("ExpUid", uid); 748 up.setField("PrefCode", "language"); 749 750 if (up.find()) { 751 language = up.getField("PrefValue"); 752 up.setField("ExpUid", uid); 753 up.setField("PrefCode", "country"); 754 755 if (up.find()) { 756 country = up.getField("PrefValue"); 757 } 758 } else { 759 760 761 762 763 try { 764 ConfigContext myContext = ConfigManager.getContext(dbName); 765 language = StringUtil.notNull(myContext.getLanguage()); 766 country = StringUtil.notNull(myContext.getCountry()); 767 } catch (ConfigurationException ce) { 768 log.error(ce); 769 } 770 } 771 } catch (DBException de) { 772 log.error(de); 773 language = ""; 774 } 775 776 if (language.equals("")) { 777 language = "en"; 778 country = "US"; 779 } 780 781 return getString(schemaClass, new Locale (language, country), 782 stringCode, args); 783 } 784 785 790 public static String getString(HttpServletRequest req, String stringCode) 791 throws ServletException { 792 Object [] args = {}; 793 794 return getString(defaultSchema, req, stringCode, args); 795 } 796 797 798 806 public static String getString(HttpServletRequest req, String stringCode, 807 String firstReplace) 808 throws ServletException { 809 Object [] args = {firstReplace}; 810 811 return getString(defaultSchema, req, stringCode, args); 812 } 813 814 815 824 public String getString(HttpServletRequest req, String stringCode, 825 String firstReplace, String secondReplace) 826 throws ServletException { 827 Object [] args = {firstReplace, secondReplace}; 828 829 return getString(defaultSchema, req, stringCode, args); 830 } 831 832 833 844 public String getString(HttpServletRequest req, String stringCode, 845 String firstReplace, String secondReplace, 846 String thirdReplace) 847 throws ServletException { 848 Object [] args = {firstReplace, secondReplace, thirdReplace}; 849 850 return getString(defaultSchema, req, stringCode, args); 851 } 852 853 854 } 855 | Popular Tags |