1
22
23 package com.liferay.portal.verify;
24
25 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.HtmlUtil;
30 import com.liferay.portal.service.ResourceLocalServiceUtil;
31 import com.liferay.portal.tools.sql.DBUtil;
32 import com.liferay.portlet.journal.model.JournalArticle;
33 import com.liferay.portlet.journal.model.JournalStructure;
34 import com.liferay.portlet.journal.model.JournalTemplate;
35 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
36 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
37 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
38 import com.liferay.portlet.tags.NoSuchAssetException;
39 import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
40
41 import java.sql.Connection;
42 import java.sql.PreparedStatement;
43 import java.sql.ResultSet;
44
45 import java.util.List;
46
47
52 public class VerifyJournal extends VerifyProcess {
53
54 public static final long DEFAULT_GROUP_ID = 14;
55
56 public static final int NUM_OF_ARTICLES = 5;
57
58 protected void doVerify() throws Exception {
59
60
62 verifyOracleNewLine();
63
64
66 List<JournalStructure> structures =
67 JournalStructureLocalServiceUtil.getStructures();
68
69 for (JournalStructure structure : structures) {
70 ResourceLocalServiceUtil.addResources(
71 structure.getCompanyId(), 0, 0,
72 JournalStructure.class.getName(), structure.getId(), false,
73 true, true);
74 }
75
76 if (_log.isDebugEnabled()) {
77 _log.debug("Permissions verified for Journal structures");
78 }
79
80
82 List<JournalTemplate> templates =
83 JournalTemplateLocalServiceUtil.getTemplates();
84
85 for (JournalTemplate template : templates) {
86 ResourceLocalServiceUtil.addResources(
87 template.getCompanyId(), 0, 0,
88 JournalTemplate.class.getName(), template.getId(), false, true,
89 true);
90 }
91
92 if (_log.isDebugEnabled()) {
93 _log.debug("Permissions verified for Journal templates");
94 }
95
96
98 List<JournalArticle> articles =
99 JournalArticleLocalServiceUtil.getArticles();
100
101 for (JournalArticle article : articles) {
102 long groupId = article.getGroupId();
103 String articleId = article.getArticleId();
104 double version = article.getVersion();
105
107 if (article.getResourcePrimKey() <= 0) {
108 article =
109 JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
110 groupId, articleId, version);
111 }
112
113 ResourceLocalServiceUtil.addResources(
114 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
115 article.getResourcePrimKey(), false, true, true);
116
117 try {
118 TagsAssetLocalServiceUtil.getAsset(
119 JournalArticle.class.getName(),
120 article.getResourcePrimKey());
121 }
122 catch (NoSuchAssetException nsae) {
123 try {
124 JournalArticleLocalServiceUtil.updateTagsAsset(
125 article.getUserId(), article, new String[0]);
126 }
127 catch (Exception e) {
128 if (_log.isWarnEnabled()) {
129 _log.warn(
130 "Unable to update tags asset for article " +
131 article.getId() + ": " + e.getMessage());
132 }
133 }
134 }
135
136 String content = GetterUtil.getString(article.getContent());
137
138 String newContent = HtmlUtil.replaceMsWordCharacters(content);
139
140
148
149 if (!content.equals(newContent)) {
150 JournalArticleLocalServiceUtil.updateContent(
151 groupId, articleId, version, newContent);
152 }
153
154 JournalArticleLocalServiceUtil.checkStructure(
155 groupId, articleId, version);
156
157 }
159
160 if (_log.isDebugEnabled()) {
161 _log.debug(
162 "Permissions and Tags assets verified for Journal articles");
163 }
164 }
165
166 protected void verifyOracleNewLine() throws Exception {
167 DBUtil dbUtil = DBUtil.getInstance();
168
169 if (!dbUtil.getType().equals(DBUtil.TYPE_ORACLE)) {
170 return;
171 }
172
173
179 boolean checkNewLine = false;
180
181 List<JournalArticle> articles = null;
182
183 if (NUM_OF_ARTICLES <= 0) {
184 checkNewLine = true;
185
186 articles = JournalArticleLocalServiceUtil.getArticles(
187 DEFAULT_GROUP_ID);
188 }
189 else {
190 articles = JournalArticleLocalServiceUtil.getArticles(
191 DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
192 }
193
194 for (JournalArticle article : articles) {
195 String content = article.getContent();
196
197 if ((content != null) && (content.indexOf("\\n") != -1)) {
198 articles = JournalArticleLocalServiceUtil.getArticles(
199 DEFAULT_GROUP_ID);
200
201 for (int j = 0; j < articles.size(); j++) {
202 article = articles.get(j);
203
204 JournalArticleLocalServiceUtil.checkNewLine(
205 article.getGroupId(), article.getArticleId(),
206 article.getVersion());
207 }
208
209 checkNewLine = true;
210
211 break;
212 }
213 }
214
215
217 if (!checkNewLine) {
218 if (_log.isInfoEnabled()) {
219 _log.debug("Do not fix oracle new line");
220 }
221
222 return;
223 }
224 else {
225 if (_log.isInfoEnabled()) {
226 _log.info("Fix oracle new line");
227 }
228 }
229
230 List<JournalStructure> structures =
231 JournalStructureLocalServiceUtil.getStructures(
232 DEFAULT_GROUP_ID, 0, 1);
233
234 if (structures.size() == 1) {
235 JournalStructure structure = structures.get(0);
236
237 String xsd = structure.getXsd();
238
239 if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
240 structures = JournalStructureLocalServiceUtil.getStructures(
241 DEFAULT_GROUP_ID);
242
243 for (int i = 0; i < structures.size(); i++) {
244 structure = structures.get(i);
245
246 JournalStructureLocalServiceUtil.checkNewLine(
247 structure.getGroupId(), structure.getStructureId());
248 }
249 }
250 }
251
252 List<JournalTemplate> templates =
253 JournalTemplateLocalServiceUtil.getTemplates(
254 DEFAULT_GROUP_ID, 0, 1);
255
256 if (templates.size() == 1) {
257 JournalTemplate template = templates.get(0);
258
259 String xsl = template.getXsl();
260
261 if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
262 templates = JournalTemplateLocalServiceUtil.getTemplates(
263 DEFAULT_GROUP_ID);
264
265 for (int i = 0; i < templates.size(); i++) {
266 template = templates.get(i);
267
268 JournalTemplateLocalServiceUtil.checkNewLine(
269 template.getGroupId(), template.getTemplateId());
270 }
271 }
272 }
273 }
274
275 protected void verifyStaleJournalArticle(JournalArticle article)
276 throws Exception {
277
278 long groupId = article.getGroupId();
279 String articleId = article.getArticleId();
280 double version = article.getVersion();
281
282 if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
283 return;
284 }
285
286 long count = getPortletPreferencesCount(articleId);
287
288 if (count == 0) {
289 if (_log.isWarnEnabled()) {
290 _log.warn(
291 "Article {groupId=" + groupId + ", articleId=" +
292 articleId + ", version=" + version +
293 "} is not used on any layouts");
294 }
295 }
296 }
297
298 protected long getPortletPreferencesCount(String articleId)
299 throws Exception {
300
301 Connection con = null;
302 PreparedStatement ps = null;
303 ResultSet rs = null;
304
305 try {
306 con = DataAccess.getConnection();
307
308 ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
309
310 ps.setString(
311 1, "%<name>article-id</name><value>" + articleId + "</value>%");
312
313 rs = ps.executeQuery();
314
315 while (rs.next()) {
316 long count = rs.getLong("count_value");
317
318 return count;
319 }
320 }
321 finally {
322 DataAccess.cleanUp(con, ps, rs);
323 }
324
325 return 0;
326 }
327
328 private static final String _GET_PORTLET_PREFERENCES_COUNT =
329 "select count(*) as count_value from PortletPreferences where " +
330 "ownerId = 0 and ownerType = 3 and portletId like " +
331 "'56_INSTANCE_%' and preferences like ?";
332
333 private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
334
335 }