001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018 import com.liferay.portal.kernel.cache.PortalCache;
019 import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.portlet.PortletRequestModel;
023 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.LayoutSet;
029 import com.liferay.portal.security.permission.ActionKeys;
030 import com.liferay.portal.theme.ThemeDisplay;
031 import com.liferay.portlet.journal.model.JournalArticleDisplay;
032 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
033 import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
034
035 import javax.portlet.RenderRequest;
036
037 import org.apache.commons.lang.time.StopWatch;
038
039
044 @DoPrivileged
045 public class JournalContentImpl implements JournalContent {
046
047 @Override
048 public void clearCache() {
049 if (ExportImportThreadLocal.isImportInProcess()) {
050 return;
051 }
052
053 portalCache.removeAll();
054 }
055
056 @Override
057 public void clearCache(
058 long groupId, String articleId, String ddmTemplateKey) {
059
060 clearCache();
061 }
062
063 @Override
064 public String getContent(
065 long groupId, String articleId, String viewMode, String languageId,
066 PortletRequestModel portletRequestModel) {
067
068 return getContent(
069 groupId, articleId, null, viewMode, languageId, portletRequestModel,
070 null);
071 }
072
073 @Override
074 public String getContent(
075 long groupId, String articleId, String ddmTemplateKey, String viewMode,
076 String languageId, PortletRequestModel portletRequestModel) {
077
078 return getContent(
079 groupId, articleId, ddmTemplateKey, viewMode, languageId,
080 portletRequestModel, null);
081 }
082
083 @Override
084 public String getContent(
085 long groupId, String articleId, String ddmTemplateKey, String viewMode,
086 String languageId, PortletRequestModel portletRequestModel,
087 ThemeDisplay themeDisplay) {
088
089 JournalArticleDisplay articleDisplay = getDisplay(
090 groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
091 portletRequestModel, themeDisplay);
092
093 if (articleDisplay != null) {
094 return articleDisplay.getContent();
095 }
096 else {
097 return null;
098 }
099 }
100
101 @Override
102 public String getContent(
103 long groupId, String articleId, String ddmTemplateKey, String viewMode,
104 String languageId, ThemeDisplay themeDisplay) {
105
106 return getContent(
107 groupId, articleId, ddmTemplateKey, viewMode, languageId,
108 (PortletRequestModel)null, themeDisplay);
109 }
110
111 @Override
112 public String getContent(
113 long groupId, String articleId, String viewMode, String languageId,
114 ThemeDisplay themeDisplay) {
115
116 return getContent(
117 groupId, articleId, null, viewMode, languageId, themeDisplay);
118 }
119
120 @Override
121 public JournalArticleDisplay getDisplay(
122 long groupId, String articleId, double version, String ddmTemplateKey,
123 String viewMode, String languageId, int page,
124 PortletRequestModel portletRequestModel, ThemeDisplay themeDisplay) {
125
126 StopWatch stopWatch = new StopWatch();
127
128 stopWatch.start();
129
130 articleId = StringUtil.toUpperCase(GetterUtil.getString(articleId));
131 ddmTemplateKey = StringUtil.toUpperCase(
132 GetterUtil.getString(ddmTemplateKey));
133
134 long layoutSetId = 0;
135 boolean secure = false;
136
137 if (themeDisplay != null) {
138 try {
139 if (!JournalArticlePermission.contains(
140 themeDisplay.getPermissionChecker(), groupId, articleId,
141 ActionKeys.VIEW)) {
142
143 return null;
144 }
145 }
146 catch (Exception e) {
147 }
148
149 LayoutSet layoutSet = themeDisplay.getLayoutSet();
150
151 layoutSetId = layoutSet.getLayoutSetId();
152
153 secure = themeDisplay.isSecure();
154 }
155
156 String key = encodeKey(
157 groupId, articleId, version, ddmTemplateKey, layoutSetId, viewMode,
158 languageId, page, secure);
159
160 JournalArticleDisplay articleDisplay = portalCache.get(key);
161
162 boolean lifecycleRender = false;
163
164 if (portletRequestModel != null) {
165 lifecycleRender = RenderRequest.RENDER_PHASE.equals(
166 portletRequestModel.getLifecycle());
167 }
168
169 if ((articleDisplay == null) || !lifecycleRender) {
170 articleDisplay = getArticleDisplay(
171 groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
172 portletRequestModel, themeDisplay);
173
174 if ((articleDisplay != null) && articleDisplay.isCacheable() &&
175 lifecycleRender) {
176
177 portalCache.put(key, articleDisplay);
178 }
179 }
180
181 if (_log.isDebugEnabled()) {
182 _log.debug(
183 "getDisplay for {" + groupId + ", " + articleId + ", " +
184 ddmTemplateKey + ", " + viewMode + ", " + languageId +
185 ", " + page + "} takes " + stopWatch.getTime() + " ms");
186 }
187
188 return articleDisplay;
189 }
190
191 @Override
192 public JournalArticleDisplay getDisplay(
193 long groupId, String articleId, String viewMode, String languageId,
194 int page, ThemeDisplay themeDisplay) {
195
196 return getDisplay(
197 groupId, articleId, null, viewMode, languageId, page,
198 (PortletRequestModel)null, themeDisplay);
199 }
200
201 @Override
202 public JournalArticleDisplay getDisplay(
203 long groupId, String articleId, String viewMode, String languageId,
204 PortletRequestModel portletRequestModel) {
205
206 return getDisplay(
207 groupId, articleId, null, viewMode, languageId, 1,
208 portletRequestModel, null);
209 }
210
211 @Override
212 public JournalArticleDisplay getDisplay(
213 long groupId, String articleId, String ddmTemplateKey, String viewMode,
214 String languageId, int page, PortletRequestModel portletRequestModel,
215 ThemeDisplay themeDisplay) {
216
217 return getDisplay(
218 groupId, articleId, 0, ddmTemplateKey, viewMode, languageId, 1,
219 portletRequestModel, themeDisplay);
220 }
221
222 @Override
223 public JournalArticleDisplay getDisplay(
224 long groupId, String articleId, String ddmTemplateKey, String viewMode,
225 String languageId, PortletRequestModel portletRequestModel) {
226
227 return getDisplay(
228 groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
229 portletRequestModel, null);
230 }
231
232 @Override
233 public JournalArticleDisplay getDisplay(
234 long groupId, String articleId, String ddmTemplateKey, String viewMode,
235 String languageId, ThemeDisplay themeDisplay) {
236
237 return getDisplay(
238 groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
239 (PortletRequestModel)null, themeDisplay);
240 }
241
242 @Override
243 public JournalArticleDisplay getDisplay(
244 long groupId, String articleId, String viewMode, String languageId,
245 ThemeDisplay themeDisplay) {
246
247 return getDisplay(
248 groupId, articleId, viewMode, languageId, 1, themeDisplay);
249 }
250
251 protected String encodeKey(
252 long groupId, String articleId, double version, String ddmTemplateKey,
253 long layoutSetId, String viewMode, String languageId, int page,
254 boolean secure) {
255
256 StringBundler sb = new StringBundler(17);
257
258 sb.append(StringUtil.toHexString(groupId));
259 sb.append(ARTICLE_SEPARATOR);
260 sb.append(articleId);
261 sb.append(VERSION_SEPARATOR);
262 sb.append(version);
263 sb.append(TEMPLATE_SEPARATOR);
264 sb.append(ddmTemplateKey);
265
266 if (layoutSetId > 0) {
267 sb.append(LAYOUT_SET_SEPARATOR);
268 sb.append(StringUtil.toHexString(layoutSetId));
269 }
270
271 if (Validator.isNotNull(viewMode)) {
272 sb.append(VIEW_MODE_SEPARATOR);
273 sb.append(viewMode);
274 }
275
276 if (Validator.isNotNull(languageId)) {
277 sb.append(LANGUAGE_SEPARATOR);
278 sb.append(languageId);
279 }
280
281 if (page > 0) {
282 sb.append(PAGE_SEPARATOR);
283 sb.append(StringUtil.toHexString(page));
284 }
285
286 sb.append(SECURE_SEPARATOR);
287 sb.append(secure);
288
289 return sb.toString();
290 }
291
292 protected JournalArticleDisplay getArticleDisplay(
293 long groupId, String articleId, String ddmTemplateKey, String viewMode,
294 String languageId, int page, PortletRequestModel portletRequestModel,
295 ThemeDisplay themeDisplay) {
296
297 try {
298 if (_log.isInfoEnabled()) {
299 _log.info(
300 "Get article display {" + groupId + ", " + articleId +
301 ", " + ddmTemplateKey + "}");
302 }
303
304 return JournalArticleLocalServiceUtil.getArticleDisplay(
305 groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
306 portletRequestModel, themeDisplay);
307 }
308 catch (Exception e) {
309 if (_log.isWarnEnabled()) {
310 _log.warn(
311 "Unable to get display for " + groupId + " " +
312 articleId + " " + languageId);
313 }
314
315 return null;
316 }
317 }
318
319 protected static final String CACHE_NAME = JournalContent.class.getName();
320
321 protected static PortalCache<String, JournalArticleDisplay> portalCache =
322 MultiVMPoolUtil.getCache(CACHE_NAME);
323
324 private static final Log _log = LogFactoryUtil.getLog(
325 JournalContentImpl.class);
326
327 }