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.impl.LayoutTemplateLocalUtil;
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 config,
62              RenderRequest req, RenderResponse res)
63          throws Exception {
64  
65          ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
66              WebKeys.THEME_DISPLAY);
67  
68          Portlet portlet = (Portlet)req.getAttribute(WebKeys.RENDER_PORTLET);
69  
70          PortletPreferences prefs =
71              PortletPreferencesFactoryUtil.getPortletSetup(
72                  req, portlet.getPortletId());
73  
74          String layoutTemplateId = prefs.getValue(
75              "layout-template-id",
76              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
77  
78          String content = StringPool.BLANK;
79  
80          if (Validator.isNotNull(layoutTemplateId)) {
81              Theme theme = themeDisplay.getTheme();
82  
83              LayoutTemplate layoutTemplate =
84                  LayoutTemplateLocalUtil.getLayoutTemplate(
85                      layoutTemplateId, false, theme.getThemeId());
86  
87              content = renameTemplateColumnsAndIds(
88                  layoutTemplate.getContent(), portlet);
89          }
90  
91          req.setAttribute(WebKeys.LAYOUT_TEMPLATE_CONTENT, content);
92  
93          return mapping.findForward("portlet.nested_portlets.view");
94      }
95  
96      protected String renameTemplateColumnsAndIds(
97          String content, Portlet portlet) {
98  
99          Matcher m = _searchColumnsAndIdsPattern.matcher(content);
100 
101         Set<String> columnIds = new HashSet<String>();
102 
103         while (m.find()) {
104             if (Validator.isNotNull(m.group(1))) {
105                 columnIds.add(m.group(1));
106             }
107 
108             if (Validator.isNotNull(m.group(2))) {
109                 columnIds.add(m.group(2));
110             }
111         }
112 
113         for (String columnId : columnIds) {
114             if (columnId.indexOf(portlet.getPortletId()) == -1) {
115                 content = content.replaceAll(
116                     columnId, portlet.getPortletId() + "_" + columnId);
117             }
118         }
119 
120         return content;
121     }
122 
123     private static final Pattern _searchColumnsAndIdsPattern = Pattern.compile(
124         "processColumn[(]\"(.*?)\"[)]|[<].*?id=[\"']([^ ]*?)[\"'].*?[>]",
125         Pattern.DOTALL);
126 
127 }