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