1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.NoSuchOrganizationException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Organization;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.OrganizationLocalServiceUtil;
26  import com.liferay.portal.service.UserGroupServiceUtil;
27  import com.liferay.portal.service.UserServiceUtil;
28  import com.liferay.portal.struts.PortletAction;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditOrganizationAssignmentsAction.java.html"><b><i>View Source</i>
42   * </b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class EditOrganizationAssignmentsAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55          try {
56              if (cmd.equals("organization_user_groups")) {
57                  updateOrganizationUserGroups(actionRequest);
58              }
59              else if (cmd.equals("organization_users")) {
60                  updateOrganizationUsers(actionRequest);
61              }
62  
63              if (Validator.isNotNull(cmd)) {
64                  String redirect = ParamUtil.getString(
65                      actionRequest, "assignmentsRedirect");
66  
67                  sendRedirect(actionRequest, actionResponse, redirect);
68              }
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchOrganizationException ||
72                  e instanceof PrincipalException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.enterprise_admin.error");
77              }
78              else {
79                  throw e;
80              }
81          }
82      }
83  
84      public ActionForward render(
85              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
86              RenderRequest renderRequest, RenderResponse renderResponse)
87          throws Exception {
88  
89          try {
90              ActionUtil.getOrganization(renderRequest);
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchOrganizationException ||
94                  e instanceof PrincipalException) {
95  
96                  SessionErrors.add(renderRequest, e.getClass().getName());
97  
98                  return mapping.findForward("portlet.enterprise_admin.error");
99              }
100             else {
101                 throw e;
102             }
103         }
104 
105         return mapping.findForward(getForward(
106             renderRequest,
107             "portlet.enterprise_admin.edit_organization_assignments"));
108     }
109 
110     protected void updateOrganizationUserGroups(ActionRequest actionRequest)
111         throws Exception {
112 
113         long organizationId = ParamUtil.getLong(
114             actionRequest, "organizationId");
115 
116         Organization organization =
117             OrganizationLocalServiceUtil.getOrganization(organizationId);
118 
119         long groupId = organization.getGroup().getGroupId();
120 
121         long[] addUserGroupIds = StringUtil.split(
122             ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
123         long[] removeUserGroupIds = StringUtil.split(
124             ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
125 
126         UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
127         UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
128     }
129 
130     protected void updateOrganizationUsers(ActionRequest actionRequest)
131         throws Exception {
132 
133         long organizationId = ParamUtil.getLong(
134             actionRequest, "organizationId");
135 
136         long[] addUserIds = StringUtil.split(
137             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
138         long[] removeUserIds = StringUtil.split(
139             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
140 
141         UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
142         UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
143     }
144 
145 }