1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.journalcontent.util;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portlet.journal.model.JournalArticleDisplay;
27  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
28  
29  import org.apache.commons.lang.time.StopWatch;
30  
31  /**
32   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Raymond Augé
36   * @author Michael Young
37   */
38  public class JournalContentImpl implements JournalContent {
39  
40      public void clearCache() {
41          cache.removeAll();
42      }
43  
44      public void clearCache(long groupId, String articleId, String templateId) {
45          clearCache();
46      }
47  
48      public String getContent(
49          long groupId, String articleId, String languageId, String xmlRequest) {
50  
51          return getContent(
52              groupId, articleId, null, languageId, null, xmlRequest);
53      }
54  
55      public String getContent(
56          long groupId, String articleId, String languageId,
57          ThemeDisplay themeDisplay) {
58  
59          return getContent(groupId, articleId, null, languageId, themeDisplay);
60      }
61  
62      public String getContent(
63          long groupId, String articleId, String templateId, String languageId,
64          String xmlRequest) {
65  
66          return getContent(
67              groupId, articleId, templateId, languageId, null, xmlRequest);
68      }
69  
70      public String getContent(
71          long groupId, String articleId, String templateId, String languageId,
72          ThemeDisplay themeDisplay) {
73  
74          return getContent(
75              groupId, articleId, templateId, languageId, themeDisplay, null);
76      }
77  
78      public String getContent(
79          long groupId, String articleId, String templateId, String languageId,
80          ThemeDisplay themeDisplay, String xmlRequest) {
81  
82          JournalArticleDisplay articleDisplay = getDisplay(
83              groupId, articleId, templateId, languageId, themeDisplay, 1,
84              xmlRequest);
85  
86          if (articleDisplay != null) {
87              return articleDisplay.getContent();
88          }
89          else {
90              return null;
91          }
92      }
93  
94      public JournalArticleDisplay getDisplay(
95          long groupId, String articleId, String languageId, String xmlRequest) {
96  
97          return getDisplay(
98              groupId, articleId, null, languageId, null, 1, xmlRequest);
99      }
100 
101     public JournalArticleDisplay getDisplay(
102         long groupId, String articleId, String languageId,
103         ThemeDisplay themeDisplay) {
104 
105         return getDisplay(
106             groupId, articleId, null, languageId, themeDisplay, 1, null);
107     }
108 
109     public JournalArticleDisplay getDisplay(
110         long groupId, String articleId, String templateId, String languageId,
111         String xmlRequest) {
112 
113         return getDisplay(
114             groupId, articleId, templateId, languageId, null, 1, xmlRequest);
115     }
116 
117     public JournalArticleDisplay getDisplay(
118         long groupId, String articleId, String templateId, String languageId,
119         ThemeDisplay themeDisplay) {
120 
121         return getDisplay(
122             groupId, articleId, templateId, languageId, themeDisplay, 1, null);
123     }
124 
125     public JournalArticleDisplay getDisplay(
126         long groupId, String articleId, String templateId, String languageId,
127         ThemeDisplay themeDisplay, int page, String xmlRequest) {
128 
129         StopWatch stopWatch = null;
130 
131         if (_log.isDebugEnabled()) {
132             stopWatch = new StopWatch();
133 
134             stopWatch.start();
135         }
136 
137         articleId = GetterUtil.getString(articleId).toUpperCase();
138         templateId = GetterUtil.getString(templateId).toUpperCase();
139 
140         boolean secure = false;
141 
142         if (themeDisplay != null) {
143             secure = themeDisplay.isSecure();
144         }
145 
146         String key = encodeKey(
147             groupId, articleId, templateId, languageId, page, secure);
148 
149         JournalArticleDisplay articleDisplay = (JournalArticleDisplay)cache.get(
150             key);
151 
152         if (articleDisplay == null) {
153             articleDisplay = getArticleDisplay(
154                 groupId, articleId, templateId, languageId, page, xmlRequest,
155                 themeDisplay);
156 
157             if ((articleDisplay != null) && articleDisplay.isCacheable()) {
158                 cache.put(key, articleDisplay);
159             }
160         }
161 
162         if (_log.isDebugEnabled()) {
163             _log.debug(
164                 "getDisplay for {" + groupId + ", " + articleId + ", " +
165                     templateId + ", " + languageId + ", " + page + "} takes " +
166                         stopWatch.getTime() + " ms");
167         }
168 
169         return articleDisplay;
170     }
171 
172     protected String encodeKey(
173         long groupId, String articleId, String templateId, String languageId,
174         int page, boolean secure) {
175 
176         StringBundler sb = new StringBundler();
177 
178         sb.append(CACHE_NAME);
179         sb.append(StringPool.POUND);
180         sb.append(groupId);
181         sb.append(ARTICLE_SEPARATOR);
182         sb.append(articleId);
183         sb.append(TEMPLATE_SEPARATOR);
184         sb.append(templateId);
185 
186         if (Validator.isNotNull(languageId)) {
187             sb.append(LANGUAGE_SEPARATOR);
188             sb.append(languageId);
189         }
190 
191         if (page > 0) {
192             sb.append(PAGE_SEPARATOR);
193             sb.append(page);
194         }
195 
196         sb.append(SECURE_SEPARATOR);
197         sb.append(secure);
198 
199         return sb.toString();
200     }
201 
202     protected JournalArticleDisplay getArticleDisplay(
203         long groupId, String articleId, String templateId, String languageId,
204         int page, String xmlRequest, ThemeDisplay themeDisplay) {
205 
206         try {
207             if (_log.isInfoEnabled()) {
208                 _log.info(
209                     "Get article display {" + groupId + ", " + articleId +
210                         ", " + templateId + "}");
211             }
212 
213             return JournalArticleLocalServiceUtil.getArticleDisplay(
214                 groupId, articleId, templateId, languageId, page, xmlRequest,
215                 themeDisplay);
216         }
217         catch (Exception e) {
218             if (_log.isWarnEnabled()) {
219                 _log.warn(
220                     "Unable to get display for " + groupId + " " +
221                         articleId + " " + languageId);
222             }
223 
224             return null;
225         }
226     }
227 
228     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
229 
230     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
231 
232 }