1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.verify;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.service.ResourceLocalServiceUtil;
29  import com.liferay.portlet.journal.model.JournalArticle;
30  import com.liferay.portlet.journal.model.JournalStructure;
31  import com.liferay.portlet.journal.model.JournalTemplate;
32  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
33  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
34  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
35  import com.liferay.portlet.tags.NoSuchAssetException;
36  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
37  
38  import java.sql.Connection;
39  import java.sql.PreparedStatement;
40  import java.sql.ResultSet;
41  
42  import java.util.List;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Alexander Chow
51   *
52   */
53  public class VerifyJournal extends VerifyProcess {
54  
55      public void verify() throws VerifyException {
56          _log.info("Verifying");
57  
58          try {
59              verifyJournal();
60          }
61          catch (Exception e) {
62              throw new VerifyException(e);
63          }
64      }
65  
66      protected void verifyJournal() throws Exception {
67  
68          // Structures
69  
70          List<JournalStructure> structures =
71              JournalStructureLocalServiceUtil.getStructures();
72  
73          for (JournalStructure structure : structures) {
74              ResourceLocalServiceUtil.addResources(
75                  structure.getCompanyId(), 0, 0,
76                  JournalStructure.class.getName(), structure.getId(), false,
77                  true, true);
78          }
79  
80          if (_log.isDebugEnabled()) {
81              _log.debug("Permissions verified for Journal structures");
82          }
83  
84          // Templates
85  
86          List<JournalTemplate> templates =
87              JournalTemplateLocalServiceUtil.getTemplates();
88  
89          for (JournalTemplate template : templates) {
90              ResourceLocalServiceUtil.addResources(
91                  template.getCompanyId(), 0, 0,
92                  JournalTemplate.class.getName(), template.getId(), false, true,
93                  true);
94          }
95  
96          if (_log.isDebugEnabled()) {
97              _log.debug("Permissions verified for Journal templates");
98          }
99  
100         // Articles
101 
102         List<JournalArticle> articles =
103             JournalArticleLocalServiceUtil.getArticles();
104 
105         for (JournalArticle article : articles) {
106             long groupId = article.getGroupId();
107             String articleId = article.getArticleId();
108             double version = article.getVersion();
109             //String structureId = article.getStructureId();
110 
111             if (article.getResourcePrimKey() <= 0) {
112                 article =
113                     JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
114                         groupId, articleId, version);
115             }
116 
117             ResourceLocalServiceUtil.addResources(
118                 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
119                 article.getResourcePrimKey(), false, true, true);
120 
121             try {
122                 TagsAssetLocalServiceUtil.getAsset(
123                     JournalArticle.class.getName(),
124                     article.getResourcePrimKey());
125             }
126             catch (NoSuchAssetException nsae) {
127                 try {
128                     JournalArticleLocalServiceUtil.updateTagsAsset(
129                         article.getUserId(), article, new String[0]);
130                 }
131                 catch (Exception e) {
132                     if (_log.isWarnEnabled()) {
133                         _log.warn(
134                             "Unable to update tags asset for article " +
135                                 article.getId() + ": " + e.getMessage());
136                     }
137                 }
138             }
139 
140             String content = GetterUtil.getString(article.getContent());
141 
142             String newContent = HtmlUtil.replaceMsWordCharacters(content);
143 
144             /*if (Validator.isNotNull(structureId)) {
145                 JournalStructure structure =
146                     JournalStructureLocalServiceUtil.getStructure(
147                         groupId, structureId);
148 
149                 newContent = JournalUtil.removeOldContent(
150                     newContent, structure.getXsd());
151             }*/
152 
153             if (!content.equals(newContent)) {
154                 JournalArticleLocalServiceUtil.updateContent(
155                     groupId, articleId, version, newContent);
156             }
157 
158             JournalArticleLocalServiceUtil.checkStructure(
159                 groupId, articleId, version);
160 
161             //verifyStaleJournalArticle(article);
162         }
163 
164         if (_log.isDebugEnabled()) {
165             _log.debug(
166                 "Permissions and Tags assets verified for Journal articles");
167         }
168     }
169 
170     protected void verifyStaleJournalArticle(JournalArticle article)
171         throws Exception {
172 
173         long groupId = article.getGroupId();
174         String articleId = article.getArticleId();
175         double version = article.getVersion();
176 
177         if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
178             return;
179         }
180 
181         long count = getPortletPreferencesCount(articleId);
182 
183         if (count == 0) {
184             if (_log.isWarnEnabled()) {
185                 _log.warn(
186                     "Article {groupId=" + groupId + ", articleId=" +
187                         articleId + ", version=" + version +
188                             "} is not used on any layouts");
189             }
190         }
191     }
192 
193     protected long getPortletPreferencesCount(String articleId)
194         throws Exception {
195 
196         Connection con = null;
197         PreparedStatement ps = null;
198         ResultSet rs = null;
199 
200         try {
201             con = DataAccess.getConnection();
202 
203             ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
204 
205             ps.setString(
206                 1, "%<name>article-id</name><value>" + articleId + "</value>%");
207 
208             rs = ps.executeQuery();
209 
210             while (rs.next()) {
211                 long count = rs.getLong("count_value");
212 
213                 return count;
214             }
215         }
216         finally {
217             DataAccess.cleanUp(con, ps, rs);
218         }
219 
220         return 0;
221     }
222 
223     private static final String _GET_PORTLET_PREFERENCES_COUNT =
224         "select count(*) as count_value from PortletPreferences where " +
225             "ownerId = 0 and ownerType = 3 and portletId like " +
226                 "'56_INSTANCE_%' and preferences like ?";
227 
228     private static Log _log = LogFactory.getLog(VerifyJournal.class);
229 
230 }