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.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.Lock;
27 import com.liferay.portal.security.permission.ActionKeys;
28 import com.liferay.portal.service.ServiceContext;
29 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30 import com.liferay.portlet.documentlibrary.model.DLFolder;
31 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
32 import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
33 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
34
35 import java.io.File;
36 import java.io.InputStream;
37
38 import java.rmi.RemoteException;
39
40 import java.util.ArrayList;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Set;
44
45
50 public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
51
52 public DLFolder addFolder(
53 long groupId, long parentFolderId, String name, String description,
54 ServiceContext serviceContext)
55 throws PortalException, SystemException {
56
57 DLFolderPermission.check(
58 getPermissionChecker(), groupId, parentFolderId,
59 ActionKeys.ADD_FOLDER);
60
61 return dlFolderLocalService.addFolder(
62 null, getUserId(), groupId, parentFolderId, name, description,
63 serviceContext);
64 }
65
66 public DLFolder copyFolder(
67 long groupId, long sourceFolderId, long parentFolderId, String name,
68 String description, ServiceContext serviceContext)
69 throws PortalException, RemoteException, SystemException {
70
71 DLFolder srcFolder = getFolder(sourceFolderId);
72
73 DLFolder destFolder = addFolder(
74 groupId, parentFolderId, name, description, serviceContext);
75
76 copyFolder(srcFolder, destFolder, serviceContext);
77
78 return destFolder;
79 }
80
81 public void deleteFolder(long folderId)
82 throws PortalException, RemoteException, SystemException {
83
84 DLFolder folder = dlFolderLocalService.getFolder(folderId);
85
86 DLFolderPermission.check(
87 getPermissionChecker(), folder, ActionKeys.DELETE);
88
89 boolean hasLock = lockLocalService.hasLock(
90 getUserId(), DLFolder.class.getName(), folderId);
91
92 Lock lock = null;
93
94 if (!hasLock) {
95
96
98 lock = lockFolder(folderId);
99 }
100
101 try {
102 dlFolderLocalService.deleteFolder(folderId);
103 }
104 finally {
105 if (!hasLock) {
106
107
109 unlockFolder(folder.getGroupId(), folderId, lock.getUuid());
110 }
111 }
112 }
113
114 public void deleteFolder(long groupId, long parentFolderId, String name)
115 throws PortalException, RemoteException, SystemException {
116
117 long folderId = getFolderId(groupId, parentFolderId, name);
118
119 deleteFolder(folderId);
120 }
121
122 public List<Object> getFileEntriesAndFileShortcuts(
123 long groupId, List<Long> folderIds, int status, int start, int end)
124 throws SystemException {
125
126 return dlFolderFinder.filterFindFE_FS_ByG_F_S(
127 groupId, folderIds, status, start, end);
128 }
129
130 public List<Object> getFileEntriesAndFileShortcuts(
131 long groupId, long folderId, int status, int start, int end)
132 throws SystemException {
133
134 List<Long> folderIds = new ArrayList<Long>();
135
136 folderIds.add(folderId);
137
138 return dlFolderFinder.filterFindFE_FS_ByG_F_S(
139 groupId, folderIds, status, start, end);
140 }
141
142 public int getFileEntriesAndFileShortcutsCount(
143 long groupId, List<Long> folderIds, int status)
144 throws SystemException {
145
146 return dlFolderFinder.filterCountFE_FS_ByG_F_S(
147 groupId, folderIds, status);
148 }
149
150 public int getFileEntriesAndFileShortcutsCount(
151 long groupId, long folderId, int status)
152 throws SystemException {
153
154 List<Long> folderIds = new ArrayList<Long>();
155
156 folderIds.add(folderId);
157
158 return dlFolderFinder.filterCountFE_FS_ByG_F_S(
159 groupId, folderIds, status);
160 }
161
162 public DLFolder getFolder(long folderId)
163 throws PortalException, SystemException {
164
165 DLFolder folder = dlFolderLocalService.getFolder(folderId);
166
167 DLFolderPermission.check(
168 getPermissionChecker(), folder, ActionKeys.VIEW);
169
170 return folder;
171 }
172
173 public DLFolder getFolder(long groupId, long parentFolderId, String name)
174 throws PortalException, SystemException {
175
176 DLFolder folder = dlFolderLocalService.getFolder(
177 groupId, parentFolderId, name);
178
179 DLFolderPermission.check(
180 getPermissionChecker(), folder, ActionKeys.VIEW);
181
182 return folder;
183 }
184
185 public long getFolderId(long groupId, long parentFolderId, String name)
186 throws PortalException, SystemException {
187
188 DLFolder folder = getFolder(groupId, parentFolderId, name);
189
190 return folder.getFolderId();
191 }
192
193 public List<DLFolder> getFolders(long groupId, long parentFolderId)
194 throws SystemException {
195
196 return dlFolderPersistence.filterFindByG_P(groupId, parentFolderId);
197 }
198
199 public List<DLFolder> getFolders(
200 long groupId, long parentFolderId, int start, int end)
201 throws SystemException {
202
203 return dlFolderPersistence.filterFindByG_P(
204 groupId, parentFolderId, start, end);
205 }
206
207 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
208 long groupId, List<Long> folderIds, int status, int start, int end)
209 throws SystemException {
210
211 return dlFolderFinder.filterFindF_FE_FS_ByG_F_S(
212 groupId, folderIds, status, start, end);
213 }
214
215 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
216 long groupId, long folderId, int status, int start, int end)
217 throws PortalException, SystemException {
218
219 DLFolderPermission.check(
220 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
221
222 List<Long> folderIds = new ArrayList<Long>();
223
224 folderIds.add(folderId);
225
226 return getFoldersAndFileEntriesAndFileShortcuts(
227 groupId, folderIds, status, start, end);
228 }
229
230 public int getFoldersAndFileEntriesAndFileShortcutsCount(
231 long groupId, List<Long> folderIds, int status)
232 throws SystemException {
233
234 return dlFolderFinder.filterCountF_FE_FS_ByG_F_S(
235 groupId, folderIds, status);
236 }
237
238 public int getFoldersAndFileEntriesAndFileShortcutsCount(
239 long groupId, long folderId, int status)
240 throws PortalException, SystemException {
241
242 DLFolderPermission.check(
243 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
244
245 List<Long> folderIds = new ArrayList<Long>();
246
247 folderIds.add(folderId);
248
249 return getFoldersAndFileEntriesAndFileShortcutsCount(
250 groupId, folderIds, status);
251 }
252
253 public int getFoldersCount(long groupId, long parentFolderId)
254 throws SystemException {
255
256 return dlFolderPersistence.filterCountByG_P(groupId, parentFolderId);
257 }
258
259 public void getSubfolderIds(
260 List<Long> folderIds, long groupId, long folderId)
261 throws PortalException, SystemException {
262
263 List<DLFolder> folders = dlFolderPersistence.filterFindByG_P(
264 groupId, folderId);
265
266 for (DLFolder folder : folders) {
267 folderIds.add(folder.getFolderId());
268
269 getSubfolderIds(
270 folderIds, folder.getGroupId(), folder.getFolderId());
271 }
272 }
273
274 public boolean hasInheritableLock(long folderId)
275 throws PortalException, SystemException {
276
277 boolean inheritable = false;
278
279 try {
280 Lock lock = lockLocalService.getLock(
281 DLFolder.class.getName(), folderId);
282
283 inheritable = lock.isInheritable();
284 }
285 catch (ExpiredLockException ele) {
286 }
287 catch (NoSuchLockException nsle) {
288 }
289
290 return inheritable;
291 }
292
293 public Lock lockFolder(long folderId)
294 throws PortalException, RemoteException, SystemException {
295
296 return lockFolder(
297 folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
298 }
299
300 public Lock lockFolder(
301 long folderId, String owner, boolean inheritable,
302 long expirationTime)
303 throws PortalException, RemoteException, SystemException {
304
305 if ((expirationTime <= 0) ||
306 (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
307
308 expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
309 }
310
311 Lock lock = lockLocalService.lock(
312 getUser().getUserId(), DLFolder.class.getName(), folderId, owner,
313 inheritable, expirationTime);
314
315 Set<String> fileNames = new HashSet<String>();
316
317 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
318
319 long groupId = folder.getGroupId();
320
321 try {
322
323 List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
324 groupId, folderId);
325
326 for (DLFileEntry fileEntry : fileEntries) {
327 dlFileEntryService.lockFileEntry(
328 groupId, folderId, fileEntry.getName(), owner,
329 expirationTime);
330
331 fileNames.add(fileEntry.getName());
332 }
333 }
334 catch (Exception e) {
335 for (String fileName : fileNames) {
336 dlFileEntryService.unlockFileEntry(groupId, folderId, fileName);
337 }
338
339 unlockFolder(groupId, folderId, lock.getUuid());
340
341 if (e instanceof PortalException) {
342 throw (PortalException)e;
343 }
344 else if (e instanceof RemoteException) {
345 throw (RemoteException)e;
346 }
347 else if (e instanceof SystemException) {
348 throw (SystemException)e;
349 }
350 else {
351 throw new PortalException(e);
352 }
353 }
354
355 return lock;
356 }
357
358 public Lock refreshFolderLock(String lockUuid, long expirationTime)
359 throws PortalException, SystemException {
360
361 return lockLocalService.refresh(lockUuid, expirationTime);
362 }
363
364 public void unlockFolder(long groupId, long folderId, String lockUuid)
365 throws PortalException, SystemException {
366
367 if (Validator.isNotNull(lockUuid)) {
368 try {
369 Lock lock = lockLocalService.getLock(
370 DLFolder.class.getName(), folderId);
371
372 if (!lock.getUuid().equals(lockUuid)) {
373 throw new InvalidLockException("UUIDs do not match");
374 }
375 }
376 catch (PortalException pe) {
377 if (pe instanceof ExpiredLockException ||
378 pe instanceof NoSuchLockException) {
379 }
380 else {
381 throw pe;
382 }
383 }
384 }
385
386 lockLocalService.unlock(DLFolder.class.getName(), folderId);
387
388 try {
389 List<DLFileEntry> fileEntries =
390 dlFileEntryLocalService.getFileEntries(groupId, folderId);
391
392 for (DLFileEntry fileEntry : fileEntries) {
393 dlFileEntryService.unlockFileEntry(
394 groupId, folderId, fileEntry.getName());
395 }
396 }
397 catch (Exception e) {
398 _log.error(e, e);
399 }
400 }
401
402 public void unlockFolder(
403 long groupId, long parentFolderId, String name, String lockUuid)
404 throws PortalException, SystemException {
405
406 long folderId = getFolderId(groupId, parentFolderId, name);
407
408 unlockFolder(groupId, folderId, lockUuid);
409 }
410
411 public DLFolder updateFolder(
412 long folderId, long parentFolderId, String name, String description,
413 ServiceContext serviceContext)
414 throws PortalException, RemoteException, SystemException {
415
416 DLFolder folder = dlFolderLocalService.getFolder(folderId);
417
418 DLFolderPermission.check(
419 getPermissionChecker(), folder, ActionKeys.UPDATE);
420
421 boolean hasLock = lockLocalService.hasLock(
422 getUserId(), DLFolder.class.getName(), folderId);
423
424 Lock lock = null;
425
426 if (!hasLock) {
427
428
430 lock = lockFolder(folderId);
431 }
432
433 try {
434 return dlFolderLocalService.updateFolder(
435 folderId, parentFolderId, name, description, serviceContext);
436 }
437 finally {
438 if (!hasLock) {
439
440
442 unlockFolder(folder.getGroupId(), folderId, lock.getUuid());
443 }
444 }
445 }
446
447 public boolean verifyInheritableLock(long folderId, String lockUuid)
448 throws PortalException, SystemException {
449
450 boolean verified = false;
451
452 try {
453 Lock lock = lockLocalService.getLock(
454 DLFolder.class.getName(), folderId);
455
456 if (!lock.isInheritable()) {
457 throw new NoSuchLockException();
458 }
459
460 if (lock.getUuid().equals(lockUuid)) {
461 verified = true;
462 }
463 }
464 catch (ExpiredLockException ele) {
465 throw new NoSuchLockException(ele);
466 }
467
468 return verified;
469 }
470
471 protected void copyFolder(
472 DLFolder srcFolder, DLFolder destFolder,
473 ServiceContext serviceContext)
474 throws PortalException, RemoteException, SystemException {
475
476 List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
477 srcFolder.getGroupId(), srcFolder.getFolderId());
478
479 for (DLFileEntry srcFileEntry : srcFileEntries) {
480 String name = srcFileEntry.getName();
481 String title = srcFileEntry.getTitle();
482 String description = srcFileEntry.getDescription();
483 String extraSettings = srcFileEntry.getExtraSettings();
484
485 File file = null;
486
487 try {
488 file = FileUtil.createTempFile(FileUtil.getExtension(title));
489
490 InputStream is = dlLocalService.getFileAsStream(
491 srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
492
493 FileUtil.write(file, is);
494 }
495 catch (Exception e) {
496 _log.error(e, e);
497
498 continue;
499 }
500
501 dlFileEntryService.addFileEntry(
502 destFolder.getGroupId(), destFolder.getFolderId(), name, title,
503 description, null, extraSettings, file, serviceContext);
504
505 file.delete();
506 }
507
508 List<DLFolder> srcSubfolders = getFolders(
509 srcFolder.getGroupId(), srcFolder.getFolderId());
510
511 for (DLFolder srcSubfolder : srcSubfolders) {
512 String name = srcSubfolder.getName();
513 String description = srcSubfolder.getDescription();
514
515 DLFolder destSubfolder = addFolder(
516 destFolder.getGroupId(), destFolder.getFolderId(), name,
517 description, serviceContext);
518
519 copyFolder(srcSubfolder, destSubfolder, serviceContext);
520 }
521 }
522
523 private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
524
525 }