001    /**
002     * Copyright (c) 2000-2013 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.Group;
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 actionMapping, ActionForm actionForm,
060                            PortletConfig portletConfig, ActionRequest actionRequest,
061                            ActionResponse actionResponse)
062                    throws Exception {
063    
064                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
065                            actionRequest);
066                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
067                            actionResponse);
068                    HttpSession session = request.getSession();
069    
070                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
071                            WebKeys.THEME_DISPLAY);
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(themeDisplay.getSiteGroupId()));
079    
080                    if (availableLocales.contains(locale)) {
081                            boolean persistState = ParamUtil.getBoolean(
082                                    request, "persistState", true);
083    
084                            if (themeDisplay.isSignedIn() && (persistState)) {
085                                    User user = themeDisplay.getUser();
086    
087                                    Contact contact = user.getContact();
088    
089                                    AdminUtil.updateUser(
090                                            actionRequest, user.getUserId(), user.getScreenName(),
091                                            user.getEmailAddress(), user.getFacebookId(),
092                                            user.getOpenId(), languageId, user.getTimeZoneId(),
093                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
094                                            contact.getAimSn(), contact.getFacebookSn(),
095                                            contact.getIcqSn(), contact.getJabberSn(),
096                                            contact.getMsnSn(), contact.getMySpaceSn(),
097                                            contact.getSkypeSn(), contact.getTwitterSn(),
098                                            contact.getYmSn());
099                            }
100    
101                            session.setAttribute(Globals.LOCALE_KEY, locale);
102    
103                            LanguageUtil.updateCookie(request, response, locale);
104                    }
105    
106                    // Send redirect
107    
108                    String redirect = ParamUtil.getString(actionRequest, "redirect");
109    
110                    String layoutURL = StringPool.BLANK;
111                    String queryString = StringPool.BLANK;
112    
113                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
114    
115                    if (pos == -1) {
116                            pos = redirect.indexOf(StringPool.QUESTION);
117                    }
118    
119                    if (pos != -1) {
120                            layoutURL = redirect.substring(0, pos);
121                            queryString = redirect.substring(pos);
122                    }
123    
124                    Layout layout = themeDisplay.getLayout();
125    
126                    Group group = layout.getGroup();
127    
128                    if (PortalUtil.isGroupFriendlyURL(
129                                    layoutURL, group.getFriendlyURL(),
130                                    layout.getFriendlyURL(locale))) {
131    
132                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
133                                    redirect = layoutURL;
134                            }
135                            else {
136                                    redirect = PortalUtil.getGroupFriendlyURL(
137                                            group, layout.isPrivateLayout(), themeDisplay, locale);
138                            }
139                    }
140                    else {
141                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
142                                    if (themeDisplay.isI18n()) {
143                                            redirect = layout.getFriendlyURL(locale);
144                                    }
145                                    else {
146                                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
147                                    }
148                            }
149                            else {
150                                    redirect = PortalUtil.getLayoutFriendlyURL(
151                                            layout, themeDisplay, locale);
152                            }
153                    }
154    
155                    redirect = redirect + queryString;
156    
157                    actionResponse.sendRedirect(redirect);
158            }
159    
160            @Override
161            public ActionForward render(
162                            ActionMapping actionMapping, ActionForm actionForm,
163                            PortletConfig portletConfig, RenderRequest renderRequest,
164                            RenderResponse renderResponse)
165                    throws Exception {
166    
167                    return actionMapping.findForward("portlet.language.view");
168            }
169    
170            @Override
171            protected boolean isCheckMethodOnProcessAction() {
172                    return _CHECK_METHOD_ON_PROCESS_ACTION;
173            }
174    
175            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
176    
177    }