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