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