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.journalcontent.util;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portlet.journal.model.JournalArticleDisplay;
36  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
37  import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
38  
39  import java.util.regex.Matcher;
40  import java.util.regex.Pattern;
41  
42  import org.apache.commons.lang.time.StopWatch;
43  
44  /**
45   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Raymond Augé
49   * @author Michael Young
50   *
51   */
52  public class JournalContentImpl implements JournalContent {
53  
54      public void clearCache() {
55          cache.removeAll();
56      }
57  
58      public void clearCache(
59          long groupId, String articleId, String templateId) {
60  
61          clearCache();
62      }
63  
64      public String getContent(
65          long groupId, String articleId, String viewMode, String languageId,
66          String xmlRequest) {
67  
68          return getContent(
69              groupId, articleId, null, viewMode, languageId, null, xmlRequest);
70      }
71  
72      public String getContent(
73          long groupId, String articleId, String viewMode, String languageId,
74          ThemeDisplay themeDisplay) {
75  
76          return getContent(
77              groupId, articleId, null, viewMode, languageId, themeDisplay);
78      }
79  
80      public String getContent(
81          long groupId, String articleId, String templateId, String viewMode,
82          String languageId, String xmlRequest) {
83  
84          return getContent(
85              groupId, articleId, templateId, viewMode, languageId, null,
86              xmlRequest);
87      }
88  
89      public String getContent(
90          long groupId, String articleId, String templateId, String viewMode,
91          String languageId, ThemeDisplay themeDisplay) {
92  
93          return getContent(
94              groupId, articleId, templateId, viewMode, languageId, themeDisplay,
95              null);
96      }
97  
98      public String getContent(
99          long groupId, String articleId, String templateId, String viewMode,
100         String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
101 
102         JournalArticleDisplay articleDisplay = getDisplay(
103             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
104             1, xmlRequest);
105 
106         if (articleDisplay != null) {
107             return articleDisplay.getContent();
108         }
109         else {
110             return null;
111         }
112     }
113 
114     public JournalArticleDisplay getDisplay(
115         long groupId, String articleId, String viewMode, String languageId,
116         String xmlRequest) {
117 
118         return getDisplay(
119             groupId, articleId, null, viewMode, languageId, null, 1,
120             xmlRequest);
121     }
122 
123     public JournalArticleDisplay getDisplay(
124         long groupId, String articleId, String viewMode, String languageId,
125         ThemeDisplay themeDisplay) {
126 
127         return getDisplay(
128             groupId, articleId, viewMode, languageId, themeDisplay, 1);
129     }
130 
131     public JournalArticleDisplay getDisplay(
132         long groupId, String articleId, String viewMode, String languageId,
133         ThemeDisplay themeDisplay, int page) {
134 
135         return getDisplay(
136             groupId, articleId, null, viewMode, languageId, themeDisplay, page,
137             null);
138     }
139 
140     public JournalArticleDisplay getDisplay(
141         long groupId, String articleId, String templateId, String viewMode,
142         String languageId, String xmlRequest) {
143 
144         return getDisplay(
145             groupId, articleId, templateId, viewMode, languageId, null, 1,
146             xmlRequest);
147     }
148 
149     public JournalArticleDisplay getDisplay(
150         long groupId, String articleId, String templateId, String viewMode,
151         String languageId, ThemeDisplay themeDisplay) {
152 
153         return getDisplay(
154             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
155             1, null);
156     }
157 
158     public JournalArticleDisplay getDisplay(
159         long groupId, String articleId, String templateId, String viewMode,
160         String languageId, ThemeDisplay themeDisplay, int page,
161         String xmlRequest) {
162 
163         StopWatch stopWatch = null;
164 
165         if (_log.isDebugEnabled()) {
166             stopWatch = new StopWatch();
167 
168             stopWatch.start();
169         }
170 
171         articleId = GetterUtil.getString(articleId).toUpperCase();
172         templateId = GetterUtil.getString(templateId).toUpperCase();
173 
174         String key = encodeKey(
175             groupId, articleId, templateId, viewMode, languageId, page);
176 
177         JournalArticleDisplay articleDisplay =
178             (JournalArticleDisplay)MultiVMPoolUtil.get(cache, key);
179 
180         boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
181 
182         if ((articleDisplay == null) || !lifecycleRender) {
183             articleDisplay = getArticleDisplay(
184                 groupId, articleId, templateId, viewMode, languageId, page,
185                 xmlRequest, themeDisplay);
186 
187             if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
188                 (lifecycleRender)) {
189 
190                 MultiVMPoolUtil.put(cache, key, articleDisplay);
191             }
192         }
193 
194         try {
195             if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
196                 (articleDisplay != null) && (themeDisplay != null) &&
197                 (!JournalArticlePermission.contains(
198                     themeDisplay.getPermissionChecker(), groupId, articleId,
199                     ActionKeys.VIEW))) {
200 
201                 articleDisplay = null;
202             }
203         }
204         catch (Exception e) {
205         }
206 
207         if (_log.isDebugEnabled()) {
208             _log.debug(
209                 "getDisplay for {" + groupId + ", " + articleId + ", " +
210                     templateId + ", " + viewMode + ", " + languageId + ", " +
211                         page + "} takes " + stopWatch.getTime() + " ms");
212         }
213 
214         return articleDisplay;
215     }
216 
217     protected String encodeKey(
218         long groupId, String articleId, String templateId, String viewMode,
219         String languageId, int page) {
220 
221         StringBuilder sb = new StringBuilder();
222 
223         sb.append(CACHE_NAME);
224         sb.append(StringPool.POUND);
225         sb.append(groupId);
226         sb.append(ARTICLE_SEPARATOR);
227         sb.append(articleId);
228         sb.append(TEMPLATE_SEPARATOR);
229         sb.append(templateId);
230 
231         if (Validator.isNotNull(viewMode)) {
232             sb.append(VIEW_MODE_SEPARATOR);
233             sb.append(viewMode);
234         }
235 
236         if (Validator.isNotNull(languageId)) {
237             sb.append(LANGUAGE_SEPARATOR);
238             sb.append(languageId);
239         }
240 
241         if (page > 0) {
242             sb.append(PAGE_SEPARATOR);
243             sb.append(page);
244         }
245 
246         return sb.toString();
247     }
248 
249     protected JournalArticleDisplay getArticleDisplay(
250         long groupId, String articleId, String templateId, String viewMode,
251         String languageId, int page, String xmlRequest,
252         ThemeDisplay themeDisplay) {
253 
254         try {
255             if (_log.isInfoEnabled()) {
256                 _log.info(
257                     "Get article display {" + groupId + ", " + articleId +
258                         ", " + templateId + "}");
259             }
260 
261             return JournalArticleLocalServiceUtil.getArticleDisplay(
262                 groupId, articleId, templateId, viewMode, languageId, page,
263                 xmlRequest, themeDisplay);
264         }
265         catch (Exception e) {
266             if (_log.isWarnEnabled()) {
267                 _log.warn(
268                     "Unable to get display for " + groupId + " " +
269                         articleId + " " + languageId);
270             }
271 
272             return null;
273         }
274     }
275 
276     protected boolean isLifecycleRender(
277         ThemeDisplay themeDisplay, String xmlRequest) {
278 
279         if (themeDisplay != null) {
280             return themeDisplay.isLifecycleRender();
281         }
282         else if (Validator.isNotNull(xmlRequest)) {
283             Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
284 
285             return matcher.find();
286         }
287         else {
288             return false;
289         }
290     }
291 
292     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
293 
294     protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
295         "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
296 
297     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
298 
299 }