1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.ResourceLocalServiceUtil;
31  import com.liferay.portal.tools.sql.DBUtil;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.model.JournalStructure;
34  import com.liferay.portlet.journal.model.JournalTemplate;
35  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
36  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
37  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
38  import com.liferay.portlet.tags.NoSuchAssetException;
39  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
40  
41  import java.util.List;
42  
43  /**
44   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alexander Chow
47   *
48   */
49  public class VerifyJournal extends VerifyProcess {
50  
51      public static final long DEFAULT_GROUP_ID = 14;
52  
53      public static final int NUM_OF_ARTICLES = 5;
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 verifyOracleNewLine() throws Exception {
67          DBUtil dbUtil = DBUtil.getInstance();
68  
69          if (!dbUtil.getType().equals(DBUtil.TYPE_ORACLE)) {
70              return;
71          }
72  
73          // This is a workaround for a limitation in Oracle sqlldr's inability
74          // insert new line characters for long varchar columns. See
75          // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
76          // information. Check several articles because some articles may not
77          // have new lines.
78  
79          boolean checkNewLine = false;
80  
81          List<JournalArticle> articles = null;
82  
83          if (NUM_OF_ARTICLES <= 0) {
84              checkNewLine = true;
85  
86              articles = JournalArticleLocalServiceUtil.getArticles(
87                  DEFAULT_GROUP_ID);
88          }
89          else {
90              articles = JournalArticleLocalServiceUtil.getArticles(
91                  DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
92          }
93  
94          for (JournalArticle article : articles) {
95              String content = article.getContent();
96  
97              if ((content != null) && (content.indexOf("\\n") != -1)) {
98                  articles = JournalArticleLocalServiceUtil.getArticles(
99                      DEFAULT_GROUP_ID);
100 
101                 for (int j = 0; j < articles.size(); j++) {
102                     article = articles.get(j);
103 
104                     JournalArticleLocalServiceUtil.checkNewLine(
105                         article.getGroupId(), article.getArticleId(),
106                         article.getVersion());
107                 }
108 
109                 checkNewLine = true;
110 
111                 break;
112             }
113         }
114 
115         // Only process this once
116 
117         if (!checkNewLine) {
118             if (_log.isInfoEnabled()) {
119                 _log.debug("Do not fix oracle new line");
120             }
121 
122             return;
123         }
124         else {
125             if (_log.isInfoEnabled()) {
126                 _log.info("Fix oracle new line");
127             }
128         }
129 
130         List<JournalStructure> structures =
131             JournalStructureLocalServiceUtil.getStructures(
132                 DEFAULT_GROUP_ID, 0, 1);
133 
134         if (structures.size() == 1) {
135             JournalStructure structure = structures.get(0);
136 
137             String xsd = structure.getXsd();
138 
139             if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
140                 structures = JournalStructureLocalServiceUtil.getStructures(
141                     DEFAULT_GROUP_ID);
142 
143                 for (int i = 0; i < structures.size(); i++) {
144                     structure = structures.get(i);
145 
146                     JournalStructureLocalServiceUtil.checkNewLine(
147                         structure.getGroupId(), structure.getStructureId());
148                 }
149             }
150         }
151 
152         List<JournalTemplate> templates =
153             JournalTemplateLocalServiceUtil.getTemplates(
154                 DEFAULT_GROUP_ID, 0, 1);
155 
156         if (templates.size() == 1) {
157             JournalTemplate template = templates.get(0);
158 
159             String xsl = template.getXsl();
160 
161             if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
162                 templates = JournalTemplateLocalServiceUtil.getTemplates(
163                     DEFAULT_GROUP_ID);
164 
165                 for (int i = 0; i < templates.size(); i++) {
166                     template = templates.get(i);
167 
168                     JournalTemplateLocalServiceUtil.checkNewLine(
169                         template.getGroupId(), template.getTemplateId());
170                 }
171             }
172         }
173     }
174 
175     protected void verifyJournal() throws Exception {
176 
177         // Oracle new line
178 
179         verifyOracleNewLine();
180 
181         // Structures
182 
183         List<JournalStructure> structures =
184             JournalStructureLocalServiceUtil.getStructures();
185 
186         for (JournalStructure structure : structures) {
187             ResourceLocalServiceUtil.addResources(
188                 structure.getCompanyId(), 0, 0,
189                 JournalStructure.class.getName(), structure.getId(), false,
190                 true, true);
191         }
192 
193         if (_log.isDebugEnabled()) {
194             _log.debug("Permissions verified for Journal structures");
195         }
196 
197         // Templates
198 
199         List<JournalTemplate> templates =
200             JournalTemplateLocalServiceUtil.getTemplates();
201 
202         for (JournalTemplate template : templates) {
203             ResourceLocalServiceUtil.addResources(
204                 template.getCompanyId(), 0, 0,
205                 JournalTemplate.class.getName(), template.getId(), false, true,
206                 true);
207         }
208 
209         if (_log.isDebugEnabled()) {
210             _log.debug("Permissions verified for Journal templates");
211         }
212 
213         // Articles
214 
215         List<JournalArticle> articles =
216             JournalArticleLocalServiceUtil.getArticles();
217 
218         for (JournalArticle article : articles) {
219             long groupId = article.getGroupId();
220             String articleId = article.getArticleId();
221             double version = article.getVersion();
222             String structureId = article.getStructureId();
223 
224             if (article.getResourcePrimKey() <= 0) {
225                 article =
226                     JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
227                         groupId, articleId, version);
228             }
229 
230             ResourceLocalServiceUtil.addResources(
231                 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
232                 article.getResourcePrimKey(), false, true, true);
233 
234             try {
235                 TagsAssetLocalServiceUtil.getAsset(
236                     JournalArticle.class.getName(),
237                     article.getResourcePrimKey());
238             }
239             catch (NoSuchAssetException nsae) {
240                 try {
241                     JournalArticleLocalServiceUtil.updateTagsAsset(
242                         article.getUserId(), article, new String[0],
243                         new String[0]);
244                 }
245                 catch (Exception e) {
246                     if (_log.isWarnEnabled()) {
247                         _log.warn(
248                             "Unable to update tags asset for article " +
249                                 article.getId() + ": " + e.getMessage());
250                     }
251                 }
252             }
253 
254             String content = GetterUtil.getString(article.getContent());
255 
256             String newContent = HtmlUtil.replaceMsWordCharacters(content);
257 
258             if (Validator.isNotNull(structureId)) {
259                 /*JournalStructure structure =
260                     JournalStructureLocalServiceUtil.getStructure(
261                         groupId, structureId);
262 
263                 newContent = JournalUtil.removeOldContent(
264                     newContent, structure.getXsd());*/
265             }
266 
267             if (!content.equals(newContent)) {
268                 JournalArticleLocalServiceUtil.updateContent(
269                     groupId, articleId, version, newContent);
270             }
271 
272             JournalArticleLocalServiceUtil.checkStructure(
273                 groupId, articleId, version);
274         }
275 
276         if (_log.isDebugEnabled()) {
277             _log.debug(
278                 "Permissions and Tags assets verified for Journal articles");
279         }
280     }
281 
282     private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
283 
284 }