1
22
23 package com.liferay.portal.verify;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.service.ResourceLocalServiceUtil;
27 import com.liferay.portal.spring.hibernate.HibernateUtil;
28 import com.liferay.portlet.journal.model.JournalArticle;
29 import com.liferay.portlet.journal.model.JournalStructure;
30 import com.liferay.portlet.journal.model.JournalTemplate;
31 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
33 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
34 import com.liferay.portlet.tags.NoSuchAssetException;
35 import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
36 import com.liferay.util.Html;
37 import com.liferay.util.dao.DataAccess;
38
39 import java.sql.Connection;
40 import java.sql.PreparedStatement;
41 import java.sql.ResultSet;
42
43 import java.util.List;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48
54 public class VerifyJournal extends VerifyProcess {
55
56 public void verify() throws VerifyException {
57 _log.info("Verifying integrity");
58
59 try {
60 verifyJournal();
61 }
62 catch (Exception e) {
63 throw new VerifyException(e);
64 }
65 }
66
67 protected void verifyJournal() throws Exception {
68
69
71 List structures = JournalStructureLocalServiceUtil.getStructures();
72
73 for (int i = 0; i < structures.size(); i++) {
74 JournalStructure structure = (JournalStructure)structures.get(i);
75
76 ResourceLocalServiceUtil.addResources(
77 structure.getCompanyId(), 0, 0,
78 JournalStructure.class.getName(), structure.getId(), false,
79 true, true);
80 }
81
82 if (_log.isDebugEnabled()) {
83 _log.debug("Permissions verified for Journal structures");
84 }
85
86
88 List templates = JournalTemplateLocalServiceUtil.getTemplates();
89
90 for (int i = 0; i < templates.size(); i++) {
91 JournalTemplate template = (JournalTemplate)templates.get(i);
92
93 ResourceLocalServiceUtil.addResources(
94 template.getCompanyId(), 0, 0,
95 JournalTemplate.class.getName(), template.getId(), false, true,
96 true);
97 }
98
99 if (_log.isDebugEnabled()) {
100 _log.debug("Permissions verified for Journal templates");
101 }
102
103
105 List articles = JournalArticleLocalServiceUtil.getArticles();
106
107 for (int i = 0; i < articles.size(); i++) {
108 JournalArticle article = (JournalArticle)articles.get(i);
109
110 long groupId = article.getGroupId();
111 String articleId = article.getArticleId();
112 double version = article.getVersion();
113
114 if (article.getResourcePrimKey() <= 0) {
115 article =
116 JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
117 groupId, articleId, version);
118 }
119
120 ResourceLocalServiceUtil.addResources(
121 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
122 article.getResourcePrimKey(), false, true, true);
123
124 try {
125 TagsAssetLocalServiceUtil.getAsset(
126 JournalArticle.class.getName(),
127 article.getResourcePrimKey());
128 }
129 catch (NoSuchAssetException nsae) {
130 try {
131 JournalArticleLocalServiceUtil.updateTagsAsset(
132 article.getUserId(), article, new String[0]);
133 }
134 catch (Exception e) {
135 if (_log.isWarnEnabled()) {
136 _log.warn(
137 "Unable to update tags asset for article " +
138 article.getId() + ": " + e.getMessage());
139 }
140 }
141 }
142
143 String content = GetterUtil.getString(article.getContent());
144
145 String newContent = Html.replaceMsWordCharacters(content);
146
147 if (!content.equals(newContent)) {
148 JournalArticleLocalServiceUtil.updateContent(
149 groupId, articleId, version, newContent);
150 }
151
152 JournalArticleLocalServiceUtil.checkStructure(
153 groupId, articleId, version);
154
155 }
157
158 if (_log.isDebugEnabled()) {
159 _log.debug(
160 "Permissions and Tags assets verified for Journal articles");
161 }
162 }
163
164 protected void verifyStaleJournalArticle(JournalArticle article)
165 throws Exception {
166
167 long groupId = article.getGroupId();
168 String articleId = article.getArticleId();
169 double version = article.getVersion();
170
171 if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
172 return;
173 }
174
175 long count = getPortletPreferencesCount(articleId);
176
177 if (count == 0) {
178 if (_log.isWarnEnabled()) {
179 _log.warn(
180 "Article {groupId=" + groupId + ", articleId=" +
181 articleId + ", version=" + version +
182 "} is not used on any layouts");
183 }
184 }
185 }
186
187 protected long getPortletPreferencesCount(String articleId)
188 throws Exception {
189
190 Connection con = null;
191 PreparedStatement ps = null;
192 ResultSet rs = null;
193
194 try {
195 con = HibernateUtil.getConnection();
196
197 ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
198
199 ps.setString(
200 1, "%<name>article-id</name><value>" + articleId + "</value>%");
201
202 rs = ps.executeQuery();
203
204 while (rs.next()) {
205 long count = rs.getLong("count_value");
206
207 return count;
208 }
209 }
210 finally {
211 DataAccess.cleanUp(con, ps, rs);
212 }
213
214 return 0;
215 }
216
217 private static final String _GET_PORTLET_PREFERENCES_COUNT =
218 "select count(*) as count_value from PortletPreferences where " +
219 "ownerId = 0 and ownerType = 3 and portletId like " +
220 "'56_INSTANCE_%' and preferences like ?";
221
222 private static Log _log = LogFactory.getLog(VerifyJournal.class);
223
224 }