1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.HtmlUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.service.ResourceLocalServiceUtil;
25  import com.liferay.portlet.asset.NoSuchEntryException;
26  import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
27  import com.liferay.portlet.journal.model.JournalArticle;
28  import com.liferay.portlet.journal.model.JournalStructure;
29  import com.liferay.portlet.journal.model.JournalTemplate;
30  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
31  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
32  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
33  
34  import java.util.List;
35  
36  /**
37   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Alexander Chow
40   */
41  public class VerifyJournal extends VerifyProcess {
42  
43      public static final long DEFAULT_GROUP_ID = 14;
44  
45      public static final int NUM_OF_ARTICLES = 5;
46  
47      protected void doVerify() throws Exception {
48  
49          // Oracle new line
50  
51          verifyOracleNewLine();
52  
53          // Structures
54  
55          List<JournalStructure> structures =
56              JournalStructureLocalServiceUtil.getStructures();
57  
58          for (JournalStructure structure : structures) {
59              ResourceLocalServiceUtil.addResources(
60                  structure.getCompanyId(), 0, 0,
61                  JournalStructure.class.getName(), structure.getId(), false,
62                  true, true);
63          }
64  
65          if (_log.isDebugEnabled()) {
66              _log.debug("Permissions verified for structures");
67          }
68  
69          // Templates
70  
71          List<JournalTemplate> templates =
72              JournalTemplateLocalServiceUtil.getTemplates();
73  
74          for (JournalTemplate template : templates) {
75              ResourceLocalServiceUtil.addResources(
76                  template.getCompanyId(), 0, 0,
77                  JournalTemplate.class.getName(), template.getId(), false, true,
78                  true);
79          }
80  
81          if (_log.isDebugEnabled()) {
82              _log.debug("Permissions verified for templates");
83          }
84  
85          // Articles
86  
87          List<JournalArticle> articles =
88              JournalArticleLocalServiceUtil.getArticles();
89  
90          for (JournalArticle article : articles) {
91              long groupId = article.getGroupId();
92              String articleId = article.getArticleId();
93              double version = article.getVersion();
94              String structureId = article.getStructureId();
95  
96              if (article.getResourcePrimKey() <= 0) {
97                  article =
98                      JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
99                          groupId, articleId, version);
100             }
101 
102             ResourceLocalServiceUtil.addResources(
103                 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
104                 article.getResourcePrimKey(), false, true, true);
105 
106             try {
107                 AssetEntryLocalServiceUtil.getEntry(
108                     JournalArticle.class.getName(),
109                     article.getResourcePrimKey());
110             }
111             catch (NoSuchEntryException nsee) {
112                 try {
113                     JournalArticleLocalServiceUtil.updateAsset(
114                         article.getUserId(), article, null, null);
115                 }
116                 catch (Exception e) {
117                     if (_log.isWarnEnabled()) {
118                         _log.warn(
119                             "Unable to update asset for article " +
120                                 article.getId() + ": " + e.getMessage());
121                     }
122                 }
123             }
124 
125             String content = GetterUtil.getString(article.getContent());
126 
127             String newContent = HtmlUtil.replaceMsWordCharacters(content);
128 
129             if (Validator.isNotNull(structureId)) {
130                 /*JournalStructure structure =
131                     JournalStructureLocalServiceUtil.getStructure(
132                         groupId, structureId);
133 
134                 newContent = JournalUtil.removeOldContent(
135                     newContent, structure.getXsd());*/
136             }
137 
138             if (!content.equals(newContent)) {
139                 JournalArticleLocalServiceUtil.updateContent(
140                     groupId, articleId, version, newContent);
141             }
142 
143             JournalArticleLocalServiceUtil.checkStructure(
144                 groupId, articleId, version);
145         }
146 
147         if (_log.isDebugEnabled()) {
148             _log.debug("Permissions and assets verified for articles");
149         }
150     }
151 
152     protected void verifyOracleNewLine() throws Exception {
153         DB db = DBFactoryUtil.getDB();
154 
155         if (!db.getType().equals(DB.TYPE_ORACLE)) {
156             return;
157         }
158 
159         // This is a workaround for a limitation in Oracle sqlldr's inability
160         // insert new line characters for long varchar columns. See
161         // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
162         // information. Check several articles because some articles may not
163         // have new lines.
164 
165         boolean checkNewLine = false;
166 
167         List<JournalArticle> articles = null;
168 
169         if (NUM_OF_ARTICLES <= 0) {
170             checkNewLine = true;
171 
172             articles = JournalArticleLocalServiceUtil.getArticles(
173                 DEFAULT_GROUP_ID);
174         }
175         else {
176             articles = JournalArticleLocalServiceUtil.getArticles(
177                 DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
178         }
179 
180         for (JournalArticle article : articles) {
181             String content = article.getContent();
182 
183             if ((content != null) && (content.indexOf("\\n") != -1)) {
184                 articles = JournalArticleLocalServiceUtil.getArticles(
185                     DEFAULT_GROUP_ID);
186 
187                 for (int j = 0; j < articles.size(); j++) {
188                     article = articles.get(j);
189 
190                     JournalArticleLocalServiceUtil.checkNewLine(
191                         article.getGroupId(), article.getArticleId(),
192                         article.getVersion());
193                 }
194 
195                 checkNewLine = true;
196 
197                 break;
198             }
199         }
200 
201         // Only process this once
202 
203         if (!checkNewLine) {
204             if (_log.isInfoEnabled()) {
205                 _log.debug("Do not fix oracle new line");
206             }
207 
208             return;
209         }
210         else {
211             if (_log.isInfoEnabled()) {
212                 _log.info("Fix oracle new line");
213             }
214         }
215 
216         List<JournalStructure> structures =
217             JournalStructureLocalServiceUtil.getStructures(
218                 DEFAULT_GROUP_ID, 0, 1);
219 
220         if (structures.size() == 1) {
221             JournalStructure structure = structures.get(0);
222 
223             String xsd = structure.getXsd();
224 
225             if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
226                 structures = JournalStructureLocalServiceUtil.getStructures(
227                     DEFAULT_GROUP_ID);
228 
229                 for (int i = 0; i < structures.size(); i++) {
230                     structure = structures.get(i);
231 
232                     JournalStructureLocalServiceUtil.checkNewLine(
233                         structure.getGroupId(), structure.getStructureId());
234                 }
235             }
236         }
237 
238         List<JournalTemplate> templates =
239             JournalTemplateLocalServiceUtil.getTemplates(
240                 DEFAULT_GROUP_ID, 0, 1);
241 
242         if (templates.size() == 1) {
243             JournalTemplate template = templates.get(0);
244 
245             String xsl = template.getXsl();
246 
247             if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
248                 templates = JournalTemplateLocalServiceUtil.getTemplates(
249                     DEFAULT_GROUP_ID);
250 
251                 for (int i = 0; i < templates.size(); i++) {
252                     template = templates.get(i);
253 
254                     JournalTemplateLocalServiceUtil.checkNewLine(
255                         template.getGroupId(), template.getTemplateId());
256                 }
257             }
258         }
259     }
260 
261     private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
262 
263 }