001    /**
002     * Copyright (c) 2000-2012 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.portlet.language.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.ListUtil;
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.model.Contact;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.Portal;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.admin.util.AdminUtil;
032    
033    import java.util.List;
034    import java.util.Locale;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    import javax.servlet.http.HttpSession;
045    
046    import org.apache.struts.Globals;
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     */
054    public class ViewAction extends PortletAction {
055    
056            @Override
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
063                            actionRequest);
064                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
065                            actionResponse);
066                    HttpSession session = request.getSession();
067    
068                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
069                            WebKeys.THEME_DISPLAY);
070    
071                    Layout layout = themeDisplay.getLayout();
072    
073                    String languageId = ParamUtil.getString(actionRequest, "languageId");
074    
075                    Locale locale = LocaleUtil.fromLanguageId(languageId);
076    
077                    List<Locale> availableLocales = ListUtil.fromArray(
078                            LanguageUtil.getAvailableLocales());
079    
080                    if (availableLocales.contains(locale)) {
081                            if (themeDisplay.isSignedIn()) {
082                                    User user = themeDisplay.getUser();
083    
084                                    Contact contact = user.getContact();
085    
086                                    AdminUtil.updateUser(
087                                            actionRequest, user.getUserId(), user.getScreenName(),
088                                            user.getEmailAddress(), user.getFacebookId(),
089                                            user.getOpenId(), languageId, user.getTimeZoneId(),
090                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
091                                            contact.getAimSn(), contact.getFacebookSn(),
092                                            contact.getIcqSn(), contact.getJabberSn(),
093                                            contact.getMsnSn(), contact.getMySpaceSn(),
094                                            contact.getSkypeSn(), contact.getTwitterSn(),
095                                            contact.getYmSn());
096                            }
097    
098                            session.setAttribute(Globals.LOCALE_KEY, locale);
099    
100                            LanguageUtil.updateCookie(request, response, locale);
101                    }
102    
103                    // Send redirect
104    
105                    String redirect = ParamUtil.getString(actionRequest, "redirect");
106    
107                    String queryString = StringPool.BLANK;
108    
109                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
110    
111                    if (pos == -1) {
112                            pos = redirect.indexOf(StringPool.QUESTION);
113                    }
114    
115                    if (pos != -1) {
116                            queryString = redirect.substring(pos);
117                    }
118    
119                    if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
120                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
121    
122                            if (themeDisplay.isI18n()) {
123                                    redirect = layout.getFriendlyURL();
124                            }
125                    }
126                    else {
127                            redirect = PortalUtil.getLayoutFriendlyURL(
128                                    layout, themeDisplay, locale);
129                    }
130    
131                    redirect = redirect + queryString;
132    
133                    actionResponse.sendRedirect(redirect);
134            }
135    
136            @Override
137            public ActionForward render(
138                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
139                            RenderRequest renderRequest, RenderResponse renderResponse)
140                    throws Exception {
141    
142                    return mapping.findForward("portlet.language.view");
143            }
144    
145            @Override
146            protected boolean isCheckMethodOnProcessAction() {
147                    return _CHECK_METHOD_ON_PROCESS_ACTION;
148            }
149    
150            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
151    
152    }