001    /**
002     * Copyright (c) 2000-present 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.asset.kernel.service.AssetCategoryLocalServiceUtil;
018    import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
019    import com.liferay.document.library.kernel.model.DLFileEntry;
020    import com.liferay.document.library.kernel.model.DLFileEntryConstants;
021    import com.liferay.document.library.kernel.service.DLFileEntryLocalServiceUtil;
022    import com.liferay.portal.kernel.util.LoggingTimer;
023    import com.liferay.portal.kernel.util.PortalUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Douglas Wong
031     */
032    public class VerifyAsset extends VerifyProcess {
033    
034            protected void deleteOrphanedAssetEntries() throws Exception {
035                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
036                            long classNameId = PortalUtil.getClassNameId(
037                                    DLFileEntryConstants.getClassName());
038    
039                            StringBundler sb = new StringBundler(5);
040    
041                            sb.append("select classPK, entryId from AssetEntry where ");
042                            sb.append("classNameId = ");
043                            sb.append(classNameId);
044                            sb.append(" and classPK not in (select fileVersionId from ");
045                            sb.append("DLFileVersion)");
046    
047                            try (PreparedStatement ps = connection.prepareStatement(
048                                            sb.toString());
049                                    ResultSet rs = ps.executeQuery()) {
050    
051                                    while (rs.next()) {
052                                            long classPK = rs.getLong("classPK");
053                                            long entryId = rs.getLong("entryId");
054    
055                                            DLFileEntry dlFileEntry =
056                                                    DLFileEntryLocalServiceUtil.fetchDLFileEntry(classPK);
057    
058                                            if (dlFileEntry == null) {
059                                                    AssetEntryLocalServiceUtil.deleteAssetEntry(entryId);
060                                            }
061                                    }
062                            }
063                    }
064            }
065    
066            @Override
067            protected void doVerify() throws Exception {
068                    deleteOrphanedAssetEntries();
069                    rebuildTree();
070            }
071    
072            protected void rebuildTree() throws Exception {
073                    try (LoggingTimer loggingTimer = new LoggingTimer();
074                            PreparedStatement ps = connection.prepareStatement(
075                                    "select distinct groupId from AssetCategory where " +
076                                            "(leftCategoryId is null) or (rightCategoryId is null)");
077                            ResultSet rs = ps.executeQuery()) {
078    
079                            while (rs.next()) {
080                                    long groupId = rs.getLong("groupId");
081    
082                                    AssetCategoryLocalServiceUtil.rebuildTree(groupId, true);
083                            }
084                    }
085            }
086    
087    }