001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.Locale;
025    import java.util.Map;
026    
027    import javax.portlet.PortletPreferences;
028    import javax.portlet.PortletRequest;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Alexander Chow
034     * @author Jorge Ferrer
035     * @author Mauro Mariuzzo
036     * @author Julio Camarero
037     * @author Brian Wing Shun Chan
038     * @see    Localization
039     */
040    public class LocalizationUtil {
041    
042            public static Object deserialize(JSONObject jsonObject) {
043                    return getLocalization().deserialize(jsonObject);
044            }
045    
046            public static String[] getAvailableLocales(String xml) {
047                    return getLocalization().getAvailableLocales(xml);
048            }
049    
050            public static Locale getDefaultImportLocale(
051                    String className, long classPK, Locale contentDefaultLocale,
052                    Locale[] contentAvailableLocales) {
053    
054                    return getLocalization().getDefaultImportLocale(
055                            className, classPK, contentDefaultLocale, contentAvailableLocales);
056            }
057    
058            public static String getDefaultLocale(String xml) {
059                    return getLocalization().getDefaultLocale(xml);
060            }
061    
062            public static Localization getLocalization() {
063                    PortalRuntimePermission.checkGetBeanProperty(LocalizationUtil.class);
064    
065                    return _localization;
066            }
067    
068            public static String getLocalization(
069                    String xml, String requestedLanguageId) {
070    
071                    return getLocalization().getLocalization(xml, requestedLanguageId);
072            }
073    
074            public static String getLocalization(
075                    String xml, String requestedLanguageId, boolean useDefault) {
076    
077                    return getLocalization().getLocalization(
078                            xml, requestedLanguageId, useDefault);
079            }
080    
081            public static Map<Locale, String> getLocalizationMap(
082                    HttpServletRequest request, String parameter) {
083    
084                    return getLocalization().getLocalizationMap(request, parameter);
085            }
086    
087            public static Map<Locale, String> getLocalizationMap(
088                    PortletPreferences preferences, String parameter) {
089    
090                    return getLocalization().getLocalizationMap(preferences, parameter);
091            }
092    
093            public static Map<Locale, String> getLocalizationMap(
094                    PortletRequest portletRequest, String parameter) {
095    
096                    return getLocalization().getLocalizationMap(portletRequest, parameter);
097            }
098    
099            public static Map<Locale, String> getLocalizationMap(String xml) {
100                    return getLocalization().getLocalizationMap(xml);
101            }
102    
103            public static Map<Locale, String> getLocalizationMap(
104                    String xml, boolean useDefault) {
105    
106                    return getLocalization().getLocalizationMap(xml, useDefault);
107            }
108    
109            public static Map<Locale, String> getLocalizationMap(
110                    String bundleName, ClassLoader classLoader, String key,
111                    boolean includeBetaLocales) {
112    
113                    return getLocalization().getLocalizationMap(
114                            bundleName, classLoader, key, includeBetaLocales);
115            }
116    
117            public static Map<Locale, String> getLocalizationMap(
118                    String[] languageIds, String[] values) {
119    
120                    return getLocalization().getLocalizationMap(languageIds, values);
121            }
122    
123            public static String getLocalizationXmlFromPreferences(
124                    PortletPreferences preferences, PortletRequest portletRequest,
125                    String parameter) {
126    
127                    return getLocalization().getLocalizationXmlFromPreferences(
128                            preferences, portletRequest, parameter);
129            }
130    
131            /**
132             * @deprecated As of 6.2.0, replaced by {@link #getLocalizationMap}
133             */
134            public static Map<Locale, String> getLocalizedParameter(
135                    PortletRequest portletRequest, String parameter) {
136    
137                    return getLocalization().getLocalizedParameter(
138                            portletRequest, parameter);
139            }
140    
141            public static List<Locale> getModifiedLocales(
142                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
143    
144                    if ((newMap == null) || newMap.isEmpty()) {
145                            return Collections.emptyList();
146                    }
147    
148                    List<Locale> modifiedLocales = new ArrayList<Locale>();
149    
150                    Locale[] locales = LanguageUtil.getAvailableLocales();
151    
152                    for (Locale locale : locales) {
153                            String oldValue = oldMap.get(locale);
154                            String newValue = newMap.get(locale);
155    
156                            if (!oldValue.equals(newValue)) {
157                                    modifiedLocales.add(locale);
158                            }
159                    }
160    
161                    return modifiedLocales;
162            }
163    
164            public static String getPreferencesKey(String key, String languageId) {
165                    return getLocalization().getPreferencesKey(key, languageId);
166            }
167    
168            public static String getPreferencesValue(
169                    PortletPreferences preferences, String key, String languageId) {
170    
171                    return getLocalization().getPreferencesValue(
172                            preferences, key, languageId);
173            }
174    
175            public static String getPreferencesValue(
176                    PortletPreferences preferences, String key, String languageId,
177                    boolean useDefault) {
178    
179                    return getLocalization().getPreferencesValue(
180                            preferences, key, languageId, useDefault);
181            }
182    
183            public static String[] getPreferencesValues(
184                    PortletPreferences preferences, String key, String languageId) {
185    
186                    return getLocalization().getPreferencesValues(
187                            preferences, key, languageId);
188            }
189    
190            public static String[] getPreferencesValues(
191                    PortletPreferences preferences, String key, String languageId,
192                    boolean useDefault) {
193    
194                    return getLocalization().getPreferencesValues(
195                            preferences, key, languageId, useDefault);
196            }
197    
198            public static String removeLocalization(
199                    String xml, String key, String requestedLanguageId) {
200    
201                    return getLocalization().removeLocalization(
202                            xml, key, requestedLanguageId);
203            }
204    
205            public static String removeLocalization(
206                    String xml, String key, String requestedLanguageId, boolean cdata) {
207    
208                    return getLocalization().removeLocalization(
209                            xml, key, requestedLanguageId, cdata);
210            }
211    
212            public static String removeLocalization(
213                    String xml, String key, String requestedLanguageId, boolean cdata,
214                    boolean localized) {
215    
216                    return getLocalization().removeLocalization(
217                            xml, key, requestedLanguageId, cdata, localized);
218            }
219    
220            public static void setLocalizedPreferencesValues(
221                            PortletRequest portletRequest, PortletPreferences preferences,
222                            String parameter)
223                    throws Exception {
224    
225                    getLocalization().setLocalizedPreferencesValues(
226                            portletRequest, preferences, parameter);
227            }
228    
229            public static void setPreferencesValue(
230                            PortletPreferences preferences, String key, String languageId,
231                            String value)
232                    throws Exception {
233    
234                    getLocalization().setPreferencesValue(
235                            preferences, key, languageId, value);
236            }
237    
238            public static void setPreferencesValues(
239                            PortletPreferences preferences, String key, String languageId,
240                            String[] values)
241                    throws Exception {
242    
243                    getLocalization().setPreferencesValues(
244                            preferences, key, languageId, values);
245            }
246    
247            public static String updateLocalization(
248                    Map<Locale, String> localizationMap, String xml, String key,
249                    String defaultLanguageId) {
250    
251                    return getLocalization().updateLocalization(
252                            localizationMap, xml, key, defaultLanguageId);
253            }
254    
255            public static String updateLocalization(
256                    String xml, String key, String value) {
257    
258                    return getLocalization().updateLocalization(xml, key, value);
259            }
260    
261            public static String updateLocalization(
262                    String xml, String key, String value, String requestedLanguageId) {
263    
264                    return getLocalization().updateLocalization(
265                            xml, key, value, requestedLanguageId);
266            }
267    
268            public static String updateLocalization(
269                    String xml, String key, String value, String requestedLanguageId,
270                    String defaultLanguageId) {
271    
272                    return getLocalization().updateLocalization(
273                            xml, key, value, requestedLanguageId, defaultLanguageId);
274            }
275    
276            public static String updateLocalization(
277                    String xml, String key, String value, String requestedLanguageId,
278                    String defaultLanguageId, boolean cdata) {
279    
280                    return getLocalization().updateLocalization(
281                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
282            }
283    
284            public static String updateLocalization(
285                    String xml, String key, String value, String requestedLanguageId,
286                    String defaultLanguageId, boolean cdata, boolean localized) {
287    
288                    return getLocalization().updateLocalization(
289                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
290                            localized);
291            }
292    
293            public void setLocalization(Localization localization) {
294                    PortalRuntimePermission.checkSetBeanProperty(getClass());
295    
296                    _localization = localization;
297            }
298    
299            private static Localization _localization;
300    
301    }