1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.language.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.ListUtil;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Contact;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.struts.PortletAction;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.PropsValues;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.admin.util.AdminUtil;
32  
33  import java.util.List;
34  import java.util.Locale;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.http.HttpSession;
45  
46  import org.apache.struts.Globals;
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class ViewAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          HttpServletRequest request = PortalUtil.getHttpServletRequest(
64              actionRequest);
65          HttpServletResponse response = PortalUtil.getHttpServletResponse(
66              actionResponse);
67          HttpSession session = request.getSession();
68  
69          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
70              WebKeys.THEME_DISPLAY);
71  
72          Layout layout = themeDisplay.getLayout();
73  
74          String languageId = ParamUtil.getString(actionRequest, "languageId");
75  
76          Locale locale = LocaleUtil.fromLanguageId(languageId);
77  
78          List<Locale> availableLocales = ListUtil.fromArray(
79              LanguageUtil.getAvailableLocales());
80  
81          if (availableLocales.contains(locale)) {
82              if (themeDisplay.isSignedIn()) {
83                  User user = themeDisplay.getUser();
84  
85                  Contact contact = user.getContact();
86  
87                  AdminUtil.updateUser(
88                      actionRequest, user.getUserId(), user.getScreenName(),
89                      user.getEmailAddress(), user.getOpenId(), languageId,
90                      user.getTimeZoneId(), user.getGreeting(),
91                      user.getComments(), contact.getSmsSn(), contact.getAimSn(),
92                      contact.getFacebookSn(), contact.getIcqSn(),
93                      contact.getJabberSn(), contact.getMsnSn(),
94                      contact.getMySpaceSn(), contact.getSkypeSn(),
95                      contact.getTwitterSn(), contact.getYmSn());
96              }
97  
98              session.setAttribute(Globals.LOCALE_KEY, locale);
99  
100             LanguageUtil.updateCookie(request, response, locale);
101         }
102 
103         // Send redirect
104 
105         String redirect = ParamUtil.getString(actionRequest, "redirect");
106 
107         if (Validator.isNull(redirect)) {
108             if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
109                 redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
110 
111                 if (themeDisplay.isI18n()) {
112                     int pos = redirect.indexOf(StringPool.SLASH, 1);
113 
114                     redirect = redirect.substring(pos);
115                 }
116             }
117             else {
118                 redirect = PortalUtil.getLayoutFriendlyURL(
119                     layout, themeDisplay, locale);
120             }
121         }
122 
123         actionResponse.sendRedirect(redirect);
124     }
125 
126     public ActionForward render(
127             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
128             RenderRequest renderRequest, RenderResponse renderResponse)
129         throws Exception {
130 
131         return mapping.findForward("portlet.language.view");
132     }
133 
134     protected boolean isCheckMethodOnProcessAction() {
135         return _CHECK_METHOD_ON_PROCESS_ACTION;
136     }
137 
138     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
139 
140 }