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.DuplicateGroupException;
26  import com.liferay.portal.GroupFriendlyURLException;
27  import com.liferay.portal.GroupNameException;
28  import com.liferay.portal.NoSuchGroupException;
29  import com.liferay.portal.RequiredGroupException;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.servlet.SessionErrors;
32  import com.liferay.portal.kernel.util.Constants;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.liveusers.LiveUsers;
35  import com.liferay.portal.model.Group;
36  import com.liferay.portal.model.GroupConstants;
37  import com.liferay.portal.model.MembershipRequest;
38  import com.liferay.portal.model.impl.MembershipRequestImpl;
39  import com.liferay.portal.security.auth.PrincipalException;
40  import com.liferay.portal.service.GroupServiceUtil;
41  import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
42  import com.liferay.portal.service.MembershipRequestServiceUtil;
43  import com.liferay.portal.service.ServiceContext;
44  import com.liferay.portal.service.ServiceContextFactory;
45  import com.liferay.portal.struts.PortletAction;
46  import com.liferay.portal.theme.ThemeDisplay;
47  import com.liferay.portal.util.PortalUtil;
48  import com.liferay.portal.util.WebKeys;
49  
50  import java.util.List;
51  
52  import javax.portlet.ActionRequest;
53  import javax.portlet.ActionResponse;
54  import javax.portlet.PortletConfig;
55  import javax.portlet.RenderRequest;
56  import javax.portlet.RenderResponse;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="EditGroupAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   */
67  public class EditGroupAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              ActionRequest actionRequest, ActionResponse actionResponse)
72          throws Exception {
73  
74          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
75  
76          try {
77              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
78                  updateGroup(actionRequest);
79              }
80              else if (cmd.equals(Constants.DELETE)) {
81                  deleteGroup(actionRequest);
82              }
83  
84              sendRedirect(actionRequest, actionResponse);
85          }
86          catch (Exception e) {
87              if (e instanceof NoSuchGroupException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(actionRequest, e.getClass().getName());
91  
92                  setForward(actionRequest, "portlet.communities.error");
93              }
94              else if (e instanceof DuplicateGroupException ||
95                       e instanceof GroupFriendlyURLException ||
96                       e instanceof GroupNameException ||
97                       e instanceof RequiredGroupException) {
98  
99                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
100 
101                 if (cmd.equals(Constants.DELETE)) {
102                     actionResponse.sendRedirect(
103                         ParamUtil.getString(actionRequest, "redirect"));
104                 }
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114             RenderRequest renderRequest, RenderResponse renderResponse)
115         throws Exception {
116 
117         try {
118             ActionUtil.getGroup(renderRequest);
119         }
120         catch (Exception e) {
121             if (e instanceof NoSuchGroupException ||
122                 e instanceof PrincipalException) {
123 
124                 SessionErrors.add(renderRequest, e.getClass().getName());
125 
126                 return mapping.findForward("portlet.communities.error");
127             }
128             else {
129                 throw e;
130             }
131         }
132 
133         return mapping.findForward(
134             getForward(renderRequest, "portlet.communities.edit_community"));
135     }
136 
137     protected void deleteGroup(ActionRequest actionRequest) throws Exception {
138         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
139             WebKeys.THEME_DISPLAY);
140 
141         long groupId = ParamUtil.getLong(actionRequest, "groupId");
142 
143         if ((groupId == themeDisplay.getDoAsGroupId()) ||
144             (groupId == themeDisplay.getScopeGroupId())) {
145 
146             throw new RequiredGroupException(String.valueOf(groupId));
147         }
148 
149         GroupServiceUtil.deleteGroup(groupId);
150 
151         LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
152     }
153 
154     protected void updateGroup(ActionRequest actionRequest) throws Exception {
155         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156             WebKeys.THEME_DISPLAY);
157 
158         long userId = PortalUtil.getUserId(actionRequest);
159 
160         long groupId = ParamUtil.getLong(actionRequest, "groupId");
161 
162         String name = ParamUtil.getString(actionRequest, "name");
163         String description = ParamUtil.getString(actionRequest, "description");
164         int type = ParamUtil.getInteger(actionRequest, "type");
165         String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
166         boolean active = ParamUtil.getBoolean(actionRequest, "active");
167 
168         ServiceContext serviceContext = ServiceContextFactory.getInstance(
169             Group.class.getName(), actionRequest);
170 
171         if (groupId <= 0) {
172 
173             // Add group
174 
175             Group group = GroupServiceUtil.addGroup(
176                 name, description, type, friendlyURL, active, serviceContext);
177 
178             LiveUsers.joinGroup(
179                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
180         }
181         else {
182 
183             // Update group
184 
185             GroupServiceUtil.updateGroup(
186                 groupId, name, description, type, friendlyURL, active,
187                 serviceContext);
188 
189             if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
190                 List<MembershipRequest> membershipRequests =
191                     MembershipRequestLocalServiceUtil.search(
192                         groupId, MembershipRequestImpl.STATUS_PENDING,
193                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
194 
195                 for (MembershipRequest membershipRequest : membershipRequests) {
196                     MembershipRequestServiceUtil.updateStatus(
197                         membershipRequest.getMembershipRequestId(),
198                         themeDisplay.translate(
199                             "your-membership-has-been-approved"),
200                         MembershipRequestImpl.STATUS_APPROVED);
201 
202                     LiveUsers.joinGroup(
203                         themeDisplay.getCompanyId(),
204                         membershipRequest.getGroupId(),
205                         new long[] {membershipRequest.getUserId()});
206                 }
207             }
208         }
209     }
210 
211 }