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());
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 layoutURL = StringPool.BLANK;
108                    String queryString = StringPool.BLANK;
109    
110                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
111    
112                    if (pos == -1) {
113                            pos = redirect.indexOf(StringPool.QUESTION);
114                    }
115    
116                    if (pos != -1) {
117                            layoutURL = redirect.substring(0, pos);
118                            queryString = redirect.substring(pos);
119                    }
120    
121                    Layout layout = themeDisplay.getLayout();
122    
123                    Group group = layout.getGroup();
124    
125                    if (PortalUtil.isGroupFriendlyURL(
126                                    layoutURL, group.getFriendlyURL(), layout.getFriendlyURL())) {
127    
128                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
129                                    redirect = layoutURL;
130                            }
131                            else {
132                                    redirect = PortalUtil.getGroupFriendlyURL(
133                                            themeDisplay.getScopeGroup(), layout.isPrivateLayout(),
134                                            themeDisplay, locale);
135                            }
136                    }
137                    else {
138                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
139                                    if (themeDisplay.isI18n()) {
140                                            redirect = layout.getFriendlyURL();
141                                    }
142                                    else {
143                                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
144                                    }
145                            }
146                            else {
147                                    redirect = PortalUtil.getLayoutFriendlyURL(
148                                            layout, themeDisplay, locale);
149                            }
150                    }
151    
152                    redirect = redirect + queryString;
153    
154                    actionResponse.sendRedirect(redirect);
155            }
156    
157            @Override
158            public ActionForward render(
159                            ActionMapping actionMapping, ActionForm actionForm,
160                            PortletConfig portletConfig, RenderRequest renderRequest,
161                            RenderResponse renderResponse)
162                    throws Exception {
163    
164                    return actionMapping.findForward("portlet.language.view");
165            }
166    
167            @Override
168            protected boolean isCheckMethodOnProcessAction() {
169                    return _CHECK_METHOD_ON_PROCESS_ACTION;
170            }
171    
172            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
173    
174    }