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