001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.verify;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.service.ResourceLocalServiceUtil;
025: import com.liferay.portal.spring.hibernate.HibernateUtil;
026: import com.liferay.portlet.journal.model.JournalArticle;
027: import com.liferay.portlet.journal.model.JournalStructure;
028: import com.liferay.portlet.journal.model.JournalTemplate;
029: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
030: import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
031: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
032: import com.liferay.portlet.tags.NoSuchAssetException;
033: import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
034: import com.liferay.util.Html;
035: import com.liferay.util.dao.DataAccess;
036:
037: import java.sql.Connection;
038: import java.sql.PreparedStatement;
039: import java.sql.ResultSet;
040:
041: import java.util.List;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Alexander Chow
050: *
051: */
052: public class VerifyJournal extends VerifyProcess {
053:
054: public void verify() throws VerifyException {
055: _log.info("Verifying integrity");
056:
057: try {
058: verifyJournal();
059: } catch (Exception e) {
060: throw new VerifyException(e);
061: }
062: }
063:
064: protected void verifyJournal() throws Exception {
065:
066: // Structures
067:
068: List structures = JournalStructureLocalServiceUtil
069: .getStructures();
070:
071: for (int i = 0; i < structures.size(); i++) {
072: JournalStructure structure = (JournalStructure) structures
073: .get(i);
074:
075: ResourceLocalServiceUtil.addResources(structure
076: .getCompanyId(), 0, 0, JournalStructure.class
077: .getName(), structure.getId(), false, true, true);
078: }
079:
080: if (_log.isDebugEnabled()) {
081: _log.debug("Permissions verified for Journal structures");
082: }
083:
084: // Templates
085:
086: List templates = JournalTemplateLocalServiceUtil.getTemplates();
087:
088: for (int i = 0; i < templates.size(); i++) {
089: JournalTemplate template = (JournalTemplate) templates
090: .get(i);
091:
092: ResourceLocalServiceUtil.addResources(template
093: .getCompanyId(), 0, 0, JournalTemplate.class
094: .getName(), template.getId(), false, true, true);
095: }
096:
097: if (_log.isDebugEnabled()) {
098: _log.debug("Permissions verified for Journal templates");
099: }
100:
101: // Articles
102:
103: List articles = JournalArticleLocalServiceUtil.getArticles();
104:
105: for (int i = 0; i < articles.size(); i++) {
106: JournalArticle article = (JournalArticle) articles.get(i);
107:
108: long groupId = article.getGroupId();
109: String articleId = article.getArticleId();
110: double version = article.getVersion();
111:
112: if (article.getResourcePrimKey() <= 0) {
113: article = JournalArticleLocalServiceUtil
114: .checkArticleResourcePrimKey(groupId,
115: articleId, version);
116: }
117:
118: ResourceLocalServiceUtil.addResources(article
119: .getCompanyId(), 0, 0, JournalArticle.class
120: .getName(), article.getResourcePrimKey(), false,
121: true, true);
122:
123: try {
124: TagsAssetLocalServiceUtil.getAsset(JournalArticle.class
125: .getName(), article.getResourcePrimKey());
126: } catch (NoSuchAssetException nsae) {
127: try {
128: JournalArticleLocalServiceUtil
129: .updateTagsAsset(article.getUserId(),
130: article, new String[0]);
131: } catch (Exception e) {
132: if (_log.isWarnEnabled()) {
133: _log
134: .warn("Unable to update tags asset for article "
135: + article.getId()
136: + ": "
137: + e.getMessage());
138: }
139: }
140: }
141:
142: String content = GetterUtil.getString(article.getContent());
143:
144: String newContent = Html.replaceMsWordCharacters(content);
145:
146: if (!content.equals(newContent)) {
147: JournalArticleLocalServiceUtil.updateContent(groupId,
148: articleId, version, newContent);
149: }
150:
151: JournalArticleLocalServiceUtil.checkStructure(groupId,
152: articleId, version);
153:
154: //verifyStaleJournalArticle(article);
155: }
156:
157: if (_log.isDebugEnabled()) {
158: _log
159: .debug("Permissions and Tags assets verified for Journal articles");
160: }
161: }
162:
163: protected void verifyStaleJournalArticle(JournalArticle article)
164: throws Exception {
165:
166: long groupId = article.getGroupId();
167: String articleId = article.getArticleId();
168: double version = article.getVersion();
169:
170: if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
171: return;
172: }
173:
174: long count = getPortletPreferencesCount(articleId);
175:
176: if (count == 0) {
177: if (_log.isWarnEnabled()) {
178: _log.warn("Article {groupId=" + groupId
179: + ", articleId=" + articleId + ", version="
180: + version + "} is not used on any layouts");
181: }
182: }
183: }
184:
185: protected long getPortletPreferencesCount(String articleId)
186: throws Exception {
187:
188: Connection con = null;
189: PreparedStatement ps = null;
190: ResultSet rs = null;
191:
192: try {
193: con = HibernateUtil.getConnection();
194:
195: ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
196:
197: ps.setString(1, "%<name>article-id</name><value>"
198: + articleId + "</value>%");
199:
200: rs = ps.executeQuery();
201:
202: while (rs.next()) {
203: long count = rs.getLong("count_value");
204:
205: return count;
206: }
207: } finally {
208: DataAccess.cleanUp(con, ps, rs);
209: }
210:
211: return 0;
212: }
213:
214: private static final String _GET_PORTLET_PREFERENCES_COUNT = "select count(*) as count_value from PortletPreferences where "
215: + "ownerId = 0 and ownerType = 3 and portletId like "
216: + "'56_INSTANCE_%' and preferences like ?";
217:
218: private static Log _log = LogFactory.getLog(VerifyJournal.class);
219:
220: }
|