001
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
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 if (fromVersionLabel.equals(toVersionLabel)) {
104 return;
105 }
106
107 recoverAndRetryOnFailure(
108 createDeleteFileStoreAction(
109 companyId, repositoryId, fileName, toVersionLabel),
110 new StoreAction() {
111
112 @Override
113 public void execute() throws PortalException, SystemException {
114 Store store = getStore();
115
116 store.copyFileVersion(
117 companyId, repositoryId, fileName, fromVersionLabel,
118 toVersionLabel);
119 }
120
121 });
122 }
123
124 @Override
125 public void updateFile(
126 final long companyId, final long repositoryId,
127 final long newRepositoryId, final String fileName)
128 throws PortalException, SystemException {
129
130 if (repositoryId == newRepositoryId) {
131 return;
132 }
133
134 recoverAndRetryOnFailure(
135 createDeleteFileStoreAction(companyId, newRepositoryId, fileName),
136 new StoreAction() {
137
138 @Override
139 public void execute() throws PortalException, SystemException {
140 Store store = getStore();
141
142 store.updateFile(
143 companyId, repositoryId, newRepositoryId, fileName);
144 }
145
146 });
147 }
148
149 @Override
150 public void updateFile(
151 final long companyId, final long repositoryId,
152 final String fileName, final String newFileName)
153 throws PortalException, SystemException {
154
155 if (fileName.equals(newFileName)) {
156 return;
157 }
158
159 recoverAndRetryOnFailure(
160 createDeleteFileStoreAction(companyId, repositoryId, newFileName),
161 new StoreAction() {
162
163 @Override
164 public void execute() throws PortalException, SystemException {
165 Store store = getStore();
166
167 store.updateFile(
168 companyId, repositoryId, fileName, newFileName);
169 }
170
171 });
172 }
173
174 @Override
175 public void updateFile(
176 final long companyId, final long repositoryId,
177 final String fileName, final String versionLabel,
178 final byte[] bytes)
179 throws PortalException, SystemException {
180
181 recoverAndRetryOnFailure(
182 createDeleteFileStoreAction(
183 companyId, repositoryId, fileName, versionLabel),
184 new StoreAction() {
185
186 @Override
187 public void execute() throws PortalException, SystemException {
188 Store store = getStore();
189
190 store.updateFile(
191 companyId, repositoryId, fileName, versionLabel, bytes);
192 }
193
194 });
195 }
196
197 @Override
198 public void updateFile(
199 final long companyId, final long repositoryId,
200 final String fileName, final String versionLabel, final File file)
201 throws PortalException, SystemException {
202
203 recoverAndRetryOnFailure(
204 createDeleteFileStoreAction(
205 companyId, repositoryId, fileName, versionLabel),
206 new StoreAction() {
207
208 @Override
209 public void execute() throws PortalException, SystemException {
210 Store store = getStore();
211
212 store.updateFile(
213 companyId, repositoryId, fileName, versionLabel, file);
214 }
215
216 });
217 }
218
219 @Override
220 public void updateFile(
221 final long companyId, final long repositoryId,
222 final String fileName, final String versionLabel,
223 final InputStream is)
224 throws PortalException, SystemException {
225
226 recoverAndRetryOnFailure(
227 createDeleteFileStoreAction(
228 companyId, repositoryId, fileName, versionLabel),
229 new StoreAction() {
230
231 @Override
232 public void execute() throws PortalException, SystemException {
233 Store store = getStore();
234
235 store.updateFile(
236 companyId, repositoryId, fileName, versionLabel, is);
237 }
238
239 });
240 }
241
242 @Override
243 public void updateFileVersion(
244 final long companyId, final long repositoryId,
245 final String fileName, final String fromVersionLabel,
246 final String toVersionLabel)
247 throws PortalException, SystemException {
248
249 if (fromVersionLabel.equals(toVersionLabel)) {
250 return;
251 }
252
253 recoverAndRetryOnFailure(
254 createDeleteFileStoreAction(
255 companyId, repositoryId, fileName, toVersionLabel),
256 new StoreAction() {
257
258 @Override
259 public void execute() throws PortalException, SystemException {
260 Store store = getStore();
261
262 store.updateFileVersion(
263 companyId, repositoryId, fileName, fromVersionLabel,
264 toVersionLabel);
265 }
266
267 });
268 }
269
270 protected static void recoverAndRetryOnFailure(
271 StoreAction recoverStoreAction, StoreAction storeAction)
272 throws PortalException, SystemException {
273
274 try {
275 storeAction.execute();
276 }
277 catch (DuplicateFileException dfe) {
278 recoverStoreAction.execute();
279
280 storeAction.execute();
281 }
282 }
283
284 protected StoreAction createDeleteFileStoreAction(
285 final long companyId, final long repositoryId, final String fileName) {
286
287 return new StoreAction() {
288
289 @Override
290 public void execute() throws PortalException, SystemException {
291 Store store = getStore();
292
293 store.deleteFile(companyId, repositoryId, fileName);
294 }
295
296 };
297 }
298
299 protected StoreAction createDeleteFileStoreAction(
300 final long companyId, final long repositoryId, final String fileName,
301 final String versionLabel) {
302
303 return new StoreAction() {
304
305 @Override
306 public void execute() throws PortalException, SystemException {
307 Store store = getStore();
308
309 store.deleteFile(
310 companyId, repositoryId, fileName, versionLabel);
311 }
312
313 };
314 }
315
316 private interface StoreAction {
317
318 public void execute() throws PortalException, SystemException;
319
320 }
321
322 }