001    /**
002     * Copyright (c) 2000-2013 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.sites.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.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.WebKeys;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Jorge Ferrer
045     */
046    public class ReplyMembershipRequestAction extends PortletAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            PortletConfig portletConfig, ActionRequest actionRequest,
052                            ActionResponse actionResponse)
053                    throws Exception {
054    
055                    try {
056                            ThemeDisplay themeDisplay =
057                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
058    
059                            long membershipRequestId = ParamUtil.getLong(
060                                    actionRequest, "membershipRequestId");
061    
062                            int statusId = ParamUtil.getInteger(actionRequest, "statusId");
063                            String replyComments = ParamUtil.getString(
064                                    actionRequest, "replyComments");
065    
066                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
067                                    actionRequest);
068    
069                            MembershipRequestServiceUtil.updateStatus(
070                                    membershipRequestId, replyComments, statusId, serviceContext);
071    
072                            if (statusId == MembershipRequestConstants.STATUS_APPROVED) {
073                                    MembershipRequest membershipRequest =
074                                            MembershipRequestServiceUtil.getMembershipRequest(
075                                                    membershipRequestId);
076    
077                                    LiveUsers.joinGroup(
078                                            themeDisplay.getCompanyId(), membershipRequest.getGroupId(),
079                                            new long[] {membershipRequest.getUserId()});
080                            }
081    
082                            SessionMessages.add(actionRequest, "membership_reply_sent");
083    
084                            sendRedirect(actionRequest, actionResponse);
085                    }
086                    catch (Exception e) {
087                            if (e instanceof NoSuchGroupException ||
088                                    e instanceof PrincipalException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass());
091    
092                                    setForward(actionRequest, "portlet.sites_admin.error");
093                            }
094                            else if (e instanceof MembershipRequestCommentsException) {
095                                    SessionErrors.add(actionRequest, e.getClass());
096    
097                                    setForward(
098                                            actionRequest,
099                                            "portlet.sites_admin.reply_membership_request");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping actionMapping, ActionForm actionForm,
110                            PortletConfig portletConfig, RenderRequest renderRequest,
111                            RenderResponse renderResponse)
112                    throws Exception {
113    
114                    try {
115                            ActionUtil.getGroup(renderRequest);
116                    }
117                    catch (Exception e) {
118                            if (e instanceof NoSuchGroupException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(renderRequest, e.getClass());
122    
123                                    return actionMapping.findForward("portlet.sites_admin.error");
124                            }
125                            else {
126                                    throw e;
127                            }
128                    }
129    
130                    return actionMapping.findForward(
131                            getForward(
132                                    renderRequest, "portlet.sites_admin.reply_membership_request"));
133            }
134    
135    }