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.portal.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Contact;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.Portal;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portlet.admin.util.AdminUtil;
033    
034    import java.util.Locale;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    import javax.servlet.http.HttpSession;
039    
040    import org.apache.struts.Globals;
041    import org.apache.struts.action.Action;
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class UpdateLanguageAction extends Action {
050    
051            @Override
052            public ActionForward execute(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    HttpSession session = request.getSession();
058    
059                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
060                            WebKeys.THEME_DISPLAY);
061    
062                    String languageId = ParamUtil.getString(request, "languageId");
063    
064                    Locale locale = LocaleUtil.fromLanguageId(languageId);
065    
066                    if (LanguageUtil.isAvailableLocale(
067                                    themeDisplay.getSiteGroupId(), locale)) {
068    
069                            boolean persistState = ParamUtil.getBoolean(
070                                    request, "persistState", true);
071    
072                            if (themeDisplay.isSignedIn() && (persistState)) {
073                                    User user = themeDisplay.getUser();
074    
075                                    Contact contact = user.getContact();
076    
077                                    AdminUtil.updateUser(
078                                            request, user.getUserId(), user.getScreenName(),
079                                            user.getEmailAddress(), user.getFacebookId(),
080                                            user.getOpenId(), languageId, user.getTimeZoneId(),
081                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
082                                            contact.getFacebookSn(), contact.getJabberSn(),
083                                            contact.getSkypeSn(), contact.getTwitterSn());
084                            }
085    
086                            session.setAttribute(Globals.LOCALE_KEY, locale);
087    
088                            LanguageUtil.updateCookie(request, response, locale);
089                    }
090    
091                    // Send redirect
092    
093                    String redirect = ParamUtil.getString(request, "redirect");
094    
095                    String layoutURL = StringPool.BLANK;
096    
097                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
098    
099                    if (pos == -1) {
100                            pos = redirect.indexOf(StringPool.QUESTION);
101                    }
102    
103                    if (pos != -1) {
104                            layoutURL = redirect.substring(0, pos);
105                    }
106                    else {
107                            layoutURL = redirect;
108                    }
109    
110                    Layout layout = themeDisplay.getLayout();
111    
112                    if (isGroupFriendlyURL(layout.getGroup(), layout, layoutURL, locale)) {
113                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
114                                    redirect = layoutURL;
115                            }
116                            else {
117                                    redirect = PortalUtil.getGroupFriendlyURL(
118                                            layout.getLayoutSet(), themeDisplay, locale);
119                            }
120                    }
121                    else {
122                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
123                                    if (themeDisplay.isI18n()) {
124                                            redirect = layout.getFriendlyURL(locale);
125                                    }
126                                    else {
127                                            redirect = PortalUtil.getLayoutURL(
128                                                    layout, themeDisplay, locale);
129                                    }
130                            }
131                            else {
132                                    redirect = PortalUtil.getLayoutFriendlyURL(
133                                            layout, themeDisplay, locale);
134                            }
135                    }
136    
137                    response.sendRedirect(redirect);
138    
139                    return null;
140            }
141    
142            protected boolean isGroupFriendlyURL(
143                    Group group, Layout layout, String layoutURL, Locale locale) {
144    
145                    if (Validator.isNull(layoutURL)) {
146                            return true;
147                    }
148    
149                    int pos = layoutURL.lastIndexOf(CharPool.SLASH);
150    
151                    String layoutURLLanguageId = layoutURL.substring(pos + 1);
152    
153                    Locale layoutURLLocale = LocaleUtil.fromLanguageId(
154                            layoutURLLanguageId, true, false);
155    
156                    if (layoutURLLocale != null) {
157                            return true;
158                    }
159    
160                    if (PortalUtil.isGroupFriendlyURL(
161                                    layoutURL, group.getFriendlyURL(),
162                                    layout.getFriendlyURL(locale))) {
163    
164                            return true;
165                    }
166    
167                    return false;
168            }
169    
170    }