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.taglib.security;
016    
017    import com.liferay.portal.kernel.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.theme.PortletDisplay;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortletURLFactoryUtil;
026    
027    import javax.portlet.PortletRequest;
028    import javax.portlet.PortletURL;
029    import javax.portlet.WindowState;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.jsp.JspException;
033    import javax.servlet.jsp.PageContext;
034    import javax.servlet.jsp.tagext.TagSupport;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class PermissionsURLTag extends TagSupport {
040    
041            public static void doTag(
042                            String redirect, String modelResource,
043                            String modelResourceDescription, String resourcePrimKey, String var,
044                            PageContext pageContext)
045                    throws Exception {
046    
047                    HttpServletRequest request =
048                            (HttpServletRequest)pageContext.getRequest();
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
054    
055                    Layout layout = themeDisplay.getLayout();
056    
057                    if (Validator.isNull(redirect)) {
058                            redirect = PortalUtil.getCurrentURL(request);
059                    }
060    
061                    PortletURL portletURL = PortletURLFactoryUtil.create(
062                            request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
063                            PortletRequest.RENDER_PHASE);
064    
065                    if (themeDisplay.isStatePopUp()) {
066                            portletURL.setWindowState(LiferayWindowState.POP_UP);
067                    }
068                    else {
069                            portletURL.setWindowState(WindowState.MAXIMIZED);
070                    }
071    
072                    portletURL.setParameter(
073                            "struts_action", "/portlet_configuration/edit_permissions");
074                    portletURL.setParameter("redirect", redirect);
075    
076                    if (!themeDisplay.isStateMaximized()) {
077                            portletURL.setParameter("returnToFullPageURL", redirect);
078                    }
079    
080                    portletURL.setParameter("portletResource", portletDisplay.getId());
081                    portletURL.setParameter("modelResource", modelResource);
082                    portletURL.setParameter(
083                            "modelResourceDescription", modelResourceDescription);
084                    portletURL.setParameter("resourcePrimKey", resourcePrimKey);
085    
086                    String portletURLToString = portletURL.toString();
087    
088                    if (Validator.isNotNull(var)) {
089                            pageContext.setAttribute(var, portletURLToString);
090                    }
091                    else {
092                            pageContext.getOut().print(portletURLToString);
093                    }
094            }
095    
096            public int doEndTag() throws JspException {
097                    try {
098                            doTag(
099                                    _redirect, _modelResource, _modelResourceDescription,
100                                    _resourcePrimKey, _var, pageContext);
101                    }
102                    catch (Exception e) {
103                            throw new JspException(e);
104                    }
105    
106                    return EVAL_PAGE;
107            }
108    
109            public void setRedirect(String redirect) {
110                    _redirect = redirect;
111            }
112    
113            public void setModelResource(String modelResource) {
114                    _modelResource = modelResource;
115            }
116    
117            public void setModelResourceDescription(String modelResourceDescription) {
118                    _modelResourceDescription = modelResourceDescription;
119            }
120    
121            public void setResourcePrimKey(String resourcePrimKey) {
122                    _resourcePrimKey = resourcePrimKey;
123            }
124    
125            public void setVar(String var) {
126                    _var = var;
127            }
128    
129            private String _redirect;
130            private String _modelResource;
131            private String _modelResourceDescription;
132            private String _resourcePrimKey;
133            private String _var;
134    
135    }