1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journalcontent.util;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portlet.journal.model.JournalArticleDisplay;
34  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
35  
36  import org.apache.commons.lang.time.StopWatch;
37  
38  /**
39   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Raymond Augé
43   * @author Michael Young
44   *
45   */
46  public class JournalContentImpl implements JournalContent {
47  
48      public void clearCache() {
49          cache.removeAll();
50      }
51  
52      public void clearCache(long groupId, String articleId, String templateId) {
53          clearCache();
54      }
55  
56      public String getContent(
57          long groupId, String articleId, String languageId, String xmlRequest) {
58  
59          return getContent(
60              groupId, articleId, null, languageId, null, xmlRequest);
61      }
62  
63      public String getContent(
64          long groupId, String articleId, String languageId,
65          ThemeDisplay themeDisplay) {
66  
67          return getContent(groupId, articleId, null, languageId, themeDisplay);
68      }
69  
70      public String getContent(
71          long groupId, String articleId, String templateId, String languageId,
72          String xmlRequest) {
73  
74          return getContent(
75              groupId, articleId, templateId, languageId, null, xmlRequest);
76      }
77  
78      public String getContent(
79          long groupId, String articleId, String templateId, String languageId,
80          ThemeDisplay themeDisplay) {
81  
82          return getContent(
83              groupId, articleId, templateId, languageId, themeDisplay, null);
84      }
85  
86      public String getContent(
87          long groupId, String articleId, String templateId, String languageId,
88          ThemeDisplay themeDisplay, String xmlRequest) {
89  
90          JournalArticleDisplay articleDisplay = getDisplay(
91              groupId, articleId, templateId, languageId, themeDisplay, 1,
92              xmlRequest);
93  
94          if (articleDisplay != null) {
95              return articleDisplay.getContent();
96          }
97          else {
98              return null;
99          }
100     }
101 
102     public JournalArticleDisplay getDisplay(
103         long groupId, String articleId, String languageId, String xmlRequest) {
104 
105         return getDisplay(
106             groupId, articleId, null, languageId, null, 1, xmlRequest);
107     }
108 
109     public JournalArticleDisplay getDisplay(
110         long groupId, String articleId, String languageId,
111         ThemeDisplay themeDisplay) {
112 
113         return getDisplay(
114             groupId, articleId, null, languageId, themeDisplay, 1, null);
115     }
116 
117     public JournalArticleDisplay getDisplay(
118         long groupId, String articleId, String templateId, String languageId,
119         String xmlRequest) {
120 
121         return getDisplay(
122             groupId, articleId, templateId, languageId, null, 1, xmlRequest);
123     }
124 
125     public JournalArticleDisplay getDisplay(
126         long groupId, String articleId, String templateId, String languageId,
127         ThemeDisplay themeDisplay) {
128 
129         return getDisplay(
130             groupId, articleId, templateId, languageId, themeDisplay, 1, null);
131     }
132 
133     public JournalArticleDisplay getDisplay(
134         long groupId, String articleId, String templateId, String languageId,
135         ThemeDisplay themeDisplay, int page, String xmlRequest) {
136 
137         StopWatch stopWatch = null;
138 
139         if (_log.isDebugEnabled()) {
140             stopWatch = new StopWatch();
141 
142             stopWatch.start();
143         }
144 
145         articleId = GetterUtil.getString(articleId).toUpperCase();
146         templateId = GetterUtil.getString(templateId).toUpperCase();
147 
148         String key = encodeKey(
149             groupId, articleId, templateId, languageId, page);
150 
151         JournalArticleDisplay articleDisplay =
152             (JournalArticleDisplay)MultiVMPoolUtil.get(cache, key);
153 
154         if (articleDisplay == null) {
155             articleDisplay = getArticleDisplay(
156                 groupId, articleId, templateId, languageId, page, xmlRequest,
157                 themeDisplay);
158 
159             if ((articleDisplay != null) && articleDisplay.isCacheable()) {
160                 MultiVMPoolUtil.put(cache, key, articleDisplay);
161             }
162         }
163 
164         if (_log.isDebugEnabled()) {
165             _log.debug(
166                 "getDisplay for {" + groupId + ", " + articleId + ", " +
167                     templateId + ", " + languageId + ", " + page + "} takes " +
168                         stopWatch.getTime() + " ms");
169         }
170 
171         return articleDisplay;
172     }
173 
174     protected String encodeKey(
175         long groupId, String articleId, String templateId, String languageId,
176         int page) {
177 
178         StringBuilder sb = new StringBuilder();
179 
180         sb.append(CACHE_NAME);
181         sb.append(StringPool.POUND);
182         sb.append(groupId);
183         sb.append(ARTICLE_SEPARATOR);
184         sb.append(articleId);
185         sb.append(TEMPLATE_SEPARATOR);
186         sb.append(templateId);
187 
188         if (Validator.isNotNull(languageId)) {
189             sb.append(LANGUAGE_SEPARATOR);
190             sb.append(languageId);
191         }
192 
193         if (page > 0) {
194             sb.append(PAGE_SEPARATOR);
195             sb.append(page);
196         }
197 
198         return sb.toString();
199     }
200 
201     protected JournalArticleDisplay getArticleDisplay(
202         long groupId, String articleId, String templateId, String languageId,
203         int page, String xmlRequest, ThemeDisplay themeDisplay) {
204 
205         try {
206             if (_log.isInfoEnabled()) {
207                 _log.info(
208                     "Get article display {" + groupId + ", " + articleId +
209                         ", " + templateId + "}");
210             }
211 
212             return JournalArticleLocalServiceUtil.getArticleDisplay(
213                 groupId, articleId, templateId, languageId, page, xmlRequest,
214                 themeDisplay);
215         }
216         catch (Exception e) {
217             if (_log.isWarnEnabled()) {
218                 _log.warn(
219                     "Unable to get display for " + groupId + " " +
220                         articleId + " " + languageId);
221             }
222 
223             return null;
224         }
225     }
226 
227     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
228 
229     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
230 
231 }