1   /**
2    * Copyright (c) 2000-2008 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.Portlet;
29  import com.liferay.portal.model.Theme;
30  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  
37  import java.util.HashSet;
38  import java.util.Set;
39  import java.util.regex.Matcher;
40  import java.util.regex.Pattern;
41  
42  import javax.portlet.PortletConfig;
43  import javax.portlet.PortletPreferences;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Berentey Zsolt
55   * @author Jorge Ferrer
56   *
57   */
58  public class ViewAction extends PortletAction {
59  
60      public ActionForward render(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              RenderRequest renderRequest, RenderResponse renderResponse)
63          throws Exception {
64  
65          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
66              WebKeys.THEME_DISPLAY);
67  
68          Portlet portlet = (Portlet)renderRequest.getAttribute(
69              WebKeys.RENDER_PORTLET);
70  
71          PortletPreferences prefs =
72              PortletPreferencesFactoryUtil.getPortletSetup(
73                  renderRequest, portlet.getPortletId());
74  
75          String layoutTemplateId = prefs.getValue(
76              "layout-template-id",
77              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
78  
79          String content = StringPool.BLANK;
80  
81          if (Validator.isNotNull(layoutTemplateId)) {
82              Theme theme = themeDisplay.getTheme();
83  
84              LayoutTemplate layoutTemplate =
85                  LayoutTemplateLocalServiceUtil.getLayoutTemplate(
86                      layoutTemplateId, false, theme.getThemeId());
87  
88              content = renameTemplateColumnsAndIds(
89                  layoutTemplate.getContent(), portlet);
90          }
91  
92          renderRequest.setAttribute(WebKeys.LAYOUT_TEMPLATE_CONTENT, content);
93  
94          return mapping.findForward("portlet.nested_portlets.view");
95      }
96  
97      protected String renameTemplateColumnsAndIds(
98          String content, Portlet portlet) {
99  
100         Matcher matcher = _pattern.matcher(content);
101 
102         Set<String> columnIds = new HashSet<String>();
103 
104         while (matcher.find()) {
105             if (Validator.isNotNull(matcher.group(1))) {
106                 columnIds.add(matcher.group(1));
107             }
108 
109             if (Validator.isNotNull(matcher.group(2))) {
110                 columnIds.add(matcher.group(2));
111             }
112         }
113 
114         for (String columnId : columnIds) {
115             if (columnId.indexOf(portlet.getPortletId()) == -1) {
116                 content = content.replaceAll(
117                     columnId, portlet.getPortletId() + "_" + columnId);
118             }
119         }
120 
121         return content;
122     }
123 
124     private static Pattern _pattern = Pattern.compile(
125         "processColumn[(]\"(.*?)\"[)]|[<].*?id=[\"']([^ ]*?)[\"'].*?[>]",
126         Pattern.DOTALL);
127 
128 }