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