1   /**
2    * Copyright (c) 2000-2008 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.LocaleUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Contact;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.admin.util.AdminUtil;
37  import com.liferay.util.ListUtil;
38  
39  import java.util.List;
40  import java.util.Locale;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  import javax.servlet.http.HttpSession;
51  
52  import org.apache.struts.Globals;
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class ViewAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig config,
67              ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
71          HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(res);
72          HttpSession httpSes = httpReq.getSession();
73  
74          ThemeDisplay themeDisplay =
75              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
76  
77          Layout layout = themeDisplay.getLayout();
78  
79          String languageId = ParamUtil.getString(req, "languageId");
80  
81          Locale locale = LocaleUtil.fromLanguageId(languageId);
82  
83          List<Locale> availableLocales = ListUtil.fromArray(
84              LanguageUtil.getAvailableLocales());
85  
86          if (availableLocales.contains(locale)) {
87              if (themeDisplay.isSignedIn()) {
88                  User user = themeDisplay.getUser();
89  
90                  Contact contact = user.getContact();
91  
92                  AdminUtil.updateUser(
93                      req, user.getUserId(), user.getScreenName(),
94                      user.getEmailAddress(), languageId, user.getTimeZoneId(),
95                      user.getGreeting(), user.getComments(), contact.getSmsSn(),
96                      contact.getAimSn(), contact.getIcqSn(),
97                      contact.getJabberSn(), contact.getMsnSn(),
98                      contact.getSkypeSn(), contact.getYmSn());
99              }
100 
101             httpSes.setAttribute(Globals.LOCALE_KEY, locale);
102 
103             LanguageUtil.updateCookie(httpRes, locale);
104         }
105 
106         // Send redirect
107 
108         String redirect = ParamUtil.getString(req, "redirect");
109 
110         if (Validator.isNull(redirect)) {
111             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
112         }
113 
114         res.sendRedirect(redirect);
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig config,
119             RenderRequest req, RenderResponse res)
120         throws Exception {
121 
122         return mapping.findForward("portlet.language.view");
123     }
124 
125     protected boolean isCheckMethodOnProcessAction() {
126         return _CHECK_METHOD_ON_PROCESS_ACTION;
127     }
128 
129     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
130 
131 }