1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ViewAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          HttpServletRequest request = PortalUtil.getHttpServletRequest(
71              actionRequest);
72          HttpServletResponse response = PortalUtil.getHttpServletResponse(
73              actionResponse);
74          HttpSession session = request.getSession();
75  
76          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
77              WebKeys.THEME_DISPLAY);
78  
79          Layout layout = themeDisplay.getLayout();
80  
81          String languageId = ParamUtil.getString(actionRequest, "languageId");
82  
83          Locale locale = LocaleUtil.fromLanguageId(languageId);
84  
85          List<Locale> availableLocales = ListUtil.fromArray(
86              LanguageUtil.getAvailableLocales());
87  
88          if (availableLocales.contains(locale)) {
89              if (themeDisplay.isSignedIn()) {
90                  User user = themeDisplay.getUser();
91  
92                  Contact contact = user.getContact();
93  
94                  AdminUtil.updateUser(
95                      actionRequest, user.getUserId(), user.getScreenName(),
96                      user.getEmailAddress(), languageId, user.getTimeZoneId(),
97                      user.getGreeting(), user.getComments(), contact.getSmsSn(),
98                      contact.getAimSn(), contact.getFacebookSn(),
99                      contact.getIcqSn(), contact.getJabberSn(),
100                     contact.getMsnSn(), contact.getMySpaceSn(),
101                     contact.getSkypeSn(), contact.getTwitterSn(),
102                     contact.getYmSn());
103             }
104 
105             session.setAttribute(Globals.LOCALE_KEY, locale);
106 
107             LanguageUtil.updateCookie(request, response, locale);
108         }
109 
110         // Send redirect
111 
112         String redirect = ParamUtil.getString(actionRequest, "redirect");
113 
114         if (Validator.isNull(redirect)) {
115             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
116 
117             if (themeDisplay.isI18n()) {
118                 int pos = redirect.indexOf(StringPool.SLASH, 1);
119 
120                 redirect = redirect.substring(pos);
121             }
122         }
123 
124         actionResponse.sendRedirect(redirect);
125     }
126 
127     public ActionForward render(
128             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129             RenderRequest renderRequest, RenderResponse renderResponse)
130         throws Exception {
131 
132         return mapping.findForward("portlet.language.view");
133     }
134 
135     protected boolean isCheckMethodOnProcessAction() {
136         return _CHECK_METHOD_ON_PROCESS_ACTION;
137     }
138 
139     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
140 
141 }