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