1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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.getPortletGroupId()) {
134             throw new RequiredGroupException(String.valueOf(groupId));
135         }
136 
137         GroupServiceUtil.deleteGroup(groupId);
138 
139         LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
140     }
141 
142     protected void updateGroup(ActionRequest actionRequest) throws Exception {
143         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
144             WebKeys.THEME_DISPLAY);
145 
146         long userId = PortalUtil.getUserId(actionRequest);
147 
148         long groupId = ParamUtil.getLong(actionRequest, "groupId");
149 
150         String name = ParamUtil.getString(actionRequest, "name");
151         String description = ParamUtil.getString(actionRequest, "description");
152         int type = ParamUtil.getInteger(actionRequest, "type");
153         String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
154         boolean active = ParamUtil.getBoolean(actionRequest, "active");
155 
156         if (groupId <= 0) {
157 
158             // Add group
159 
160             Group group = GroupServiceUtil.addGroup(
161                 name, description, type, friendlyURL, active);
162 
163             LiveUsers.joinGroup(
164                 themeDisplay.getCompanyId(), group.getGroupId(), userId);
165         }
166         else {
167 
168             // Update group
169 
170             GroupServiceUtil.updateGroup(
171                 groupId, name, description, type, friendlyURL, active);
172         }
173     }
174 
175 }