001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.action;
016    
017    import com.liferay.portal.MembershipRequestCommentsException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.liveusers.LiveUsers;
023    import com.liferay.portal.model.MembershipRequest;
024    import com.liferay.portal.model.MembershipRequestConstants;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.MembershipRequestServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Jorge Ferrer
043     */
044    public class ReplyMembershipRequestAction extends PortletAction {
045    
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    try {
052                            ThemeDisplay themeDisplay =
053                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
054    
055                            long membershipRequestId = ParamUtil.getLong(
056                                    actionRequest, "membershipRequestId");
057    
058                            int statusId = ParamUtil.getInteger(actionRequest, "statusId");
059                            String replyComments = ParamUtil.getString(
060                                    actionRequest, "replyComments");
061    
062                            MembershipRequest membershipRequest =
063                                    MembershipRequestServiceUtil.getMembershipRequest(
064                                            membershipRequestId);
065    
066                            MembershipRequestServiceUtil.updateStatus(
067                                    membershipRequestId, replyComments, statusId);
068    
069                            if (statusId == MembershipRequestConstants.STATUS_APPROVED) {
070                                    LiveUsers.joinGroup(
071                                            themeDisplay.getCompanyId(),
072                                            membershipRequest.getGroupId(),
073                                            new long[] {membershipRequest.getUserId()});
074                            }
075    
076                            SessionMessages.add(actionRequest, "membership_reply_sent");
077    
078                            sendRedirect(actionRequest, actionResponse);
079                    }
080                    catch (Exception e) {
081                            if (e instanceof NoSuchGroupException ||
082                                    e instanceof PrincipalException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass().getName());
085    
086                                    setForward(actionRequest, "portlet.communities.error");
087                            }
088                            else if (e instanceof MembershipRequestCommentsException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass().getName());
091    
092                                    setForward(
093                                            actionRequest,
094                                            "portlet.communities.reply_membership_request");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100            }
101            public ActionForward render(
102                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getGroup(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchGroupException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass().getName());
114    
115                                    return mapping.findForward("portlet.communities.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    return mapping.findForward(getForward(
123                            renderRequest, "portlet.communities.reply_membership_request"));
124            }
125    
126    }