001    /**
002     * Copyright (c) 2000-2012 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.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.service.ResourceLocalServiceUtil;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.asset.NoSuchEntryException;
029    import com.liferay.portlet.asset.model.AssetEntry;
030    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
031    import com.liferay.portlet.journal.model.JournalArticle;
032    import com.liferay.portlet.journal.model.JournalArticleConstants;
033    import com.liferay.portlet.journal.model.JournalContentSearch;
034    import com.liferay.portlet.journal.model.JournalStructure;
035    import com.liferay.portlet.journal.model.JournalTemplate;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
038    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
039    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
040    
041    import java.sql.Connection;
042    import java.sql.PreparedStatement;
043    import java.sql.ResultSet;
044    
045    import java.util.HashSet;
046    import java.util.List;
047    import java.util.Set;
048    
049    import javax.portlet.PortletPreferences;
050    
051    /**
052     * @author Alexander Chow
053     * @author Shinn Lok
054     */
055    public class VerifyJournal extends VerifyProcess {
056    
057            public static final long DEFAULT_GROUP_ID = 14;
058    
059            public static final int NUM_OF_ARTICLES = 5;
060    
061            @Override
062            protected void doVerify() throws Exception {
063    
064                    // Oracle new line
065    
066                    verifyOracleNewLine();
067    
068                    // Structures
069    
070                    List<JournalStructure> structures =
071                            JournalStructureLocalServiceUtil.getStructures();
072    
073                    for (JournalStructure structure : structures) {
074                            ResourceLocalServiceUtil.addResources(
075                                    structure.getCompanyId(), 0, 0,
076                                    JournalStructure.class.getName(), structure.getId(), false,
077                                    false, false);
078                    }
079    
080                    if (_log.isDebugEnabled()) {
081                            _log.debug("Permissions verified for structures");
082                    }
083    
084                    // Templates
085    
086                    List<JournalTemplate> templates =
087                            JournalTemplateLocalServiceUtil.getTemplates();
088    
089                    for (JournalTemplate template : templates) {
090                            ResourceLocalServiceUtil.addResources(
091                                    template.getCompanyId(), 0, 0, JournalTemplate.class.getName(),
092                                    template.getId(), false, false, false);
093                    }
094    
095                    if (_log.isDebugEnabled()) {
096                            _log.debug("Permissions verified for templates");
097                    }
098    
099                    // Articles
100    
101                    List<JournalArticle> articles =
102                            JournalArticleLocalServiceUtil.getArticles();
103    
104                    for (JournalArticle article : articles) {
105                            long groupId = article.getGroupId();
106                            String articleId = article.getArticleId();
107                            double version = article.getVersion();
108                            String structureId = article.getStructureId();
109    
110                            if (article.getResourcePrimKey() <= 0) {
111                                    article =
112                                            JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
113                                                    groupId, articleId, version);
114                            }
115    
116                            ResourceLocalServiceUtil.addResources(
117                                    article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
118                                    article.getResourcePrimKey(), false, false, false);
119    
120                            try {
121                                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
122                                            JournalArticle.class.getName(),
123                                            article.getResourcePrimKey());
124    
125                                    if ((article.getStatus() == WorkflowConstants.STATUS_DRAFT) &&
126                                            (article.getVersion() ==
127                                                    JournalArticleConstants.VERSION_DEFAULT)) {
128    
129                                            AssetEntryLocalServiceUtil.updateEntry(
130                                                    assetEntry.getClassName(), assetEntry.getClassPK(),
131                                                    null, assetEntry.isVisible());
132                                    }
133                            }
134                            catch (NoSuchEntryException nsee) {
135                                    try {
136                                            JournalArticleLocalServiceUtil.updateAsset(
137                                                    article.getUserId(), article, null, null, null);
138                                    }
139                                    catch (Exception e) {
140                                            if (_log.isWarnEnabled()) {
141                                                    _log.warn(
142                                                            "Unable to update asset for article " +
143                                                                    article.getId() + ": " + e.getMessage());
144                                            }
145                                    }
146                            }
147    
148                            String content = GetterUtil.getString(article.getContent());
149    
150                            String newContent = HtmlUtil.replaceMsWordCharacters(content);
151    
152                            if (Validator.isNotNull(structureId)) {
153                                    /*JournalStructure structure =
154                                            JournalStructureLocalServiceUtil.getStructure(
155                                                    groupId, structureId);
156    
157                                    newContent = JournalUtil.removeOldContent(
158                                            newContent, structure.getXsd());*/
159                            }
160    
161                            if (!content.equals(newContent)) {
162                                    JournalArticleLocalServiceUtil.updateContent(
163                                            groupId, articleId, version, newContent);
164                            }
165    
166                            JournalArticleLocalServiceUtil.checkStructure(
167                                    groupId, articleId, version);
168                    }
169    
170                    if (_log.isDebugEnabled()) {
171                            _log.debug("Permissions and assets verified for articles");
172                    }
173    
174                    // Content searches
175    
176                    verifyContentSearch();
177            }
178    
179            protected void verifyContentSearch() throws Exception {
180                    List<JournalContentSearch> contentSearches =
181                            JournalContentSearchLocalServiceUtil.getArticleContentSearches();
182    
183                    Set<String> portletIds = new HashSet<String>();
184    
185                    for (JournalContentSearch contentSearch : contentSearches) {
186                            portletIds.add(contentSearch.getPortletId());
187                    }
188    
189                    for (String portletId : portletIds) {
190                            verifyContentSearch(portletId);
191                    }
192            }
193    
194            protected void verifyContentSearch(String portletId) throws Exception {
195                    List<JournalContentSearch> contentSearches =
196                            JournalContentSearchLocalServiceUtil.getPortletContentSearches(
197                                    portletId);
198    
199                    if (contentSearches.size() <= 1) {
200                            return;
201                    }
202    
203                    Connection con = null;
204                    PreparedStatement ps = null;
205                    ResultSet rs = null;
206    
207                    try {
208                            con = DataAccess.getUpgradeOptimizedConnection();
209    
210                            ps = con.prepareStatement(
211                                    "select preferences from PortletPreferences where portletId " +
212                                            "= ?");
213    
214                            ps.setString(1, portletId);
215    
216                            rs = ps.executeQuery();
217    
218                            while (rs.next()) {
219                                    String xml = rs.getString("preferences");
220    
221                                    PortletPreferences portletPreferences =
222                                            PortletPreferencesFactoryUtil.fromDefaultXML(xml);
223    
224                                    String articleId = portletPreferences.getValue(
225                                            "articleId", null);
226    
227                                    JournalContentSearch contentSearch = contentSearches.get(1);
228    
229                                    JournalContentSearchLocalServiceUtil.updateContentSearch(
230                                            contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
231                                            contentSearch.getLayoutId(), contentSearch.getPortletId(),
232                                            articleId, true);
233                            }
234                    }
235                    finally {
236                            DataAccess.cleanUp(con, ps, rs);
237                    }
238            }
239    
240            protected void verifyOracleNewLine() throws Exception {
241                    DB db = DBFactoryUtil.getDB();
242    
243                    String dbType = db.getType();
244    
245                    if (!dbType.equals(DB.TYPE_ORACLE)) {
246                            return;
247                    }
248    
249                    // This is a workaround for a limitation in Oracle sqlldr's inability
250                    // insert new line characters for long varchar columns. See
251                    // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
252                    // information. Check several articles because some articles may not
253                    // have new lines.
254    
255                    boolean checkNewLine = false;
256    
257                    List<JournalArticle> articles =
258                            JournalArticleLocalServiceUtil.getArticles(
259                                    DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
260    
261                    for (JournalArticle article : articles) {
262                            String content = article.getContent();
263    
264                            if ((content != null) && content.contains("\\n")) {
265                                    articles = JournalArticleLocalServiceUtil.getArticles(
266                                            DEFAULT_GROUP_ID);
267    
268                                    for (int j = 0; j < articles.size(); j++) {
269                                            article = articles.get(j);
270    
271                                            JournalArticleLocalServiceUtil.checkNewLine(
272                                                    article.getGroupId(), article.getArticleId(),
273                                                    article.getVersion());
274                                    }
275    
276                                    checkNewLine = true;
277    
278                                    break;
279                            }
280                    }
281    
282                    // Only process this once
283    
284                    if (!checkNewLine) {
285                            if (_log.isInfoEnabled()) {
286                                    _log.debug("Do not fix oracle new line");
287                            }
288    
289                            return;
290                    }
291                    else {
292                            if (_log.isInfoEnabled()) {
293                                    _log.info("Fix oracle new line");
294                            }
295                    }
296    
297                    List<JournalStructure> structures =
298                            JournalStructureLocalServiceUtil.getStructures(
299                                    DEFAULT_GROUP_ID, 0, 1);
300    
301                    if (structures.size() == 1) {
302                            JournalStructure structure = structures.get(0);
303    
304                            String xsd = structure.getXsd();
305    
306                            if ((xsd != null) && xsd.contains("\\n")) {
307                                    structures = JournalStructureLocalServiceUtil.getStructures(
308                                            DEFAULT_GROUP_ID);
309    
310                                    for (int i = 0; i < structures.size(); i++) {
311                                            structure = structures.get(i);
312    
313                                            JournalStructureLocalServiceUtil.checkNewLine(
314                                                    structure.getGroupId(), structure.getStructureId());
315                                    }
316                            }
317                    }
318    
319                    List<JournalTemplate> templates =
320                            JournalTemplateLocalServiceUtil.getTemplates(
321                                    DEFAULT_GROUP_ID, 0, 1);
322    
323                    if (templates.size() == 1) {
324                            JournalTemplate template = templates.get(0);
325    
326                            String xsl = template.getXsl();
327    
328                            if ((xsl != null) && xsl.contains("\\n")) {
329                                    templates = JournalTemplateLocalServiceUtil.getTemplates(
330                                            DEFAULT_GROUP_ID);
331    
332                                    for (int i = 0; i < templates.size(); i++) {
333                                            template = templates.get(i);
334    
335                                            JournalTemplateLocalServiceUtil.checkNewLine(
336                                                    template.getGroupId(), template.getTemplateId());
337                                    }
338                            }
339                    }
340            }
341    
342            private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
343    
344    }