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.util.PropsUtil;
27  import com.liferay.portlet.journal.action.ExportAction;
28  import com.liferay.portlet.journal.model.JournalArticle;
29  import com.liferay.portlet.journal.model.JournalStructure;
30  import com.liferay.portlet.journal.model.JournalTemplate;
31  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
33  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
34  
35  import java.util.List;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="VerifyOracle.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class VerifyOracle extends VerifyProcess {
47  
48      public static final int NUM_OF_ARTICLES = GetterUtil.getInteger(
49          PropsUtil.get(VerifyOracle.class.getName()), 5);
50  
51      public void verify() throws VerifyException {
52          _log.info("Verifying");
53  
54          try {
55              verifyOracle();
56          }
57          catch (Exception e) {
58              throw new VerifyException(e);
59          }
60      }
61  
62      protected void verifyOracle() throws Exception {
63  
64          // This is a workaround for a limitation in Oracle sqlldr's inability
65          // insert new line characters for long varchar columns. See
66          // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
67          // information. Check several articles because some articles may not
68          // have new lines.
69  
70          boolean checkNewLine = false;
71  
72          List<JournalArticle> articles = null;
73  
74          if (NUM_OF_ARTICLES <= 0) {
75              checkNewLine = true;
76  
77              articles = JournalArticleLocalServiceUtil.getArticles(
78                  ExportAction.DEFAULT_GROUP_ID);
79          }
80          else {
81              articles = JournalArticleLocalServiceUtil.getArticles(
82                  ExportAction.DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
83          }
84  
85          for (int i = 0; i < articles.size(); i++) {
86              JournalArticle article = articles.get(i);
87  
88              String content = article.getContent();
89  
90              if ((content != null) && (content.indexOf("\\n") != -1)) {
91                  articles = JournalArticleLocalServiceUtil.getArticles(
92                      ExportAction.DEFAULT_GROUP_ID);
93  
94                  for (int j = 0; j < articles.size(); j++) {
95                      article = articles.get(j);
96  
97                      JournalArticleLocalServiceUtil.checkNewLine(
98                          article.getGroupId(), article.getArticleId(),
99                          article.getVersion());
100                 }
101 
102                 checkNewLine = true;
103 
104                 break;
105             }
106         }
107 
108         // Only process this once
109 
110         if (!checkNewLine) {
111             if (_log.isInfoEnabled()) {
112                 _log.debug("Do not fix oracle new line");
113             }
114 
115             return;
116         }
117         else {
118             if (_log.isInfoEnabled()) {
119                 _log.info("Fix oracle new line");
120             }
121         }
122 
123         List<JournalStructure> structures =
124             JournalStructureLocalServiceUtil.getStructures(
125                 ExportAction.DEFAULT_GROUP_ID, 0, 1);
126 
127         if (structures.size() == 1) {
128             JournalStructure structure = structures.get(0);
129 
130             String xsd = structure.getXsd();
131 
132             if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
133                 structures = JournalStructureLocalServiceUtil.getStructures(
134                     ExportAction.DEFAULT_GROUP_ID);
135 
136                 for (int i = 0; i < structures.size(); i++) {
137                     structure = structures.get(i);
138 
139                     JournalStructureLocalServiceUtil.checkNewLine(
140                         structure.getGroupId(), structure.getStructureId());
141                 }
142             }
143         }
144 
145         List<JournalTemplate> templates =
146             JournalTemplateLocalServiceUtil.getTemplates(
147                 ExportAction.DEFAULT_GROUP_ID, 0, 1);
148 
149         if (templates.size() == 1) {
150             JournalTemplate template = templates.get(0);
151 
152             String xsl = template.getXsl();
153 
154             if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
155                 templates = JournalTemplateLocalServiceUtil.getTemplates(
156                     ExportAction.DEFAULT_GROUP_ID);
157 
158                 for (int i = 0; i < templates.size(); i++) {
159                     template = templates.get(i);
160 
161                     JournalTemplateLocalServiceUtil.checkNewLine(
162                         template.getGroupId(), template.getTemplateId());
163                 }
164             }
165         }
166     }
167 
168     private static Log _log = LogFactory.getLog(VerifyOracle.class);
169 
170 }