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.kernel.exception.PortalException;
21 import com.liferay.portal.kernel.exception.SystemException;
22 import com.liferay.portal.kernel.util.OrderByComparator;
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.portal.util.PropsValues;
28 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
30 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
31 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
32 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
33 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
34 import com.liferay.portlet.documentlibrary.util.DLUtil;
35 import com.liferay.portlet.documentlibrary.util.comparator.FileEntryModifiedDateComparator;
36
37 import java.io.File;
38
39 import java.util.List;
40
41
46 public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
47
48 public DLFileEntry addFileEntry(
49 long groupId, long folderId, String name, String title,
50 String description, String versionDescription, String extraSettings,
51 byte[] bytes, ServiceContext serviceContext)
52 throws PortalException, SystemException {
53
54 DLFolderPermission.check(
55 getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
56
57 return dlFileEntryLocalService.addFileEntry(
58 null, getUserId(), groupId, folderId, name, title, description,
59 versionDescription, extraSettings, bytes, serviceContext);
60 }
61
62 public DLFileEntry addFileEntry(
63 long groupId, long folderId, String name, String title,
64 String description, String versionDescription, String extraSettings,
65 File file, ServiceContext serviceContext)
66 throws PortalException, SystemException {
67
68 DLFolderPermission.check(
69 getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
70
71 return dlFileEntryLocalService.addFileEntry(
72 null, getUserId(), groupId, folderId, name, title, description,
73 versionDescription, extraSettings, file, serviceContext);
74 }
75
76 public void deleteFileEntry(long groupId, long folderId, String name)
77 throws PortalException, SystemException {
78
79 DLFileEntryPermission.check(
80 getPermissionChecker(), groupId, folderId, name, ActionKeys.DELETE);
81
82 boolean hasLock = hasFileEntryLock(groupId, folderId, name);
83
84 if (!hasLock) {
85
86
88 lockFileEntry(groupId, folderId, name);
89 }
90
91 try {
92 dlFileEntryLocalService.deleteFileEntry(groupId, folderId, name);
93 }
94 finally {
95
96
98 unlockFileEntry(groupId, folderId, name);
99 }
100 }
101
102 public void deleteFileEntry(
103 long groupId, long folderId, String name, String version)
104 throws PortalException, SystemException {
105
106 DLFileEntryPermission.check(
107 getPermissionChecker(), groupId, folderId, name, ActionKeys.DELETE);
108
109 boolean hasLock = hasFileEntryLock(groupId, folderId, name);
110
111 if (!hasLock) {
112
113
115 lockFileEntry(groupId, folderId, name);
116 }
117
118 try {
119 dlFileEntryLocalService.deleteFileEntry(
120 groupId, folderId, name, version);
121 }
122 finally {
123
124
126 unlockFileEntry(groupId, folderId, name);
127 }
128 }
129
130 public void deleteFileEntryByTitle(
131 long groupId, long folderId, String titleWithExtension)
132 throws PortalException, SystemException {
133
134 DLFileEntry fileEntry = getFileEntryByTitle(
135 groupId, folderId, titleWithExtension);
136
137 deleteFileEntry(groupId, folderId, fileEntry.getName());
138 }
139
140 public List<DLFileEntry> getFileEntries(long groupId, long folderId)
141 throws PortalException, SystemException {
142
143 DLFolderPermission.check(
144 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
145
146 return dlFileEntryPersistence.filterFindByG_F(groupId, folderId);
147 }
148
149 public List<DLFileEntry> getFileEntries(
150 long groupId, long folderId, int start, int end)
151 throws PortalException, SystemException {
152
153 DLFolderPermission.check(
154 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
155
156 return dlFileEntryPersistence.filterFindByG_F(
157 groupId, folderId, start, end);
158 }
159
160 public List<DLFileEntry> getFileEntries(
161 long groupId, long folderId, int start, int end,
162 OrderByComparator obc)
163 throws PortalException, SystemException {
164
165 DLFolderPermission.check(
166 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
167
168 return dlFileEntryPersistence.filterFindByG_F(
169 groupId, folderId, start, end, obc);
170 }
171
172 public int getFileEntriesCount(long groupId, long folderId)
173 throws PortalException, SystemException {
174
175 DLFolderPermission.check(
176 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
177
178 return dlFileEntryPersistence.filterCountByG_F(groupId, folderId);
179 }
180
181 public DLFileEntry getFileEntry(long groupId, long folderId, String name)
182 throws PortalException, SystemException {
183
184 DLFileEntryPermission.check(
185 getPermissionChecker(), groupId, folderId, name, ActionKeys.VIEW);
186
187 return dlFileEntryLocalService.getFileEntry(groupId, folderId, name);
188 }
189
190 public DLFileEntry getFileEntryByTitle(
191 long groupId, long folderId, String titleWithExtension)
192 throws PortalException, SystemException {
193
194 DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
195 groupId, folderId, titleWithExtension);
196
197 DLFileEntryPermission.check(
198 getPermissionChecker(), fileEntry, ActionKeys.VIEW);
199
200 return fileEntry;
201 }
202
203 public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
204 throws PortalException, SystemException {
205
206 DLFileEntry fileEntry = dlFileEntryPersistence.findByUUID_G(
207 uuid, groupId);
208
209 DLFileEntryPermission.check(
210 getPermissionChecker(), fileEntry, ActionKeys.VIEW);
211
212 return fileEntry;
213 }
214
215 public int getFoldersFileEntriesCount(
216 long groupId, List<Long> folderIds, int status)
217 throws SystemException {
218
219 if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
220 return dlFileEntryFinder.filterCountByG_F_S(
221 groupId, folderIds, status);
222 }
223 else {
224 int start = 0;
225 int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
226
227 int filesCount = dlFileEntryFinder.filterCountByG_F_S(
228 groupId, folderIds.subList(start, end), status);
229
230 folderIds.subList(start, end).clear();
231
232 filesCount += getFoldersFileEntriesCount(
233 groupId, folderIds, status);
234
235 return filesCount;
236 }
237 }
238
239 public List<DLFileEntry> getGroupFileEntries(
240 long groupId, long userId, int start, int end)
241 throws SystemException {
242
243 return getGroupFileEntries(
244 groupId, userId, start, end, new FileEntryModifiedDateComparator());
245 }
246
247 public List<DLFileEntry> getGroupFileEntries(
248 long groupId, long userId, int start, int end,
249 OrderByComparator obc)
250 throws SystemException {
251
252 if (userId <= 0) {
253 return dlFileEntryPersistence.filterFindByGroupId(
254 groupId, start, end, obc);
255 }
256 else {
257 return dlFileEntryPersistence.filterFindByG_U(
258 groupId, userId, start, end, obc);
259 }
260 }
261
262 public int getGroupFileEntriesCount(long groupId, long userId)
263 throws SystemException {
264
265 if (userId <= 0) {
266 return dlFileEntryPersistence.filterCountByGroupId(groupId);
267 }
268 else {
269 return dlFileEntryPersistence.filterCountByG_U(groupId, userId);
270 }
271 }
272
273 public boolean hasFileEntryLock(long groupId, long folderId, String name)
274 throws PortalException, SystemException {
275
276 String lockId = DLUtil.getLockId(groupId, folderId, name);
277
278 boolean hasLock = lockLocalService.hasLock(
279 getUserId(), DLFileEntry.class.getName(), lockId);
280
281 if ((!hasLock) &&
282 (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
283
284 hasLock = dlFolderService.hasInheritableLock(folderId);
285 }
286
287 return hasLock;
288 }
289
290 public Lock lockFileEntry(long groupId, long folderId, String name)
291 throws PortalException, SystemException {
292
293 return lockFileEntry(
294 groupId, folderId, name, null,
295 DLFileEntryImpl.LOCK_EXPIRATION_TIME);
296 }
297
298 public Lock lockFileEntry(
299 long groupId, long folderId, String name, String owner,
300 long expirationTime)
301 throws PortalException, SystemException {
302
303 if ((expirationTime <= 0) ||
304 (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
305
306 expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
307 }
308
309 String lockId = DLUtil.getLockId(groupId, folderId, name);
310
311 return lockLocalService.lock(
312 getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
313 false, expirationTime);
314 }
315
316 public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
317 throws PortalException, SystemException {
318
319 return lockLocalService.refresh(lockUuid, expirationTime);
320 }
321
322 public void unlockFileEntry(long groupId, long folderId, String name)
323 throws SystemException {
324
325 String lockId = DLUtil.getLockId(groupId, folderId, name);
326
327 lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
328 }
329
330 public void unlockFileEntry(
331 long groupId, long folderId, String name, String lockUuid)
332 throws PortalException, SystemException {
333
334 String lockId = DLUtil.getLockId(groupId, folderId, name);
335
336 if (Validator.isNotNull(lockUuid)) {
337 try {
338 Lock lock = lockLocalService.getLock(
339 DLFileEntry.class.getName(), lockId);
340
341 if (!lock.getUuid().equals(lockUuid)) {
342 throw new InvalidLockException("UUIDs do not match");
343 }
344 }
345 catch (PortalException pe) {
346 if ((pe instanceof ExpiredLockException) ||
347 (pe instanceof NoSuchLockException)) {
348 }
349 else {
350 throw pe;
351 }
352 }
353 }
354
355 lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
356 }
357
358 public DLFileEntry updateFileEntry(
359 long groupId, long folderId, long newFolderId, String name,
360 String sourceFileName, String title, String description,
361 String versionDescription, boolean majorVersion,
362 String extraSettings, byte[] bytes, ServiceContext serviceContext)
363 throws PortalException, SystemException {
364
365 DLFileEntryPermission.check(
366 getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
367
368 boolean hasLock = hasFileEntryLock(groupId, folderId, name);
369
370 if (!hasLock) {
371
372
374 lockFileEntry(groupId, folderId, name);
375 }
376
377 DLFileEntry fileEntry = null;
378
379 try {
380 fileEntry = dlFileEntryLocalService.updateFileEntry(
381 getUserId(), groupId, folderId, newFolderId, name,
382 sourceFileName, title, description, versionDescription,
383 majorVersion, extraSettings, bytes, serviceContext);
384 }
385 finally {
386 if (!hasLock) {
387
388
390 unlockFileEntry(groupId, folderId, name);
391 }
392 }
393
394 return fileEntry;
395 }
396
397 public DLFileEntry updateFileEntry(
398 long groupId, long folderId, long newFolderId, String name,
399 String sourceFileName, String title, String description,
400 String versionDescription, boolean majorVersion,
401 String extraSettings, File file, ServiceContext serviceContext)
402 throws PortalException, SystemException {
403
404 DLFileEntryPermission.check(
405 getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
406
407 boolean hasLock = hasFileEntryLock(groupId, folderId, name);
408
409 if (!hasLock) {
410
411
413 lockFileEntry(groupId, folderId, name);
414 }
415
416 DLFileEntry fileEntry = null;
417
418 try {
419 fileEntry = dlFileEntryLocalService.updateFileEntry(
420 getUserId(), groupId, folderId, newFolderId, name,
421 sourceFileName, title, description, versionDescription,
422 majorVersion, extraSettings, file, serviceContext);
423 }
424 finally {
425 if (!hasLock) {
426
427
429 unlockFileEntry(groupId, folderId, name);
430 }
431 }
432
433 return fileEntry;
434 }
435
436 public boolean verifyFileEntryLock(
437 long groupId, long folderId, String name, String lockUuid)
438 throws PortalException, SystemException {
439
440 boolean verified = false;
441
442 try {
443 String lockId = DLUtil.getLockId(groupId, folderId, name);
444
445 Lock lock = lockLocalService.getLock(
446 DLFileEntry.class.getName(), lockId);
447
448 if (lock.getUuid().equals(lockUuid)) {
449 verified = true;
450 }
451 }
452 catch (PortalException pe) {
453 if ((pe instanceof ExpiredLockException) ||
454 (pe instanceof NoSuchLockException)) {
455
456 verified = dlFolderService.verifyInheritableLock(
457 folderId, lockUuid);
458 }
459 else {
460 throw pe;
461 }
462 }
463
464 return verified;
465 }
466
467 }