1
14
15 package com.liferay.portlet.journalcontent.util;
16
17 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18 import com.liferay.portal.kernel.cache.PortalCache;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.GetterUtil;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.security.permission.ActionKeys;
26 import com.liferay.portal.theme.ThemeDisplay;
27 import com.liferay.portal.util.PropsValues;
28 import com.liferay.portlet.journal.model.JournalArticleDisplay;
29 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
30 import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
31
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
35 import org.apache.commons.lang.time.StopWatch;
36
37
44 public class JournalContentImpl implements JournalContent {
45
46 public void clearCache() {
47 cache.removeAll();
48 }
49
50 public void clearCache(
51 long groupId, String articleId, String templateId) {
52
53 clearCache();
54 }
55
56 public String getContent(
57 long groupId, String articleId, String viewMode, String languageId,
58 String xmlRequest) {
59
60 return getContent(
61 groupId, articleId, null, viewMode, languageId, null, xmlRequest);
62 }
63
64 public String getContent(
65 long groupId, String articleId, String viewMode, String languageId,
66 ThemeDisplay themeDisplay) {
67
68 return getContent(
69 groupId, articleId, null, viewMode, languageId, themeDisplay);
70 }
71
72 public String getContent(
73 long groupId, String articleId, String templateId, String viewMode,
74 String languageId, String xmlRequest) {
75
76 return getContent(
77 groupId, articleId, templateId, viewMode, languageId, null,
78 xmlRequest);
79 }
80
81 public String getContent(
82 long groupId, String articleId, String templateId, String viewMode,
83 String languageId, ThemeDisplay themeDisplay) {
84
85 return getContent(
86 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
87 null);
88 }
89
90 public String getContent(
91 long groupId, String articleId, String templateId, String viewMode,
92 String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
93
94 JournalArticleDisplay articleDisplay = getDisplay(
95 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
96 1, xmlRequest);
97
98 if (articleDisplay != null) {
99 return articleDisplay.getContent();
100 }
101 else {
102 return null;
103 }
104 }
105
106 public JournalArticleDisplay getDisplay(
107 long groupId, String articleId, String viewMode, String languageId,
108 String xmlRequest) {
109
110 return getDisplay(
111 groupId, articleId, null, viewMode, languageId, null, 1,
112 xmlRequest);
113 }
114
115 public JournalArticleDisplay getDisplay(
116 long groupId, String articleId, String viewMode, String languageId,
117 ThemeDisplay themeDisplay) {
118
119 return getDisplay(
120 groupId, articleId, viewMode, languageId, themeDisplay, 1);
121 }
122
123 public JournalArticleDisplay getDisplay(
124 long groupId, String articleId, String viewMode, String languageId,
125 ThemeDisplay themeDisplay, int page) {
126
127 return getDisplay(
128 groupId, articleId, null, viewMode, languageId, themeDisplay, page,
129 null);
130 }
131
132 public JournalArticleDisplay getDisplay(
133 long groupId, String articleId, String templateId, String viewMode,
134 String languageId, String xmlRequest) {
135
136 return getDisplay(
137 groupId, articleId, templateId, viewMode, languageId, null, 1,
138 xmlRequest);
139 }
140
141 public JournalArticleDisplay getDisplay(
142 long groupId, String articleId, String templateId, String viewMode,
143 String languageId, ThemeDisplay themeDisplay) {
144
145 return getDisplay(
146 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
147 1, null);
148 }
149
150 public JournalArticleDisplay getDisplay(
151 long groupId, String articleId, String templateId, String viewMode,
152 String languageId, ThemeDisplay themeDisplay, int page,
153 String xmlRequest) {
154
155 StopWatch stopWatch = null;
156
157 if (_log.isDebugEnabled()) {
158 stopWatch = new StopWatch();
159
160 stopWatch.start();
161 }
162
163 articleId = GetterUtil.getString(articleId).toUpperCase();
164 templateId = GetterUtil.getString(templateId).toUpperCase();
165
166 boolean secure = false;
167
168 if (themeDisplay != null) {
169 secure = themeDisplay.isSecure();
170 }
171
172 String key = encodeKey(
173 groupId, articleId, templateId, viewMode, languageId, page, secure);
174
175 JournalArticleDisplay articleDisplay = (JournalArticleDisplay)cache.get(
176 key);
177
178 boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
179
180 if ((articleDisplay == null) || !lifecycleRender) {
181 articleDisplay = getArticleDisplay(
182 groupId, articleId, templateId, viewMode, languageId, page,
183 xmlRequest, themeDisplay);
184
185 if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
186 (lifecycleRender)) {
187
188 cache.put(key, articleDisplay);
189 }
190 }
191
192 try {
193 if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
194 (articleDisplay != null) && (themeDisplay != null) &&
195 (!JournalArticlePermission.contains(
196 themeDisplay.getPermissionChecker(), groupId, articleId,
197 ActionKeys.VIEW))) {
198
199 articleDisplay = null;
200 }
201 }
202 catch (Exception e) {
203 }
204
205 if (_log.isDebugEnabled()) {
206 _log.debug(
207 "getDisplay for {" + groupId + ", " + articleId + ", " +
208 templateId + ", " + viewMode + ", " + languageId + ", " +
209 page + "} takes " + stopWatch.getTime() + " ms");
210 }
211
212 return articleDisplay;
213 }
214
215 protected String encodeKey(
216 long groupId, String articleId, String templateId, String viewMode,
217 String languageId, int page, boolean secure) {
218
219 StringBundler sb = new StringBundler();
220
221 sb.append(CACHE_NAME);
222 sb.append(StringPool.POUND);
223 sb.append(groupId);
224 sb.append(ARTICLE_SEPARATOR);
225 sb.append(articleId);
226 sb.append(TEMPLATE_SEPARATOR);
227 sb.append(templateId);
228
229 if (Validator.isNotNull(viewMode)) {
230 sb.append(VIEW_MODE_SEPARATOR);
231 sb.append(viewMode);
232 }
233
234 if (Validator.isNotNull(languageId)) {
235 sb.append(LANGUAGE_SEPARATOR);
236 sb.append(languageId);
237 }
238
239 if (page > 0) {
240 sb.append(PAGE_SEPARATOR);
241 sb.append(page);
242 }
243
244 sb.append(SECURE_SEPARATOR);
245 sb.append(secure);
246
247 return sb.toString();
248 }
249
250 protected JournalArticleDisplay getArticleDisplay(
251 long groupId, String articleId, String templateId, String viewMode,
252 String languageId, int page, String xmlRequest,
253 ThemeDisplay themeDisplay) {
254
255 try {
256 if (_log.isInfoEnabled()) {
257 _log.info(
258 "Get article display {" + groupId + ", " + articleId +
259 ", " + templateId + "}");
260 }
261
262 return JournalArticleLocalServiceUtil.getArticleDisplay(
263 groupId, articleId, templateId, viewMode, languageId, page,
264 xmlRequest, themeDisplay);
265 }
266 catch (Exception e) {
267 if (_log.isWarnEnabled()) {
268 _log.warn(
269 "Unable to get display for " + groupId + " " +
270 articleId + " " + languageId);
271 }
272
273 return null;
274 }
275 }
276
277 protected boolean isLifecycleRender(
278 ThemeDisplay themeDisplay, String xmlRequest) {
279
280 if (themeDisplay != null) {
281 return themeDisplay.isLifecycleRender();
282 }
283 else if (Validator.isNotNull(xmlRequest)) {
284 Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
285
286 return matcher.find();
287 }
288 else {
289 return false;
290 }
291 }
292
293 protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
294
295 protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
296 "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
297
298 private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
299
300 }