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.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.LocalRepository;
019    import com.liferay.portal.kernel.repository.capabilities.ProcessorCapability;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.FileVersion;
022    import com.liferay.portal.repository.util.LocalRepositoryWrapper;
023    import com.liferay.portal.service.ServiceContext;
024    
025    import java.io.File;
026    import java.io.InputStream;
027    
028    /**
029     * @author Adolfo P??rez
030     */
031    public class LiferayProcessorLocalRepositoryWrapper
032            extends LocalRepositoryWrapper {
033    
034            public LiferayProcessorLocalRepositoryWrapper(
035                    LocalRepository localRepository,
036                    ProcessorCapability processorCapability) {
037    
038                    super(localRepository);
039    
040                    _processorCapability = processorCapability;
041            }
042    
043            @Override
044            public FileEntry addFileEntry(
045                            long userId, long folderId, String sourceFileName, String mimeType,
046                            String title, String description, String changeLog, File file,
047                            ServiceContext serviceContext)
048                    throws PortalException {
049    
050                    FileEntry fileEntry = super.addFileEntry(
051                            userId, folderId, sourceFileName, mimeType, title, description,
052                            changeLog, file, serviceContext);
053    
054                    _processorCapability.generateNew(fileEntry);
055    
056                    return fileEntry;
057            }
058    
059            @Override
060            public FileEntry addFileEntry(
061                            long userId, long folderId, String sourceFileName, String mimeType,
062                            String title, String description, String changeLog, InputStream is,
063                            long size, ServiceContext serviceContext)
064                    throws PortalException {
065    
066                    FileEntry fileEntry = super.addFileEntry(
067                            userId, folderId, sourceFileName, mimeType, title, description,
068                            changeLog, is, size, serviceContext);
069    
070                    _processorCapability.generateNew(fileEntry);
071    
072                    return fileEntry;
073            }
074    
075            @Override
076            public void checkInFileEntry(
077                            long userId, long fileEntryId, boolean major, String changeLog,
078                            ServiceContext serviceContext)
079                    throws PortalException {
080    
081                    FileEntry fileEntry = getFileEntry(fileEntryId);
082    
083                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion(true));
084    
085                    super.checkInFileEntry(
086                            userId, fileEntryId, major, changeLog, serviceContext);
087    
088                    _processorCapability.copy(fileEntry, fileEntry.getFileVersion());
089            }
090    
091            @Override
092            public void checkInFileEntry(
093                            long userId, long fileEntryId, String lockUuid,
094                            ServiceContext serviceContext)
095                    throws PortalException {
096    
097                    FileEntry fileEntry = getFileEntry(fileEntryId);
098    
099                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion(true));
100    
101                    super.checkInFileEntry(userId, fileEntryId, lockUuid, serviceContext);
102    
103                    _processorCapability.copy(fileEntry, fileEntry.getFileVersion());
104            }
105    
106            @Override
107            public void revertFileEntry(
108                            long userId, long fileEntryId, String version,
109                            ServiceContext serviceContext)
110                    throws PortalException {
111    
112                    super.revertFileEntry(userId, fileEntryId, version, serviceContext);
113    
114                    FileEntry fileEntry = getFileEntry(fileEntryId);
115    
116                    _processorCapability.copy(fileEntry, fileEntry.getFileVersion(version));
117            }
118    
119            @Override
120            public FileEntry updateFileEntry(
121                            long userId, long fileEntryId, String sourceFileName,
122                            String mimeType, String title, String description, String changeLog,
123                            boolean majorVersion, File file, ServiceContext serviceContext)
124                    throws PortalException {
125    
126                    FileEntry fileEntry = super.updateFileEntry(
127                            userId, fileEntryId, sourceFileName, mimeType, title, description,
128                            changeLog, majorVersion, file, serviceContext);
129    
130                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion(true));
131                    _processorCapability.generateNew(fileEntry);
132    
133                    return fileEntry;
134            }
135    
136            @Override
137            public FileEntry updateFileEntry(
138                            long userId, long fileEntryId, String sourceFileName,
139                            String mimeType, String title, String description, String changeLog,
140                            boolean majorVersion, InputStream is, long size,
141                            ServiceContext serviceContext)
142                    throws PortalException {
143    
144                    FileEntry oldFileEntry = null;
145                    FileVersion oldFileVersion = null;
146    
147                    if (is == null) {
148                            oldFileEntry = getFileEntry(fileEntryId);
149                            oldFileVersion = oldFileEntry.getLatestFileVersion(true);
150                    }
151    
152                    FileEntry fileEntry = super.updateFileEntry(
153                            userId, fileEntryId, sourceFileName, mimeType, title, description,
154                            changeLog, majorVersion, is, size, serviceContext);
155    
156                    if (is == null) {
157                            _processorCapability.copy(fileEntry, oldFileVersion);
158                    }
159                    else {
160                            _processorCapability.cleanUp(fileEntry.getLatestFileVersion(true));
161                            _processorCapability.generateNew(fileEntry);
162                    }
163    
164                    return fileEntry;
165            }
166    
167            private final ProcessorCapability _processorCapability;
168    
169    }