1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.servlet.SessionErrors;
31  import com.liferay.portal.kernel.util.Constants;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.liveusers.LiveUsers;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.service.GroupServiceUtil;
37  import com.liferay.portal.service.ServiceContext;
38  import com.liferay.portal.service.ServiceContextFactory;
39  import com.liferay.portal.struts.PortletAction;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.WebKeys;
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   */
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         if (groupId <= 0) {
165 
166             // Add group
167 
168             Group group = GroupServiceUtil.addGroup(
169                 name, description, type, friendlyURL, active, serviceContext);
170 
171             LiveUsers.joinGroup(
172                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
173         }
174         else {
175 
176             // Update group
177 
178             GroupServiceUtil.updateGroup(
179                 groupId, name, description, type, friendlyURL, active,
180                 serviceContext);
181         }
182     }
183 
184 }