1
19
20 package com.liferay.portlet.journalcontent.util;
21
22 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
23 import com.liferay.portal.kernel.cache.PortalCache;
24 import com.liferay.portal.kernel.log.Log;
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.theme.ThemeDisplay;
30 import com.liferay.portlet.journal.model.JournalArticleDisplay;
31 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32
33 import org.apache.commons.lang.time.StopWatch;
34
35
43 public class JournalContentUtil {
44
45 public static final String CACHE_NAME = JournalContentUtil.class.getName();
46
47 public static String ARTICLE_SEPARATOR = "_ARTICLE_";
48
49 public static String TEMPLATE_SEPARATOR = "_TEMPLATE_";
50
51 public static String LANGUAGE_SEPARATOR = "_LANGUAGE_";
52
53 public static String PAGE_SEPARATOR = "_PAGE_";
54
55 public static void clearCache() {
56 cache.removeAll();
57 }
58
59 public static void clearCache(
60 long groupId, String articleId, String templateId) {
61
62 clearCache();
63 }
64
65 public static String getContent(
66 long groupId, String articleId, String languageId, String xmlRequest) {
67
68 return getContent(
69 groupId, articleId, null, languageId, null, xmlRequest);
70 }
71
72 public static String getContent(
73 long groupId, String articleId, String languageId,
74 ThemeDisplay themeDisplay) {
75
76 return getContent(groupId, articleId, null, languageId, themeDisplay);
77 }
78
79 public static String getContent(
80 long groupId, String articleId, String templateId, String languageId,
81 String xmlRequest) {
82
83 return getContent(
84 groupId, articleId, templateId, languageId, null, xmlRequest);
85 }
86
87 public static String getContent(
88 long groupId, String articleId, String templateId, String languageId,
89 ThemeDisplay themeDisplay) {
90
91 return getContent(
92 groupId, articleId, templateId, languageId, themeDisplay, null);
93 }
94
95 public static String getContent(
96 long groupId, String articleId, String templateId, String languageId,
97 ThemeDisplay themeDisplay, String xmlRequest) {
98
99 JournalArticleDisplay articleDisplay = getDisplay(
100 groupId, articleId, templateId, languageId, themeDisplay, 1,
101 xmlRequest);
102
103 if (articleDisplay != null) {
104 return articleDisplay.getContent();
105 }
106 else {
107 return null;
108 }
109 }
110
111 public static JournalArticleDisplay getDisplay(
112 long groupId, String articleId, String languageId, String xmlRequest) {
113
114 return getDisplay(
115 groupId, articleId, null, languageId, null, 1, xmlRequest);
116 }
117
118 public static JournalArticleDisplay getDisplay(
119 long groupId, String articleId, String languageId,
120 ThemeDisplay themeDisplay) {
121
122 return getDisplay(
123 groupId, articleId, null, languageId, themeDisplay, 1, null);
124 }
125
126 public static JournalArticleDisplay getDisplay(
127 long groupId, String articleId, String templateId, String languageId,
128 String xmlRequest) {
129
130 return getDisplay(
131 groupId, articleId, templateId, languageId, null, 1, xmlRequest);
132 }
133
134 public static JournalArticleDisplay getDisplay(
135 long groupId, String articleId, String templateId, String languageId,
136 ThemeDisplay themeDisplay) {
137
138 return getDisplay(
139 groupId, articleId, templateId, languageId, themeDisplay, 1, null);
140 }
141
142 public static JournalArticleDisplay getDisplay(
143 long groupId, String articleId, String templateId, String languageId,
144 ThemeDisplay themeDisplay, int page, String xmlRequest) {
145
146 StopWatch stopWatch = null;
147
148 if (_log.isDebugEnabled()) {
149 stopWatch = new StopWatch();
150
151 stopWatch.start();
152 }
153
154 articleId = GetterUtil.getString(articleId).toUpperCase();
155 templateId = GetterUtil.getString(templateId).toUpperCase();
156
157 String key = encodeKey(
158 groupId, articleId, templateId, languageId, page);
159
160 JournalArticleDisplay articleDisplay =
161 (JournalArticleDisplay)MultiVMPoolUtil.get(cache, key);
162
163 if (articleDisplay == null) {
164 articleDisplay = getArticleDisplay(
165 groupId, articleId, templateId, languageId, page, xmlRequest,
166 themeDisplay);
167
168 if ((articleDisplay != null) && articleDisplay.isCacheable()) {
169 MultiVMPoolUtil.put(cache, key, articleDisplay);
170 }
171 }
172
173 if (_log.isDebugEnabled()) {
174 _log.debug(
175 "getDisplay for {" + groupId + ", " + articleId + ", " +
176 templateId + ", " + languageId + ", " + page + "} takes " +
177 stopWatch.getTime() + " ms");
178 }
179
180 return articleDisplay;
181 }
182
183 protected static String encodeKey(
184 long groupId, String articleId, String templateId, String languageId,
185 int page) {
186
187 StringBuilder sb = new StringBuilder();
188
189 sb.append(CACHE_NAME);
190 sb.append(StringPool.POUND);
191 sb.append(groupId);
192 sb.append(ARTICLE_SEPARATOR);
193 sb.append(articleId);
194 sb.append(TEMPLATE_SEPARATOR);
195 sb.append(templateId);
196
197 if (Validator.isNotNull(languageId)) {
198 sb.append(LANGUAGE_SEPARATOR);
199 sb.append(languageId);
200 }
201
202 if (page > 0) {
203 sb.append(PAGE_SEPARATOR);
204 sb.append(page);
205 }
206
207 return sb.toString();
208 }
209
210 protected static JournalArticleDisplay getArticleDisplay(
211 long groupId, String articleId, String templateId, String languageId,
212 int page, String xmlRequest, ThemeDisplay themeDisplay) {
213
214 try {
215 if (_log.isInfoEnabled()) {
216 _log.info(
217 "Get article display {" + groupId + ", " + articleId +
218 ", " + templateId + "}");
219 }
220
221 return JournalArticleLocalServiceUtil.getArticleDisplay(
222 groupId, articleId, templateId, languageId, page, xmlRequest,
223 themeDisplay);
224 }
225 catch (Exception e) {
226 if (_log.isWarnEnabled()) {
227 _log.warn(
228 "Unable to get display for " + groupId + " " +
229 articleId + " " + languageId);
230 }
231
232 return null;
233 }
234 }
235
236 protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
237
238 private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
239
240 }