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                            String url = null;
184    
185                            if (!LocaleUtil.equals(locale, currentLocale)) {
186                                    url = HttpUtil.setParameter(
187                                            formAction, parameterName, LocaleUtil.toLanguageId(locale));
188                            }
189                            else if (!displayCurrentLocale) {
190                                    continue;
191                            }
192    
193                            languageEntries.add(
194                                    new LanguageEntry(
195                                            duplicateLanguages, currentLocale, locale, url));
196                    }
197    
198                    return languageEntries;
199            }
200    
201            protected Collection<Locale> getLocales() {
202                    if (ArrayUtil.isNotEmpty(_languageIds)) {
203                            return Arrays.asList(LocaleUtil.fromLanguageIds(_languageIds));
204                    }
205    
206                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
207                            WebKeys.THEME_DISPLAY);
208    
209                    return LanguageUtil.getAvailableLocales(themeDisplay.getSiteGroupId());
210            }
211    
212            protected String getNamespacedName() {
213                    String name = _name;
214    
215                    if (!_useNamespace) {
216                            return name;
217                    }
218    
219                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
220                            JavaConstants.JAVAX_PORTLET_REQUEST);
221                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
222                            JavaConstants.JAVAX_PORTLET_RESPONSE);
223    
224                    String namespace = AUIUtil.getNamespace(
225                            portletRequest, portletResponse);
226    
227                    if (Validator.isNotNull(namespace)) {
228                            name = namespace.concat(name);
229                    }
230    
231                    return name;
232            }
233    
234            @Override
235            protected String getPage() {
236                    return _PAGE;
237            }
238    
239            @Override
240            protected void setAttributes(HttpServletRequest request) {
241                    request.setAttribute(
242                            "liferay-ui:language:displayCurrentLocale",
243                            String.valueOf(_displayCurrentLocale));
244                    request.setAttribute(
245                            "liferay-ui:language:displayStyle", getDisplayStyle());
246                    request.setAttribute(
247                            "liferay-ui:language:displayStyleGroupId",
248                            getDisplayStyleGroupId());
249                    request.setAttribute("liferay-ui:language:formAction", getFormAction());
250                    request.setAttribute("liferay-ui:language:formName", _formName);
251                    request.setAttribute(
252                            "liferay-ui:language:languageEntries",
253                            getLanguageEntries(
254                                    getLocales(), _displayCurrentLocale, getFormAction(),
255                                    getNamespacedName()));
256                    request.setAttribute("liferay-ui:language:languageId", _languageId);
257                    request.setAttribute("liferay-ui:language:name", _name);
258            }
259    
260            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
261    
262            private long _ddmTemplateGroupId;
263            private String _ddmTemplateKey;
264            private boolean _displayCurrentLocale = true;
265            private String _formAction;
266            private String _formName = "fm";
267            private String _languageId;
268            private String[] _languageIds;
269            private String _name = "languageId";
270            private boolean _useNamespace = true;
271    
272    }