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.Repository;
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.RepositoryWrapper;
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 LiferayProcessorRepositoryWrapper extends RepositoryWrapper {
032    
033            public LiferayProcessorRepositoryWrapper(
034                    Repository repository, ProcessorCapability processorCapability) {
035    
036                    super(repository);
037    
038                    _processorCapability = processorCapability;
039            }
040    
041            @Override
042            public FileEntry addFileEntry(
043                            long userId, long folderId, String sourceFileName, String mimeType,
044                            String title, String description, String changeLog, File file,
045                            ServiceContext serviceContext)
046                    throws PortalException {
047    
048                    FileEntry fileEntry = super.addFileEntry(
049                            userId, folderId, sourceFileName, mimeType, title, description,
050                            changeLog, file, serviceContext);
051    
052                    _processorCapability.generateNew(fileEntry);
053    
054                    return fileEntry;
055            }
056    
057            @Override
058            public FileEntry addFileEntry(
059                            long userId, long folderId, String sourceFileName, String mimeType,
060                            String title, String description, String changeLog, InputStream is,
061                            long size, ServiceContext serviceContext)
062                    throws PortalException {
063    
064                    FileEntry fileEntry = super.addFileEntry(
065                            userId, folderId, sourceFileName, mimeType, title, description,
066                            changeLog, is, size, serviceContext);
067    
068                    _processorCapability.generateNew(fileEntry);
069    
070                    return fileEntry;
071            }
072    
073            @Override
074            public FileVersion cancelCheckOut(long fileEntryId) throws PortalException {
075                    FileEntry fileEntry = getFileEntry(fileEntryId);
076    
077                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion());
078    
079                    FileVersion fileVersion = super.cancelCheckOut(fileEntryId);
080    
081                    _processorCapability.generateNew(fileEntry);
082    
083                    return fileVersion;
084            }
085    
086            @Override
087            public void checkInFileEntry(
088                            long userId, long fileEntryId, boolean major, String changeLog,
089                            ServiceContext serviceContext)
090                    throws PortalException {
091    
092                    FileEntry fileEntry = getFileEntry(fileEntryId);
093    
094                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion());
095    
096                    super.checkInFileEntry(
097                            userId, fileEntryId, major, changeLog, serviceContext);
098    
099                    _processorCapability.copyPrevious(fileEntry.getFileVersion());
100            }
101    
102            @Override
103            public void checkInFileEntry(
104                            long userId, long fileEntryId, String lockUuid,
105                            ServiceContext serviceContext)
106                    throws PortalException {
107    
108                    FileEntry fileEntry = getFileEntry(fileEntryId);
109    
110                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion());
111    
112                    super.checkInFileEntry(userId, fileEntryId, lockUuid, serviceContext);
113    
114                    _processorCapability.copyPrevious(fileEntry.getFileVersion());
115            }
116    
117            @Override
118            public FileEntry checkOutFileEntry(
119                            long fileEntryId, ServiceContext serviceContext)
120                    throws PortalException {
121    
122                    FileEntry oldFileEntry = getFileEntry(fileEntryId);
123    
124                    FileVersion oldFileVersion = oldFileEntry.getFileVersion();
125    
126                    FileEntry fileEntry = super.checkOutFileEntry(
127                            fileEntryId, serviceContext);
128    
129                    _processorCapability.copyPrevious(oldFileVersion);
130    
131                    return fileEntry;
132            }
133    
134            @Override
135            public FileEntry checkOutFileEntry(
136                            long fileEntryId, String owner, long expirationTime,
137                            ServiceContext serviceContext)
138                    throws PortalException {
139    
140                    FileEntry oldFileEntry = getFileEntry(fileEntryId);
141    
142                    FileVersion oldFileVersion = oldFileEntry.getFileVersion();
143    
144                    FileEntry fileEntry = super.checkOutFileEntry(
145                            fileEntryId, owner, expirationTime, serviceContext);
146    
147                    _processorCapability.copyPrevious(oldFileVersion);
148    
149                    return fileEntry;
150            }
151    
152            @Override
153            public void deleteFileEntry(long fileEntryId) throws PortalException {
154                    FileEntry fileEntry = getFileEntry(fileEntryId);
155    
156                    super.deleteFileEntry(fileEntryId);
157    
158                    _processorCapability.cleanUp(fileEntry);
159            }
160    
161            @Override
162            public void deleteFileEntry(long folderId, String title)
163                    throws PortalException {
164    
165                    FileEntry fileEntry = getFileEntry(folderId, title);
166    
167                    super.deleteFileEntry(folderId, title);
168    
169                    _processorCapability.cleanUp(fileEntry);
170            }
171    
172            @Override
173            public void deleteFileVersion(long fileEntryId, String version)
174                    throws PortalException {
175    
176                    FileEntry fileEntry = getFileEntry(fileEntryId);
177    
178                    FileVersion fileVersion = fileEntry.getFileVersion(version);
179    
180                    super.deleteFileVersion(fileEntryId, version);
181    
182                    _processorCapability.cleanUp(fileVersion);
183            }
184    
185            @Override
186            public void revertFileEntry(
187                            long userId, long fileEntryId, String version,
188                            ServiceContext serviceContext)
189                    throws PortalException {
190    
191                    super.revertFileEntry(userId, fileEntryId, version, serviceContext);
192    
193                    FileEntry fileEntry = getFileEntry(fileEntryId);
194    
195                    _processorCapability.copyPrevious(fileEntry.getFileVersion(version));
196            }
197    
198            @Override
199            public FileEntry updateFileEntry(
200                            long userId, long fileEntryId, String sourceFileName,
201                            String mimeType, String title, String description, String changeLog,
202                            boolean majorVersion, File file, ServiceContext serviceContext)
203                    throws PortalException {
204    
205                    FileEntry fileEntry = super.updateFileEntry(
206                            userId, fileEntryId, sourceFileName, mimeType, title, description,
207                            changeLog, majorVersion, file, serviceContext);
208    
209                    _processorCapability.cleanUp(fileEntry.getLatestFileVersion());
210                    _processorCapability.generateNew(fileEntry);
211    
212                    return fileEntry;
213            }
214    
215            @Override
216            public FileEntry updateFileEntry(
217                            long userId, long fileEntryId, String sourceFileName,
218                            String mimeType, String title, String description, String changeLog,
219                            boolean majorVersion, InputStream is, long size,
220                            ServiceContext serviceContext)
221                    throws PortalException {
222    
223                    FileEntry oldFileEntry = null;
224                    FileVersion oldFileVersion = null;
225    
226                    if (is == null) {
227                            oldFileEntry = getFileEntry(fileEntryId);
228                            oldFileVersion = oldFileEntry.getLatestFileVersion(true);
229                    }
230    
231                    FileEntry fileEntry = super.updateFileEntry(
232                            userId, fileEntryId, sourceFileName, mimeType, title, description,
233                            changeLog, majorVersion, is, size, serviceContext);
234    
235                    if (is == null) {
236                            _processorCapability.copyPrevious(oldFileVersion);
237                    }
238                    else {
239                            _processorCapability.cleanUp(fileEntry.getLatestFileVersion());
240                            _processorCapability.generateNew(fileEntry);
241                    }
242    
243                    return fileEntry;
244            }
245    
246            private final ProcessorCapability _processorCapability;
247    
248    }