1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.impl.MembershipRequestImpl;
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  
42  import java.util.List;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="EditGroupAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   */
59  public class EditGroupAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateGroup(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteGroup(actionRequest);
74              }
75  
76              sendRedirect(actionRequest, actionResponse);
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchGroupException ||
80                  e instanceof PrincipalException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  setForward(actionRequest, "portlet.communities.error");
85              }
86              else if (e instanceof DuplicateGroupException ||
87                       e instanceof GroupFriendlyURLException ||
88                       e instanceof GroupNameException ||
89                       e instanceof RequiredGroupException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
92  
93                  if (cmd.equals(Constants.DELETE)) {
94                      actionResponse.sendRedirect(
95                          ParamUtil.getString(actionRequest, "redirect"));
96                  }
97              }
98              else {
99                  throw e;
100             }
101         }
102     }
103 
104     public ActionForward render(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws Exception {
108 
109         try {
110             ActionUtil.getGroup(renderRequest);
111         }
112         catch (Exception e) {
113             if (e instanceof NoSuchGroupException ||
114                 e instanceof PrincipalException) {
115 
116                 SessionErrors.add(renderRequest, e.getClass().getName());
117 
118                 return mapping.findForward("portlet.communities.error");
119             }
120             else {
121                 throw e;
122             }
123         }
124 
125         return mapping.findForward(
126             getForward(renderRequest, "portlet.communities.edit_community"));
127     }
128 
129     protected void deleteGroup(ActionRequest actionRequest) throws Exception {
130         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
131             WebKeys.THEME_DISPLAY);
132 
133         long groupId = ParamUtil.getLong(actionRequest, "groupId");
134 
135         if ((groupId == themeDisplay.getDoAsGroupId()) ||
136             (groupId == themeDisplay.getScopeGroupId())) {
137 
138             throw new RequiredGroupException(String.valueOf(groupId));
139         }
140 
141         GroupServiceUtil.deleteGroup(groupId);
142 
143         LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
144     }
145 
146     protected void updateGroup(ActionRequest actionRequest) throws Exception {
147         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148             WebKeys.THEME_DISPLAY);
149 
150         long userId = PortalUtil.getUserId(actionRequest);
151 
152         long groupId = ParamUtil.getLong(actionRequest, "groupId");
153 
154         String name = ParamUtil.getString(actionRequest, "name");
155         String description = ParamUtil.getString(actionRequest, "description");
156         int type = ParamUtil.getInteger(actionRequest, "type");
157         String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
158         boolean active = ParamUtil.getBoolean(actionRequest, "active");
159 
160         ServiceContext serviceContext = ServiceContextFactory.getInstance(
161             Group.class.getName(), actionRequest);
162 
163         if (groupId <= 0) {
164 
165             // Add group
166 
167             Group group = GroupServiceUtil.addGroup(
168                 name, description, type, friendlyURL, active, serviceContext);
169 
170             LiveUsers.joinGroup(
171                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
172         }
173         else {
174 
175             // Update group
176 
177             GroupServiceUtil.updateGroup(
178                 groupId, name, description, type, friendlyURL, active,
179                 serviceContext);
180 
181             if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
182                 List<MembershipRequest> membershipRequests =
183                     MembershipRequestLocalServiceUtil.search(
184                         groupId, MembershipRequestImpl.STATUS_PENDING,
185                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
186 
187                 for (MembershipRequest membershipRequest : membershipRequests) {
188                     MembershipRequestServiceUtil.updateStatus(
189                         membershipRequest.getMembershipRequestId(),
190                         themeDisplay.translate(
191                             "your-membership-has-been-approved"),
192                         MembershipRequestImpl.STATUS_APPROVED);
193 
194                     LiveUsers.joinGroup(
195                         themeDisplay.getCompanyId(),
196                         membershipRequest.getGroupId(),
197                         new long[] {membershipRequest.getUserId()});
198                 }
199             }
200         }
201     }
202 
203 }