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.model.Contact;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.Portal;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
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.getAimSn(), contact.getFacebookSn(),
083                                            contact.getIcqSn(), contact.getJabberSn(),
084                                            contact.getMySpaceSn(), contact.getSkypeSn(),
085                                            contact.getTwitterSn(), contact.getYmSn());
086                            }
087    
088                            session.setAttribute(Globals.LOCALE_KEY, locale);
089    
090                            LanguageUtil.updateCookie(request, response, locale);
091                    }
092    
093                    // Send redirect
094    
095                    String redirect = ParamUtil.getString(request, "redirect");
096    
097                    String layoutURL = StringPool.BLANK;
098    
099                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
100    
101                    if (pos == -1) {
102                            pos = redirect.indexOf(StringPool.QUESTION);
103                    }
104    
105                    if (pos != -1) {
106                            layoutURL = redirect.substring(0, pos);
107                    }
108                    else {
109                            layoutURL = redirect;
110                    }
111    
112                    Layout layout = themeDisplay.getLayout();
113    
114                    if (isGroupFriendlyURL(layout.getGroup(), layout, layoutURL, locale)) {
115                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
116                                    redirect = layoutURL;
117                            }
118                            else {
119                                    redirect = PortalUtil.getGroupFriendlyURL(
120                                            layout.getLayoutSet(), themeDisplay, locale);
121                            }
122                    }
123                    else {
124                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
125                                    if (themeDisplay.isI18n()) {
126                                            redirect = layout.getFriendlyURL(locale);
127                                    }
128                                    else {
129                                            redirect = PortalUtil.getLayoutURL(
130                                                    layout, themeDisplay, locale);
131                                    }
132                            }
133                            else {
134                                    redirect = PortalUtil.getLayoutFriendlyURL(
135                                            layout, themeDisplay, locale);
136                            }
137                    }
138    
139                    response.sendRedirect(redirect);
140    
141                    return null;
142            }
143    
144            protected boolean isGroupFriendlyURL(
145                    Group group, Layout layout, String layoutURL, Locale locale) {
146    
147                    if (Validator.isNull(layoutURL)) {
148                            return true;
149                    }
150    
151                    int pos = layoutURL.lastIndexOf(CharPool.SLASH);
152    
153                    String layoutURLLanguageId = layoutURL.substring(pos + 1);
154    
155                    Locale layoutURLLocale = LocaleUtil.fromLanguageId(
156                            layoutURLLanguageId, true, false);
157    
158                    if (layoutURLLocale != null) {
159                            return true;
160                    }
161    
162                    if (PortalUtil.isGroupFriendlyURL(
163                                    layoutURL, group.getFriendlyURL(),
164                                    layout.getFriendlyURL(locale))) {
165    
166                            return true;
167                    }
168    
169                    return false;
170            }
171    
172    }