001    /**
002     * Copyright (c) 2000-2010 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.kernel.util.Validator;
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.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            public void processAction(
057                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
058                            ActionRequest actionRequest, ActionResponse actionResponse)
059                    throws Exception {
060    
061                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
062                            actionRequest);
063                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
064                            actionResponse);
065                    HttpSession session = request.getSession();
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    Layout layout = themeDisplay.getLayout();
071    
072                    String languageId = ParamUtil.getString(actionRequest, "languageId");
073    
074                    Locale locale = LocaleUtil.fromLanguageId(languageId);
075    
076                    List<Locale> availableLocales = ListUtil.fromArray(
077                            LanguageUtil.getAvailableLocales());
078    
079                    if (availableLocales.contains(locale)) {
080                            if (themeDisplay.isSignedIn()) {
081                                    User user = themeDisplay.getUser();
082    
083                                    Contact contact = user.getContact();
084    
085                                    AdminUtil.updateUser(
086                                            actionRequest, user.getUserId(), user.getScreenName(),
087                                            user.getEmailAddress(), user.getFacebookId(),
088                                            user.getOpenId(), languageId, user.getTimeZoneId(),
089                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
090                                            contact.getAimSn(), contact.getFacebookSn(),
091                                            contact.getIcqSn(), contact.getJabberSn(),
092                                            contact.getMsnSn(), contact.getMySpaceSn(),
093                                            contact.getSkypeSn(), contact.getTwitterSn(),
094                                            contact.getYmSn());
095                            }
096    
097                            session.setAttribute(Globals.LOCALE_KEY, locale);
098    
099                            LanguageUtil.updateCookie(request, response, locale);
100                    }
101    
102                    // Send redirect
103    
104                    String redirect = ParamUtil.getString(actionRequest, "redirect");
105    
106                    if (Validator.isNull(redirect)) {
107                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
108                                    redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
109    
110                                    if (themeDisplay.isI18n()) {
111                                            int pos = redirect.indexOf(StringPool.SLASH, 1);
112    
113                                            redirect = redirect.substring(pos);
114                                    }
115                            }
116                            else {
117                                    redirect = PortalUtil.getLayoutFriendlyURL(
118                                            layout, themeDisplay, locale);
119                            }
120                    }
121    
122                    actionResponse.sendRedirect(redirect);
123            }
124    
125            public ActionForward render(
126                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
127                            RenderRequest renderRequest, RenderResponse renderResponse)
128                    throws Exception {
129    
130                    return mapping.findForward("portlet.language.view");
131            }
132    
133            protected boolean isCheckMethodOnProcessAction() {
134                    return _CHECK_METHOD_ON_PROCESS_ACTION;
135            }
136    
137            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
138    
139    }