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.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 javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditGroupAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class EditGroupAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateGroup(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteGroup(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchGroupException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.communities.error");
84              }
85              else if (e instanceof DuplicateGroupException ||
86                       e instanceof GroupFriendlyURLException ||
87                       e instanceof GroupNameException ||
88                       e instanceof RequiredGroupException) {
89  
90                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
91  
92                  if (cmd.equals(Constants.DELETE)) {
93                      actionResponse.sendRedirect(
94                          ParamUtil.getString(actionRequest, "redirect"));
95                  }
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         try {
109             ActionUtil.getGroup(renderRequest);
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchGroupException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(renderRequest, e.getClass().getName());
116 
117                 return mapping.findForward("portlet.communities.error");
118             }
119             else {
120                 throw e;
121             }
122         }
123 
124         return mapping.findForward(
125             getForward(renderRequest, "portlet.communities.edit_community"));
126     }
127 
128     protected void deleteGroup(ActionRequest actionRequest) throws Exception {
129         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130             WebKeys.THEME_DISPLAY);
131 
132         long groupId = ParamUtil.getLong(actionRequest, "groupId");
133 
134         GroupServiceUtil.deleteGroup(groupId);
135 
136         LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
137     }
138 
139     protected void updateGroup(ActionRequest actionRequest) throws Exception {
140         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
141             WebKeys.THEME_DISPLAY);
142 
143         long userId = PortalUtil.getUserId(actionRequest);
144 
145         long groupId = ParamUtil.getLong(actionRequest, "groupId");
146 
147         String name = ParamUtil.getString(actionRequest, "name");
148         String description = ParamUtil.getString(actionRequest, "description");
149         int type = ParamUtil.getInteger(actionRequest, "type");
150         String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
151         boolean active = ParamUtil.getBoolean(actionRequest, "active");
152 
153         if (groupId <= 0) {
154 
155             // Add group
156 
157             Group group = GroupServiceUtil.addGroup(
158                 name, description, type, friendlyURL, active);
159 
160             LiveUsers.joinGroup(
161                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
162         }
163         else {
164 
165             // Update group
166 
167             GroupServiceUtil.updateGroup(
168                 groupId, name, description, type, friendlyURL, active);
169         }
170     }
171 
172 }