1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.communities.action;
21  
22  import com.liferay.portal.DuplicateGroupException;
23  import com.liferay.portal.GroupFriendlyURLException;
24  import com.liferay.portal.GroupNameException;
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.RequiredGroupException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.liveusers.LiveUsers;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.GroupServiceUtil;
34  import com.liferay.portal.service.ServiceContext;
35  import com.liferay.portal.service.ServiceContextFactory;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditGroupAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class EditGroupAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
68                  updateGroup(actionRequest);
69              }
70              else if (cmd.equals(Constants.DELETE)) {
71                  deleteGroup(actionRequest);
72              }
73  
74              sendRedirect(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchGroupException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.communities.error");
83              }
84              else if (e instanceof DuplicateGroupException ||
85                       e instanceof GroupFriendlyURLException ||
86                       e instanceof GroupNameException ||
87                       e instanceof RequiredGroupException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
90  
91                  if (cmd.equals(Constants.DELETE)) {
92                      actionResponse.sendRedirect(
93                          ParamUtil.getString(actionRequest, "redirect"));
94                  }
95              }
96              else {
97                  throw e;
98              }
99          }
100     }
101 
102     public ActionForward render(
103             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
104             RenderRequest renderRequest, RenderResponse renderResponse)
105         throws Exception {
106 
107         try {
108             ActionUtil.getGroup(renderRequest);
109         }
110         catch (Exception e) {
111             if (e instanceof NoSuchGroupException ||
112                 e instanceof PrincipalException) {
113 
114                 SessionErrors.add(renderRequest, e.getClass().getName());
115 
116                 return mapping.findForward("portlet.communities.error");
117             }
118             else {
119                 throw e;
120             }
121         }
122 
123         return mapping.findForward(
124             getForward(renderRequest, "portlet.communities.edit_community"));
125     }
126 
127     protected void deleteGroup(ActionRequest actionRequest) throws Exception {
128         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         long groupId = ParamUtil.getLong(actionRequest, "groupId");
132 
133         if ((groupId == themeDisplay.getDoAsGroupId()) ||
134             (groupId == themeDisplay.getScopeGroupId())) {
135 
136             throw new RequiredGroupException(String.valueOf(groupId));
137         }
138 
139         GroupServiceUtil.deleteGroup(groupId);
140 
141         LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
142     }
143 
144     protected void updateGroup(ActionRequest actionRequest) throws Exception {
145         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
146             WebKeys.THEME_DISPLAY);
147 
148         long userId = PortalUtil.getUserId(actionRequest);
149 
150         long groupId = ParamUtil.getLong(actionRequest, "groupId");
151 
152         String name = ParamUtil.getString(actionRequest, "name");
153         String description = ParamUtil.getString(actionRequest, "description");
154         int type = ParamUtil.getInteger(actionRequest, "type");
155         String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
156         boolean active = ParamUtil.getBoolean(actionRequest, "active");
157 
158         ServiceContext serviceContext = ServiceContextFactory.getInstance(
159             Group.class.getName(), actionRequest);
160 
161         if (groupId <= 0) {
162 
163             // Add group
164 
165             Group group = GroupServiceUtil.addGroup(
166                 name, description, type, friendlyURL, active, serviceContext);
167 
168             LiveUsers.joinGroup(
169                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
170         }
171         else {
172 
173             // Update group
174 
175             GroupServiceUtil.updateGroup(
176                 groupId, name, description, type, friendlyURL, active,
177                 serviceContext);
178         }
179     }
180 
181 }