1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journalcontent.util;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.LayoutSet;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PropsValues;
30  import com.liferay.portlet.journal.model.JournalArticleDisplay;
31  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32  import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
33  
34  import java.util.regex.Matcher;
35  import java.util.regex.Pattern;
36  
37  import org.apache.commons.lang.time.StopWatch;
38  
39  /**
40   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Raymond Augé
44   * @author Michael Young
45   */
46  public class JournalContentImpl implements JournalContent {
47  
48      public void clearCache() {
49          portalCache.removeAll();
50      }
51  
52      public void clearCache(
53          long groupId, String articleId, String templateId) {
54  
55          clearCache();
56      }
57  
58      public String getContent(
59          long groupId, String articleId, String viewMode, String languageId,
60          String xmlRequest) {
61  
62          return getContent(
63              groupId, articleId, null, viewMode, languageId, null, xmlRequest);
64      }
65  
66      public String getContent(
67          long groupId, String articleId, String viewMode, String languageId,
68          ThemeDisplay themeDisplay) {
69  
70          return getContent(
71              groupId, articleId, null, viewMode, languageId, themeDisplay);
72      }
73  
74      public String getContent(
75          long groupId, String articleId, String templateId, String viewMode,
76          String languageId, String xmlRequest) {
77  
78          return getContent(
79              groupId, articleId, templateId, viewMode, languageId, null,
80              xmlRequest);
81      }
82  
83      public String getContent(
84          long groupId, String articleId, String templateId, String viewMode,
85          String languageId, ThemeDisplay themeDisplay) {
86  
87          return getContent(
88              groupId, articleId, templateId, viewMode, languageId, themeDisplay,
89              null);
90      }
91  
92      public String getContent(
93          long groupId, String articleId, String templateId, String viewMode,
94          String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
95  
96          JournalArticleDisplay articleDisplay = getDisplay(
97              groupId, articleId, templateId, viewMode, languageId, themeDisplay,
98              1, xmlRequest);
99  
100         if (articleDisplay != null) {
101             return articleDisplay.getContent();
102         }
103         else {
104             return null;
105         }
106     }
107 
108     public JournalArticleDisplay getDisplay(
109         long groupId, String articleId, String viewMode, String languageId,
110         String xmlRequest) {
111 
112         return getDisplay(
113             groupId, articleId, null, viewMode, languageId, null, 1,
114             xmlRequest);
115     }
116 
117     public JournalArticleDisplay getDisplay(
118         long groupId, String articleId, String viewMode, String languageId,
119         ThemeDisplay themeDisplay) {
120 
121         return getDisplay(
122             groupId, articleId, viewMode, languageId, themeDisplay, 1);
123     }
124 
125     public JournalArticleDisplay getDisplay(
126         long groupId, String articleId, String viewMode, String languageId,
127         ThemeDisplay themeDisplay, int page) {
128 
129         return getDisplay(
130             groupId, articleId, null, viewMode, languageId, themeDisplay, page,
131             null);
132     }
133 
134     public JournalArticleDisplay getDisplay(
135         long groupId, String articleId, String templateId, String viewMode,
136         String languageId, String xmlRequest) {
137 
138         return getDisplay(
139             groupId, articleId, templateId, viewMode, languageId, null, 1,
140             xmlRequest);
141     }
142 
143     public JournalArticleDisplay getDisplay(
144         long groupId, String articleId, String templateId, String viewMode,
145         String languageId, ThemeDisplay themeDisplay) {
146 
147         return getDisplay(
148             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
149             1, null);
150     }
151 
152     public JournalArticleDisplay getDisplay(
153         long groupId, String articleId, String templateId, String viewMode,
154         String languageId, ThemeDisplay themeDisplay, int page,
155         String xmlRequest) {
156 
157         StopWatch stopWatch = null;
158 
159         if (_log.isDebugEnabled()) {
160             stopWatch = new StopWatch();
161 
162             stopWatch.start();
163         }
164 
165         articleId = GetterUtil.getString(articleId).toUpperCase();
166         templateId = GetterUtil.getString(templateId).toUpperCase();
167 
168         long layoutSetId = 0;
169         boolean secure = false;
170 
171         if (themeDisplay != null) {
172             try {
173                 Layout layout = themeDisplay.getLayout();
174 
175                 LayoutSet layoutSet = layout.getLayoutSet();
176 
177                 layoutSetId = layoutSet.getLayoutSetId();
178             }
179             catch (Exception e) {
180             }
181 
182             secure = themeDisplay.isSecure();
183         }
184 
185         String key = encodeKey(
186             groupId, articleId, templateId, layoutSetId, viewMode, languageId,
187             page, secure);
188 
189         JournalArticleDisplay articleDisplay =
190             (JournalArticleDisplay)portalCache.get(key);
191 
192         boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
193 
194         if ((articleDisplay == null) || !lifecycleRender) {
195             articleDisplay = getArticleDisplay(
196                 groupId, articleId, templateId, viewMode, languageId, page,
197                 xmlRequest, themeDisplay);
198 
199             if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
200                 (lifecycleRender)) {
201 
202                 portalCache.put(key, articleDisplay);
203             }
204         }
205 
206         try {
207             if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
208                 (articleDisplay != null) && (themeDisplay != null) &&
209                 (!JournalArticlePermission.contains(
210                     themeDisplay.getPermissionChecker(), groupId, articleId,
211                     ActionKeys.VIEW))) {
212 
213                 articleDisplay = null;
214             }
215         }
216         catch (Exception e) {
217         }
218 
219         if (_log.isDebugEnabled()) {
220             _log.debug(
221                 "getDisplay for {" + groupId + ", " + articleId + ", " +
222                     templateId + ", " + viewMode + ", " + languageId + ", " +
223                         page + "} takes " + stopWatch.getTime() + " ms");
224         }
225 
226         return articleDisplay;
227     }
228 
229     protected String encodeKey(
230         long groupId, String articleId, String templateId, long layoutSetId,
231         String viewMode, String languageId, int page, boolean secure) {
232 
233         StringBundler sb = new StringBundler();
234 
235         sb.append(CACHE_NAME);
236         sb.append(StringPool.POUND);
237         sb.append(groupId);
238         sb.append(ARTICLE_SEPARATOR);
239         sb.append(articleId);
240         sb.append(TEMPLATE_SEPARATOR);
241         sb.append(templateId);
242 
243         if (layoutSetId > 0) {
244             sb.append(LAYOUT_SET_SEPARATOR);
245             sb.append(layoutSetId);
246         }
247 
248         if (Validator.isNotNull(viewMode)) {
249             sb.append(VIEW_MODE_SEPARATOR);
250             sb.append(viewMode);
251         }
252 
253         if (Validator.isNotNull(languageId)) {
254             sb.append(LANGUAGE_SEPARATOR);
255             sb.append(languageId);
256         }
257 
258         if (page > 0) {
259             sb.append(PAGE_SEPARATOR);
260             sb.append(page);
261         }
262 
263         sb.append(SECURE_SEPARATOR);
264         sb.append(secure);
265 
266         return sb.toString();
267     }
268 
269     protected JournalArticleDisplay getArticleDisplay(
270         long groupId, String articleId, String templateId, String viewMode,
271         String languageId, int page, String xmlRequest,
272         ThemeDisplay themeDisplay) {
273 
274         try {
275             if (_log.isInfoEnabled()) {
276                 _log.info(
277                     "Get article display {" + groupId + ", " + articleId +
278                         ", " + templateId + "}");
279             }
280 
281             return JournalArticleLocalServiceUtil.getArticleDisplay(
282                 groupId, articleId, templateId, viewMode, languageId, page,
283                 xmlRequest, themeDisplay);
284         }
285         catch (Exception e) {
286             if (_log.isWarnEnabled()) {
287                 _log.warn(
288                     "Unable to get display for " + groupId + " " +
289                         articleId + " " + languageId);
290             }
291 
292             return null;
293         }
294     }
295 
296     protected boolean isLifecycleRender(
297         ThemeDisplay themeDisplay, String xmlRequest) {
298 
299         if (themeDisplay != null) {
300             return themeDisplay.isLifecycleRender();
301         }
302         else if (Validator.isNotNull(xmlRequest)) {
303             Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
304 
305             return matcher.find();
306         }
307         else {
308             return false;
309         }
310     }
311 
312     protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
313         "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
314     protected static PortalCache portalCache = MultiVMPoolUtil.getCache(
315         CACHE_NAME);
316 
317     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
318 
319 }