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.repository.capabilities;
016    
017    import com.liferay.portal.kernel.repository.LocalRepository;
018    import com.liferay.portal.kernel.repository.Repository;
019    import com.liferay.portal.kernel.repository.capabilities.ProcessorCapability;
020    import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
021    import com.liferay.portal.kernel.repository.event.RepositoryEventListener;
022    import com.liferay.portal.kernel.repository.event.RepositoryEventType;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.FileVersion;
025    import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
026    import com.liferay.portal.kernel.transaction.TransactionCommitCallbackUtil;
027    import com.liferay.portal.repository.liferayrepository.LiferayProcessorLocalRepositoryWrapper;
028    import com.liferay.portal.repository.liferayrepository.LiferayProcessorRepositoryWrapper;
029    import com.liferay.portal.repository.util.RepositoryWrapperAware;
030    import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
031    
032    import java.util.concurrent.Callable;
033    
034    /**
035     * @author Adolfo P??rez
036     */
037    public class LiferayProcessorCapability
038            implements ProcessorCapability, RepositoryEventAware,
039                               RepositoryWrapperAware {
040    
041            @Override
042            public void cleanUp(FileEntry fileEntry) {
043                    DLProcessorRegistryUtil.cleanUp(fileEntry);
044            }
045    
046            @Override
047            public void cleanUp(FileVersion fileVersion) {
048                    DLProcessorRegistryUtil.cleanUp(fileVersion);
049            }
050    
051            @Override
052            public void copy(FileEntry fileEntry, FileVersion fileVersion) {
053                    registerDLProcessorCallback(fileEntry, fileVersion);
054            }
055    
056            @Override
057            public void generateNew(FileEntry fileEntry) {
058                    registerDLProcessorCallback(fileEntry, null);
059            }
060    
061            @Override
062            public void registerRepositoryEventListeners(
063                    RepositoryEventRegistry repositoryEventRegistry) {
064    
065                    repositoryEventRegistry.registerRepositoryEventListener(
066                            RepositoryEventType.Delete.class, FileEntry.class,
067                            new RepositoryEventListener
068                                    <RepositoryEventType.Delete, FileEntry>() {
069    
070                                    @Override
071                                    public void execute(FileEntry fileEntry) {
072                                            cleanUp(fileEntry);
073                                    }
074    
075                            });
076            }
077    
078            @Override
079            public LocalRepository wrapLocalRepository(
080                    LocalRepository localRepository) {
081    
082                    return new LiferayProcessorLocalRepositoryWrapper(
083                            localRepository, this);
084            }
085    
086            @Override
087            public Repository wrapRepository(Repository repository) {
088                    return new LiferayProcessorRepositoryWrapper(repository, this);
089            }
090    
091            protected void registerDLProcessorCallback(
092                    final FileEntry fileEntry, final FileVersion fileVersion) {
093    
094                    TransactionCommitCallbackUtil.registerCallback(
095                            new Callable<Void>() {
096    
097                                    @Override
098                                    public Void call() throws Exception {
099                                            DLProcessorRegistryUtil.trigger(
100                                                    fileEntry, fileVersion, true);
101    
102                                            return null;
103                                    }
104    
105                            });
106            }
107    
108    }