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.enterpriseadmin.action;
24  
25  import com.liferay.portal.NoSuchOrganizationException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.UserServiceUtil;
33  import com.liferay.portal.struts.PortletAction;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditOrganizationAssignmentsAction.java.html"><b><i>View Source</i>
47   * </b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class EditOrganizationAssignmentsAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals("organization_users")) {
63                  updateOrganizationUsers(actionRequest);
64              }
65  
66              if (Validator.isNotNull(cmd)) {
67                  String redirect = ParamUtil.getString(
68                      actionRequest, "assignmentsRedirect");
69  
70                  sendRedirect(actionRequest, actionResponse, redirect);
71              }
72          }
73          catch (Exception e) {
74              if (e instanceof NoSuchOrganizationException ||
75                  e instanceof PrincipalException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.enterprise_admin.error");
80              }
81              else {
82                  throw e;
83              }
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89              RenderRequest renderRequest, RenderResponse renderResponse)
90          throws Exception {
91  
92          try {
93              ActionUtil.getOrganization(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchOrganizationException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.enterprise_admin.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(getForward(
109             renderRequest,
110             "portlet.enterprise_admin.edit_organization_assignments"));
111     }
112 
113     protected void updateOrganizationUsers(ActionRequest actionRequest)
114         throws Exception {
115 
116         long organizationId = ParamUtil.getLong(
117             actionRequest, "organizationId");
118 
119         long[] addUserIds = StringUtil.split(
120             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
121         long[] removeUserIds = StringUtil.split(
122             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
123 
124         UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
125         UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
126     }
127 
128 }