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