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  public class ReplyMembershipRequestAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          try {
63              ThemeDisplay themeDisplay =
64                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
65  
66              long membershipRequestId = ParamUtil.getLong(
67                  actionRequest, "membershipRequestId");
68  
69              int statusId = ParamUtil.getInteger(actionRequest, "statusId");
70              String replyComments = ParamUtil.getString(
71                  actionRequest, "replyComments");
72  
73              MembershipRequest membershipRequest =
74                  MembershipRequestServiceUtil.getMembershipRequest(
75                      membershipRequestId);
76  
77              MembershipRequestServiceUtil.updateStatus(
78                  membershipRequestId, replyComments, statusId);
79  
80              if (statusId == MembershipRequestImpl.STATUS_APPROVED) {
81                  LiveUsers.joinGroup(
82                      themeDisplay.getCompanyId(),
83                      membershipRequest.getGroupId(),
84                      new long[] {membershipRequest.getUserId()});
85              }
86  
87              SessionMessages.add(actionRequest, "membership_reply_sent");
88  
89              sendRedirect(actionRequest, actionResponse);
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchGroupException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96  
97                  setForward(actionRequest, "portlet.communities.error");
98              }
99              else if (e instanceof MembershipRequestCommentsException) {
100 
101                 SessionErrors.add(actionRequest, e.getClass().getName());
102 
103                 setForward(
104                     actionRequest,
105                     "portlet.communities.reply_membership_request");
106             }
107             else {
108                 throw e;
109             }
110         }
111     }
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114             RenderRequest renderRequest, RenderResponse renderResponse)
115         throws Exception {
116 
117         try {
118             ActionUtil.getGroup(renderRequest);
119         }
120         catch (Exception e) {
121             if (e instanceof NoSuchGroupException ||
122                 e instanceof PrincipalException) {
123 
124                 SessionErrors.add(renderRequest, e.getClass().getName());
125 
126                 return mapping.findForward("portlet.communities.error");
127             }
128             else {
129                 throw e;
130             }
131         }
132 
133         return mapping.findForward(getForward(
134             renderRequest, "portlet.communities.reply_membership_request"));
135     }
136 
137 }