001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.portal.ExpiredLockException;
018 import com.liferay.portal.InvalidLockException;
019 import com.liferay.portal.NoSuchLockException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.ArrayUtil;
025 import com.liferay.portal.kernel.util.FileUtil;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.Lock;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031 import com.liferay.portlet.documentlibrary.model.DLFolder;
032 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
033 import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
034 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
035
036 import java.io.File;
037 import java.io.InputStream;
038
039 import java.rmi.RemoteException;
040
041 import java.util.ArrayList;
042 import java.util.HashSet;
043 import java.util.List;
044 import java.util.Set;
045
046
049 public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
050
051 public DLFolder addFolder(
052 long groupId, long parentFolderId, String name, String description,
053 ServiceContext serviceContext)
054 throws PortalException, SystemException {
055
056 DLFolderPermission.check(
057 getPermissionChecker(), groupId, parentFolderId,
058 ActionKeys.ADD_FOLDER);
059
060 return dlFolderLocalService.addFolder(
061 getUserId(), groupId, parentFolderId, name, description,
062 serviceContext);
063 }
064
065 public DLFolder copyFolder(
066 long groupId, long sourceFolderId, long parentFolderId, String name,
067 String description, ServiceContext serviceContext)
068 throws PortalException, RemoteException, SystemException {
069
070 DLFolder srcFolder = getFolder(sourceFolderId);
071
072 DLFolder destFolder = addFolder(
073 groupId, parentFolderId, name, description, serviceContext);
074
075 copyFolder(srcFolder, destFolder, serviceContext);
076
077 return destFolder;
078 }
079
080 public void deleteFolder(long folderId)
081 throws PortalException, RemoteException, SystemException {
082
083 DLFolder folder = dlFolderLocalService.getFolder(folderId);
084
085 DLFolderPermission.check(
086 getPermissionChecker(), folder, ActionKeys.DELETE);
087
088 boolean hasLock = lockLocalService.hasLock(
089 getUserId(), DLFolder.class.getName(), folderId);
090
091 Lock lock = null;
092
093 if (!hasLock) {
094
095
096
097 lock = lockFolder(folderId);
098 }
099
100 try {
101 dlFolderLocalService.deleteFolder(folderId);
102 }
103 finally {
104 if (!hasLock) {
105
106
107
108 unlockFolder(folder.getGroupId(), folderId, lock.getUuid());
109 }
110 }
111 }
112
113 public void deleteFolder(long groupId, long parentFolderId, String name)
114 throws PortalException, RemoteException, SystemException {
115
116 long folderId = getFolderId(groupId, parentFolderId, name);
117
118 deleteFolder(folderId);
119 }
120
121 public List<Object> getFileEntriesAndFileShortcuts(
122 long groupId, List<Long> folderIds, int status, int start, int end)
123 throws SystemException {
124
125 return dlFolderFinder.filterFindFE_FS_ByG_F_S(
126 groupId, folderIds, status, start, end);
127 }
128
129 public List<Object> getFileEntriesAndFileShortcuts(
130 long groupId, long folderId, int status, int start, int end)
131 throws SystemException {
132
133 List<Long> folderIds = new ArrayList<Long>();
134
135 folderIds.add(folderId);
136
137 return dlFolderFinder.filterFindFE_FS_ByG_F_S(
138 groupId, folderIds, status, start, end);
139 }
140
141 public int getFileEntriesAndFileShortcutsCount(
142 long groupId, List<Long> folderIds, int status)
143 throws SystemException {
144
145 return dlFolderFinder.filterCountFE_FS_ByG_F_S(
146 groupId, folderIds, status);
147 }
148
149 public int getFileEntriesAndFileShortcutsCount(
150 long groupId, long folderId, int status)
151 throws SystemException {
152
153 List<Long> folderIds = new ArrayList<Long>();
154
155 folderIds.add(folderId);
156
157 return dlFolderFinder.filterCountFE_FS_ByG_F_S(
158 groupId, folderIds, status);
159 }
160
161 public DLFolder getFolder(long folderId)
162 throws PortalException, SystemException {
163
164 DLFolder folder = dlFolderLocalService.getFolder(folderId);
165
166 DLFolderPermission.check(
167 getPermissionChecker(), folder, ActionKeys.VIEW);
168
169 return folder;
170 }
171
172 public DLFolder getFolder(long groupId, long parentFolderId, String name)
173 throws PortalException, SystemException {
174
175 DLFolder folder = dlFolderLocalService.getFolder(
176 groupId, parentFolderId, name);
177
178 DLFolderPermission.check(
179 getPermissionChecker(), folder, ActionKeys.VIEW);
180
181 return folder;
182 }
183
184 public long getFolderId(long groupId, long parentFolderId, String name)
185 throws PortalException, SystemException {
186
187 DLFolder folder = getFolder(groupId, parentFolderId, name);
188
189 return folder.getFolderId();
190 }
191
192 public long[] getFolderIds(long groupId, long folderId)
193 throws SystemException {
194
195 List<Long> folderIds = new ArrayList<Long>();
196
197 folderIds.add(folderId);
198
199 getSubfolderIds(folderIds, groupId, folderId);
200
201 return ArrayUtil.toArray(
202 folderIds.toArray(new Long[folderIds.size()]));
203 }
204
205 public List<DLFolder> getFolders(long groupId, long parentFolderId)
206 throws SystemException {
207
208 return dlFolderPersistence.filterFindByG_P(groupId, parentFolderId);
209 }
210
211 public List<DLFolder> getFolders(
212 long groupId, long parentFolderId, int start, int end)
213 throws SystemException {
214
215 return dlFolderPersistence.filterFindByG_P(
216 groupId, parentFolderId, start, end);
217 }
218
219 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
220 long groupId, List<Long> folderIds, int status, int start, int end)
221 throws SystemException {
222
223 return dlFolderFinder.filterFindF_FE_FS_ByG_F_S(
224 groupId, folderIds, status, start, end);
225 }
226
227 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
228 long groupId, long folderId, int status, int start, int end)
229 throws PortalException, SystemException {
230
231 DLFolderPermission.check(
232 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
233
234 List<Long> folderIds = new ArrayList<Long>();
235
236 folderIds.add(folderId);
237
238 return getFoldersAndFileEntriesAndFileShortcuts(
239 groupId, folderIds, status, start, end);
240 }
241
242 public int getFoldersAndFileEntriesAndFileShortcutsCount(
243 long groupId, List<Long> folderIds, int status)
244 throws SystemException {
245
246 return dlFolderFinder.filterCountF_FE_FS_ByG_F_S(
247 groupId, folderIds, status);
248 }
249
250 public int getFoldersAndFileEntriesAndFileShortcutsCount(
251 long groupId, long folderId, int status)
252 throws PortalException, SystemException {
253
254 DLFolderPermission.check(
255 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
256
257 List<Long> folderIds = new ArrayList<Long>();
258
259 folderIds.add(folderId);
260
261 return getFoldersAndFileEntriesAndFileShortcutsCount(
262 groupId, folderIds, status);
263 }
264
265 public int getFoldersCount(long groupId, long parentFolderId)
266 throws SystemException {
267
268 return dlFolderPersistence.filterCountByG_P(groupId, parentFolderId);
269 }
270
271 public void getSubfolderIds(
272 List<Long> folderIds, long groupId, long folderId)
273 throws SystemException {
274
275 getSubfolderIds(folderIds, groupId, folderId, true);
276 }
277
278 public void getSubfolderIds(
279 List<Long> folderIds, long groupId, long folderId, boolean recurse)
280 throws SystemException {
281
282 List<DLFolder> folders = dlFolderPersistence.filterFindByG_P(
283 groupId, folderId);
284
285 for (DLFolder folder : folders) {
286 folderIds.add(folder.getFolderId());
287
288 if (recurse) {
289 getSubfolderIds(
290 folderIds, folder.getGroupId(), folder.getFolderId());
291 }
292 }
293 }
294
295 public boolean hasInheritableLock(long folderId)
296 throws PortalException, SystemException {
297
298 boolean inheritable = false;
299
300 try {
301 Lock lock = lockLocalService.getLock(
302 DLFolder.class.getName(), folderId);
303
304 inheritable = lock.isInheritable();
305 }
306 catch (ExpiredLockException ele) {
307 }
308 catch (NoSuchLockException nsle) {
309 }
310
311 return inheritable;
312 }
313
314 public Lock lockFolder(long folderId)
315 throws PortalException, RemoteException, SystemException {
316
317 return lockFolder(
318 folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
319 }
320
321 public Lock lockFolder(
322 long folderId, String owner, boolean inheritable,
323 long expirationTime)
324 throws PortalException, RemoteException, SystemException {
325
326 if ((expirationTime <= 0) ||
327 (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
328
329 expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
330 }
331
332 Lock lock = lockLocalService.lock(
333 getUser().getUserId(), DLFolder.class.getName(), folderId, owner,
334 inheritable, expirationTime);
335
336 Set<String> fileNames = new HashSet<String>();
337
338 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
339
340 long groupId = folder.getGroupId();
341
342 try {
343
344 List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
345 groupId, folderId);
346
347 for (DLFileEntry fileEntry : fileEntries) {
348 dlFileEntryService.lockFileEntry(
349 groupId, folderId, fileEntry.getName(), owner,
350 expirationTime);
351
352 fileNames.add(fileEntry.getName());
353 }
354 }
355 catch (Exception e) {
356 for (String fileName : fileNames) {
357 dlFileEntryService.unlockFileEntry(groupId, folderId, fileName);
358 }
359
360 unlockFolder(groupId, folderId, lock.getUuid());
361
362 if (e instanceof PortalException) {
363 throw (PortalException)e;
364 }
365 else if (e instanceof RemoteException) {
366 throw (RemoteException)e;
367 }
368 else if (e instanceof SystemException) {
369 throw (SystemException)e;
370 }
371 else {
372 throw new PortalException(e);
373 }
374 }
375
376 return lock;
377 }
378
379 public Lock refreshFolderLock(String lockUuid, long expirationTime)
380 throws PortalException, SystemException {
381
382 return lockLocalService.refresh(lockUuid, expirationTime);
383 }
384
385 public void unlockFolder(long groupId, long folderId, String lockUuid)
386 throws PortalException, SystemException {
387
388 if (Validator.isNotNull(lockUuid)) {
389 try {
390 Lock lock = lockLocalService.getLock(
391 DLFolder.class.getName(), folderId);
392
393 if (!lock.getUuid().equals(lockUuid)) {
394 throw new InvalidLockException("UUIDs do not match");
395 }
396 }
397 catch (PortalException pe) {
398 if (pe instanceof ExpiredLockException ||
399 pe instanceof NoSuchLockException) {
400 }
401 else {
402 throw pe;
403 }
404 }
405 }
406
407 lockLocalService.unlock(DLFolder.class.getName(), folderId);
408
409 try {
410 List<DLFileEntry> fileEntries =
411 dlFileEntryLocalService.getFileEntries(groupId, folderId);
412
413 for (DLFileEntry fileEntry : fileEntries) {
414 dlFileEntryService.unlockFileEntry(
415 groupId, folderId, fileEntry.getName());
416 }
417 }
418 catch (Exception e) {
419 _log.error(e, e);
420 }
421 }
422
423 public void unlockFolder(
424 long groupId, long parentFolderId, String name, String lockUuid)
425 throws PortalException, SystemException {
426
427 long folderId = getFolderId(groupId, parentFolderId, name);
428
429 unlockFolder(groupId, folderId, lockUuid);
430 }
431
432 public DLFolder updateFolder(
433 long folderId, long parentFolderId, String name, String description,
434 ServiceContext serviceContext)
435 throws PortalException, RemoteException, SystemException {
436
437 DLFolder folder = dlFolderLocalService.getFolder(folderId);
438
439 DLFolderPermission.check(
440 getPermissionChecker(), folder, ActionKeys.UPDATE);
441
442 boolean hasLock = lockLocalService.hasLock(
443 getUserId(), DLFolder.class.getName(), folderId);
444
445 Lock lock = null;
446
447 if (!hasLock) {
448
449
450
451 lock = lockFolder(folderId);
452 }
453
454 try {
455 return dlFolderLocalService.updateFolder(
456 folderId, parentFolderId, name, description, serviceContext);
457 }
458 finally {
459 if (!hasLock) {
460
461
462
463 unlockFolder(folder.getGroupId(), folderId, lock.getUuid());
464 }
465 }
466 }
467
468 public boolean verifyInheritableLock(long folderId, String lockUuid)
469 throws PortalException, SystemException {
470
471 boolean verified = false;
472
473 try {
474 Lock lock = lockLocalService.getLock(
475 DLFolder.class.getName(), folderId);
476
477 if (!lock.isInheritable()) {
478 throw new NoSuchLockException();
479 }
480
481 if (lock.getUuid().equals(lockUuid)) {
482 verified = true;
483 }
484 }
485 catch (ExpiredLockException ele) {
486 throw new NoSuchLockException(ele);
487 }
488
489 return verified;
490 }
491
492 protected void copyFolder(
493 DLFolder srcFolder, DLFolder destFolder,
494 ServiceContext serviceContext)
495 throws PortalException, RemoteException, SystemException {
496
497 List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
498 srcFolder.getGroupId(), srcFolder.getFolderId());
499
500 for (DLFileEntry srcFileEntry : srcFileEntries) {
501 String name = srcFileEntry.getName();
502 String title = srcFileEntry.getTitle();
503 String description = srcFileEntry.getDescription();
504 String extraSettings = srcFileEntry.getExtraSettings();
505
506 File file = null;
507
508 try {
509 file = FileUtil.createTempFile(FileUtil.getExtension(title));
510
511 InputStream is = dlLocalService.getFileAsStream(
512 srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
513
514 FileUtil.write(file, is);
515 }
516 catch (Exception e) {
517 _log.error(e, e);
518
519 continue;
520 }
521
522 dlFileEntryService.addFileEntry(
523 destFolder.getGroupId(), destFolder.getFolderId(), name, title,
524 description, null, extraSettings, file, serviceContext);
525
526 file.delete();
527 }
528
529 List<DLFolder> srcSubfolders = getFolders(
530 srcFolder.getGroupId(), srcFolder.getFolderId());
531
532 for (DLFolder srcSubfolder : srcSubfolders) {
533 String name = srcSubfolder.getName();
534 String description = srcSubfolder.getDescription();
535
536 DLFolder destSubfolder = addFolder(
537 destFolder.getGroupId(), destFolder.getFolderId(), name,
538 description, serviceContext);
539
540 copyFolder(srcSubfolder, destSubfolder, serviceContext);
541 }
542 }
543
544 private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
545
546 }