1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.taglib.security;
16  
17  import com.liferay.portal.kernel.portlet.LiferayWindowState;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.theme.PortletDisplay;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.PortletURLFactoryUtil;
26  
27  import javax.portlet.PortletRequest;
28  import javax.portlet.PortletURL;
29  import javax.portlet.WindowState;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.PageContext;
34  import javax.servlet.jsp.tagext.TagSupport;
35  
36  /**
37   * <a href="PermissionsURLTag.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class PermissionsURLTag extends TagSupport {
42  
43      public static String doTag(
44              String redirect, String modelResource,
45              String modelResourceDescription, String resourcePrimKey, String var,
46              boolean writeOutput, PageContext pageContext)
47          throws Exception {
48  
49          HttpServletRequest request =
50              (HttpServletRequest)pageContext.getRequest();
51  
52          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
53              WebKeys.THEME_DISPLAY);
54  
55          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
56  
57          Layout layout = themeDisplay.getLayout();
58  
59          if (Validator.isNull(redirect)) {
60              redirect = PortalUtil.getCurrentURL(request);
61          }
62  
63          PortletURL portletURL = PortletURLFactoryUtil.create(
64              request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
65              PortletRequest.RENDER_PHASE);
66  
67          if (themeDisplay.isStatePopUp()) {
68              portletURL.setWindowState(LiferayWindowState.POP_UP);
69          }
70          else {
71              portletURL.setWindowState(WindowState.MAXIMIZED);
72          }
73  
74          portletURL.setParameter(
75              "struts_action", "/portlet_configuration/edit_permissions");
76          portletURL.setParameter("redirect", redirect);
77  
78          if (!themeDisplay.isStateMaximized()) {
79              portletURL.setParameter("returnToFullPageURL", redirect);
80          }
81  
82          portletURL.setParameter("portletResource", portletDisplay.getId());
83          portletURL.setParameter("modelResource", modelResource);
84          portletURL.setParameter(
85              "modelResourceDescription", modelResourceDescription);
86          portletURL.setParameter("resourcePrimKey", resourcePrimKey);
87  
88          String portletURLToString = portletURL.toString();
89  
90          if (Validator.isNotNull(var)) {
91              pageContext.setAttribute(var, portletURLToString);
92          }
93          else if (writeOutput) {
94              pageContext.getOut().print(portletURLToString);
95          }
96  
97          return portletURLToString;
98      }
99  
100     public int doEndTag() throws JspException {
101         try {
102             doTag(
103                 _redirect, _modelResource, _modelResourceDescription,
104                 _resourcePrimKey, _var, true, pageContext);
105         }
106         catch (Exception e) {
107             throw new JspException(e);
108         }
109 
110         return EVAL_PAGE;
111     }
112 
113     public void setRedirect(String redirect) {
114         _redirect = redirect;
115     }
116 
117     public void setModelResource(String modelResource) {
118         _modelResource = modelResource;
119     }
120 
121     public void setModelResourceDescription(String modelResourceDescription) {
122         _modelResourceDescription = modelResourceDescription;
123     }
124 
125     public void setResourcePrimKey(String resourcePrimKey) {
126         _resourcePrimKey = resourcePrimKey;
127     }
128 
129     public void setVar(String var) {
130         _var = var;
131     }
132 
133     private String _redirect;
134     private String _modelResource;
135     private String _modelResourceDescription;
136     private String _resourcePrimKey;
137     private String _var;
138 
139 }