1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.nestedportlets.action;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.UnicodeProperties;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.LayoutTemplate;
26  import com.liferay.portal.model.LayoutTemplateConstants;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.model.Theme;
29  import com.liferay.portal.model.impl.LayoutTypePortletImpl;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
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.Collection;
39  import java.util.HashMap;
40  import java.util.Map;
41  import java.util.regex.Matcher;
42  import java.util.regex.Pattern;
43  
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Berentey Zsolt
57   * @author Jorge Ferrer
58   * @author Raymond Augé
59   * @author Jesper Weissglas
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                         renderResponse.getNamespace() + 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" + renderResponse.getNamespace() + "$2$3");
122         }
123 
124         checkLayout(themeDisplay.getLayout(), columnIds.values());
125 
126         renderRequest.setAttribute(
127             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_ID, velocityTemplateId);
128         renderRequest.setAttribute(
129             WebKeys.NESTED_PORTLET_VELOCITY_TEMPLATE_CONTENT,
130             velocityTemplateContent);
131 
132         Map<String, Object> vmVariables =
133             (Map<String, Object>)renderRequest.getAttribute(
134                 WebKeys.VM_VARIABLES);
135 
136         if (vmVariables != null) {
137             vmVariables.putAll(columnIds);
138         }
139         else {
140             renderRequest.setAttribute(WebKeys.VM_VARIABLES, columnIds);
141         }
142 
143         return mapping.findForward("portlet.nested_portlets.view");
144     }
145 
146     protected void checkLayout(Layout layout, Collection<String> columnIds) {
147         UnicodeProperties properties = layout.getTypeSettingsProperties();
148 
149         String[] layoutColumnIds = StringUtil.split(
150             properties.get(LayoutTypePortletImpl.NESTED_COLUMN_IDS));
151 
152         boolean updateColumnIds = false;
153 
154         for (String columnId : columnIds) {
155             String portletIds = properties.getProperty(columnId);
156 
157             if (Validator.isNotNull(portletIds) &&
158                 !ArrayUtil.contains(layoutColumnIds, columnId)) {
159 
160                 layoutColumnIds = ArrayUtil.append(layoutColumnIds, columnId);
161 
162                 updateColumnIds = true;
163             }
164         }
165 
166         if (updateColumnIds) {
167             properties.setProperty(
168                 LayoutTypePortletImpl.NESTED_COLUMN_IDS,
169                 StringUtil.merge(layoutColumnIds));
170 
171             layout.setTypeSettingsProperties(properties);
172 
173             try {
174                 LayoutLocalServiceUtil.updateLayout(
175                     layout.getGroupId(), layout.isPrivateLayout(),
176                     layout.getLayoutId(), layout.getTypeSettings());
177             }
178             catch (Exception e) {
179                 if (_log.isWarnEnabled()) {
180                     _log.warn(e, e);
181                 }
182             }
183         }
184     }
185 
186     private static Log _log = LogFactoryUtil.getLog(ViewAction.class);
187 
188     private static Pattern _columnIdPattern = Pattern.compile(
189         "([<].*?id=[\"'])([^ ]*?)([\"'].*?[>])", Pattern.DOTALL);
190     private static Pattern _processColumnPattern = Pattern.compile(
191         "(processColumn[(]\")(.*?)(\"[)])", Pattern.DOTALL);
192 
193 }