1   /**
2    * Copyright (c) 2000-2007 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.communities.action;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.OrganizationServiceUtil;
32  import com.liferay.portal.service.UserGroupRoleServiceUtil;
33  import com.liferay.portal.service.UserGroupServiceUtil;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.util.LiveUsers;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.util.servlet.SessionErrors;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditGroupAssignmentsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class EditGroupAssignmentsAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig config,
60              ActionRequest req, ActionResponse res)
61          throws Exception {
62  
63          String cmd = ParamUtil.getString(req, Constants.CMD);
64  
65          try {
66              if (cmd.equals("group_organizations")) {
67                  updateGroupOrganizations(req);
68              }
69              else if (cmd.equals("group_user_groups")) {
70                  updateGroupUserGroups(req);
71              }
72              else if (cmd.equals("group_users")) {
73                  updateGroupUsers(req);
74              }
75              else if (cmd.equals("user_group_role")) {
76                  updateUserGroupRole(req);
77              }
78  
79              sendRedirect(req, res);
80          }
81          catch (Exception e) {
82              if (e instanceof NoSuchGroupException ||
83                  e instanceof PrincipalException) {
84  
85                  SessionErrors.add(req, e.getClass().getName());
86  
87                  setForward(req, "portlet.communities.error");
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig config,
97              RenderRequest req, RenderResponse res)
98          throws Exception {
99  
100         try {
101             ActionUtil.getGroup(req);
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchGroupException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(req, e.getClass().getName());
108 
109                 return mapping.findForward("portlet.communities.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115 
116         return mapping.findForward(
117             getForward(req, "portlet.communities.edit_community_assignments"));
118     }
119 
120     protected void updateGroupOrganizations(ActionRequest req)
121         throws Exception {
122 
123         long groupId = ParamUtil.getLong(req, "groupId");
124 
125         long[] addOrganizationIds = StringUtil.split(
126             ParamUtil.getString(req, "addOrganizationIds"), 0L);
127         long[] removeOrganizationIds = StringUtil.split(
128             ParamUtil.getString(req, "removeOrganizationIds"), 0L);
129 
130         OrganizationServiceUtil.addGroupOrganizations(
131             groupId, addOrganizationIds);
132         OrganizationServiceUtil.unsetGroupOrganizations(
133             groupId, removeOrganizationIds);
134     }
135 
136     protected void updateGroupUserGroups(ActionRequest req)
137         throws Exception {
138 
139         long groupId = ParamUtil.getLong(req, "groupId");
140 
141         long[] addUserGroupIds = StringUtil.split(
142             ParamUtil.getString(req, "addUserGroupIds"), 0L);
143         long[] removeUserGroupIds = StringUtil.split(
144             ParamUtil.getString(req, "removeUserGroupIds"), 0L);
145 
146         UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
147         UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
148     }
149 
150     protected void updateGroupUsers(ActionRequest req) throws Exception {
151         long groupId = ParamUtil.getLong(req, "groupId");
152 
153         long[] addUserIds = StringUtil.split(
154             ParamUtil.getString(req, "addUserIds"), 0L);
155         long[] removeUserIds = StringUtil.split(
156             ParamUtil.getString(req, "removeUserIds"), 0L);
157 
158         UserServiceUtil.addGroupUsers(groupId, addUserIds);
159         UserServiceUtil.unsetGroupUsers(groupId, removeUserIds);
160 
161         LiveUsers.joinGroup(addUserIds, groupId);
162         LiveUsers.leaveGroup(removeUserIds, groupId);
163     }
164 
165     protected void updateUserGroupRole(ActionRequest req) throws Exception {
166         User user = PortalUtil.getSelectedUser(req, false);
167 
168         if (user == null) {
169             return;
170         }
171 
172         long groupId = ParamUtil.getLong(req, "groupId");
173 
174         long[] addRoleIds = StringUtil.split(
175             ParamUtil.getString(req, "addRoleIds"), 0L);
176         long[] removeRoleIds = StringUtil.split(
177             ParamUtil.getString(req, "removeRoleIds"), 0L);
178 
179         UserGroupRoleServiceUtil.addUserGroupRoles(
180             user.getUserId(), groupId, addRoleIds);
181         UserGroupRoleServiceUtil.deleteUserGroupRoles(
182             user.getUserId(), groupId, removeRoleIds);
183     }
184 
185 }