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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.FileVersion;
021    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
022    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
026    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Raymond Augé
035     * @author Douglas Wong
036     */
037    public class VerifyDocumentLibrary extends VerifyProcess {
038    
039            protected void checkFileEntryType() throws Exception {
040                    DLFileEntryType dlFileEntryType =
041                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
042                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
043    
044                    if (dlFileEntryType != null) {
045                            return;
046                    }
047    
048                    Date now = new Date();
049    
050                    dlFileEntryType = DLFileEntryTypeLocalServiceUtil.createDLFileEntryType(
051                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
052    
053                    dlFileEntryType.setCreateDate(now);
054                    dlFileEntryType.setModifiedDate(now);
055                    dlFileEntryType.setName(DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT);
056    
057                    DLFileEntryTypeLocalServiceUtil.updateDLFileEntryType(
058                            dlFileEntryType, false);
059            }
060    
061            @Override
062            protected void doVerify() throws Exception {
063                    checkFileEntryType();
064                    removeOrphanedFileEntries();
065                    updateAssets();
066            }
067    
068            protected void removeOrphanedFileEntries() throws Exception {
069                    List<DLFileEntry> dlFileEntries =
070                            DLFileEntryLocalServiceUtil.getOrphanedFileEntries();
071    
072                    if (_log.isDebugEnabled()) {
073                            _log.debug(
074                                    "Processing " + dlFileEntries.size() +
075                                            " file entries with no group");
076                    }
077    
078                    for (DLFileEntry dlFileEntry : dlFileEntries) {
079                            try {
080                                    DLFileEntryLocalServiceUtil.deleteFileEntry(
081                                            dlFileEntry.getFileEntryId());
082                            }
083                            catch (Exception e) {
084                                    if (_log.isWarnEnabled()) {
085                                            _log.warn(
086                                                    "Unable to remove file entry " +
087                                                            dlFileEntry.getFileEntryId() + ": " +
088                                                                    e.getMessage());
089                                    }
090                            }
091                    }
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Removed orphaned file entries");
095                    }
096            }
097    
098            protected void updateAssets() throws Exception {
099                    List<DLFileEntry> dlFileEntries =
100                            DLFileEntryLocalServiceUtil.getNoAssetFileEntries();
101    
102                    if (_log.isDebugEnabled()) {
103                            _log.debug(
104                                    "Processing " + dlFileEntries.size() +
105                                            " file entries with no asset");
106                    }
107    
108                    for (DLFileEntry dlFileEntry : dlFileEntries) {
109                            FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
110                            FileVersion fileVersion = new LiferayFileVersion(
111                                    dlFileEntry.getFileVersion());
112    
113                            try {
114                                    DLAppHelperLocalServiceUtil.updateAsset(
115                                            dlFileEntry.getUserId(), fileEntry, fileVersion, null, null,
116                                            null);
117                            }
118                            catch (Exception e) {
119                                    if (_log.isWarnEnabled()) {
120                                            _log.warn(
121                                                    "Unable to update asset for file entry " +
122                                                            dlFileEntry.getFileEntryId() + ": " +
123                                                                    e.getMessage());
124                                    }
125                            }
126                    }
127    
128                    if (_log.isDebugEnabled()) {
129                            _log.debug("Assets verified for file entries");
130                    }
131            }
132    
133            private static Log _log = LogFactoryUtil.getLog(
134                    VerifyDocumentLibrary.class);
135    
136    }