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