1   /**
2    * Copyright (c) 2000-2009 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.MembershipRequestCommentsException;
26  import com.liferay.portal.NoSuchGroupException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.liveusers.LiveUsers;
31  import com.liferay.portal.model.MembershipRequest;
32  import com.liferay.portal.model.impl.MembershipRequestImpl;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.MembershipRequestServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
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="ReplyMembershipRequestAction.java.html"><b><i>View Source</i></b>
51   * </a>
52   *
53   * @author Jorge Ferrer
54   *
55   */
56  public class ReplyMembershipRequestAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          try {
64              ThemeDisplay themeDisplay =
65                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
66  
67              long membershipRequestId = ParamUtil.getLong(
68                  actionRequest, "membershipRequestId");
69  
70              int statusId = ParamUtil.getInteger(actionRequest, "statusId");
71              String replyComments = ParamUtil.getString(
72                  actionRequest, "replyComments");
73  
74              MembershipRequest membershipRequest =
75                  MembershipRequestServiceUtil.getMembershipRequest(
76                      membershipRequestId);
77  
78              MembershipRequestServiceUtil.updateStatus(
79                  membershipRequestId, replyComments, statusId);
80  
81              if (statusId == MembershipRequestImpl.STATUS_APPROVED) {
82                  LiveUsers.joinGroup(
83                      themeDisplay.getCompanyId(),
84                      membershipRequest.getGroupId(),
85                      new long[] {membershipRequest.getUserId()});
86              }
87  
88              SessionMessages.add(actionRequest, "membership_reply_sent");
89  
90              sendRedirect(actionRequest, actionResponse);
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchGroupException ||
94                  e instanceof PrincipalException) {
95  
96                  SessionErrors.add(actionRequest, e.getClass().getName());
97  
98                  setForward(actionRequest, "portlet.communities.error");
99              }
100             else if (e instanceof MembershipRequestCommentsException) {
101 
102                 SessionErrors.add(actionRequest, e.getClass().getName());
103 
104                 setForward(
105                     actionRequest,
106                     "portlet.communities.reply_membership_request");
107             }
108             else {
109                 throw e;
110             }
111         }
112     }
113     public ActionForward render(
114             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
115             RenderRequest renderRequest, RenderResponse renderResponse)
116         throws Exception {
117 
118         try {
119             ActionUtil.getGroup(renderRequest);
120         }
121         catch (Exception e) {
122             if (e instanceof NoSuchGroupException ||
123                 e instanceof PrincipalException) {
124 
125                 SessionErrors.add(renderRequest, e.getClass().getName());
126 
127                 return mapping.findForward("portlet.communities.error");
128             }
129             else {
130                 throw e;
131             }
132         }
133 
134         return mapping.findForward(getForward(
135             renderRequest, "portlet.communities.reply_membership_request"));
136     }
137 
138 }