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.Iterator;
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   *
58   */
59  public class ViewAction extends PortletAction {
60  
61      public ActionForward render(
62              ActionMapping mapping, ActionForm form, PortletConfig config,
63              RenderRequest req, RenderResponse res)
64          throws Exception {
65  
66          ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
67              WebKeys.THEME_DISPLAY);
68  
69          Portlet portlet = (Portlet)req.getAttribute(WebKeys.RENDER_PORTLET);
70  
71          PortletPreferences prefs =
72              PortletPreferencesFactoryUtil.getPortletSetup(
73                  req, portlet.getPortletId(), true, true);
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                  LayoutTemplateLocalUtil.getLayoutTemplate(
86                      layoutTemplateId, false, theme.getThemeId());
87  
88              content = renameTemplateColumnsAndIds(
89                  layoutTemplate.getContent(), portlet);
90          }
91  
92          req.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 m = _searchColumnsAndIdsPattern.matcher(content);
101 
102         Set columnIds = new HashSet();
103 
104         while (m.find()) {
105             if (Validator.isNotNull(m.group(1))) {
106                 columnIds.add(m.group(1));
107             }
108 
109             if (Validator.isNotNull(m.group(2))) {
110                 columnIds.add(m.group(2));
111             }
112         }
113 
114         Iterator itr = columnIds.iterator();
115 
116         while (itr.hasNext()) {
117             String columnId = (String)itr.next();
118 
119             if (columnId.indexOf(portlet.getPortletId()) == -1) {
120                 content = content.replaceAll(
121                     columnId, portlet.getPortletId() + "_" + columnId);
122             }
123         }
124 
125         return content;
126     }
127 
128     private static final Pattern _searchColumnsAndIdsPattern = Pattern.compile(
129         "processColumn[(]\"(.*?)\"[)]|[<].*?id=[\"']([^ ]*?)[\"'].*?[>]",
130         Pattern.DOTALL);
131 
132 }