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.communities.action;
16  
17  import com.liferay.portal.NoSuchTeamException;
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.security.auth.PrincipalException;
24  import com.liferay.portal.service.UserServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditTeamAssignmentsAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class EditTeamAssignmentsAction extends PortletAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          try {
52              if (cmd.equals("team_users")) {
53                  updateTeamUsers(actionRequest);
54              }
55  
56              if (Validator.isNotNull(cmd)) {
57                  String redirect = ParamUtil.getString(
58                      actionRequest, "assignmentsRedirect");
59  
60                  sendRedirect(actionRequest, actionResponse, redirect);
61              }
62          }
63          catch (Exception e) {
64              if (e instanceof NoSuchTeamException ||
65                  e instanceof PrincipalException) {
66  
67                  SessionErrors.add(actionRequest, e.getClass().getName());
68  
69                  setForward(actionRequest, "portlet.communities.error");
70              }
71              else {
72                  throw e;
73              }
74          }
75      }
76  
77      public ActionForward render(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              RenderRequest renderRequest, RenderResponse renderResponse)
80          throws Exception {
81  
82          try {
83              ActionUtil.getTeam(renderRequest);
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchTeamException ||
87                  e instanceof PrincipalException) {
88  
89                  SessionErrors.add(renderRequest, e.getClass().getName());
90  
91                  return mapping.findForward("portlet.communities.error");
92              }
93              else {
94                  throw e;
95              }
96          }
97  
98          return mapping.findForward(getForward(
99              renderRequest, "portlet.communities.edit_team_assignments"));
100     }
101 
102     protected void updateTeamUsers(ActionRequest actionRequest)
103         throws Exception {
104 
105         long teamId = ParamUtil.getLong(actionRequest, "teamId");
106 
107         long[] addUserIds = StringUtil.split(
108             ParamUtil.getString(actionRequest, "addUserIds"), 0L);
109         long[] removeUserIds = StringUtil.split(
110             ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
111 
112         UserServiceUtil.addTeamUsers(teamId, addUserIds);
113         UserServiceUtil.unsetTeamUsers(teamId, removeUserIds);
114     }
115 
116 }