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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.HtmlUtil;
27  import com.liferay.portal.service.ResourceLocalServiceUtil;
28  import com.liferay.portal.spring.hibernate.HibernateUtil;
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  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  /**
49   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Alexander Chow
52   *
53   */
54  public class VerifyJournal extends VerifyProcess {
55  
56      public void verify() throws VerifyException {
57          _log.info("Verifying");
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          // Structures
70  
71          List<JournalStructure> structures =
72              JournalStructureLocalServiceUtil.getStructures();
73  
74          for (JournalStructure structure : structures) {
75              ResourceLocalServiceUtil.addResources(
76                  structure.getCompanyId(), 0, 0,
77                  JournalStructure.class.getName(), structure.getId(), false,
78                  true, true);
79          }
80  
81          if (_log.isDebugEnabled()) {
82              _log.debug("Permissions verified for Journal structures");
83          }
84  
85          // Templates
86  
87          List<JournalTemplate> templates =
88              JournalTemplateLocalServiceUtil.getTemplates();
89  
90          for (JournalTemplate template : templates) {
91              ResourceLocalServiceUtil.addResources(
92                  template.getCompanyId(), 0, 0,
93                  JournalTemplate.class.getName(), template.getId(), false, true,
94                  true);
95          }
96  
97          if (_log.isDebugEnabled()) {
98              _log.debug("Permissions verified for Journal templates");
99          }
100 
101         // Articles
102 
103         List<JournalArticle> articles =
104             JournalArticleLocalServiceUtil.getArticles();
105 
106         for (JournalArticle article : articles) {
107             long groupId = article.getGroupId();
108             String articleId = article.getArticleId();
109             double version = article.getVersion();
110             //String structureId = article.getStructureId();
111 
112             if (article.getResourcePrimKey() <= 0) {
113                 article =
114                     JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
115                         groupId, articleId, version);
116             }
117 
118             ResourceLocalServiceUtil.addResources(
119                 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
120                 article.getResourcePrimKey(), false, true, true);
121 
122             try {
123                 TagsAssetLocalServiceUtil.getAsset(
124                     JournalArticle.class.getName(),
125                     article.getResourcePrimKey());
126             }
127             catch (NoSuchAssetException nsae) {
128                 try {
129                     JournalArticleLocalServiceUtil.updateTagsAsset(
130                         article.getUserId(), article, new String[0]);
131                 }
132                 catch (Exception e) {
133                     if (_log.isWarnEnabled()) {
134                         _log.warn(
135                             "Unable to update tags asset for article " +
136                                 article.getId() + ": " + e.getMessage());
137                     }
138                 }
139             }
140 
141             String content = GetterUtil.getString(article.getContent());
142 
143             String newContent = HtmlUtil.replaceMsWordCharacters(content);
144 
145             /*if (Validator.isNotNull(structureId)) {
146                 JournalStructure structure =
147                     JournalStructureLocalServiceUtil.getStructure(
148                         groupId, structureId);
149 
150                 newContent = JournalUtil.removeOldContent(
151                     newContent, structure.getXsd());
152             }*/
153 
154             if (!content.equals(newContent)) {
155                 JournalArticleLocalServiceUtil.updateContent(
156                     groupId, articleId, version, newContent);
157             }
158 
159             JournalArticleLocalServiceUtil.checkStructure(
160                 groupId, articleId, version);
161 
162             //verifyStaleJournalArticle(article);
163         }
164 
165         if (_log.isDebugEnabled()) {
166             _log.debug(
167                 "Permissions and Tags assets verified for Journal articles");
168         }
169     }
170 
171     protected void verifyStaleJournalArticle(JournalArticle article)
172         throws Exception {
173 
174         long groupId = article.getGroupId();
175         String articleId = article.getArticleId();
176         double version = article.getVersion();
177 
178         if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
179             return;
180         }
181 
182         long count = getPortletPreferencesCount(articleId);
183 
184         if (count == 0) {
185             if (_log.isWarnEnabled()) {
186                 _log.warn(
187                     "Article {groupId=" + groupId + ", articleId=" +
188                         articleId + ", version=" + version +
189                             "} is not used on any layouts");
190             }
191         }
192     }
193 
194     protected long getPortletPreferencesCount(String articleId)
195         throws Exception {
196 
197         Connection con = null;
198         PreparedStatement ps = null;
199         ResultSet rs = null;
200 
201         try {
202             con = HibernateUtil.getConnection();
203 
204             ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
205 
206             ps.setString(
207                 1, "%<name>article-id</name><value>" + articleId + "</value>%");
208 
209             rs = ps.executeQuery();
210 
211             while (rs.next()) {
212                 long count = rs.getLong("count_value");
213 
214                 return count;
215             }
216         }
217         finally {
218             DataAccess.cleanUp(con, ps, rs);
219         }
220 
221         return 0;
222     }
223 
224     private static final String _GET_PORTLET_PREFERENCES_COUNT =
225         "select count(*) as count_value from PortletPreferences where " +
226             "ownerId = 0 and ownerType = 3 and portletId like " +
227                 "'56_INSTANCE_%' and preferences like ?";
228 
229     private static Log _log = LogFactory.getLog(VerifyJournal.class);
230 
231 }