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 mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
064                            actionRequest);
065                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
066                            actionResponse);
067                    HttpSession session = request.getSession();
068    
069                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
070                            WebKeys.THEME_DISPLAY);
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                    String layoutURL = StringPool.BLANK;
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                            layoutURL = redirect.substring(0, pos);
117                            queryString = redirect.substring(pos);
118                    }
119    
120                    Layout layout = themeDisplay.getLayout();
121    
122                    Group group = layout.getGroup();
123    
124                    if (PortalUtil.isGroupFriendlyURL(
125                                    layoutURL, group.getFriendlyURL(), layout.getFriendlyURL())) {
126    
127                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
128                                    redirect = layoutURL;
129                            }
130                            else {
131                                    redirect = PortalUtil.getGroupFriendlyURL(
132                                            themeDisplay.getScopeGroup(), layout.isPrivateLayout(),
133                                            themeDisplay, locale);
134                            }
135                    }
136                    else {
137                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
138                                    if (themeDisplay.isI18n()) {
139                                            redirect = layout.getFriendlyURL();
140                                    }
141                                    else {
142                                            redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
143                                    }
144                            }
145                            else {
146                                    redirect = PortalUtil.getLayoutFriendlyURL(
147                                            layout, themeDisplay, locale);
148                            }
149                    }
150    
151                    redirect = redirect + queryString;
152    
153                    actionResponse.sendRedirect(redirect);
154            }
155    
156            @Override
157            public ActionForward render(
158                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
159                            RenderRequest renderRequest, RenderResponse renderResponse)
160                    throws Exception {
161    
162                    return mapping.findForward("portlet.language.view");
163            }
164    
165            @Override
166            protected boolean isCheckMethodOnProcessAction() {
167                    return _CHECK_METHOD_ON_PROCESS_ACTION;
168            }
169    
170            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
171    
172    }