1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JournalContentImpl implements JournalContent {
46  
47      public void clearCache() {
48          cache.removeAll();
49      }
50  
51      public void clearCache(long groupId, String articleId, String templateId) {
52          clearCache();
53      }
54  
55      public String getContent(
56          long groupId, String articleId, String languageId, String xmlRequest) {
57  
58          return getContent(
59              groupId, articleId, null, languageId, null, xmlRequest);
60      }
61  
62      public String getContent(
63          long groupId, String articleId, String languageId,
64          ThemeDisplay themeDisplay) {
65  
66          return getContent(groupId, articleId, null, languageId, themeDisplay);
67      }
68  
69      public String getContent(
70          long groupId, String articleId, String templateId, String languageId,
71          String xmlRequest) {
72  
73          return getContent(
74              groupId, articleId, templateId, languageId, null, xmlRequest);
75      }
76  
77      public String getContent(
78          long groupId, String articleId, String templateId, String languageId,
79          ThemeDisplay themeDisplay) {
80  
81          return getContent(
82              groupId, articleId, templateId, languageId, themeDisplay, null);
83      }
84  
85      public String getContent(
86          long groupId, String articleId, String templateId, String languageId,
87          ThemeDisplay themeDisplay, String xmlRequest) {
88  
89          JournalArticleDisplay articleDisplay = getDisplay(
90              groupId, articleId, templateId, languageId, themeDisplay, 1,
91              xmlRequest);
92  
93          if (articleDisplay != null) {
94              return articleDisplay.getContent();
95          }
96          else {
97              return null;
98          }
99      }
100 
101     public JournalArticleDisplay getDisplay(
102         long groupId, String articleId, String languageId, String xmlRequest) {
103 
104         return getDisplay(
105             groupId, articleId, null, languageId, null, 1, xmlRequest);
106     }
107 
108     public JournalArticleDisplay getDisplay(
109         long groupId, String articleId, String languageId,
110         ThemeDisplay themeDisplay) {
111 
112         return getDisplay(
113             groupId, articleId, null, languageId, themeDisplay, 1, null);
114     }
115 
116     public JournalArticleDisplay getDisplay(
117         long groupId, String articleId, String templateId, String languageId,
118         String xmlRequest) {
119 
120         return getDisplay(
121             groupId, articleId, templateId, languageId, null, 1, xmlRequest);
122     }
123 
124     public JournalArticleDisplay getDisplay(
125         long groupId, String articleId, String templateId, String languageId,
126         ThemeDisplay themeDisplay) {
127 
128         return getDisplay(
129             groupId, articleId, templateId, languageId, themeDisplay, 1, null);
130     }
131 
132     public JournalArticleDisplay getDisplay(
133         long groupId, String articleId, String templateId, String languageId,
134         ThemeDisplay themeDisplay, int page, String xmlRequest) {
135 
136         StopWatch stopWatch = null;
137 
138         if (_log.isDebugEnabled()) {
139             stopWatch = new StopWatch();
140 
141             stopWatch.start();
142         }
143 
144         articleId = GetterUtil.getString(articleId).toUpperCase();
145         templateId = GetterUtil.getString(templateId).toUpperCase();
146 
147         String key = encodeKey(
148             groupId, articleId, templateId, languageId, page,
149             themeDisplay.isSecure());
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, boolean secure) {
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         sb.append(SECURE_SEPARATOR);
199         sb.append(secure);
200 
201         return sb.toString();
202     }
203 
204     protected JournalArticleDisplay getArticleDisplay(
205         long groupId, String articleId, String templateId, String languageId,
206         int page, String xmlRequest, ThemeDisplay themeDisplay) {
207 
208         try {
209             if (_log.isInfoEnabled()) {
210                 _log.info(
211                     "Get article display {" + groupId + ", " + articleId +
212                         ", " + templateId + "}");
213             }
214 
215             return JournalArticleLocalServiceUtil.getArticleDisplay(
216                 groupId, articleId, templateId, languageId, page, xmlRequest,
217                 themeDisplay);
218         }
219         catch (Exception e) {
220             if (_log.isWarnEnabled()) {
221                 _log.warn(
222                     "Unable to get display for " + groupId + " " +
223                         articleId + " " + languageId);
224             }
225 
226             return null;
227         }
228     }
229 
230     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
231 
232     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
233 
234 }