1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.language.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.ListUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Contact;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.admin.util.AdminUtil;
39  
40  import java.util.List;
41  import java.util.Locale;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  import javax.servlet.http.HttpSession;
52  
53  import org.apache.struts.Globals;
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class ViewAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          HttpServletRequest request = PortalUtil.getHttpServletRequest(
72              actionRequest);
73          HttpServletResponse response = PortalUtil.getHttpServletResponse(
74              actionResponse);
75          HttpSession session = request.getSession();
76  
77          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
78              WebKeys.THEME_DISPLAY);
79  
80          Layout layout = themeDisplay.getLayout();
81  
82          String languageId = ParamUtil.getString(actionRequest, "languageId");
83  
84          Locale locale = LocaleUtil.fromLanguageId(languageId);
85  
86          List<Locale> availableLocales = ListUtil.fromArray(
87              LanguageUtil.getAvailableLocales());
88  
89          if (availableLocales.contains(locale)) {
90              if (themeDisplay.isSignedIn()) {
91                  User user = themeDisplay.getUser();
92  
93                  Contact contact = user.getContact();
94  
95                  AdminUtil.updateUser(
96                      actionRequest, user.getUserId(), user.getScreenName(),
97                      user.getEmailAddress(), user.getOpenId(), languageId,
98                      user.getTimeZoneId(), user.getGreeting(),
99                      user.getComments(), contact.getSmsSn(), contact.getAimSn(),
100                     contact.getFacebookSn(), contact.getIcqSn(),
101                     contact.getJabberSn(), contact.getMsnSn(),
102                     contact.getMySpaceSn(), contact.getSkypeSn(),
103                     contact.getTwitterSn(), contact.getYmSn());
104             }
105 
106             session.setAttribute(Globals.LOCALE_KEY, locale);
107 
108             LanguageUtil.updateCookie(request, response, locale);
109         }
110 
111         // Send redirect
112 
113         String redirect = ParamUtil.getString(actionRequest, "redirect");
114 
115         if (Validator.isNull(redirect)) {
116             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
117 
118             if (themeDisplay.isI18n()) {
119                 int pos = redirect.indexOf(StringPool.SLASH, 1);
120 
121                 redirect = redirect.substring(pos);
122             }
123         }
124 
125         actionResponse.sendRedirect(redirect);
126     }
127 
128     public ActionForward render(
129             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
130             RenderRequest renderRequest, RenderResponse renderResponse)
131         throws Exception {
132 
133         return mapping.findForward("portlet.language.view");
134     }
135 
136     protected boolean isCheckMethodOnProcessAction() {
137         return _CHECK_METHOD_ON_PROCESS_ACTION;
138     }
139 
140     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
141 
142 }