001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.mdarad.framework.util.struts;
025:
026: import org.apache.struts.Globals;
027: import org.dataisland.primitives.bean.LocalizationContext;
028: import org.dataisland.primitives.util.Keys;
029: import org.dataisland.primitives.util.LocalizationUtils;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import java.util.ArrayList;
033: import java.util.Collections;
034: import java.util.Enumeration;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Locale;
038: import java.util.SortedMap;
039: import java.util.TreeMap;
040:
041: public class StrutsLocalizationUtils {
042:
043: protected static LocalizationContext createLocalizationContext(
044: HttpServletRequest httpServletRequest) {
045:
046: LocalizationContext localizationContext = new LocalizationContext();
047:
048: List localeList = new ArrayList();
049:
050: Locale locale = (Locale) httpServletRequest.getSession()
051: .getAttribute(Globals.LOCALE_KEY);
052: localeList.add(locale);
053:
054: Enumeration localeEnumeration = httpServletRequest.getLocales();
055: while (localeEnumeration.hasMoreElements()) {
056: Locale requestLocale = (Locale) localeEnumeration
057: .nextElement();
058: if (!localeList.contains(requestLocale)) {
059: localeList.add(requestLocale);
060: }
061: }
062:
063: localizationContext.setLocales(localeList);
064:
065: return localizationContext;
066: }
067:
068: public static void cacheLocalizationContext(
069: HttpServletRequest httpServletRequest,
070: LocalizationContext localizationContext) {
071: httpServletRequest
072: .getSession()
073: .setAttribute(
074: org.dataisland.primitives.util.Keys.LOCALIZATIONCONTEXT_KEY,
075: localizationContext);
076: }
077:
078: /**
079: * @param httpServletRequest
080: * @return
081: */
082: public static LocalizationContext getLocalizationContext(
083: HttpServletRequest httpServletRequest) {
084: LocalizationContext localizationContext = (LocalizationContext) httpServletRequest
085: .getSession()
086: .getAttribute(Keys.LOCALIZATIONCONTEXT_KEY);
087: if (localizationContext == null) {
088: localizationContext = createLocalizationContext(httpServletRequest);
089: cacheLocalizationContext(httpServletRequest,
090: localizationContext);
091: }
092:
093: return localizationContext;
094: }
095:
096: public static LocalizationContext getCompositeLocalizationContext(
097: HttpServletRequest httpServletRequest,
098: String localizationContextHumanReadableKey)
099: throws org.dataisland.primitives.exception.LocalizationException {
100: LocalizationContext output = (LocalizationContext) httpServletRequest
101: .getAttribute(org.dataisland.primitives.util.Keys.LOCALIZATIONCONTEXT_KEY);
102: if (output == null) {
103: org.dataisland.primitives.bean.LocalizationContext clientLocalizationContext = getLocalizationContext(httpServletRequest);
104: output = org.mdarad.framework.facade.LocalizationFacade
105: .getInstance().buildLocalizationContext(
106: clientLocalizationContext,
107: localizationContextHumanReadableKey);
108: httpServletRequest
109: .setAttribute(
110: org.dataisland.primitives.util.Keys.LOCALIZATIONCONTEXT_KEY,
111: output);
112: }
113: return output;
114: }
115:
116: static public void processLocale(
117: HttpServletRequest httpServletRequest,
118: String localizationContextHumanReadableKey)
119: throws org.dataisland.primitives.exception.LocalizationException {
120: LocalizationContext clientLocalizationContext = getLocalizationContext(httpServletRequest);
121: LocalizationContext compositeLocalizationContext = org.mdarad.framework.facade.LocalizationFacade
122: .getInstance().buildLocalizationContext(
123: clientLocalizationContext,
124: localizationContextHumanReadableKey);
125: List locales = compositeLocalizationContext.getLocales();
126:
127: Locale locale = (Locale) locales.get(0);
128: httpServletRequest.getSession().setAttribute(
129: Globals.LOCALE_KEY, locale);
130:
131: // Put available locales in request
132: List sortedLocales = new ArrayList(locales);
133: Collections.sort(sortedLocales, LocalizationUtils
134: .getDefaultLocaleComparator());
135:
136: httpServletRequest
137: .setAttribute(Keys.LOCALES_KEY, sortedLocales);
138: }
139:
140: public static SortedMap getEmptyStringKeyedMap(List locales) {
141: SortedMap lOutputMap = new TreeMap(
142: org.dataisland.primitives.util.LocalizationUtils
143: .getDefaultObjectComparator());
144: Iterator localeIterator = locales.iterator();
145:
146: while (localeIterator.hasNext()) {
147: Locale localeKey = (Locale) localeIterator.next();
148: lOutputMap.put(localeKey.toString(), "");
149: }
150:
151: return lOutputMap;
152: }
153: }
|