001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            protected void doVerify() throws Exception {
046    
047                    // Oracle new line
048    
049                    verifyOracleNewLine();
050    
051                    // Structures
052    
053                    List<JournalStructure> structures =
054                            JournalStructureLocalServiceUtil.getStructures();
055    
056                    for (JournalStructure structure : structures) {
057                            ResourceLocalServiceUtil.addResources(
058                                    structure.getCompanyId(), 0, 0,
059                                    JournalStructure.class.getName(), structure.getId(), false,
060                                    true, true);
061                    }
062    
063                    if (_log.isDebugEnabled()) {
064                            _log.debug("Permissions verified for structures");
065                    }
066    
067                    // Templates
068    
069                    List<JournalTemplate> templates =
070                            JournalTemplateLocalServiceUtil.getTemplates();
071    
072                    for (JournalTemplate template : templates) {
073                            ResourceLocalServiceUtil.addResources(
074                                    template.getCompanyId(), 0, 0,
075                                    JournalTemplate.class.getName(), template.getId(), false, true,
076                                    true);
077                    }
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug("Permissions verified for templates");
081                    }
082    
083                    // Articles
084    
085                    List<JournalArticle> articles =
086                            JournalArticleLocalServiceUtil.getArticles();
087    
088                    for (JournalArticle article : articles) {
089                            long groupId = article.getGroupId();
090                            String articleId = article.getArticleId();
091                            double version = article.getVersion();
092                            String structureId = article.getStructureId();
093    
094                            if (article.getResourcePrimKey() <= 0) {
095                                    article =
096                                            JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
097                                                    groupId, articleId, version);
098                            }
099    
100                            ResourceLocalServiceUtil.addResources(
101                                    article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
102                                    article.getResourcePrimKey(), false, true, true);
103    
104                            try {
105                                    AssetEntryLocalServiceUtil.getEntry(
106                                            JournalArticle.class.getName(),
107                                            article.getResourcePrimKey());
108                            }
109                            catch (NoSuchEntryException nsee) {
110                                    try {
111                                            JournalArticleLocalServiceUtil.updateAsset(
112                                                    article.getUserId(), article, null, null);
113                                    }
114                                    catch (Exception e) {
115                                            if (_log.isWarnEnabled()) {
116                                                    _log.warn(
117                                                            "Unable to update asset for article " +
118                                                                    article.getId() + ": " + e.getMessage());
119                                            }
120                                    }
121                            }
122    
123                            String content = GetterUtil.getString(article.getContent());
124    
125                            String newContent = HtmlUtil.replaceMsWordCharacters(content);
126    
127                            if (Validator.isNotNull(structureId)) {
128                                    /*JournalStructure structure =
129                                            JournalStructureLocalServiceUtil.getStructure(
130                                                    groupId, structureId);
131    
132                                    newContent = JournalUtil.removeOldContent(
133                                            newContent, structure.getXsd());*/
134                            }
135    
136                            if (!content.equals(newContent)) {
137                                    JournalArticleLocalServiceUtil.updateContent(
138                                            groupId, articleId, version, newContent);
139                            }
140    
141                            JournalArticleLocalServiceUtil.checkStructure(
142                                    groupId, articleId, version);
143                    }
144    
145                    if (_log.isDebugEnabled()) {
146                            _log.debug("Permissions and assets verified for articles");
147                    }
148            }
149    
150            protected void verifyOracleNewLine() throws Exception {
151                    DB db = DBFactoryUtil.getDB();
152    
153                    if (!db.getType().equals(DB.TYPE_ORACLE)) {
154                            return;
155                    }
156    
157                    // This is a workaround for a limitation in Oracle sqlldr's inability
158                    // insert new line characters for long varchar columns. See
159                    // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
160                    // information. Check several articles because some articles may not
161                    // have new lines.
162    
163                    boolean checkNewLine = false;
164    
165                    List<JournalArticle> articles =
166                            JournalArticleLocalServiceUtil.getArticles(
167                                    DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
168    
169                    for (JournalArticle article : articles) {
170                            String content = article.getContent();
171    
172                            if ((content != null) && (content.indexOf("\\n") != -1)) {
173                                    articles = JournalArticleLocalServiceUtil.getArticles(
174                                            DEFAULT_GROUP_ID);
175    
176                                    for (int j = 0; j < articles.size(); j++) {
177                                            article = articles.get(j);
178    
179                                            JournalArticleLocalServiceUtil.checkNewLine(
180                                                    article.getGroupId(), article.getArticleId(),
181                                                    article.getVersion());
182                                    }
183    
184                                    checkNewLine = true;
185    
186                                    break;
187                            }
188                    }
189    
190                    // Only process this once
191    
192                    if (!checkNewLine) {
193                            if (_log.isInfoEnabled()) {
194                                    _log.debug("Do not fix oracle new line");
195                            }
196    
197                            return;
198                    }
199                    else {
200                            if (_log.isInfoEnabled()) {
201                                    _log.info("Fix oracle new line");
202                            }
203                    }
204    
205                    List<JournalStructure> structures =
206                            JournalStructureLocalServiceUtil.getStructures(
207                                    DEFAULT_GROUP_ID, 0, 1);
208    
209                    if (structures.size() == 1) {
210                            JournalStructure structure = structures.get(0);
211    
212                            String xsd = structure.getXsd();
213    
214                            if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
215                                    structures = JournalStructureLocalServiceUtil.getStructures(
216                                            DEFAULT_GROUP_ID);
217    
218                                    for (int i = 0; i < structures.size(); i++) {
219                                            structure = structures.get(i);
220    
221                                            JournalStructureLocalServiceUtil.checkNewLine(
222                                                    structure.getGroupId(), structure.getStructureId());
223                                    }
224                            }
225                    }
226    
227                    List<JournalTemplate> templates =
228                            JournalTemplateLocalServiceUtil.getTemplates(
229                                    DEFAULT_GROUP_ID, 0, 1);
230    
231                    if (templates.size() == 1) {
232                            JournalTemplate template = templates.get(0);
233    
234                            String xsl = template.getXsl();
235    
236                            if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
237                                    templates = JournalTemplateLocalServiceUtil.getTemplates(
238                                            DEFAULT_GROUP_ID);
239    
240                                    for (int i = 0; i < templates.size(); i++) {
241                                            template = templates.get(i);
242    
243                                            JournalTemplateLocalServiceUtil.checkNewLine(
244                                                    template.getGroupId(), template.getTemplateId());
245                                    }
246                            }
247                    }
248            }
249    
250            private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
251    
252    }