1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.nestedportlets.action;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.LayoutTemplate;
28  import com.liferay.portal.model.LayoutTemplateConstants;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.model.Theme;
31  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.PortletPreferencesFactoryUtil;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.regex.Matcher;
41  import java.util.regex.Pattern;
42  
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletPreferences;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Berentey Zsolt
56   * @author Jorge Ferrer
57   * @author Raymond Augé
58   * @author Jesper Weissglas
59   */
60  public class ViewAction extends PortletAction {
61  
62      public ActionForward render(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              RenderRequest renderRequest, RenderResponse renderResponse)
65          throws Exception {
66  
67          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
68              WebKeys.THEME_DISPLAY);
69  
70          Portlet portlet = (Portlet)renderRequest.getAttribute(
71              WebKeys.RENDER_PORTLET);
72  
73          PortletPreferences prefs =
74              PortletPreferencesFactoryUtil.getPortletSetup(
75                  renderRequest, portlet.getPortletId());
76  
77          String layoutTemplateId = prefs.getValue(
78              "layout-template-id",
79              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
80  
81          String velocityTemplateId = StringPool.BLANK;
82          String velocityTemplateContent = StringPool.BLANK;
83  
84          Map<String, String> columnIds = new HashMap<String, String>();
85  
86          if (Validator.isNotNull(layoutTemplateId)) {
87              Theme theme = themeDisplay.getTheme();
88  
89              LayoutTemplate layoutTemplate =
90                  LayoutTemplateLocalServiceUtil.getLayoutTemplate(
91                      layoutTemplateId, false, theme.getThemeId());
92  
93              String content = layoutTemplate.getContent();
94  
95              Matcher processColumnMatcher = _processColumnPattern.matcher(
96                  content);
97  
98              while (processColumnMatcher.find()) {
99                  String columnId = processColumnMatcher.group(2);
100 
101                 if (Validator.isNotNull(columnId)) {
102                     columnIds.put(
103                         columnId,
104                         portlet.getPortletId() + StringPool.UNDERLINE +
105                             columnId);
106                 }
107             }
108 
109             processColumnMatcher.reset();
110 
111             content = processColumnMatcher.replaceAll("$1\\${$2}$3");
112 
113             velocityTemplateId =
114                 theme.getThemeId() + LayoutTemplateConstants.CUSTOM_SEPARATOR +
115                     layoutTemplateId;
116 
117             Matcher columnIdMatcher = _columnIdPattern.matcher(content);
118 
119             velocityTemplateContent = columnIdMatcher.replaceAll(
120                 "$1" + portlet.getPortletId() + "$2$3");
121         }
122 
123         renderRequest.setAttribute(
124             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_ID, velocityTemplateId);
125         renderRequest.setAttribute(
126             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_CONTENT,
127             velocityTemplateContent);
128 
129         Map<String, Object> vmVariables =
130             (Map<String, Object>)renderRequest.getAttribute(
131                 WebKeys.VM_VARIABLES);
132 
133         if (vmVariables != null) {
134             vmVariables.putAll(columnIds);
135         }
136         else {
137             renderRequest.setAttribute(WebKeys.VM_VARIABLES, columnIds);
138         }
139 
140         return mapping.findForward("portlet.nested_portlets.view");
141     }
142 
143     private static Pattern _columnIdPattern = Pattern.compile(
144         "([<].*?id=[\"'])([^ ]*?)([\"'].*?[>])", Pattern.DOTALL);
145     private static Pattern _processColumnPattern = Pattern.compile(
146         "(processColumn[(]\")(.*?)(\"[)])", Pattern.DOTALL);
147 
148 }