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.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                    PreparedStatement ps = null;
036                    ResultSet rs = null;
037    
038                    try {
039                            long classNameId = PortalUtil.getClassNameId(
040                                    DLFileEntryConstants.getClassName());
041    
042                            StringBundler sb = new StringBundler(5);
043    
044                            sb.append("select classPK, entryId from AssetEntry where ");
045                            sb.append("classNameId = ");
046                            sb.append(classNameId);
047                            sb.append(" and classPK not in (select fileVersionId from ");
048                            sb.append("DLFileVersion)");
049    
050                            ps = connection.prepareStatement(sb.toString());
051    
052                            rs = ps.executeQuery();
053    
054                            while (rs.next()) {
055                                    long classPK = rs.getLong("classPK");
056                                    long entryId = rs.getLong("entryId");
057    
058                                    DLFileEntry dlFileEntry =
059                                            DLFileEntryLocalServiceUtil.fetchDLFileEntry(classPK);
060    
061                                    if (dlFileEntry == null) {
062                                            AssetEntryLocalServiceUtil.deleteAssetEntry(entryId);
063                                    }
064                            }
065                    }
066                    finally {
067                            DataAccess.cleanUp(ps, rs);
068                    }
069            }
070    
071            @Override
072            protected void doVerify() throws Exception {
073                    deleteOrphanedAssetEntries();
074                    rebuildTree();
075            }
076    
077            protected void rebuildTree() throws Exception {
078                    PreparedStatement ps = null;
079                    ResultSet rs = null;
080    
081                    try {
082                            ps = connection.prepareStatement(
083                                    "select distinct groupId from AssetCategory where " +
084                                            "(leftCategoryId is null) or (rightCategoryId is null)");
085    
086                            rs = ps.executeQuery();
087    
088                            while (rs.next()) {
089                                    long groupId = rs.getLong("groupId");
090    
091                                    AssetCategoryLocalServiceUtil.rebuildTree(groupId, true);
092                            }
093                    }
094                    finally {
095                            DataAccess.cleanUp(ps, rs);
096                    }
097            }
098    
099    }