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