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