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.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  public class EditOrganizationAssignmentsAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (cmd.equals("organization_user_groups")) {
65                  updateOrganizationUserGroups(actionRequest);
66              }
67              else if (cmd.equals("organization_users")) {
68                  updateOrganizationUsers(actionRequest);
69              }
70  
71              if (Validator.isNotNull(cmd)) {
72                  String redirect = ParamUtil.getString(
73                      actionRequest, "assignmentsRedirect");
74  
75                  sendRedirect(actionRequest, actionResponse, redirect);
76              }
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchOrganizationException ||
80                  e instanceof PrincipalException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  setForward(actionRequest, "portlet.enterprise_admin.error");
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws Exception {
96  
97          try {
98              ActionUtil.getOrganization(renderRequest);
99          }
100         catch (Exception e) {
101             if (e instanceof NoSuchOrganizationException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(renderRequest, e.getClass().getName());
105 
106                 return mapping.findForward("portlet.enterprise_admin.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112 
113         return mapping.findForward(getForward(
114             renderRequest,
115             "portlet.enterprise_admin.edit_organization_assignments"));
116     }
117 
118     protected void updateOrganizationUserGroups(ActionRequest actionRequest)
119         throws Exception {
120 
121         long organizationId = ParamUtil.getLong(
122             actionRequest, "organizationId");
123 
124         Organization organization =
125             OrganizationLocalServiceUtil.getOrganization(organizationId);
126 
127         long groupId = organization.getGroup().getGroupId();
128 
129         long[] addUserGroupIds = StringUtil.split(
130             ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
131         long[] removeUserGroupIds = StringUtil.split(
132             ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
133 
134         UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
135         UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
136     }
137 
138     protected void updateOrganizationUsers(ActionRequest actionRequest)
139         throws Exception {
140 
141         long organizationId = ParamUtil.getLong(
142             actionRequest, "organizationId");
143 
144         long[] addUserIds = StringUtil.split(
145             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
146         long[] removeUserIds = StringUtil.split(
147             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
148 
149         UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
150         UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
151     }
152 
153 }