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