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.portlet.documentlibrary.messaging;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.messaging.BaseMessageListener;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.repository.LocalRepository;
024    import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
025    import com.liferay.portal.kernel.repository.capabilities.TemporaryFileEntriesCapability;
026    import com.liferay.portal.model.Repository;
027    import com.liferay.portal.service.RepositoryLocalServiceUtil;
028    
029    /**
030     * @author Iv??n Zaera
031     */
032    public class TempFileEntriesMessageListener extends BaseMessageListener {
033    
034            protected void deleteExpiredTemporaryFileEntries(Repository repository) {
035                    LocalRepository localRepository = null;
036    
037                    try {
038                            localRepository = RepositoryProviderUtil.getLocalRepository(
039                                    repository.getRepositoryId());
040                    }
041                    catch (PortalException pe) {
042                            if (_log.isWarnEnabled()) {
043                                    _log.warn(
044                                            "Unable to get implementation for repository " +
045                                                    repository.getRepositoryId(),
046                                            pe);
047                            }
048    
049                            return;
050                    }
051    
052                    try {
053                            if (localRepository.isCapabilityProvided(
054                                            TemporaryFileEntriesCapability.class)) {
055    
056                                    TemporaryFileEntriesCapability temporaryFileEntriesCapability =
057                                            localRepository.getCapability(
058                                                    TemporaryFileEntriesCapability.class);
059    
060                                    temporaryFileEntriesCapability.
061                                            deleteExpiredTemporaryFileEntries();
062                            }
063                    }
064                    catch (Exception pe) {
065                            if (_log.isWarnEnabled()) {
066                                    _log.warn(
067                                            "Unable to delete expired temporary file entries in " +
068                                                    "repository " + repository.getRepositoryId(),
069                                            pe);
070                            }
071                    }
072            }
073    
074            @Override
075            protected void doReceive(Message message) throws Exception {
076                    ActionableDynamicQuery actionableDynamicQuery =
077                            RepositoryLocalServiceUtil.getActionableDynamicQuery();
078    
079                    actionableDynamicQuery.setPerformActionMethod(
080                            new ActionableDynamicQuery.PerformActionMethod() {
081    
082                                    @Override
083                                    public void performAction(Object object) {
084                                            Repository repository = (Repository)object;
085    
086                                            deleteExpiredTemporaryFileEntries(repository);
087                                    }
088    
089                            });
090    
091                    actionableDynamicQuery.performActions();
092            }
093    
094            private static final Log _log = LogFactoryUtil.getLog(
095                    TempFileEntriesMessageListener.class);
096    
097    }