1
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.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringMaker;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.theme.ThemeDisplay;
32 import com.liferay.portlet.journal.model.JournalArticleDisplay;
33 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34 import com.liferay.util.CollectionFactory;
35
36 import java.util.Map;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41
49 public class JournalContentUtil {
50
51 public static final String CACHE_NAME = JournalContentUtil.class.getName();
52
53 public static String ARTICLE_SEPARATOR = "_ARTICLE_";
54
55 public static String TEMPLATE_SEPARATOR = "_TEMPLATE_";
56
57 public static String LANGUAGE_SEPARATOR = "_LANGUAGE_";
58
59 public static void clearCache() {
60 _cache.removeAll();
61 }
62
63 public static void clearCache(
64 long groupId, String articleId, String templateId) {
65
66 articleId = GetterUtil.getString(articleId).toUpperCase();
67 templateId = GetterUtil.getString(templateId).toUpperCase();
68
69 String groupKey = _encodeGroupKey(groupId, articleId, templateId);
70
71 MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
72 }
73
74 public static String getContent(
75 long groupId, String articleId, String languageId,
76 ThemeDisplay themeDisplay) {
77
78 return getContent(groupId, articleId, null, languageId, themeDisplay);
79 }
80
81 public static String getContent(
82 long groupId, String articleId, String templateId, String languageId,
83 ThemeDisplay themeDisplay) {
84
85 return getContent(
86 groupId, articleId, null, languageId, themeDisplay, false);
87 }
88
89 public static String getContent(
90 long groupId, String articleId, String templateId, String languageId,
91 ThemeDisplay themeDisplay, boolean disableCaching) {
92
93 JournalArticleDisplay articleDisplay = getDisplay(
94 groupId, articleId, templateId, languageId, themeDisplay,
95 disableCaching);
96
97 if (articleDisplay != null) {
98 return articleDisplay.getContent();
99 }
100 else {
101 return null;
102 }
103 }
104
105 public static JournalArticleDisplay getDisplay(
106 long groupId, String articleId, String templateId, String languageId,
107 ThemeDisplay themeDisplay, boolean disableCaching) {
108
109 return getDisplay(
110 groupId, articleId, templateId, languageId, themeDisplay,
111 disableCaching, null);
112 }
113
114 public static JournalArticleDisplay getDisplay(
115 long groupId, String articleId, String templateId, String languageId,
116 ThemeDisplay themeDisplay, boolean disableCaching, String xmlRequest) {
117
118 articleId = GetterUtil.getString(articleId).toUpperCase();
119 templateId = GetterUtil.getString(templateId).toUpperCase();
120
121 if (disableCaching) {
122 return _getArticleDisplay(
123 groupId, articleId, templateId, languageId, themeDisplay,
124 xmlRequest);
125 }
126
127 String key = _encodeKey(groupId, articleId, templateId, languageId);
128
129 JournalArticleDisplay articleDisplay =
130 (JournalArticleDisplay)MultiVMPoolUtil.get(_cache, key);
131
132 if (articleDisplay == null) {
133 articleDisplay = _getArticleDisplay(
134 groupId, articleId, templateId, languageId, themeDisplay,
135 xmlRequest);
136
137 if (articleDisplay != null) {
138 String groupKey = _encodeGroupKey(
139 groupId, articleId, templateId);
140
141 MultiVMPoolUtil.put(_cache, key, _groups, groupKey, articleDisplay);
142 }
143 }
144
145 return articleDisplay;
146 }
147
148 private static String _encodeGroupKey(
149 long groupId, String articleId, String templateId) {
150
151 return _encodeKey(groupId, articleId, templateId, null);
152 }
153
154 private static String _encodeKey(
155 long groupId, String articleId, String templateId, String languageId) {
156
157 StringMaker sm = new StringMaker();
158
159 sm.append(CACHE_NAME);
160 sm.append(StringPool.POUND);
161 sm.append(groupId);
162 sm.append(ARTICLE_SEPARATOR);
163 sm.append(articleId);
164 sm.append(TEMPLATE_SEPARATOR);
165 sm.append(templateId);
166
167 if (Validator.isNotNull(languageId)) {
168 sm.append(LANGUAGE_SEPARATOR);
169 sm.append(languageId);
170 }
171
172 return sm.toString();
173 }
174
175 private static JournalArticleDisplay _getArticleDisplay(
176 long groupId, String articleId, String templateId, String languageId,
177 ThemeDisplay themeDisplay, String xmlRequest) {
178
179 try {
180 return JournalArticleLocalServiceUtil.getArticleDisplay(
181 groupId, articleId, templateId, languageId, themeDisplay,
182 xmlRequest);
183 }
184 catch (Exception e) {
185 if (_log.isWarnEnabled()) {
186 _log.warn(
187 "Unable to get display for " + groupId + " " +
188 articleId + " " + languageId);
189 }
190
191 return null;
192 }
193 }
194
195 private static Log _log = LogFactory.getLog(JournalContentUtil.class);
196
197 private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
198
199 private static Map _groups = CollectionFactory.getSyncHashMap();
200
201 }