001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.language.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.ListUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Contact;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.struts.PortletAction;
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.List;
035    import java.util.Locale;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import javax.servlet.http.HttpServletRequest;
044    import javax.servlet.http.HttpServletResponse;
045    import javax.servlet.http.HttpSession;
046    
047    import org.apache.struts.Globals;
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Brian Wing Shun Chan
054     */
055    public class ViewAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
064                            actionRequest);
065                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
066                            actionResponse);
067                    HttpSession session = request.getSession();
068    
069                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
070                            WebKeys.THEME_DISPLAY);
071    
072                    Layout layout = themeDisplay.getLayout();
073    
074                    String languageId = ParamUtil.getString(actionRequest, "languageId");
075    
076                    Locale locale = LocaleUtil.fromLanguageId(languageId);
077    
078                    List<Locale> availableLocales = ListUtil.fromArray(
079                            LanguageUtil.getAvailableLocales());
080    
081                    if (availableLocales.contains(locale)) {
082                            if (themeDisplay.isSignedIn()) {
083                                    User user = themeDisplay.getUser();
084    
085                                    Contact contact = user.getContact();
086    
087                                    AdminUtil.updateUser(
088                                            actionRequest, user.getUserId(), user.getScreenName(),
089                                            user.getEmailAddress(), user.getFacebookId(),
090                                            user.getOpenId(), languageId, user.getTimeZoneId(),
091                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
092                                            contact.getAimSn(), contact.getFacebookSn(),
093                                            contact.getIcqSn(), contact.getJabberSn(),
094                                            contact.getMsnSn(), contact.getMySpaceSn(),
095                                            contact.getSkypeSn(), contact.getTwitterSn(),
096                                            contact.getYmSn());
097                            }
098    
099                            session.setAttribute(Globals.LOCALE_KEY, locale);
100    
101                            LanguageUtil.updateCookie(request, response, locale);
102                    }
103    
104                    // Send redirect
105    
106                    String redirect = ParamUtil.getString(actionRequest, "redirect");
107    
108                    if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
109                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
110    
111                            if (themeDisplay.isI18n()) {
112                                    int pos = redirect.indexOf(CharPool.SLASH, 1);
113    
114                                    redirect = redirect.substring(pos);
115                            }
116                    }
117                    else {
118                            String layoutURL = PortalUtil.getLayoutFriendlyURL(
119                                    layout, themeDisplay, locale);
120    
121                            int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
122    
123                            if (pos == -1) {
124                                    pos = redirect.indexOf(StringPool.QUESTION);
125                            }
126    
127                            if (pos != -1) {
128                                    redirect = layoutURL + redirect.substring(pos);
129                            }
130                            else {
131                                    redirect = layoutURL;
132                            }
133                    }
134    
135                    actionResponse.sendRedirect(redirect);
136            }
137    
138            @Override
139            public ActionForward render(
140                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141                            RenderRequest renderRequest, RenderResponse renderResponse)
142                    throws Exception {
143    
144                    return mapping.findForward("portlet.language.view");
145            }
146    
147            @Override
148            protected boolean isCheckMethodOnProcessAction() {
149                    return _CHECK_METHOD_ON_PROCESS_ACTION;
150            }
151    
152            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
153    
154    }