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.servlet.taglib.ui.LanguageEntry;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplateUtil;
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 PortletDisplayTemplateUtil.getDisplayStyle(_ddmTemplateKey);
103                    }
104    
105                    return null;
106            }
107    
108            protected String getFormAction() {
109                    String formAction = _formAction;
110    
111                    if (Validator.isNotNull(formAction)) {
112                            return formAction;
113                    }
114    
115                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
116                            WebKeys.THEME_DISPLAY);
117    
118                    formAction =
119                            themeDisplay.getPathMain() +
120                                    "/portal/update_language?p_l_id=" + themeDisplay.getPlid();
121                    formAction = HttpUtil.setParameter(
122                            formAction, "redirect", PortalUtil.getCurrentURL(request));
123    
124                    return formAction;
125            }
126    
127            protected List<LanguageEntry> getLanguageEntries(
128                    Collection<Locale> locales, boolean displayCurrentLocale,
129                    String formAction, String parameterName) {
130    
131                    List<LanguageEntry> languageEntries = new ArrayList<>();
132    
133                    Map<String, Integer> counts = new HashMap<>();
134    
135                    for (Locale locale : locales) {
136                            Integer count = counts.get(locale.getLanguage());
137    
138                            if (count == null) {
139                                    count = new Integer(1);
140                            }
141                            else {
142                                    count = new Integer(count.intValue() + 1);
143                            }
144    
145                            counts.put(locale.getLanguage(), count);
146                    }
147    
148                    Set<String> duplicateLanguages = new HashSet<>();
149    
150                    for (Locale locale : locales) {
151                            Integer count = counts.get(locale.getLanguage());
152    
153                            if (count.intValue() != 1) {
154                                    duplicateLanguages.add(locale.getLanguage());
155                            }
156                    }
157    
158                    Locale currentLocale = null;
159    
160                    if (Validator.isNotNull(_languageId)) {
161                            currentLocale = LocaleUtil.fromLanguageId(_languageId);
162                    }
163                    else {
164                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
165                                    WebKeys.THEME_DISPLAY);
166    
167                            currentLocale = themeDisplay.getLocale();
168                    }
169    
170                    for (Locale locale : locales) {
171                            String url = null;
172    
173                            if (!LocaleUtil.equals(locale, currentLocale)) {
174                                    url = HttpUtil.setParameter(
175                                            formAction, parameterName, LocaleUtil.toLanguageId(locale));
176                            }
177                            else if (!displayCurrentLocale) {
178                                    continue;
179                            }
180    
181                            languageEntries.add(
182                                    new LanguageEntry(
183                                            duplicateLanguages, currentLocale, locale, url));
184                    }
185    
186                    return languageEntries;
187            }
188    
189            protected Collection<Locale> getLocales() {
190                    if (ArrayUtil.isNotEmpty(_languageIds)) {
191                            return Arrays.asList(LocaleUtil.fromLanguageIds(_languageIds));
192                    }
193    
194                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
195                            WebKeys.THEME_DISPLAY);
196    
197                    return LanguageUtil.getAvailableLocales(themeDisplay.getSiteGroupId());
198            }
199    
200            protected String getNamespacedName() {
201                    String name = _name;
202    
203                    if (!_useNamespace) {
204                            return name;
205                    }
206    
207                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
208                            JavaConstants.JAVAX_PORTLET_REQUEST);
209                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
210                            JavaConstants.JAVAX_PORTLET_RESPONSE);
211    
212                    String namespace = AUIUtil.getNamespace(
213                            portletRequest, portletResponse);
214    
215                    if (Validator.isNotNull(namespace)) {
216                            name = namespace.concat(name);
217                    }
218    
219                    return name;
220            }
221    
222            @Override
223            protected String getPage() {
224                    return _PAGE;
225            }
226    
227            @Override
228            protected void setAttributes(HttpServletRequest request) {
229                    request.setAttribute(
230                            "liferay-ui:language:displayCurrentLocale",
231                            String.valueOf(_displayCurrentLocale));
232                    request.setAttribute(
233                            "liferay-ui:language:displayStyle", getDisplayStyle());
234                    request.setAttribute(
235                            "liferay-ui:language:displayStyleGroupId", _ddmTemplateGroupId);
236                    request.setAttribute("liferay-ui:language:formAction", getFormAction());
237                    request.setAttribute("liferay-ui:language:formName", _formName);
238                    request.setAttribute(
239                            "liferay-ui:language:languageEntries",
240                            getLanguageEntries(
241                                    getLocales(), _displayCurrentLocale, getFormAction(),
242                                    getNamespacedName()));
243                    request.setAttribute("liferay-ui:language:languageId", _languageId);
244                    request.setAttribute("liferay-ui:language:name", _name);
245            }
246    
247            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
248    
249            private long _ddmTemplateGroupId;
250            private String _ddmTemplateKey;
251            private boolean _displayCurrentLocale = true;
252            private String _formAction;
253            private String _formName = "fm";
254            private String _languageId;
255            private String[] _languageIds;
256            private String _name = "languageId";
257            private boolean _useNamespace = true;
258    
259    }