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