001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
040            protected void doVerify() throws Exception {
041                    checkFileEntryType();
042                    removeOrphanedFileEntries();
043                    updateAssets();
044            }
045    
046            protected void checkFileEntryType() throws Exception {
047                    DLFileEntryType dlFileEntryType =
048                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
049                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
050    
051                    if (dlFileEntryType != null) {
052                            return;
053                    }
054    
055                    Date now = new Date();
056    
057                    dlFileEntryType = DLFileEntryTypeLocalServiceUtil.createDLFileEntryType(
058                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);
059    
060                    dlFileEntryType.setCreateDate(now);
061                    dlFileEntryType.setModifiedDate(now);
062                    dlFileEntryType.setName(DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT);
063    
064                    DLFileEntryTypeLocalServiceUtil.updateDLFileEntryType(
065                            dlFileEntryType, false);
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    }