1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.journal.model.JournalArticle;
26  import com.liferay.portlet.journal.model.JournalStructure;
27  import com.liferay.portlet.journal.model.JournalTemplate;
28  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
29  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
30  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
31  import com.liferay.portlet.tags.NoSuchAssetException;
32  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
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 Journal 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 Journal 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                 TagsAssetLocalServiceUtil.getAsset(
108                     JournalArticle.class.getName(),
109                     article.getResourcePrimKey());
110             }
111             catch (NoSuchAssetException nsae) {
112                 try {
113                     JournalArticleLocalServiceUtil.updateTagsAsset(
114                         article.getUserId(), article, new String[0],
115                         new String[0]);
116                 }
117                 catch (Exception e) {
118                     if (_log.isWarnEnabled()) {
119                         _log.warn(
120                             "Unable to update tags asset for article " +
121                                 article.getId() + ": " + e.getMessage());
122                     }
123                 }
124             }
125 
126             String content = GetterUtil.getString(article.getContent());
127 
128             String newContent = HtmlUtil.replaceMsWordCharacters(content);
129 
130             if (Validator.isNotNull(structureId)) {
131                 /*JournalStructure structure =
132                     JournalStructureLocalServiceUtil.getStructure(
133                         groupId, structureId);
134 
135                 newContent = JournalUtil.removeOldContent(
136                     newContent, structure.getXsd());*/
137             }
138 
139             if (!content.equals(newContent)) {
140                 JournalArticleLocalServiceUtil.updateContent(
141                     groupId, articleId, version, newContent);
142             }
143 
144             JournalArticleLocalServiceUtil.checkStructure(
145                 groupId, articleId, version);
146         }
147 
148         if (_log.isDebugEnabled()) {
149             _log.debug(
150                 "Permissions and Tags assets verified for Journal articles");
151         }
152     }
153 
154     protected void verifyOracleNewLine() throws Exception {
155         DB db = DBFactoryUtil.getDB();
156 
157         if (!db.getType().equals(DB.TYPE_ORACLE)) {
158             return;
159         }
160 
161         // This is a workaround for a limitation in Oracle sqlldr's inability
162         // insert new line characters for long varchar columns. See
163         // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
164         // information. Check several articles because some articles may not
165         // have new lines.
166 
167         boolean checkNewLine = false;
168 
169         List<JournalArticle> articles = null;
170 
171         if (NUM_OF_ARTICLES <= 0) {
172             checkNewLine = true;
173 
174             articles = JournalArticleLocalServiceUtil.getArticles(
175                 DEFAULT_GROUP_ID);
176         }
177         else {
178             articles = JournalArticleLocalServiceUtil.getArticles(
179                 DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
180         }
181 
182         for (JournalArticle article : articles) {
183             String content = article.getContent();
184 
185             if ((content != null) && (content.indexOf("\\n") != -1)) {
186                 articles = JournalArticleLocalServiceUtil.getArticles(
187                     DEFAULT_GROUP_ID);
188 
189                 for (int j = 0; j < articles.size(); j++) {
190                     article = articles.get(j);
191 
192                     JournalArticleLocalServiceUtil.checkNewLine(
193                         article.getGroupId(), article.getArticleId(),
194                         article.getVersion());
195                 }
196 
197                 checkNewLine = true;
198 
199                 break;
200             }
201         }
202 
203         // Only process this once
204 
205         if (!checkNewLine) {
206             if (_log.isInfoEnabled()) {
207                 _log.debug("Do not fix oracle new line");
208             }
209 
210             return;
211         }
212         else {
213             if (_log.isInfoEnabled()) {
214                 _log.info("Fix oracle new line");
215             }
216         }
217 
218         List<JournalStructure> structures =
219             JournalStructureLocalServiceUtil.getStructures(
220                 DEFAULT_GROUP_ID, 0, 1);
221 
222         if (structures.size() == 1) {
223             JournalStructure structure = structures.get(0);
224 
225             String xsd = structure.getXsd();
226 
227             if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
228                 structures = JournalStructureLocalServiceUtil.getStructures(
229                     DEFAULT_GROUP_ID);
230 
231                 for (int i = 0; i < structures.size(); i++) {
232                     structure = structures.get(i);
233 
234                     JournalStructureLocalServiceUtil.checkNewLine(
235                         structure.getGroupId(), structure.getStructureId());
236                 }
237             }
238         }
239 
240         List<JournalTemplate> templates =
241             JournalTemplateLocalServiceUtil.getTemplates(
242                 DEFAULT_GROUP_ID, 0, 1);
243 
244         if (templates.size() == 1) {
245             JournalTemplate template = templates.get(0);
246 
247             String xsl = template.getXsl();
248 
249             if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
250                 templates = JournalTemplateLocalServiceUtil.getTemplates(
251                     DEFAULT_GROUP_ID);
252 
253                 for (int i = 0; i < templates.size(); i++) {
254                     template = templates.get(i);
255 
256                     JournalTemplateLocalServiceUtil.checkNewLine(
257                         template.getGroupId(), template.getTemplateId());
258                 }
259             }
260         }
261     }
262 
263     private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
264 
265 }