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   */
61  public class ViewAction extends PortletAction {
62  
63      public ActionForward render(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              RenderRequest renderRequest, RenderResponse renderResponse)
66          throws Exception {
67  
68          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
69              WebKeys.THEME_DISPLAY);
70  
71          Portlet portlet = (Portlet)renderRequest.getAttribute(
72              WebKeys.RENDER_PORTLET);
73  
74          PortletPreferences preferences =
75              PortletPreferencesFactoryUtil.getPortletSetup(
76                  renderRequest, portlet.getPortletId());
77  
78          String layoutTemplateId = preferences.getValue(
79              "layout-template-id",
80              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
81  
82          String velocityTemplateId = StringPool.BLANK;
83          String velocityTemplateContent = StringPool.BLANK;
84  
85          Map<String, String> columnIds = new HashMap<String, String>();
86  
87          if (Validator.isNotNull(layoutTemplateId)) {
88              Theme theme = themeDisplay.getTheme();
89  
90              LayoutTemplate layoutTemplate =
91                  LayoutTemplateLocalServiceUtil.getLayoutTemplate(
92                      layoutTemplateId, false, theme.getThemeId());
93  
94              String content = layoutTemplate.getContent();
95  
96              Matcher processColumnMatcher = _processColumnPattern.matcher(
97                  content);
98  
99              while (processColumnMatcher.find()) {
100                 String columnId = processColumnMatcher.group(2);
101 
102                 if (Validator.isNotNull(columnId)) {
103                     columnIds.put(
104                         columnId,
105                         portlet.getPortletId() + StringPool.UNDERLINE +
106                             columnId);
107                 }
108             }
109 
110             processColumnMatcher.reset();
111 
112             content = processColumnMatcher.replaceAll("$1\\${$2}$3");
113 
114             velocityTemplateId =
115                 theme.getThemeId() + LayoutTemplateConstants.CUSTOM_SEPARATOR +
116                     layoutTemplateId;
117 
118             Matcher columnIdMatcher = _columnIdPattern.matcher(content);
119 
120             velocityTemplateContent = columnIdMatcher.replaceAll(
121                 "$1" + portlet.getPortletId() + "$2$3");
122         }
123 
124         renderRequest.setAttribute(
125             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_ID, velocityTemplateId);
126         renderRequest.setAttribute(
127             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_CONTENT,
128             velocityTemplateContent);
129 
130         Map<String, Object> vmVariables =
131             (Map<String, Object>)renderRequest.getAttribute(
132                 WebKeys.VM_VARIABLES);
133 
134         if (vmVariables != null) {
135             vmVariables.putAll(columnIds);
136         }
137         else {
138             renderRequest.setAttribute(WebKeys.VM_VARIABLES, columnIds);
139         }
140 
141         return mapping.findForward("portlet.nested_portlets.view");
142     }
143 
144     private static Pattern _columnIdPattern = Pattern.compile(
145         "([<].*?id=[\"'])([^ ]*?)([\"'].*?[>])", Pattern.DOTALL);
146     private static Pattern _processColumnPattern = Pattern.compile(
147         "(processColumn[(]\")(.*?)(\"[)])", Pattern.DOTALL);
148 
149 }