1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portlet.journal.model.JournalArticleDisplay;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  
35  import java.util.Map;
36  import java.util.Set;
37  import java.util.concurrent.ConcurrentHashMap;
38  
39  import org.apache.commons.lang.time.StopWatch;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="JournalContentUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Raymond Augé
48   * @author Michael Young
49   *
50   */
51  public class JournalContentUtil {
52  
53      public static final String CACHE_NAME = JournalContentUtil.class.getName();
54  
55      public static String ARTICLE_SEPARATOR = "_ARTICLE_";
56  
57      public static String TEMPLATE_SEPARATOR = "_TEMPLATE_";
58  
59      public static String LANGUAGE_SEPARATOR = "_LANGUAGE_";
60  
61      public static String PAGE_SEPARATOR = "_PAGE_";
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          articleId = GetterUtil.getString(articleId).toUpperCase();
71          templateId = GetterUtil.getString(templateId).toUpperCase();
72  
73          String groupKey = _encodeGroupKey(groupId, articleId, templateId);
74  
75          MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
76      }
77  
78      public static String getContent(
79          long groupId, String articleId, String languageId,
80          ThemeDisplay themeDisplay) {
81  
82          return getContent(groupId, articleId, null, languageId, themeDisplay);
83      }
84  
85      public static String getContent(
86          long groupId, String articleId, String templateId, String languageId,
87          ThemeDisplay themeDisplay) {
88  
89          JournalArticleDisplay articleDisplay = getDisplay(
90              groupId, articleId, templateId, languageId, themeDisplay);
91  
92          if (articleDisplay != null) {
93              return articleDisplay.getContent();
94          }
95          else {
96              return null;
97          }
98      }
99  
100     public static JournalArticleDisplay getDisplay(
101         long groupId, String articleId, String templateId, String languageId,
102         ThemeDisplay themeDisplay) {
103 
104         return getDisplay(
105             groupId, articleId, templateId, languageId, themeDisplay, 1, null);
106     }
107 
108     public static JournalArticleDisplay getDisplay(
109         long groupId, String articleId, String templateId, String languageId,
110         ThemeDisplay themeDisplay, int page, String xmlRequest) {
111 
112         StopWatch stopWatch = null;
113 
114         if (_log.isDebugEnabled()) {
115             stopWatch = new StopWatch();
116 
117             stopWatch.start();
118         }
119 
120         articleId = GetterUtil.getString(articleId).toUpperCase();
121         templateId = GetterUtil.getString(templateId).toUpperCase();
122 
123         String key = _encodeKey(
124             groupId, articleId, templateId, languageId, page);
125 
126         JournalArticleDisplay articleDisplay =
127             (JournalArticleDisplay)MultiVMPoolUtil.get(_cache, key);
128 
129         if (articleDisplay == null) {
130             articleDisplay = _getArticleDisplay(
131                 groupId, articleId, templateId, languageId, page, xmlRequest,
132                 themeDisplay);
133 
134             if ((articleDisplay != null) && articleDisplay.isCacheable()) {
135                 String groupKey = _encodeGroupKey(
136                     groupId, articleId, templateId);
137 
138                 MultiVMPoolUtil.put(
139                     _cache, key, _groups, groupKey, articleDisplay);
140             }
141         }
142 
143         if (_log.isDebugEnabled()) {
144             _log.debug(
145                 "getDisplay for {" + groupId + ", " + articleId + ", " +
146                     templateId + ", " + languageId + ", " + page + "} takes " +
147                         stopWatch.getTime() + " ms");
148         }
149 
150         return articleDisplay;
151     }
152 
153     private static String _encodeGroupKey(
154         long groupId, String articleId, String templateId) {
155 
156         return _encodeKey(groupId, articleId, templateId, null, 0);
157     }
158 
159     private static String _encodeKey(
160         long groupId, String articleId, String templateId, String languageId,
161         int page) {
162 
163         StringMaker sm = new StringMaker();
164 
165         sm.append(CACHE_NAME);
166         sm.append(StringPool.POUND);
167         sm.append(groupId);
168         sm.append(ARTICLE_SEPARATOR);
169         sm.append(articleId);
170         sm.append(TEMPLATE_SEPARATOR);
171         sm.append(templateId);
172 
173         if (Validator.isNotNull(languageId)) {
174             sm.append(LANGUAGE_SEPARATOR);
175             sm.append(languageId);
176         }
177 
178         if (page > 0) {
179             sm.append(PAGE_SEPARATOR);
180             sm.append(page);
181         }
182 
183         return sm.toString();
184     }
185 
186     private static JournalArticleDisplay _getArticleDisplay(
187         long groupId, String articleId, String templateId, String languageId,
188         int page, String xmlRequest, ThemeDisplay themeDisplay) {
189 
190         try {
191             if (_log.isInfoEnabled()) {
192                 _log.info(
193                     "Get article display {" + groupId + ", " + articleId +
194                         ", " + templateId + "}");
195             }
196 
197             return JournalArticleLocalServiceUtil.getArticleDisplay(
198                 groupId, articleId, templateId, languageId, page, xmlRequest,
199                 themeDisplay);
200         }
201         catch (Exception e) {
202             if (_log.isWarnEnabled()) {
203                 _log.warn(
204                     "Unable to get display for " + groupId + " " +
205                         articleId + " " + languageId);
206             }
207 
208             return null;
209         }
210     }
211 
212     private static Log _log = LogFactory.getLog(JournalContentUtil.class);
213 
214     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
215 
216     private static Map<String, Set<String>> _groups =
217         new ConcurrentHashMap<String, Set<String>>();
218 
219 }