1
14
15 package com.liferay.portlet.documentlibrary.service.impl;
16
17 import com.liferay.portal.ExpiredLockException;
18 import com.liferay.portal.InvalidLockException;
19 import com.liferay.portal.NoSuchLockException;
20 import com.liferay.portal.PortalException;
21 import com.liferay.portal.SystemException;
22 import com.liferay.portal.kernel.util.ListUtil;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.model.Lock;
25 import com.liferay.portal.security.permission.ActionKeys;
26 import com.liferay.portal.service.ServiceContext;
27 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
29 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
30 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
31 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
32 import com.liferay.portlet.documentlibrary.util.DLUtil;
33
34 import java.io.File;
35
36 import java.util.Iterator;
37 import java.util.List;
38
39
44 public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
45
46
49 public DLFileEntry addFileEntry(
50 long folderId, String name, String title, String description,
51 String extraSettings, byte[] bytes, ServiceContext serviceContext)
52 throws PortalException, SystemException {
53
54 return addFileEntry(
55 folderId, name, title, description, null, extraSettings, bytes,
56 serviceContext);
57 }
58
59
62 public DLFileEntry addFileEntry(
63 long folderId, String name, String title, String description,
64 String extraSettings, File file, ServiceContext serviceContext)
65 throws PortalException, SystemException {
66
67 return addFileEntry(
68 folderId, name, title, description, null, extraSettings, file,
69 serviceContext);
70 }
71
72 public DLFileEntry addFileEntry(
73 long folderId, String name, String title, String description,
74 String versionDescription, String extraSettings, byte[] bytes,
75 ServiceContext serviceContext)
76 throws PortalException, SystemException {
77
78 DLFolderPermission.check(
79 getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
80
81 return dlFileEntryLocalService.addFileEntry(
82 null, getUserId(), folderId, name, title, description,
83 versionDescription, extraSettings, bytes, serviceContext);
84 }
85
86 public DLFileEntry addFileEntry(
87 long folderId, String name, String title, String description,
88 String versionDescription, String extraSettings, File file,
89 ServiceContext serviceContext)
90 throws PortalException, SystemException {
91
92 DLFolderPermission.check(
93 getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
94
95 return dlFileEntryLocalService.addFileEntry(
96 null, getUserId(), folderId, name, title, description,
97 versionDescription, extraSettings, file, serviceContext);
98 }
99
100 public void deleteFileEntry(long folderId, String name)
101 throws PortalException, SystemException {
102
103 DLFileEntryPermission.check(
104 getPermissionChecker(), folderId, name, ActionKeys.DELETE);
105
106 boolean hasLock = hasFileEntryLock(folderId, name);
107
108 if (!hasLock) {
109
110
112 lockFileEntry(folderId, name);
113 }
114
115 try {
116 dlFileEntryLocalService.deleteFileEntry(folderId, name);
117 }
118 finally {
119
120
122 unlockFileEntry(folderId, name);
123 }
124 }
125
126 public void deleteFileEntry(long folderId, String name, double version)
127 throws PortalException, SystemException {
128
129 DLFileEntryPermission.check(
130 getPermissionChecker(), folderId, name, ActionKeys.DELETE);
131
132 boolean hasLock = hasFileEntryLock(folderId, name);
133
134 if (!hasLock) {
135
136
138 lockFileEntry(folderId, name);
139 }
140
141 try {
142 dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
143 }
144 finally {
145
146
148 unlockFileEntry(folderId, name);
149 }
150 }
151
152 public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
153 throws PortalException, SystemException {
154
155 DLFileEntry fileEntry = getFileEntryByTitle(
156 folderId, titleWithExtension);
157
158 deleteFileEntry(folderId, fileEntry.getName());
159 }
160
161 public List<DLFileEntry> getFileEntries(long folderId)
162 throws PortalException, SystemException {
163
164 List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
165 folderId);
166
167 fileEntries = ListUtil.copy(fileEntries);
168
169 Iterator<DLFileEntry> itr = fileEntries.iterator();
170
171 while (itr.hasNext()) {
172 DLFileEntry fileEntry = itr.next();
173
174 if (!DLFileEntryPermission.contains(
175 getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
176
177 itr.remove();
178 }
179 }
180
181 return fileEntries;
182 }
183
184 public DLFileEntry getFileEntry(long folderId, String name)
185 throws PortalException, SystemException {
186
187 DLFileEntryPermission.check(
188 getPermissionChecker(), folderId, name, ActionKeys.VIEW);
189
190 return dlFileEntryLocalService.getFileEntry(folderId, name);
191 }
192
193 public DLFileEntry getFileEntryByTitle(
194 long folderId, String titleWithExtension)
195 throws PortalException, SystemException {
196
197 DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
198 folderId, titleWithExtension);
199
200 DLFileEntryPermission.check(
201 getPermissionChecker(), fileEntry, ActionKeys.VIEW);
202
203 return fileEntry;
204 }
205
206 public Lock getFileEntryLock(long folderId, String name)
207 throws PortalException, SystemException {
208
209 String lockId = DLUtil.getLockId(folderId, name);
210
211 return lockLocalService.getLock(DLFileEntry.class.getName(), lockId);
212 }
213
214 public boolean hasFileEntryLock(long folderId, String name)
215 throws PortalException, SystemException {
216
217 String lockId = DLUtil.getLockId(folderId, name);
218
219 boolean hasLock = lockLocalService.hasLock(
220 getUserId(), DLFileEntry.class.getName(), lockId);
221
222 if (!hasLock) {
223 hasLock = dlFolderService.hasInheritableLock(folderId);
224 }
225
226 return hasLock;
227 }
228
229 public Lock lockFileEntry(long folderId, String name)
230 throws PortalException, SystemException {
231
232 return lockFileEntry(
233 folderId, name, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
234 }
235
236 public Lock lockFileEntry(
237 long folderId, String name, String owner, long expirationTime)
238 throws PortalException, SystemException {
239
240 if ((expirationTime <= 0) ||
241 (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
242
243 expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
244 }
245
246 String lockId = DLUtil.getLockId(folderId, name);
247
248 return lockLocalService.lock(
249 getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
250 false, expirationTime);
251 }
252
253 public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
254 throws PortalException, SystemException {
255
256 return lockLocalService.refresh(lockUuid, expirationTime);
257 }
258
259 public void unlockFileEntry(long folderId, String name)
260 throws SystemException {
261
262 String lockId = DLUtil.getLockId(folderId, name);
263
264 lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
265 }
266
267 public void unlockFileEntry(long folderId, String name, String lockUuid)
268 throws PortalException, SystemException {
269
270 String lockId = DLUtil.getLockId(folderId, name);
271
272 if (Validator.isNotNull(lockUuid)) {
273 try {
274 Lock lock = lockLocalService.getLock(
275 DLFileEntry.class.getName(), lockId);
276
277 if (!lock.getUuid().equals(lockUuid)) {
278 throw new InvalidLockException("UUIDs do not match");
279 }
280 }
281 catch (PortalException pe) {
282 if (pe instanceof ExpiredLockException ||
283 pe instanceof NoSuchLockException) {
284 }
285 else {
286 throw pe;
287 }
288 }
289 }
290
291 lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
292 }
293
294
297 public DLFileEntry updateFileEntry(
298 long folderId, long newFolderId, String name, String sourceFileName,
299 String title, String description, String extraSettings,
300 byte[] bytes, ServiceContext serviceContext)
301 throws PortalException, SystemException {
302
303 return updateFileEntry(
304 folderId, newFolderId, name, sourceFileName, title, description,
305 null, extraSettings, bytes, serviceContext);
306 }
307
308
311 public DLFileEntry updateFileEntry(
312 long folderId, long newFolderId, String name, String sourceFileName,
313 String title, String description, String extraSettings, File file,
314 ServiceContext serviceContext)
315 throws PortalException, SystemException {
316
317 return updateFileEntry(
318 folderId, newFolderId, name, sourceFileName, title, description,
319 null, extraSettings, file, serviceContext);
320 }
321
322 public DLFileEntry updateFileEntry(
323 long folderId, long newFolderId, String name, String sourceFileName,
324 String title, String description, String versionDescription,
325 String extraSettings, byte[] bytes, ServiceContext serviceContext)
326 throws PortalException, SystemException {
327
328 DLFileEntryPermission.check(
329 getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
330
331 boolean hasLock = hasFileEntryLock(folderId, name);
332
333 if (!hasLock) {
334
335
337 lockFileEntry(folderId, name);
338 }
339
340 DLFileEntry fileEntry = null;
341
342 try {
343 fileEntry = dlFileEntryLocalService.updateFileEntry(
344 getUserId(), folderId, newFolderId, name, sourceFileName, title,
345 description, extraSettings, bytes, serviceContext);
346 }
347 finally {
348 if (!hasLock) {
349
350
352 unlockFileEntry(folderId, name);
353 }
354 }
355
356 return fileEntry;
357 }
358
359 public DLFileEntry updateFileEntry(
360 long folderId, long newFolderId, String name, String sourceFileName,
361 String title, String description, String versionDescription,
362 String extraSettings, File file, ServiceContext serviceContext)
363 throws PortalException, SystemException {
364
365 DLFileEntryPermission.check(
366 getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
367
368 boolean hasLock = hasFileEntryLock(folderId, name);
369
370 if (!hasLock) {
371
372
374 lockFileEntry(folderId, name);
375 }
376
377 DLFileEntry fileEntry = null;
378
379 try {
380 fileEntry = dlFileEntryLocalService.updateFileEntry(
381 getUserId(), folderId, newFolderId, name, sourceFileName, title,
382 description, extraSettings, file, serviceContext);
383 }
384 finally {
385 if (!hasLock) {
386
387
389 unlockFileEntry(folderId, name);
390 }
391 }
392
393 return fileEntry;
394 }
395
396 public boolean verifyFileEntryLock(
397 long folderId, String name, String lockUuid)
398 throws PortalException, SystemException {
399
400 boolean verified = false;
401
402 try {
403 String lockId = DLUtil.getLockId(folderId, name);
404
405 Lock lock = lockLocalService.getLock(
406 DLFileEntry.class.getName(), lockId);
407
408 if (lock.getUuid().equals(lockUuid)) {
409 verified = true;
410 }
411 }
412 catch (PortalException pe) {
413 if (pe instanceof ExpiredLockException ||
414 pe instanceof NoSuchLockException) {
415
416 verified = dlFolderService.verifyInheritableLock(
417 folderId, lockUuid);
418 }
419 else {
420 throw pe;
421 }
422 }
423
424 return verified;
425 }
426
427 }