001
014
015 package com.liferay.portlet.journalcontent.util;
016
017 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018 import com.liferay.portal.kernel.cache.PortalCache;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.LayoutSet;
027 import com.liferay.portal.security.permission.ActionKeys;
028 import com.liferay.portal.theme.ThemeDisplay;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.portlet.journal.model.JournalArticleDisplay;
031 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
032 import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
033
034 import java.util.regex.Matcher;
035 import java.util.regex.Pattern;
036
037 import org.apache.commons.lang.time.StopWatch;
038
039
044 public class JournalContentImpl implements JournalContent {
045
046 public void clearCache() {
047 portalCache.removeAll();
048 }
049
050 public void clearCache(
051 long groupId, String articleId, String templateId) {
052
053 clearCache();
054 }
055
056 public String getContent(
057 long groupId, String articleId, String viewMode, String languageId,
058 String xmlRequest) {
059
060 return getContent(
061 groupId, articleId, null, viewMode, languageId, null, xmlRequest);
062 }
063
064 public String getContent(
065 long groupId, String articleId, String viewMode, String languageId,
066 ThemeDisplay themeDisplay) {
067
068 return getContent(
069 groupId, articleId, null, viewMode, languageId, themeDisplay);
070 }
071
072 public String getContent(
073 long groupId, String articleId, String templateId, String viewMode,
074 String languageId, String xmlRequest) {
075
076 return getContent(
077 groupId, articleId, templateId, viewMode, languageId, null,
078 xmlRequest);
079 }
080
081 public String getContent(
082 long groupId, String articleId, String templateId, String viewMode,
083 String languageId, ThemeDisplay themeDisplay) {
084
085 return getContent(
086 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
087 null);
088 }
089
090 public String getContent(
091 long groupId, String articleId, String templateId, String viewMode,
092 String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
093
094 JournalArticleDisplay articleDisplay = getDisplay(
095 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
096 1, xmlRequest);
097
098 if (articleDisplay != null) {
099 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 long layoutSetId = 0;
167 boolean secure = false;
168
169 if (themeDisplay != null) {
170 try {
171 Layout layout = themeDisplay.getLayout();
172
173 LayoutSet layoutSet = layout.getLayoutSet();
174
175 layoutSetId = layoutSet.getLayoutSetId();
176 }
177 catch (Exception e) {
178 }
179
180 secure = themeDisplay.isSecure();
181 }
182
183 String key = encodeKey(
184 groupId, articleId, templateId, layoutSetId, viewMode, languageId,
185 page, secure);
186
187 JournalArticleDisplay articleDisplay =
188 (JournalArticleDisplay)portalCache.get(key);
189
190 boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
191
192 if ((articleDisplay == null) || !lifecycleRender) {
193 articleDisplay = getArticleDisplay(
194 groupId, articleId, templateId, viewMode, languageId, page,
195 xmlRequest, themeDisplay);
196
197 if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
198 (lifecycleRender)) {
199
200 portalCache.put(key, articleDisplay);
201 }
202 }
203
204 try {
205 if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
206 (articleDisplay != null) && (themeDisplay != null) &&
207 (!JournalArticlePermission.contains(
208 themeDisplay.getPermissionChecker(), groupId, articleId,
209 ActionKeys.VIEW))) {
210
211 articleDisplay = null;
212 }
213 }
214 catch (Exception e) {
215 }
216
217 if (_log.isDebugEnabled()) {
218 _log.debug(
219 "getDisplay for {" + groupId + ", " + articleId + ", " +
220 templateId + ", " + viewMode + ", " + languageId + ", " +
221 page + "} takes " + stopWatch.getTime() + " ms");
222 }
223
224 return articleDisplay;
225 }
226
227 protected String encodeKey(
228 long groupId, String articleId, String templateId, long layoutSetId,
229 String viewMode, String languageId, int page, boolean secure) {
230
231 StringBundler sb = new StringBundler();
232
233 sb.append(CACHE_NAME);
234 sb.append(StringPool.POUND);
235 sb.append(groupId);
236 sb.append(ARTICLE_SEPARATOR);
237 sb.append(articleId);
238 sb.append(TEMPLATE_SEPARATOR);
239 sb.append(templateId);
240
241 if (layoutSetId > 0) {
242 sb.append(LAYOUT_SET_SEPARATOR);
243 sb.append(layoutSetId);
244 }
245
246 if (Validator.isNotNull(viewMode)) {
247 sb.append(VIEW_MODE_SEPARATOR);
248 sb.append(viewMode);
249 }
250
251 if (Validator.isNotNull(languageId)) {
252 sb.append(LANGUAGE_SEPARATOR);
253 sb.append(languageId);
254 }
255
256 if (page > 0) {
257 sb.append(PAGE_SEPARATOR);
258 sb.append(page);
259 }
260
261 sb.append(SECURE_SEPARATOR);
262 sb.append(secure);
263
264 return sb.toString();
265 }
266
267 protected JournalArticleDisplay getArticleDisplay(
268 long groupId, String articleId, String templateId, String viewMode,
269 String languageId, int page, String xmlRequest,
270 ThemeDisplay themeDisplay) {
271
272 try {
273 if (_log.isInfoEnabled()) {
274 _log.info(
275 "Get article display {" + groupId + ", " + articleId +
276 ", " + templateId + "}");
277 }
278
279 return JournalArticleLocalServiceUtil.getArticleDisplay(
280 groupId, articleId, templateId, viewMode, languageId, page,
281 xmlRequest, themeDisplay);
282 }
283 catch (Exception e) {
284 if (_log.isWarnEnabled()) {
285 _log.warn(
286 "Unable to get display for " + groupId + " " +
287 articleId + " " + languageId);
288 }
289
290 return null;
291 }
292 }
293
294 protected boolean isLifecycleRender(
295 ThemeDisplay themeDisplay, String xmlRequest) {
296
297 if (themeDisplay != null) {
298 return themeDisplay.isLifecycleRender();
299 }
300 else if (Validator.isNotNull(xmlRequest)) {
301 Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
302
303 return matcher.find();
304 }
305 else {
306 return false;
307 }
308 }
309
310 protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
311 "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
312 protected static PortalCache portalCache = MultiVMPoolUtil.getCache(
313 CACHE_NAME);
314
315 private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
316
317 }