001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.struts.PortletAction;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Jorge Ferrer
038     */
039    public class PostMembershipRequestAction extends PortletAction {
040    
041            @Override
042            public void processAction(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    try {
048                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
049                            String comments = ParamUtil.getString(actionRequest, "comments");
050    
051                            MembershipRequestServiceUtil.addMembershipRequest(
052                                    groupId, comments);
053    
054                            SessionMessages.add(actionRequest, "membership_request_sent");
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof NoSuchGroupException ||
060                                    e instanceof PrincipalException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063    
064                                    setForward(actionRequest, "portlet.sites_admin.error");
065                            }
066                            else if (e instanceof MembershipRequestCommentsException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass().getName());
069    
070                                    setForward(
071                                            actionRequest,
072                                            "portlet.sites_admin.post_membership_request");
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079            @Override
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    try {
086                            ActionUtil.getGroup(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchGroupException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(renderRequest, e.getClass().getName());
093    
094                                    return mapping.findForward("portlet.sites_admin.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100    
101                    return mapping.findForward(getForward(
102                            renderRequest, "portlet.sites_admin.post_membership_request"));
103            }
104    
105    }