001    /**
002     * Copyright (c) 2000-present 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.taglib.ui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateManagerUtil;
019    import com.liferay.portal.kernel.servlet.taglib.ui.LanguageEntry;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.JavaConstants;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.taglib.aui.AUIUtil;
029    import com.liferay.taglib.util.IncludeTag;
030    
031    import java.util.ArrayList;
032    import java.util.Arrays;
033    import java.util.Collection;
034    import java.util.HashMap;
035    import java.util.HashSet;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    import java.util.Set;
040    
041    import javax.portlet.PortletRequest;
042    import javax.portlet.PortletResponse;
043    
044    import javax.servlet.http.HttpServletRequest;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class LanguageTag extends IncludeTag {
050    
051            public void setDdmTemplateGroupId(long ddmTemplateGroupId) {
052                    _ddmTemplateGroupId = ddmTemplateGroupId;
053            }
054    
055            public void setDdmTemplateKey(String ddmTemplateKey) {
056                    _ddmTemplateKey = ddmTemplateKey;
057            }
058    
059            public void setDisplayCurrentLocale(boolean displayCurrentLocale) {
060                    _displayCurrentLocale = displayCurrentLocale;
061            }
062    
063            public void setFormAction(String formAction) {
064                    _formAction = formAction;
065            }
066    
067            public void setFormName(String formName) {
068                    _formName = formName;
069            }
070    
071            public void setLanguageId(String languageId) {
072                    _languageId = languageId;
073            }
074    
075            public void setLanguageIds(String[] languageIds) {
076                    _languageIds = languageIds;
077            }
078    
079            public void setName(String name) {
080                    _name = name;
081            }
082    
083            public void setUseNamespace(boolean useNamespace) {
084                    _useNamespace = useNamespace;
085            }
086    
087            @Override
088            protected void cleanUp() {
089                    _ddmTemplateGroupId = 0;
090                    _ddmTemplateKey = null;
091                    _displayCurrentLocale = true;
092                    _formAction = null;
093                    _formName = "fm";
094                    _languageId = null;
095                    _languageIds = null;
096                    _name = "languageId";
097                    _useNamespace = true;
098            }
099    
100            protected String getDisplayStyle() {
101                    if (Validator.isNotNull(_ddmTemplateKey)) {
102                            return PortletDisplayTemplateManagerUtil.getDisplayStyle(
103                                    _ddmTemplateKey);
104                    }
105    
106                    return null;
107            }
108    
109            protected long getDisplayStyleGroupId() {
110                    if (_ddmTemplateGroupId > 0) {
111                            return _ddmTemplateGroupId;
112                    }
113    
114                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
115                            WebKeys.THEME_DISPLAY);
116    
117                    return themeDisplay.getScopeGroupId();
118            }
119    
120            protected String getFormAction() {
121                    String formAction = _formAction;
122    
123                    if (Validator.isNotNull(formAction)) {
124                            return formAction;
125                    }
126    
127                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    formAction =
131                            themeDisplay.getPathMain() +
132                                    "/portal/update_language?p_l_id=" + themeDisplay.getPlid();
133                    formAction = HttpUtil.setParameter(
134                            formAction, "redirect", PortalUtil.getCurrentURL(request));
135    
136                    return formAction;
137            }
138    
139            protected List<LanguageEntry> getLanguageEntries(
140                    Collection<Locale> locales, boolean displayCurrentLocale,
141                    String formAction, String parameterName) {
142    
143                    List<LanguageEntry> languageEntries = new ArrayList<>();
144    
145                    Map<String, Integer> counts = new HashMap<>();
146    
147                    for (Locale locale : locales) {
148                            Integer count = counts.get(locale.getLanguage());
149    
150                            if (count == null) {
151                                    count = Integer.valueOf(1);
152                            }
153                            else {
154                                    count = Integer.valueOf(count.intValue() + 1);
155                            }
156    
157                            counts.put(locale.getLanguage(), count);
158                    }
159    
160                    Set<String> duplicateLanguages = new HashSet<>();
161    
162                    for (Locale locale : locales) {
163                            Integer count = counts.get(locale.getLanguage());
164    
165                            if (count.intValue() != 1) {
166                                    duplicateLanguages.add(locale.getLanguage());
167                            }
168                    }
169    
170                    Locale currentLocale = null;
171    
172                    if (Validator.isNotNull(_languageId)) {
173                            currentLocale = LocaleUtil.fromLanguageId(_languageId);
174                    }
175                    else {
176                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
177                                    WebKeys.THEME_DISPLAY);
178    
179                            currentLocale = themeDisplay.getLocale();
180                    }
181    
182                    for (Locale locale : locales) {
183                            boolean disabled = false;
184                            String url = null;
185    
186                            if (!LocaleUtil.equals(locale, currentLocale)) {
187                                    url = HttpUtil.setParameter(
188                                            formAction, parameterName, LocaleUtil.toLanguageId(locale));
189                            }
190                            else if (!displayCurrentLocale) {
191                                    disabled = true;
192                            }
193    
194                            LanguageEntry languageEntry = new LanguageEntry(
195                                    duplicateLanguages, currentLocale, locale, url, disabled);
196    
197                            languageEntries.add(languageEntry);
198                    }
199    
200                    return languageEntries;
201            }
202    
203            protected Collection<Locale> getLocales() {
204                    if (ArrayUtil.isNotEmpty(_languageIds)) {
205                            return Arrays.asList(LocaleUtil.fromLanguageIds(_languageIds));
206                    }
207    
208                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
209                            WebKeys.THEME_DISPLAY);
210    
211                    return LanguageUtil.getAvailableLocales(themeDisplay.getSiteGroupId());
212            }
213    
214            protected String getNamespacedName() {
215                    String name = _name;
216    
217                    if (!_useNamespace) {
218                            return name;
219                    }
220    
221                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
222                            JavaConstants.JAVAX_PORTLET_REQUEST);
223                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
224                            JavaConstants.JAVAX_PORTLET_RESPONSE);
225    
226                    String namespace = AUIUtil.getNamespace(
227                            portletRequest, portletResponse);
228    
229                    if (Validator.isNotNull(namespace)) {
230                            name = namespace.concat(name);
231                    }
232    
233                    return name;
234            }
235    
236            @Override
237            protected String getPage() {
238                    return _PAGE;
239            }
240    
241            @Override
242            protected void setAttributes(HttpServletRequest request) {
243                    request.setAttribute(
244                            "liferay-ui:language:displayCurrentLocale",
245                            String.valueOf(_displayCurrentLocale));
246                    request.setAttribute(
247                            "liferay-ui:language:displayStyle", getDisplayStyle());
248                    request.setAttribute(
249                            "liferay-ui:language:displayStyleGroupId",
250                            getDisplayStyleGroupId());
251                    request.setAttribute("liferay-ui:language:formAction", getFormAction());
252                    request.setAttribute("liferay-ui:language:formName", _formName);
253                    request.setAttribute(
254                            "liferay-ui:language:languageEntries",
255                            getLanguageEntries(
256                                    getLocales(), _displayCurrentLocale, getFormAction(),
257                                    getNamespacedName()));
258                    request.setAttribute("liferay-ui:language:languageId", _languageId);
259                    request.setAttribute("liferay-ui:language:name", _name);
260            }
261    
262            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
263    
264            private long _ddmTemplateGroupId;
265            private String _ddmTemplateKey;
266            private boolean _displayCurrentLocale = true;
267            private String _formAction;
268            private String _formName = "fm";
269            private String _languageId;
270            private String[] _languageIds;
271            private String _name = "languageId";
272            private boolean _useNamespace = true;
273    
274    }