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