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