001    /**
002     * Copyright (c) 2000-2012 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.security.auth.PrincipalException;
023    import com.liferay.portal.service.MembershipRequestServiceUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.struts.PortletAction;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Jorge Ferrer
040     */
041    public class PostMembershipRequestAction extends PortletAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    try {
050                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
051                            String comments = ParamUtil.getString(actionRequest, "comments");
052    
053                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
054                                    actionRequest);
055    
056                            MembershipRequestServiceUtil.addMembershipRequest(
057                                    groupId, comments, serviceContext);
058    
059                            SessionMessages.add(actionRequest, "membership_request_sent");
060    
061                            sendRedirect(actionRequest, actionResponse);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof NoSuchGroupException ||
065                                    e instanceof PrincipalException) {
066    
067                                    SessionErrors.add(actionRequest, e.getClass().getName());
068    
069                                    setForward(actionRequest, "portlet.sites_admin.error");
070                            }
071                            else if (e instanceof MembershipRequestCommentsException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass().getName());
074    
075                                    setForward(
076                                            actionRequest,
077                                            "portlet.sites_admin.post_membership_request");
078                            }
079                            else {
080                                    throw e;
081                            }
082                    }
083            }
084            @Override
085            public ActionForward render(
086                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
087                            RenderRequest renderRequest, RenderResponse renderResponse)
088                    throws Exception {
089    
090                    try {
091                            ActionUtil.getGroup(renderRequest);
092                    }
093                    catch (Exception e) {
094                            if (e instanceof NoSuchGroupException ||
095                                    e instanceof PrincipalException) {
096    
097                                    SessionErrors.add(renderRequest, e.getClass().getName());
098    
099                                    return mapping.findForward("portlet.sites_admin.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return mapping.findForward(getForward(
107                            renderRequest, "portlet.sites_admin.post_membership_request"));
108            }
109    
110    }