1
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
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, String xmlRequest) {
79
80 return getContent(
81 groupId, articleId, null, languageId, null, xmlRequest);
82 }
83
84 public static String getContent(
85 long groupId, String articleId, String languageId,
86 ThemeDisplay themeDisplay) {
87
88 return getContent(groupId, articleId, null, languageId, themeDisplay);
89 }
90
91 public static String getContent(
92 long groupId, String articleId, String templateId, String languageId,
93 String xmlRequest) {
94
95 return getContent(
96 groupId, articleId, templateId, languageId, null, xmlRequest);
97 }
98
99 public static String getContent(
100 long groupId, String articleId, String templateId, String languageId,
101 ThemeDisplay themeDisplay) {
102
103 return getContent(
104 groupId, articleId, templateId, languageId, themeDisplay, null);
105 }
106
107 public static String getContent(
108 long groupId, String articleId, String templateId, String languageId,
109 ThemeDisplay themeDisplay, String xmlRequest) {
110
111 JournalArticleDisplay articleDisplay = getDisplay(
112 groupId, articleId, templateId, languageId, themeDisplay, 1,
113 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 languageId, String xmlRequest) {
125
126 return getDisplay(
127 groupId, articleId, null, languageId, null, 1, xmlRequest);
128 }
129
130 public static JournalArticleDisplay getDisplay(
131 long groupId, String articleId, String languageId,
132 ThemeDisplay themeDisplay) {
133
134 return getDisplay(
135 groupId, articleId, null, languageId, themeDisplay, 1, null);
136 }
137
138 public static JournalArticleDisplay getDisplay(
139 long groupId, String articleId, String templateId, String languageId,
140 String xmlRequest) {
141
142 return getDisplay(
143 groupId, articleId, templateId, languageId, null, 1, xmlRequest);
144 }
145
146 public static JournalArticleDisplay getDisplay(
147 long groupId, String articleId, String templateId, String languageId,
148 ThemeDisplay themeDisplay) {
149
150 return getDisplay(
151 groupId, articleId, templateId, languageId, themeDisplay, 1, null);
152 }
153
154 public static JournalArticleDisplay getDisplay(
155 long groupId, String articleId, String templateId, String languageId,
156 ThemeDisplay themeDisplay, int page, String xmlRequest) {
157
158 StopWatch stopWatch = null;
159
160 if (_log.isDebugEnabled()) {
161 stopWatch = new StopWatch();
162
163 stopWatch.start();
164 }
165
166 articleId = GetterUtil.getString(articleId).toUpperCase();
167 templateId = GetterUtil.getString(templateId).toUpperCase();
168
169 String key = _encodeKey(
170 groupId, articleId, templateId, languageId, page);
171
172 JournalArticleDisplay articleDisplay =
173 (JournalArticleDisplay)MultiVMPoolUtil.get(_cache, key);
174
175 if (articleDisplay == null) {
176 articleDisplay = _getArticleDisplay(
177 groupId, articleId, templateId, languageId, page, xmlRequest,
178 themeDisplay);
179
180 if ((articleDisplay != null) && articleDisplay.isCacheable()) {
181 String groupKey = _encodeGroupKey(
182 groupId, articleId, templateId);
183
184 MultiVMPoolUtil.put(
185 _cache, key, _groups, groupKey, articleDisplay);
186 }
187 }
188
189 if (_log.isDebugEnabled()) {
190 _log.debug(
191 "getDisplay for {" + groupId + ", " + articleId + ", " +
192 templateId + ", " + languageId + ", " + page + "} takes " +
193 stopWatch.getTime() + " ms");
194 }
195
196 return articleDisplay;
197 }
198
199 private static String _encodeGroupKey(
200 long groupId, String articleId, String templateId) {
201
202 return _encodeKey(groupId, articleId, templateId, null, 0);
203 }
204
205 private static String _encodeKey(
206 long groupId, String articleId, String templateId, String languageId,
207 int page) {
208
209 StringBuilder sb = new StringBuilder();
210
211 sb.append(CACHE_NAME);
212 sb.append(StringPool.POUND);
213 sb.append(groupId);
214 sb.append(ARTICLE_SEPARATOR);
215 sb.append(articleId);
216 sb.append(TEMPLATE_SEPARATOR);
217 sb.append(templateId);
218
219 if (Validator.isNotNull(languageId)) {
220 sb.append(LANGUAGE_SEPARATOR);
221 sb.append(languageId);
222 }
223
224 if (page > 0) {
225 sb.append(PAGE_SEPARATOR);
226 sb.append(page);
227 }
228
229 return sb.toString();
230 }
231
232 private static JournalArticleDisplay _getArticleDisplay(
233 long groupId, String articleId, String templateId, String languageId,
234 int page, String xmlRequest, ThemeDisplay themeDisplay) {
235
236 try {
237 if (_log.isInfoEnabled()) {
238 _log.info(
239 "Get article display {" + groupId + ", " + articleId +
240 ", " + templateId + "}");
241 }
242
243 return JournalArticleLocalServiceUtil.getArticleDisplay(
244 groupId, articleId, templateId, languageId, page, xmlRequest,
245 themeDisplay);
246 }
247 catch (Exception e) {
248 if (_log.isWarnEnabled()) {
249 _log.warn(
250 "Unable to get display for " + groupId + " " +
251 articleId + " " + languageId);
252 }
253
254 return null;
255 }
256 }
257
258 private static Log _log = LogFactory.getLog(JournalContentUtil.class);
259
260 private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
261
262 private static Map<String, Set<String>> _groups =
263 new ConcurrentHashMap<String, Set<String>>();
264
265 }