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.store;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portlet.documentlibrary.DuplicateFileException;
020    
021    import java.io.File;
022    import java.io.InputStream;
023    
024    /**
025     * @author Adolfo P??rez
026     */
027    public class IgnoreDuplicatesStoreWrapper extends BaseStoreWrapper {
028    
029            public IgnoreDuplicatesStoreWrapper(Store store) {
030                    super(store);
031            }
032    
033            @Override
034            public void addFile(
035                            final long companyId, final long repositoryId,
036                            final String fileName, final byte[] bytes)
037                    throws PortalException, SystemException {
038    
039                    recoverAndRetryOnFailure(
040                            createDeleteFileStoreAction(
041                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT),
042                            new StoreAction() {
043    
044                                    @Override
045                                    public void execute() throws PortalException, SystemException {
046                                            Store store = getStore();
047    
048                                            store.addFile(companyId, repositoryId, fileName, bytes);
049                                    }
050    
051                            });
052            }
053    
054            @Override
055            public void addFile(
056                            final long companyId, final long repositoryId,
057                            final String fileName, final File file)
058                    throws PortalException, SystemException {
059    
060                    recoverAndRetryOnFailure(
061                            createDeleteFileStoreAction(
062                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT),
063                            new StoreAction() {
064    
065                                    @Override
066                                    public void execute() throws PortalException, SystemException {
067                                            Store store = getStore();
068    
069                                            store.addFile(companyId, repositoryId, fileName, file);
070                                    }
071    
072                            });
073            }
074    
075            @Override
076            public void addFile(
077                            final long companyId, final long repositoryId,
078                            final String fileName, final InputStream is)
079                    throws PortalException, SystemException {
080    
081                    recoverAndRetryOnFailure(
082                            createDeleteFileStoreAction(
083                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT),
084                            new StoreAction() {
085    
086                                    @Override
087                                    public void execute() throws PortalException, SystemException {
088                                            Store store = getStore();
089    
090                                            store.addFile(companyId, repositoryId, fileName, is);
091                                    }
092    
093                            });
094            }
095    
096            @Override
097            public void copyFileVersion(
098                            final long companyId, final long repositoryId,
099                            final String fileName, final String fromVersionLabel,
100                            final String toVersionLabel)
101                    throws PortalException, SystemException {
102    
103                    recoverAndRetryOnFailure(
104                            createDeleteFileStoreAction(
105                                    companyId, repositoryId, fileName, toVersionLabel),
106                            new StoreAction() {
107    
108                                    @Override
109                                    public void execute() throws PortalException, SystemException {
110                                            Store store = getStore();
111    
112                                            store.copyFileVersion(
113                                                    companyId, repositoryId, fileName, fromVersionLabel,
114                                                    toVersionLabel);
115                                    }
116    
117                            });
118            }
119    
120            @Override
121            public void updateFile(
122                            final long companyId, final long repositoryId,
123                            final long newRepositoryId, final String fileName)
124                    throws PortalException, SystemException {
125    
126                    recoverAndRetryOnFailure(
127                            createDeleteFileStoreAction(companyId, newRepositoryId, fileName),
128                            new StoreAction() {
129    
130                                    @Override
131                                    public void execute() throws PortalException, SystemException {
132                                            Store store = getStore();
133    
134                                            store.updateFile(
135                                                    companyId, repositoryId, newRepositoryId, fileName);
136                                    }
137    
138                            });
139            }
140    
141            @Override
142            public void updateFile(
143                            final long companyId, final long repositoryId,
144                            final String fileName, final String newFileName)
145                    throws PortalException, SystemException {
146    
147                    recoverAndRetryOnFailure(
148                            createDeleteFileStoreAction(companyId, repositoryId, newFileName),
149                            new StoreAction() {
150    
151                                    @Override
152                                    public void execute() throws PortalException, SystemException {
153                                            Store store = getStore();
154    
155                                            store.updateFile(
156                                                    companyId, repositoryId, fileName, newFileName);
157                                    }
158    
159                            });
160            }
161    
162            @Override
163            public void updateFile(
164                            final long companyId, final long repositoryId,
165                            final String fileName, final String versionLabel,
166                            final byte[] bytes)
167                    throws PortalException, SystemException {
168    
169                    recoverAndRetryOnFailure(
170                            createDeleteFileStoreAction(
171                                    companyId, repositoryId, fileName, versionLabel),
172                            new StoreAction() {
173    
174                                    @Override
175                                    public void execute() throws PortalException, SystemException {
176                                            Store store = getStore();
177    
178                                            store.updateFile(
179                                                    companyId, repositoryId, fileName, versionLabel, bytes);
180                                    }
181    
182                            });
183            }
184    
185            @Override
186            public void updateFile(
187                            final long companyId, final long repositoryId,
188                            final String fileName, final String versionLabel, final File file)
189                    throws PortalException, SystemException {
190    
191                    recoverAndRetryOnFailure(
192                            createDeleteFileStoreAction(
193                                    companyId, repositoryId, fileName, versionLabel),
194                            new StoreAction() {
195    
196                                    @Override
197                                    public void execute() throws PortalException, SystemException {
198                                            Store store = getStore();
199    
200                                            store.updateFile(
201                                                    companyId, repositoryId, fileName, versionLabel, file);
202                                    }
203    
204                            });
205            }
206    
207            @Override
208            public void updateFile(
209                            final long companyId, final long repositoryId,
210                            final String fileName, final String versionLabel,
211                            final InputStream is)
212                    throws PortalException, SystemException {
213    
214                    recoverAndRetryOnFailure(
215                            createDeleteFileStoreAction(
216                                    companyId, repositoryId, fileName, versionLabel),
217                            new StoreAction() {
218    
219                                    @Override
220                                    public void execute() throws PortalException, SystemException {
221                                            Store store = getStore();
222    
223                                            store.updateFile(
224                                                    companyId, repositoryId, fileName, versionLabel, is);
225                                    }
226    
227                            });
228            }
229    
230            @Override
231            public void updateFileVersion(
232                            final long companyId, final long repositoryId,
233                            final String fileName, final String fromVersionLabel,
234                            final String toVersionLabel)
235                    throws PortalException, SystemException {
236    
237                    recoverAndRetryOnFailure(
238                            createDeleteFileStoreAction(
239                                    companyId, repositoryId, fileName, toVersionLabel),
240                            new StoreAction() {
241    
242                                    @Override
243                                    public void execute() throws PortalException, SystemException {
244                                            Store store = getStore();
245    
246                                            store.updateFileVersion(
247                                                    companyId, repositoryId, fileName, fromVersionLabel,
248                                                    toVersionLabel);
249                                    }
250    
251                            });
252            }
253    
254            protected static void recoverAndRetryOnFailure(
255                            StoreAction recoverStoreAction, StoreAction storeAction)
256                    throws PortalException, SystemException {
257    
258                    try {
259                            storeAction.execute();
260                    }
261                    catch (DuplicateFileException dfe) {
262                            recoverStoreAction.execute();
263    
264                            storeAction.execute();
265                    }
266            }
267    
268            protected StoreAction createDeleteFileStoreAction(
269                    final long companyId, final long repositoryId, final String fileName) {
270    
271                    return new StoreAction() {
272    
273                            @Override
274                            public void execute() throws PortalException, SystemException {
275                                    Store store = getStore();
276    
277                                    store.deleteFile(companyId, repositoryId, fileName);
278                            }
279    
280                    };
281            }
282    
283            protected StoreAction createDeleteFileStoreAction(
284                    final long companyId, final long repositoryId, final String fileName,
285                    final String versionLabel) {
286    
287                    return new StoreAction() {
288    
289                            @Override
290                            public void execute() throws PortalException, SystemException {
291                                    Store store = getStore();
292    
293                                    store.deleteFile(
294                                            companyId, repositoryId, fileName, versionLabel);
295                            }
296    
297                    };
298            }
299    
300            private interface StoreAction {
301    
302                    public void execute() throws PortalException, SystemException;
303    
304            }
305    
306    }