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