1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journalcontent.util;
21  
22  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
23  import com.liferay.portal.kernel.cache.PortalCache;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PropsValues;
32  import com.liferay.portlet.journal.model.JournalArticleDisplay;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
35  
36  import java.util.regex.Matcher;
37  import java.util.regex.Pattern;
38  
39  import org.apache.commons.lang.time.StopWatch;
40  
41  /**
42   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Raymond Augé
46   * @author Michael Young
47   *
48   */
49  public class JournalContentImpl implements JournalContent {
50  
51      public void clearCache() {
52          cache.removeAll();
53      }
54  
55      public void clearCache(
56          long groupId, String articleId, String templateId) {
57  
58          clearCache();
59      }
60  
61      public String getContent(
62          long groupId, String articleId, String viewMode, String languageId,
63          String xmlRequest) {
64  
65          return getContent(
66              groupId, articleId, null, viewMode, languageId, null, xmlRequest);
67      }
68  
69      public String getContent(
70          long groupId, String articleId, String viewMode, String languageId,
71          ThemeDisplay themeDisplay) {
72  
73          return getContent(
74              groupId, articleId, null, viewMode, languageId, themeDisplay);
75      }
76  
77      public String getContent(
78          long groupId, String articleId, String templateId, String viewMode,
79          String languageId, String xmlRequest) {
80  
81          return getContent(
82              groupId, articleId, templateId, viewMode, languageId, null,
83              xmlRequest);
84      }
85  
86      public String getContent(
87          long groupId, String articleId, String templateId, String viewMode,
88          String languageId, ThemeDisplay themeDisplay) {
89  
90          return getContent(
91              groupId, articleId, templateId, viewMode, languageId, themeDisplay,
92              null);
93      }
94  
95      public String getContent(
96          long groupId, String articleId, String templateId, String viewMode,
97          String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
98  
99          JournalArticleDisplay articleDisplay = getDisplay(
100             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
101             1, xmlRequest);
102 
103         if (articleDisplay != null) {
104             return articleDisplay.getContent();
105         }
106         else {
107             return null;
108         }
109     }
110 
111     public JournalArticleDisplay getDisplay(
112         long groupId, String articleId, String viewMode, String languageId,
113         String xmlRequest) {
114 
115         return getDisplay(
116             groupId, articleId, null, viewMode, languageId, null, 1,
117             xmlRequest);
118     }
119 
120     public JournalArticleDisplay getDisplay(
121         long groupId, String articleId, String viewMode, String languageId,
122         ThemeDisplay themeDisplay) {
123 
124         return getDisplay(
125             groupId, articleId, viewMode, languageId, themeDisplay, 1);
126     }
127 
128     public JournalArticleDisplay getDisplay(
129         long groupId, String articleId, String viewMode, String languageId,
130         ThemeDisplay themeDisplay, int page) {
131 
132         return getDisplay(
133             groupId, articleId, null, viewMode, languageId, themeDisplay, page,
134             null);
135     }
136 
137     public JournalArticleDisplay getDisplay(
138         long groupId, String articleId, String templateId, String viewMode,
139         String languageId, String xmlRequest) {
140 
141         return getDisplay(
142             groupId, articleId, templateId, viewMode, languageId, null, 1,
143             xmlRequest);
144     }
145 
146     public JournalArticleDisplay getDisplay(
147         long groupId, String articleId, String templateId, String viewMode,
148         String languageId, ThemeDisplay themeDisplay) {
149 
150         return getDisplay(
151             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
152             1, null);
153     }
154 
155     public JournalArticleDisplay getDisplay(
156         long groupId, String articleId, String templateId, String viewMode,
157         String languageId, ThemeDisplay themeDisplay, int page,
158         String xmlRequest) {
159 
160         StopWatch stopWatch = null;
161 
162         if (_log.isDebugEnabled()) {
163             stopWatch = new StopWatch();
164 
165             stopWatch.start();
166         }
167 
168         articleId = GetterUtil.getString(articleId).toUpperCase();
169         templateId = GetterUtil.getString(templateId).toUpperCase();
170 
171         String key = encodeKey(
172             groupId, articleId, templateId, viewMode, languageId, page);
173 
174         JournalArticleDisplay articleDisplay =
175             (JournalArticleDisplay)MultiVMPoolUtil.get(cache, key);
176 
177         boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
178 
179         if ((articleDisplay == null) || !lifecycleRender) {
180             articleDisplay = getArticleDisplay(
181                 groupId, articleId, templateId, viewMode, languageId, page,
182                 xmlRequest, themeDisplay);
183 
184             if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
185                 (lifecycleRender)) {
186 
187                 MultiVMPoolUtil.put(cache, key, articleDisplay);
188             }
189         }
190 
191         try {
192             if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
193                 (articleDisplay != null) && (themeDisplay != null) &&
194                 (!JournalArticlePermission.contains(
195                     themeDisplay.getPermissionChecker(), groupId, articleId,
196                     ActionKeys.VIEW))) {
197 
198                 articleDisplay = null;
199             }
200         }
201         catch (Exception e) {
202         }
203 
204         if (_log.isDebugEnabled()) {
205             _log.debug(
206                 "getDisplay for {" + groupId + ", " + articleId + ", " +
207                     templateId + ", " + viewMode + ", " + languageId + ", " +
208                         page + "} takes " + stopWatch.getTime() + " ms");
209         }
210 
211         return articleDisplay;
212     }
213 
214     protected String encodeKey(
215         long groupId, String articleId, String templateId, String viewMode,
216         String languageId, int page) {
217 
218         StringBuilder sb = new StringBuilder();
219 
220         sb.append(CACHE_NAME);
221         sb.append(StringPool.POUND);
222         sb.append(groupId);
223         sb.append(ARTICLE_SEPARATOR);
224         sb.append(articleId);
225         sb.append(TEMPLATE_SEPARATOR);
226         sb.append(templateId);
227 
228         if (Validator.isNotNull(viewMode)) {
229             sb.append(VIEW_MODE_SEPARATOR);
230             sb.append(viewMode);
231         }
232 
233         if (Validator.isNotNull(languageId)) {
234             sb.append(LANGUAGE_SEPARATOR);
235             sb.append(languageId);
236         }
237 
238         if (page > 0) {
239             sb.append(PAGE_SEPARATOR);
240             sb.append(page);
241         }
242 
243         return sb.toString();
244     }
245 
246     protected JournalArticleDisplay getArticleDisplay(
247         long groupId, String articleId, String templateId, String viewMode,
248         String languageId, int page, String xmlRequest,
249         ThemeDisplay themeDisplay) {
250 
251         try {
252             if (_log.isInfoEnabled()) {
253                 _log.info(
254                     "Get article display {" + groupId + ", " + articleId +
255                         ", " + templateId + "}");
256             }
257 
258             return JournalArticleLocalServiceUtil.getArticleDisplay(
259                 groupId, articleId, templateId, viewMode, languageId, page,
260                 xmlRequest, themeDisplay);
261         }
262         catch (Exception e) {
263             if (_log.isWarnEnabled()) {
264                 _log.warn(
265                     "Unable to get display for " + groupId + " " +
266                         articleId + " " + languageId);
267             }
268 
269             return null;
270         }
271     }
272 
273     protected boolean isLifecycleRender(
274         ThemeDisplay themeDisplay, String xmlRequest) {
275 
276         if (themeDisplay != null) {
277             return themeDisplay.isLifecycleRender();
278         }
279         else if (Validator.isNotNull(xmlRequest)) {
280             Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
281 
282             return matcher.find();
283         }
284         else {
285             return false;
286         }
287     }
288 
289     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
290 
291     protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
292         "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
293 
294     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
295 
296 }