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.HashSet;
39  import java.util.Set;
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   *
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 preferences =
74              PortletPreferencesFactoryUtil.getPortletSetup(
75                  renderRequest, portlet.getPortletId());
76  
77          String layoutTemplateId = preferences.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          if (Validator.isNotNull(layoutTemplateId)) {
85              Theme theme = themeDisplay.getTheme();
86  
87              LayoutTemplate layoutTemplate =
88                  LayoutTemplateLocalServiceUtil.getLayoutTemplate(
89                      layoutTemplateId, false, theme.getThemeId());
90  
91              velocityTemplateId =
92                  theme.getThemeId() + LayoutTemplateConstants.CUSTOM_SEPARATOR +
93                      layoutTemplateId;
94              velocityTemplateContent = renameTemplateColumnsAndIds(
95                  layoutTemplate.getContent(), portlet);
96          }
97  
98          renderRequest.setAttribute(
99              WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_ID, velocityTemplateId);
100         renderRequest.setAttribute(
101             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_CONTENT,
102             velocityTemplateContent);
103 
104         return mapping.findForward("portlet.nested_portlets.view");
105     }
106 
107     protected String renameTemplateColumnsAndIds(
108         String content, Portlet portlet) {
109 
110         Matcher matcher = _pattern.matcher(content);
111 
112         Set<String> columnIds = new HashSet<String>();
113 
114         while (matcher.find()) {
115             if (Validator.isNotNull(matcher.group(1))) {
116                 columnIds.add(matcher.group(1));
117             }
118 
119             if (Validator.isNotNull(matcher.group(2))) {
120                 columnIds.add(matcher.group(2));
121             }
122         }
123 
124         for (String columnId : columnIds) {
125             if (columnId.indexOf(portlet.getPortletId()) == -1) {
126                 content = content.replaceAll(
127                     columnId, portlet.getPortletId() + "_" + columnId);
128             }
129         }
130 
131         return content;
132     }
133 
134     private static Pattern _pattern = Pattern.compile(
135         "processColumn[(]\"(.*?)\"[)]|[<].*?id=[\"']([^ ]*?)[\"'].*?[>]",
136         Pattern.DOTALL);
137 
138 }