1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.MembershipRequestCommentsException;
18  import com.liferay.portal.NoSuchGroupException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.servlet.SessionMessages;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.MembershipRequestServiceUtil;
24  import com.liferay.portal.struts.PortletAction;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  
36  /**
37   * <a href="PostMembershipRequestAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class PostMembershipRequestAction extends PortletAction {
42  
43      public void processAction(
44              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
45              ActionRequest actionRequest, ActionResponse actionResponse)
46          throws Exception {
47  
48          try {
49              long groupId = ParamUtil.getLong(actionRequest, "groupId");
50              String comments = ParamUtil.getString(actionRequest, "comments");
51  
52              MembershipRequestServiceUtil.addMembershipRequest(
53                  groupId, comments);
54  
55              SessionMessages.add(actionRequest, "membership_request_sent");
56  
57              sendRedirect(actionRequest, actionResponse);
58          }
59          catch (Exception e) {
60              if (e instanceof NoSuchGroupException ||
61                  e instanceof PrincipalException) {
62  
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(actionRequest, "portlet.communities.error");
66              }
67              else if (e instanceof MembershipRequestCommentsException) {
68  
69                  SessionErrors.add(actionRequest, e.getClass().getName());
70  
71                  setForward(
72                      actionRequest,
73                      "portlet.communities.post_membership_request");
74              }
75              else {
76                  throw e;
77              }
78          }
79      }
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws Exception {
84  
85          try {
86              ActionUtil.getGroup(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchGroupException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.communities.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(getForward(
102             renderRequest, "portlet.communities.post_membership_request"));
103     }
104 
105 }