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.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 actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    try {
051                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
052                            String comments = ParamUtil.getString(actionRequest, "comments");
053    
054                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
055                                    actionRequest);
056    
057                            MembershipRequestServiceUtil.addMembershipRequest(
058                                    groupId, comments, serviceContext);
059    
060                            SessionMessages.add(actionRequest, "membershipRequestSent");
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof NoSuchGroupException ||
066                                    e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass());
069    
070                                    setForward(actionRequest, "portlet.sites_admin.error");
071                            }
072                            else if (e instanceof MembershipRequestCommentsException) {
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    setForward(
076                                            actionRequest,
077                                            "portlet.sites_admin.post_membership_request");
078                            }
079                            else {
080                                    throw e;
081                            }
082                    }
083            }
084    
085            @Override
086            public ActionForward render(
087                            ActionMapping actionMapping, ActionForm actionForm,
088                            PortletConfig portletConfig, RenderRequest renderRequest,
089                            RenderResponse renderResponse)
090                    throws Exception {
091    
092                    try {
093                            ActionUtil.getGroup(renderRequest);
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchGroupException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(renderRequest, e.getClass());
100    
101                                    return actionMapping.findForward("portlet.sites_admin.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107    
108                    return actionMapping.findForward(
109                            getForward(
110                                    renderRequest, "portlet.sites_admin.post_membership_request"));
111            }
112    
113    }