001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.service.ResourceLocalServiceUtil;
025    import com.liferay.portlet.asset.NoSuchEntryException;
026    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
027    import com.liferay.portlet.journal.model.JournalArticle;
028    import com.liferay.portlet.journal.model.JournalStructure;
029    import com.liferay.portlet.journal.model.JournalTemplate;
030    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
031    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
032    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
033    
034    import java.util.List;
035    
036    /**
037     * @author Alexander Chow
038     */
039    public class VerifyJournal extends VerifyProcess {
040    
041            public static final long DEFAULT_GROUP_ID = 14;
042    
043            public static final int NUM_OF_ARTICLES = 5;
044    
045            @Override
046            protected void doVerify() throws Exception {
047    
048                    // Oracle new line
049    
050                    verifyOracleNewLine();
051    
052                    // Structures
053    
054                    List<JournalStructure> structures =
055                            JournalStructureLocalServiceUtil.getStructures();
056    
057                    for (JournalStructure structure : structures) {
058                            ResourceLocalServiceUtil.addResources(
059                                    structure.getCompanyId(), 0, 0,
060                                    JournalStructure.class.getName(), structure.getId(), false,
061                                    false, false);
062                    }
063    
064                    if (_log.isDebugEnabled()) {
065                            _log.debug("Permissions verified for structures");
066                    }
067    
068                    // Templates
069    
070                    List<JournalTemplate> templates =
071                            JournalTemplateLocalServiceUtil.getTemplates();
072    
073                    for (JournalTemplate template : templates) {
074                            ResourceLocalServiceUtil.addResources(
075                                    template.getCompanyId(), 0, 0,
076                                    JournalTemplate.class.getName(), template.getId(), false, false,
077                                    false);
078                    }
079    
080                    if (_log.isDebugEnabled()) {
081                            _log.debug("Permissions verified for templates");
082                    }
083    
084                    // Articles
085    
086                    List<JournalArticle> articles =
087                            JournalArticleLocalServiceUtil.getArticles();
088    
089                    for (JournalArticle article : articles) {
090                            long groupId = article.getGroupId();
091                            String articleId = article.getArticleId();
092                            double version = article.getVersion();
093                            String structureId = article.getStructureId();
094    
095                            if (article.getResourcePrimKey() <= 0) {
096                                    article =
097                                            JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
098                                                    groupId, articleId, version);
099                            }
100    
101                            ResourceLocalServiceUtil.addResources(
102                                    article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
103                                    article.getResourcePrimKey(), false, false, false);
104    
105                            try {
106                                    AssetEntryLocalServiceUtil.getEntry(
107                                            JournalArticle.class.getName(),
108                                            article.getResourcePrimKey());
109                            }
110                            catch (NoSuchEntryException nsee) {
111                                    try {
112                                            JournalArticleLocalServiceUtil.updateAsset(
113                                                    article.getUserId(), article, null, null, null);
114                                    }
115                                    catch (Exception e) {
116                                            if (_log.isWarnEnabled()) {
117                                                    _log.warn(
118                                                            "Unable to update asset for article " +
119                                                                    article.getId() + ": " + e.getMessage());
120                                            }
121                                    }
122                            }
123    
124                            String content = GetterUtil.getString(article.getContent());
125    
126                            String newContent = HtmlUtil.replaceMsWordCharacters(content);
127    
128                            if (Validator.isNotNull(structureId)) {
129                                    /*JournalStructure structure =
130                                            JournalStructureLocalServiceUtil.getStructure(
131                                                    groupId, structureId);
132    
133                                    newContent = JournalUtil.removeOldContent(
134                                            newContent, structure.getXsd());*/
135                            }
136    
137                            if (!content.equals(newContent)) {
138                                    JournalArticleLocalServiceUtil.updateContent(
139                                            groupId, articleId, version, newContent);
140                            }
141    
142                            JournalArticleLocalServiceUtil.checkStructure(
143                                    groupId, articleId, version);
144                    }
145    
146                    if (_log.isDebugEnabled()) {
147                            _log.debug("Permissions and assets verified for articles");
148                    }
149            }
150    
151            protected void verifyOracleNewLine() throws Exception {
152                    DB db = DBFactoryUtil.getDB();
153    
154                    String dbType = db.getType();
155    
156                    if (!dbType.equals(DB.TYPE_ORACLE)) {
157                            return;
158                    }
159    
160                    // This is a workaround for a limitation in Oracle sqlldr's inability
161                    // insert new line characters for long varchar columns. See
162                    // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
163                    // information. Check several articles because some articles may not
164                    // have new lines.
165    
166                    boolean checkNewLine = false;
167    
168                    List<JournalArticle> articles =
169                            JournalArticleLocalServiceUtil.getArticles(
170                                    DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
171    
172                    for (JournalArticle article : articles) {
173                            String content = article.getContent();
174    
175                            if ((content != null) && (content.indexOf("\\n") != -1)) {
176                                    articles = JournalArticleLocalServiceUtil.getArticles(
177                                            DEFAULT_GROUP_ID);
178    
179                                    for (int j = 0; j < articles.size(); j++) {
180                                            article = articles.get(j);
181    
182                                            JournalArticleLocalServiceUtil.checkNewLine(
183                                                    article.getGroupId(), article.getArticleId(),
184                                                    article.getVersion());
185                                    }
186    
187                                    checkNewLine = true;
188    
189                                    break;
190                            }
191                    }
192    
193                    // Only process this once
194    
195                    if (!checkNewLine) {
196                            if (_log.isInfoEnabled()) {
197                                    _log.debug("Do not fix oracle new line");
198                            }
199    
200                            return;
201                    }
202                    else {
203                            if (_log.isInfoEnabled()) {
204                                    _log.info("Fix oracle new line");
205                            }
206                    }
207    
208                    List<JournalStructure> structures =
209                            JournalStructureLocalServiceUtil.getStructures(
210                                    DEFAULT_GROUP_ID, 0, 1);
211    
212                    if (structures.size() == 1) {
213                            JournalStructure structure = structures.get(0);
214    
215                            String xsd = structure.getXsd();
216    
217                            if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
218                                    structures = JournalStructureLocalServiceUtil.getStructures(
219                                            DEFAULT_GROUP_ID);
220    
221                                    for (int i = 0; i < structures.size(); i++) {
222                                            structure = structures.get(i);
223    
224                                            JournalStructureLocalServiceUtil.checkNewLine(
225                                                    structure.getGroupId(), structure.getStructureId());
226                                    }
227                            }
228                    }
229    
230                    List<JournalTemplate> templates =
231                            JournalTemplateLocalServiceUtil.getTemplates(
232                                    DEFAULT_GROUP_ID, 0, 1);
233    
234                    if (templates.size() == 1) {
235                            JournalTemplate template = templates.get(0);
236    
237                            String xsl = template.getXsl();
238    
239                            if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
240                                    templates = JournalTemplateLocalServiceUtil.getTemplates(
241                                            DEFAULT_GROUP_ID);
242    
243                                    for (int i = 0; i < templates.size(); i++) {
244                                            template = templates.get(i);
245    
246                                            JournalTemplateLocalServiceUtil.checkNewLine(
247                                                    template.getGroupId(), template.getTemplateId());
248                                    }
249                            }
250                    }
251            }
252    
253            private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
254    
255    }