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                    if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
108                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
109    
110                            if (themeDisplay.isI18n()) {
111                                    redirect = layout.getFriendlyURL();
112                            }
113                    }
114                    else {
115                            String layoutURL = PortalUtil.getLayoutFriendlyURL(
116                                    layout, themeDisplay, locale);
117    
118                            int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
119    
120                            if (pos == -1) {
121                                    pos = redirect.indexOf(StringPool.QUESTION);
122                            }
123    
124                            if (pos != -1) {
125                                    redirect = layoutURL + redirect.substring(pos);
126                            }
127                            else {
128                                    redirect = layoutURL;
129                            }
130                    }
131    
132                    actionResponse.sendRedirect(redirect);
133            }
134    
135            @Override
136            public ActionForward render(
137                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
138                            RenderRequest renderRequest, RenderResponse renderResponse)
139                    throws Exception {
140    
141                    return mapping.findForward("portlet.language.view");
142            }
143    
144            @Override
145            protected boolean isCheckMethodOnProcessAction() {
146                    return _CHECK_METHOD_ON_PROCESS_ACTION;
147            }
148    
149            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
150    
151    }