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