001    /**
002     * Copyright (c) 2000-present 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.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.cache.index.IndexedCacheKey;
020    import com.liferay.portal.kernel.cache.index.PortalCacheIndexer;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.portlet.PortletRequestModel;
024    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.HashUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.LayoutSet;
032    import com.liferay.portal.security.permission.ActionKeys;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
035    import com.liferay.portlet.journal.model.JournalArticleDisplay;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
038    
039    import javax.portlet.RenderRequest;
040    
041    import org.apache.commons.lang.time.StopWatch;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Raymond Aug??
046     * @author Michael Young
047     */
048    @DoPrivileged
049    public class JournalContentImpl implements JournalContent {
050    
051            @Override
052            public void clearCache() {
053                    if (ExportImportThreadLocal.isImportInProcess()) {
054                            return;
055                    }
056    
057                    _portalCache.removeAll();
058            }
059    
060            @Override
061            public void clearCache(
062                    long groupId, String articleId, String ddmTemplateKey) {
063    
064                    _portalCacheIndexer.removeIndexedCacheKeys(
065                            JournalContentKey.getIndex(groupId, articleId, ddmTemplateKey));
066            }
067    
068            @Override
069            public String getContent(
070                    long groupId, String articleId, String viewMode, String languageId,
071                    PortletRequestModel portletRequestModel) {
072    
073                    return getContent(
074                            groupId, articleId, null, viewMode, languageId, portletRequestModel,
075                            null);
076            }
077    
078            @Override
079            public String getContent(
080                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
081                    String languageId, PortletRequestModel portletRequestModel) {
082    
083                    return getContent(
084                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
085                            portletRequestModel, null);
086            }
087    
088            @Override
089            public String getContent(
090                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
091                    String languageId, PortletRequestModel portletRequestModel,
092                    ThemeDisplay themeDisplay) {
093    
094                    JournalArticleDisplay articleDisplay = getDisplay(
095                            groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
096                            portletRequestModel, themeDisplay);
097    
098                    if (articleDisplay != null) {
099                            return articleDisplay.getContent();
100                    }
101                    else {
102                            return null;
103                    }
104            }
105    
106            @Override
107            public String getContent(
108                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
109                    String languageId, ThemeDisplay themeDisplay) {
110    
111                    return getContent(
112                            groupId, articleId, ddmTemplateKey, viewMode, languageId,
113                            (PortletRequestModel)null, themeDisplay);
114            }
115    
116            @Override
117            public String getContent(
118                    long groupId, String articleId, String viewMode, String languageId,
119                    ThemeDisplay themeDisplay) {
120    
121                    return getContent(
122                            groupId, articleId, null, viewMode, languageId, themeDisplay);
123            }
124    
125            @Override
126            public JournalArticleDisplay getDisplay(
127                    long groupId, String articleId, double version, String ddmTemplateKey,
128                    String viewMode, String languageId, int page,
129                    PortletRequestModel portletRequestModel, ThemeDisplay themeDisplay) {
130    
131                    StopWatch stopWatch = new StopWatch();
132    
133                    stopWatch.start();
134    
135                    articleId = StringUtil.toUpperCase(GetterUtil.getString(articleId));
136                    ddmTemplateKey = StringUtil.toUpperCase(
137                            GetterUtil.getString(ddmTemplateKey));
138    
139                    long layoutSetId = 0;
140                    boolean secure = false;
141    
142                    if (themeDisplay != null) {
143                            try {
144                                    if (!JournalArticlePermission.contains(
145                                                    themeDisplay.getPermissionChecker(), groupId, articleId,
146                                                    ActionKeys.VIEW)) {
147    
148                                            return null;
149                                    }
150                            }
151                            catch (Exception e) {
152                            }
153    
154                            LayoutSet layoutSet = themeDisplay.getLayoutSet();
155    
156                            layoutSetId = layoutSet.getLayoutSetId();
157    
158                            secure = themeDisplay.isSecure();
159                    }
160    
161                    JournalContentKey journalContentKey = new JournalContentKey(
162                            groupId, articleId, version, ddmTemplateKey, layoutSetId, viewMode,
163                            languageId, page, secure);
164    
165                    JournalArticleDisplay articleDisplay = _portalCache.get(
166                            journalContentKey);
167    
168                    boolean lifecycleRender = false;
169    
170                    if (portletRequestModel != null) {
171                            lifecycleRender = RenderRequest.RENDER_PHASE.equals(
172                                    portletRequestModel.getLifecycle());
173                    }
174    
175                    if ((articleDisplay == null) || !lifecycleRender) {
176                            articleDisplay = getArticleDisplay(
177                                    groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
178                                    portletRequestModel, themeDisplay);
179    
180                            if ((articleDisplay != null) && articleDisplay.isCacheable() &&
181                                    lifecycleRender) {
182    
183                                    _portalCache.put(journalContentKey, articleDisplay);
184                            }
185                    }
186    
187                    if (_log.isDebugEnabled()) {
188                            _log.debug(
189                                    "getDisplay for {" + groupId + ", " + articleId + ", " +
190                                            ddmTemplateKey + ", " + viewMode + ", " + languageId +
191                                                    ", " + page + "} takes " + stopWatch.getTime() + " ms");
192                    }
193    
194                    return articleDisplay;
195            }
196    
197            @Override
198            public JournalArticleDisplay getDisplay(
199                    long groupId, String articleId, String viewMode, String languageId,
200                    int page, ThemeDisplay themeDisplay) {
201    
202                    return getDisplay(
203                            groupId, articleId, null, viewMode, languageId, page,
204                            (PortletRequestModel)null, themeDisplay);
205            }
206    
207            @Override
208            public JournalArticleDisplay getDisplay(
209                    long groupId, String articleId, String viewMode, String languageId,
210                    PortletRequestModel portletRequestModel) {
211    
212                    return getDisplay(
213                            groupId, articleId, null, viewMode, languageId, 1,
214                            portletRequestModel, null);
215            }
216    
217            @Override
218            public JournalArticleDisplay getDisplay(
219                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
220                    String languageId, int page, PortletRequestModel portletRequestModel,
221                    ThemeDisplay themeDisplay) {
222    
223                    return getDisplay(
224                            groupId, articleId, 0, ddmTemplateKey, viewMode, languageId, 1,
225                            portletRequestModel, themeDisplay);
226            }
227    
228            @Override
229            public JournalArticleDisplay getDisplay(
230                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
231                    String languageId, PortletRequestModel portletRequestModel) {
232    
233                    return getDisplay(
234                            groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
235                            portletRequestModel, null);
236            }
237    
238            @Override
239            public JournalArticleDisplay getDisplay(
240                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
241                    String languageId, ThemeDisplay themeDisplay) {
242    
243                    return getDisplay(
244                            groupId, articleId, ddmTemplateKey, viewMode, languageId, 1,
245                            (PortletRequestModel)null, themeDisplay);
246            }
247    
248            @Override
249            public JournalArticleDisplay getDisplay(
250                    long groupId, String articleId, String viewMode, String languageId,
251                    ThemeDisplay themeDisplay) {
252    
253                    return getDisplay(
254                            groupId, articleId, viewMode, languageId, 1, themeDisplay);
255            }
256    
257            protected JournalArticleDisplay getArticleDisplay(
258                    long groupId, String articleId, String ddmTemplateKey, String viewMode,
259                    String languageId, int page, PortletRequestModel portletRequestModel,
260                    ThemeDisplay themeDisplay) {
261    
262                    try {
263                            if (_log.isInfoEnabled()) {
264                                    _log.info(
265                                            "Get article display {" + groupId + ", " + articleId +
266                                                    ", " + ddmTemplateKey + "}");
267                            }
268    
269                            return JournalArticleLocalServiceUtil.getArticleDisplay(
270                                    groupId, articleId, ddmTemplateKey, viewMode, languageId, page,
271                                    portletRequestModel, themeDisplay);
272                    }
273                    catch (Exception e) {
274                            if (_log.isWarnEnabled()) {
275                                    _log.warn(
276                                            "Unable to get display for " + groupId + " " +
277                                                    articleId + " " + languageId);
278                            }
279    
280                            return null;
281                    }
282            }
283    
284            protected static final String CACHE_NAME = JournalContent.class.getName();
285    
286            private static final Log _log = LogFactoryUtil.getLog(
287                    JournalContentImpl.class);
288    
289            private static final PortalCache<JournalContentKey, JournalArticleDisplay>
290                    _portalCache = MultiVMPoolUtil.getCache(CACHE_NAME);
291            private static final PortalCacheIndexer
292                    <String, JournalContentKey, JournalArticleDisplay> _portalCacheIndexer =
293                            new PortalCacheIndexer<>(_portalCache);
294    
295            private static class JournalContentKey implements IndexedCacheKey<String> {
296    
297                    public static String getIndex(
298                            long groupId, String articleId, String ddmTemplateKey) {
299    
300                            StringBundler sb = new StringBundler(5);
301    
302                            sb.append(groupId);
303                            sb.append(StringPool.UNDERLINE);
304                            sb.append(articleId);
305                            sb.append(StringPool.UNDERLINE);
306                            sb.append(ddmTemplateKey);
307    
308                            return sb.toString();
309                    }
310    
311                    @Override
312                    public boolean equals(Object obj) {
313                            JournalContentKey journalContentKey = (JournalContentKey)obj;
314    
315                            if ((journalContentKey._groupId == _groupId) &&
316                                    Validator.equals(journalContentKey._articleId, _articleId) &&
317                                    (journalContentKey._version == _version) &&
318                                    Validator.equals(
319                                            journalContentKey._ddmTemplateKey, _ddmTemplateKey) &&
320                                    (journalContentKey._layoutSetId == _layoutSetId) &&
321                                    Validator.equals(journalContentKey._viewMode, _viewMode) &&
322                                    Validator.equals(journalContentKey._languageId, _languageId) &&
323                                    (journalContentKey._page == _page) &&
324                                    (journalContentKey._secure == _secure)) {
325    
326                                    return true;
327                            }
328    
329                            return false;
330                    }
331    
332                    @Override
333                    public String getIndex() {
334                            return getIndex(_groupId, _articleId, _ddmTemplateKey);
335                    }
336    
337                    @Override
338                    public int hashCode() {
339                            int hashCode = HashUtil.hash(0, _groupId);
340    
341                            hashCode = HashUtil.hash(hashCode, _articleId);
342                            hashCode = HashUtil.hash(hashCode, _version);
343                            hashCode = HashUtil.hash(hashCode, _ddmTemplateKey);
344                            hashCode = HashUtil.hash(hashCode, _layoutSetId);
345                            hashCode = HashUtil.hash(hashCode, _viewMode);
346                            hashCode = HashUtil.hash(hashCode, _languageId);
347                            hashCode = HashUtil.hash(hashCode, _page);
348    
349                            return HashUtil.hash(hashCode, _secure);
350                    }
351    
352                    private JournalContentKey(
353                            long groupId, String articleId, double version,
354                            String ddmTemplateKey, long layoutSetId, String viewMode,
355                            String languageId, int page, boolean secure) {
356    
357                            _groupId = groupId;
358                            _articleId = articleId;
359                            _version = version;
360                            _ddmTemplateKey = ddmTemplateKey;
361                            _layoutSetId = layoutSetId;
362                            _viewMode = viewMode;
363                            _languageId = languageId;
364                            _page = page;
365                            _secure = secure;
366                    }
367    
368                    private static final long serialVersionUID = 1L;
369    
370                    private final String _articleId;
371                    private final String _ddmTemplateKey;
372                    private final long _groupId;
373                    private final String _languageId;
374                    private final long _layoutSetId;
375                    private final int _page;
376                    private final boolean _secure;
377                    private final double _version;
378                    private final String _viewMode;
379    
380            }
381    
382    }