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