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