1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.language.action;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.util.ListUtil;
24  import com.liferay.portal.kernel.util.LocaleUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Contact;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.admin.util.AdminUtil;
36  
37  import java.util.List;
38  import java.util.Locale;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import javax.servlet.http.HttpSession;
49  
50  import org.apache.struts.Globals;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class ViewAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          HttpServletRequest request = PortalUtil.getHttpServletRequest(
69              actionRequest);
70          HttpServletResponse response = PortalUtil.getHttpServletResponse(
71              actionResponse);
72          HttpSession session = request.getSession();
73  
74          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
75              WebKeys.THEME_DISPLAY);
76  
77          Layout layout = themeDisplay.getLayout();
78  
79          String languageId = ParamUtil.getString(actionRequest, "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                      actionRequest, user.getUserId(), user.getScreenName(),
94                      user.getEmailAddress(), languageId, user.getTimeZoneId(),
95                      user.getGreeting(), user.getComments(), contact.getSmsSn(),
96                      contact.getAimSn(), contact.getFacebookSn(),
97                      contact.getIcqSn(), contact.getJabberSn(),
98                      contact.getMsnSn(), contact.getMySpaceSn(),
99                      contact.getSkypeSn(), contact.getTwitterSn(),
100                     contact.getYmSn());
101             }
102 
103             session.setAttribute(Globals.LOCALE_KEY, locale);
104 
105             LanguageUtil.updateCookie(request, response, locale);
106         }
107 
108         // Send redirect
109 
110         String redirect = ParamUtil.getString(actionRequest, "redirect");
111 
112         if (Validator.isNull(redirect)) {
113             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
114 
115             if (themeDisplay.isI18n()) {
116                 int pos = redirect.indexOf(StringPool.SLASH, 1);
117 
118                 redirect = redirect.substring(pos);
119             }
120         }
121 
122         actionResponse.sendRedirect(redirect);
123     }
124 
125     public ActionForward render(
126             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
127             RenderRequest renderRequest, RenderResponse renderResponse)
128         throws Exception {
129 
130         return mapping.findForward("portlet.language.view");
131     }
132 
133     protected boolean isCheckMethodOnProcessAction() {
134         return _CHECK_METHOD_ON_PROCESS_ACTION;
135     }
136 
137     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
138 
139 }