001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.lar.ExportImportThreadLocal;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Layout;
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 java.util.regex.Matcher;
036    import java.util.regex.Pattern;
037    
038    import org.apache.commons.lang.time.StopWatch;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Raymond Aug??
043     * @author Michael Young
044     */
045    @DoPrivileged
046    public class JournalContentImpl implements JournalContent {
047    
048            @Override
049            public void clearCache() {
050                    if (ExportImportThreadLocal.isImportInProcess()) {
051                            return;
052                    }
053    
054                    portalCache.removeAll();
055            }
056    
057            @Override
058            public void clearCache(
059                    long groupId, String articleId, String ddmTemplateKey) {
060    
061                    clearCache();
062            }
063    
064            @Override
065            public String getContent(
066                    long groupId, String articleId, String viewMode, String languageId,
067                    String xmlRequest) {
068    
069                    return getContent(
070                            groupId, articleId, null, viewMode, languageId, null, xmlRequest);
071            }
072    
073            @Override
074            public String getContent(
075                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
076                    String languageId, String xmlRequest) {
077    
078                    return getContent(
079                            groupId, articleId, ddmTemplateKey, viewMode, languageId, null,
080                            xmlRequest);
081            }
082    
083            @Override
084            public String getContent(
085                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
086                    String languageId, ThemeDisplay themeDisplay) {
087    
088                    return getContent(
089                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
090                            themeDisplay, null);
091            }
092    
093            @Override
094            public String getContent(
095                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
096                    String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
097    
098                    JournalArticleDisplay articleDisplay = getDisplay(
099                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
100                            themeDisplay, 1, xmlRequest);
101    
102                    if (articleDisplay != null) {
103                            return articleDisplay.getContent();
104                    }
105                    else {
106                            return null;
107                    }
108            }
109    
110            @Override
111            public String getContent(
112                    long groupId, String articleId, String viewMode, String languageId,
113                    ThemeDisplay themeDisplay) {
114    
115                    return getContent(
116                            groupId, articleId, null, viewMode, languageId, themeDisplay);
117            }
118    
119            @Override
120            public JournalArticleDisplay getDisplay(
121                    long groupId, String articleId, double version, String ddmTemplateKey,
122                    String viewMode, String languageId, ThemeDisplay themeDisplay, int page,
123                    String xmlRequest) {
124    
125                    StopWatch stopWatch = new StopWatch();
126    
127                    stopWatch.start();
128    
129                    articleId = StringUtil.toUpperCase(GetterUtil.getString(articleId));
130                    ddmTemplateKey = StringUtil.toUpperCase(
131                            GetterUtil.getString(ddmTemplateKey));
132    
133                    long layoutSetId = 0;
134                    boolean secure = false;
135    
136                    if (themeDisplay != null) {
137                            try {
138                                    if (!JournalArticlePermission.contains(
139                                                    themeDisplay.getPermissionChecker(), groupId, articleId,
140                                                    ActionKeys.VIEW)) {
141    
142                                            return null;
143                                    }
144    
145                                    Layout layout = themeDisplay.getLayout();
146    
147                                    LayoutSet layoutSet = layout.getLayoutSet();
148    
149                                    layoutSetId = layoutSet.getLayoutSetId();
150                            }
151                            catch (Exception e) {
152                            }
153    
154                            secure = themeDisplay.isSecure();
155                    }
156    
157                    String key = encodeKey(
158                            groupId, articleId, version, ddmTemplateKey, layoutSetId, viewMode,
159                            languageId, page, secure);
160    
161                    JournalArticleDisplay articleDisplay = portalCache.get(key);
162    
163                    boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
164    
165                    if ((articleDisplay == null) || !lifecycleRender) {
166                            articleDisplay = getArticleDisplay(
167                                    groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
168                                    xmlRequest, themeDisplay);
169    
170                            if ((articleDisplay != null) && articleDisplay.isCacheable() &&
171                                    lifecycleRender) {
172    
173                                    portalCache.put(key, articleDisplay);
174                            }
175                    }
176    
177                    if (_log.isDebugEnabled()) {
178                            _log.debug(
179                                    "getDisplay for {" + groupId + ", " + articleId + ", " +
180                                            ddmTemplateKey + ", " + viewMode + ", " + languageId +
181                                                    ", " + page + "} takes " + stopWatch.getTime() + " ms");
182                    }
183    
184                    return articleDisplay;
185            }
186    
187            @Override
188            public JournalArticleDisplay getDisplay(
189                    long groupId, String articleId, String viewMode, String languageId,
190                    String xmlRequest) {
191    
192                    return getDisplay(
193                            groupId, articleId, null, viewMode, languageId, null, 1,
194                            xmlRequest);
195            }
196    
197            @Override
198            public JournalArticleDisplay getDisplay(
199                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
200                    String languageId, String xmlRequest) {
201    
202                    return getDisplay(
203                            groupId, articleId, ddmTemplateKey, viewMode, languageId, null, 1,
204                            xmlRequest);
205            }
206    
207            @Override
208            public JournalArticleDisplay getDisplay(
209                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
210                    String languageId, ThemeDisplay themeDisplay) {
211    
212                    return getDisplay(
213                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
214                            themeDisplay, 1, null);
215            }
216    
217            @Override
218            public JournalArticleDisplay getDisplay(
219                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
220                    String languageId, ThemeDisplay themeDisplay, int page,
221                    String xmlRequest) {
222    
223                    return getDisplay(
224                            groupId, articleId, 0, ddmTemplateKey, viewMode, languageId,
225                            themeDisplay, 1, xmlRequest);
226            }
227    
228            @Override
229            public JournalArticleDisplay getDisplay(
230                    long groupId, String articleId, String viewMode, String languageId,
231                    ThemeDisplay themeDisplay) {
232    
233                    return getDisplay(
234                            groupId, articleId, viewMode, languageId, themeDisplay, 1);
235            }
236    
237            @Override
238            public JournalArticleDisplay getDisplay(
239                    long groupId, String articleId, String viewMode, String languageId,
240                    ThemeDisplay themeDisplay, int page) {
241    
242                    return getDisplay(
243                            groupId, articleId, null, viewMode, languageId, themeDisplay, page,
244                            null);
245            }
246    
247            protected String encodeKey(
248                    long groupId, String articleId, double version, String ddmTemplateKey,
249                    long layoutSetId, String viewMode, String languageId, int page,
250                    boolean secure) {
251    
252                    StringBundler sb = new StringBundler();
253    
254                    sb.append(StringUtil.toHexString(groupId));
255                    sb.append(ARTICLE_SEPARATOR);
256                    sb.append(articleId);
257                    sb.append(VERSION_SEPARATOR);
258                    sb.append(version);
259                    sb.append(TEMPLATE_SEPARATOR);
260                    sb.append(ddmTemplateKey);
261    
262                    if (layoutSetId > 0) {
263                            sb.append(LAYOUT_SET_SEPARATOR);
264                            sb.append(StringUtil.toHexString(layoutSetId));
265                    }
266    
267                    if (Validator.isNotNull(viewMode)) {
268                            sb.append(VIEW_MODE_SEPARATOR);
269                            sb.append(viewMode);
270                    }
271    
272                    if (Validator.isNotNull(languageId)) {
273                            sb.append(LANGUAGE_SEPARATOR);
274                            sb.append(languageId);
275                    }
276    
277                    if (page > 0) {
278                            sb.append(PAGE_SEPARATOR);
279                            sb.append(StringUtil.toHexString(page));
280                    }
281    
282                    sb.append(SECURE_SEPARATOR);
283                    sb.append(secure);
284    
285                    return sb.toString();
286            }
287    
288            protected JournalArticleDisplay getArticleDisplay(
289                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
290                    String languageId, int page, String xmlRequest,
291                    ThemeDisplay themeDisplay) {
292    
293                    try {
294                            if (_log.isInfoEnabled()) {
295                                    _log.info(
296                                            "Get article display {" + groupId + ", " + articleId +
297                                                    ", " + ddmTemplateKey + "}");
298                            }
299    
300                            return JournalArticleLocalServiceUtil.getArticleDisplay(
301                                    groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
302                                    xmlRequest, themeDisplay);
303                    }
304                    catch (Exception e) {
305                            if (_log.isWarnEnabled()) {
306                                    _log.warn(
307                                            "Unable to get display for " + groupId + " " +
308                                                    articleId + " " + languageId);
309                            }
310    
311                            return null;
312                    }
313            }
314    
315            protected boolean isLifecycleRender(
316                    ThemeDisplay themeDisplay, String xmlRequest) {
317    
318                    if (themeDisplay != null) {
319                            return themeDisplay.isLifecycleRender();
320                    }
321                    else if (Validator.isNotNull(xmlRequest)) {
322                            Matcher matcher = lifecycleRenderPhasePattern.matcher(xmlRequest);
323    
324                            return matcher.find();
325                    }
326                    else {
327                            return false;
328                    }
329            }
330    
331            protected static final String CACHE_NAME = JournalContent.class.getName();
332    
333            protected static Pattern lifecycleRenderPhasePattern = Pattern.compile(
334                    "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
335            protected static PortalCache<String, JournalArticleDisplay> portalCache =
336                    MultiVMPoolUtil.getCache(CACHE_NAME);
337    
338            private static Log _log = LogFactoryUtil.getLog(JournalContentImpl.class);
339    
340    }