001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.ObjectValuePair;
022 import com.liferay.portal.kernel.util.OrderByComparator;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.workflow.WorkflowConstants;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.ResourceConstants;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.util.PropsValues;
032 import com.liferay.portlet.asset.util.AssetUtil;
033 import com.liferay.portlet.documentlibrary.DuplicateFileException;
034 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
035 import com.liferay.portlet.documentlibrary.FolderNameException;
036 import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
037 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
038 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
039 import com.liferay.portlet.documentlibrary.model.DLFolder;
040 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
041 import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
042 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
043
044 import java.util.ArrayList;
045 import java.util.Collections;
046 import java.util.Date;
047 import java.util.List;
048
049
053 public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
054
055 public DLFolder addFolder(
056 long userId, long groupId, long repositoryId, boolean mountPoint,
057 long parentFolderId, String name, String description,
058 ServiceContext serviceContext)
059 throws PortalException, SystemException {
060
061
062
063 User user = userPersistence.findByPrimaryKey(userId);
064 parentFolderId = getParentFolderId(groupId, parentFolderId);
065 Date now = new Date();
066
067 validateFolder(groupId, parentFolderId, name);
068
069 long folderId = counterLocalService.increment();
070
071 DLFolder dlFolder = dlFolderPersistence.create(folderId);
072
073 dlFolder.setUuid(serviceContext.getUuid());
074 dlFolder.setGroupId(groupId);
075 dlFolder.setCompanyId(user.getCompanyId());
076 dlFolder.setUserId(user.getUserId());
077 dlFolder.setCreateDate(serviceContext.getCreateDate(now));
078 dlFolder.setModifiedDate(serviceContext.getModifiedDate(now));
079 dlFolder.setRepositoryId(repositoryId);
080 dlFolder.setMountPoint(mountPoint);
081 dlFolder.setParentFolderId(parentFolderId);
082 dlFolder.setName(name);
083 dlFolder.setDescription(description);
084 dlFolder.setOverrideFileEntryTypes(false);
085 dlFolder.setExpandoBridgeAttributes(serviceContext);
086
087 dlFolderPersistence.update(dlFolder, false);
088
089
090
091 if (serviceContext.isAddGroupPermissions() ||
092 serviceContext.isAddGuestPermissions()) {
093
094 addFolderResources(
095 dlFolder, serviceContext.isAddGroupPermissions(),
096 serviceContext.isAddGuestPermissions());
097 }
098 else {
099 if (serviceContext.isDeriveDefaultPermissions()) {
100 serviceContext.deriveDefaultPermissions(
101 repositoryId, DLFolderConstants.getClassName());
102 }
103
104 addFolderResources(
105 dlFolder, serviceContext.getGroupPermissions(),
106 serviceContext.getGuestPermissions());
107 }
108
109
110
111 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
112 DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(
113 parentFolderId);
114
115 parentDLFolder.setLastPostDate(now);
116
117 dlFolderPersistence.update(parentDLFolder, false);
118 }
119
120
121
122 dlAppHelperLocalService.addFolder(
123 new LiferayFolder(dlFolder), serviceContext);
124
125 return dlFolder;
126 }
127
128 public void deleteAll(long groupId)
129 throws PortalException, SystemException {
130
131 Group group = groupLocalService.getGroup(groupId);
132
133 List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
134 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
135
136 for (DLFolder dlFolder : dlFolders) {
137 deleteFolder(dlFolder);
138 }
139
140 dlFileEntryLocalService.deleteFileEntries(
141 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
142
143 try {
144 DLStoreUtil.deleteDirectory(
145 group.getCompanyId(), groupId, StringPool.BLANK);
146 }
147 catch (NoSuchDirectoryException nsde) {
148 if (_log.isDebugEnabled()) {
149 _log.debug(nsde.getMessage());
150 }
151 }
152 }
153
154 public void deleteFolder(long folderId)
155 throws PortalException, SystemException {
156
157 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
158
159 deleteFolder(dlFolder);
160 }
161
162 public List<DLFolder> getCompanyFolders(long companyId, int start, int end)
163 throws SystemException {
164
165 return dlFolderPersistence.findByCompanyId(companyId, start, end);
166 }
167
168 public int getCompanyFoldersCount(long companyId) throws SystemException {
169 return dlFolderPersistence.countByCompanyId(companyId);
170 }
171
172 public List<Object> getFileEntriesAndFileShortcuts(
173 long groupId, long folderId, int status, int start, int end)
174 throws SystemException {
175
176 return dlFolderFinder.findFE_FS_ByG_F_S(
177 groupId, folderId, status, start, end);
178 }
179
180 public int getFileEntriesAndFileShortcutsCount(
181 long groupId, long folderId, int status)
182 throws SystemException {
183
184 int fileEntriesCount = 0;
185
186 if ((status == WorkflowConstants.STATUS_ANY)) {
187 fileEntriesCount = dlFileEntryPersistence.countByG_F(
188 groupId, folderId);
189 }
190 else {
191 fileEntriesCount = dlFolderFinder.countFE_ByG_F_S(
192 groupId, folderId, status);
193 }
194
195 int fileShortcutsCount = dlFileShortcutPersistence.countByG_F_S(
196 groupId, folderId, 0);
197
198 return fileEntriesCount + fileShortcutsCount;
199 }
200
201 public DLFolder getFolder(long folderId)
202 throws PortalException, SystemException {
203
204 return dlFolderPersistence.findByPrimaryKey(folderId);
205 }
206
207 public DLFolder getFolder(long groupId, long parentFolderId, String name)
208 throws PortalException, SystemException {
209
210 return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
211 }
212
213 public long getFolderId(long companyId, long folderId)
214 throws SystemException {
215
216 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
217
218
219
220 DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
221
222 if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
223 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
224 }
225 }
226
227 return folderId;
228 }
229
230 public List<DLFolder> getFolders(long groupId, long parentFolderId)
231 throws SystemException {
232
233 return getFolders(groupId, parentFolderId, true);
234 }
235
236 public List<DLFolder> getFolders(
237 long groupId, long parentFolderId, boolean includeMountfolders)
238 throws SystemException {
239
240 if (includeMountfolders) {
241 return dlFolderPersistence.findByG_P(groupId, parentFolderId);
242 }
243 else {
244 return dlFolderPersistence.findByG_P_M(
245 groupId, parentFolderId, false);
246 }
247 }
248
249 public List<DLFolder> getFolders(
250 long groupId, long parentFolderId, boolean includeMountfolders,
251 int start, int end, OrderByComparator obc)
252 throws SystemException {
253
254 if (includeMountfolders) {
255 return dlFolderPersistence.findByG_P(
256 groupId, parentFolderId, start, end, obc);
257 }
258 else {
259 return dlFolderPersistence.findByG_P_M(
260 groupId, parentFolderId, false, start, end, obc);
261 }
262 }
263
264 public List<DLFolder> getFolders(
265 long groupId, long parentFolderId, int start, int end,
266 OrderByComparator obc)
267 throws SystemException {
268
269 return getFolders(groupId, parentFolderId, true, start, end, obc);
270 }
271
272 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
273 long groupId, long folderId, int status,
274 boolean includeMountFolders, int start, int end,
275 OrderByComparator obc)
276 throws SystemException {
277
278 return dlFolderFinder.findF_FE_FS_ByG_F_S_M_M(
279 groupId, folderId, status, null, includeMountFolders, start, end,
280 obc);
281 }
282
283 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
284 long groupId, long folderId, int status, String[] mimeTypes,
285 boolean includeMountFolders, int start, int end,
286 OrderByComparator obc)
287 throws SystemException {
288
289 return dlFolderFinder.findF_FE_FS_ByG_F_S_M_M(
290 groupId, folderId, status, mimeTypes, includeMountFolders, start,
291 end, obc);
292 }
293
294 public int getFoldersAndFileEntriesAndFileShortcutsCount(
295 long groupId, long folderId, int status,
296 boolean includeMountFolders)
297 throws SystemException {
298
299 return dlFolderFinder.countF_FE_FS_ByG_F_S_M_M(
300 groupId, folderId, status, null, includeMountFolders);
301 }
302
303 public int getFoldersAndFileEntriesAndFileShortcutsCount(
304 long groupId, long folderId, int status, String[] mimeTypes,
305 boolean includeMountFolders)
306 throws SystemException {
307
308 return dlFolderFinder.countF_FE_FS_ByG_F_S_M_M(
309 groupId, folderId, status, mimeTypes, includeMountFolders);
310 }
311
312 public int getFoldersCount(long groupId, long parentFolderId)
313 throws SystemException {
314
315 return getFoldersCount(groupId, parentFolderId, true);
316 }
317
318 public int getFoldersCount(
319 long groupId, long parentFolderId, boolean includeMountfolders)
320 throws SystemException {
321
322 if (includeMountfolders) {
323 return dlFolderPersistence.countByG_P(groupId, parentFolderId);
324 }
325 else {
326 return dlFolderPersistence.countByG_P_M(
327 groupId, parentFolderId, false);
328 }
329 }
330
331 public int getFoldersFileEntriesCount(
332 long groupId, List<Long> folderIds, int status)
333 throws SystemException {
334
335 if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
336 return dlFileEntryFinder.countByG_F_S(groupId, folderIds, status);
337 }
338 else {
339 int start = 0;
340 int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
341
342 int filesCount = dlFileEntryFinder.countByG_F_S(
343 groupId, folderIds.subList(start, end), status);
344
345 folderIds.subList(start, end).clear();
346
347 filesCount += getFoldersFileEntriesCount(
348 groupId, folderIds, status);
349
350 return filesCount;
351 }
352 }
353
354 public DLFolder getMountFolder(long repositoryId)
355 throws PortalException, SystemException {
356
357 return dlFolderPersistence.findByRepositoryId(repositoryId);
358 }
359
360 public List<DLFolder> getMountFolders(
361 long groupId, long parentFolderId, int start, int end,
362 OrderByComparator obc)
363 throws SystemException {
364
365 return dlFolderPersistence.findByG_P_M(
366 groupId, parentFolderId, true, start, end, obc);
367 }
368
369 public int getMountFoldersCount(long groupId, long parentFolderId)
370 throws SystemException {
371
372 return dlFolderPersistence.countByG_P_M(groupId, parentFolderId, true);
373 }
374
375 public void getSubfolderIds(
376 List<Long> folderIds, long groupId, long folderId)
377 throws SystemException {
378
379 List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
380 groupId, folderId);
381
382 for (DLFolder dlFolder : dlFolders) {
383 folderIds.add(dlFolder.getFolderId());
384
385 getSubfolderIds(
386 folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
387 }
388 }
389
390 public DLFolder moveFolder(
391 long folderId, long parentFolderId, ServiceContext serviceContext)
392 throws PortalException, SystemException {
393
394 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
395
396 parentFolderId = getParentFolderId(dlFolder, parentFolderId);
397
398 validateFolder(
399 dlFolder.getFolderId(), dlFolder.getGroupId(), parentFolderId,
400 dlFolder.getName());
401
402 dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
403 dlFolder.setParentFolderId(parentFolderId);
404 dlFolder.setExpandoBridgeAttributes(serviceContext);
405
406 dlFolderPersistence.update(dlFolder, false);
407
408 dlAppHelperLocalService.moveFolder(new LiferayFolder(dlFolder));
409
410 return dlFolder;
411 }
412
413 public DLFolder updateFolder(
414 long folderId, long parentFolderId, String name, String description,
415 long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
416 boolean overrideFileEntryTypes, ServiceContext serviceContext)
417 throws PortalException, SystemException {
418
419
420
421 DLFolder dlFolder = null;
422
423 if (folderId > DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
424 dlFolder = dlFolderLocalService.updateFolderAndFileEntryTypes(
425 folderId, parentFolderId, name, description,
426 defaultFileEntryTypeId, fileEntryTypeIds,
427 overrideFileEntryTypes, serviceContext);
428
429 dlFileEntryTypeLocalService.cascadeFileEntryTypes(
430 serviceContext.getUserId(), dlFolder);
431 }
432
433
434
435 List<ObjectValuePair<Long, String>> workflowDefinitions =
436 new ArrayList<ObjectValuePair<Long, String>>();
437
438 if (fileEntryTypeIds.isEmpty()) {
439 fileEntryTypeIds.add(
440 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
441 }
442 else {
443 workflowDefinitions.add(
444 new ObjectValuePair<Long, String>(
445 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
446 StringPool.BLANK));
447 }
448
449 for (long fileEntryTypeId : fileEntryTypeIds) {
450 String workflowDefinition = ParamUtil.getString(
451 serviceContext, "workflowDefinition" + fileEntryTypeId);
452
453 workflowDefinitions.add(
454 new ObjectValuePair<Long, String>(
455 fileEntryTypeId, workflowDefinition));
456 }
457
458 workflowDefinitionLinkLocalService.updateWorkflowDefinitionLinks(
459 serviceContext.getUserId(), serviceContext.getCompanyId(),
460 serviceContext.getScopeGroupId(), DLFolder.class.getName(),
461 folderId, workflowDefinitions);
462
463 return dlFolder;
464 }
465
466 public DLFolder updateFolder(
467 long folderId, String name, String description,
468 long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
469 boolean overrideFileEntryTypes, ServiceContext serviceContext)
470 throws PortalException, SystemException {
471
472 return updateFolder(
473 folderId, folderId, name, description, defaultFileEntryTypeId,
474 fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
475 }
476
477 public DLFolder updateFolderAndFileEntryTypes(
478 long folderId, long parentFolderId, String name, String description,
479 long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
480 boolean overrideFileEntryTypes, ServiceContext serviceContext)
481 throws PortalException, SystemException {
482
483
484
485 if (!overrideFileEntryTypes) {
486 fileEntryTypeIds = Collections.emptyList();
487 }
488
489 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
490
491 parentFolderId = getParentFolderId(dlFolder, parentFolderId);
492
493 validateFolder(folderId, dlFolder.getGroupId(), parentFolderId, name);
494
495 dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
496 dlFolder.setParentFolderId(parentFolderId);
497 dlFolder.setName(name);
498 dlFolder.setDescription(description);
499 dlFolder.setExpandoBridgeAttributes(serviceContext);
500 dlFolder.setOverrideFileEntryTypes(overrideFileEntryTypes);
501 dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
502
503 dlFolderPersistence.update(dlFolder, false);
504
505
506
507 if (fileEntryTypeIds != null) {
508 dlFileEntryTypeLocalService.updateFolderFileEntryTypes(
509 dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
510 serviceContext);
511 }
512
513
514
515 dlAppHelperLocalService.updateFolder(
516 new LiferayFolder(dlFolder), serviceContext);
517
518 return dlFolder;
519 }
520
521 public void updateLastPostDate(long folderId, Date lastPostDate)
522 throws PortalException, SystemException {
523
524 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
525
526 dlFolder.setLastPostDate(lastPostDate);
527
528 dlFolderPersistence.update(dlFolder, false);
529 }
530
531 protected void addFolderResources(
532 DLFolder dlFolder, boolean addGroupPermissions,
533 boolean addGuestPermissions)
534 throws PortalException, SystemException {
535
536 resourceLocalService.addResources(
537 dlFolder.getCompanyId(), dlFolder.getGroupId(),
538 dlFolder.getUserId(), DLFolder.class.getName(),
539 dlFolder.getFolderId(), false, addGroupPermissions,
540 addGuestPermissions);
541 }
542
543 protected void addFolderResources(
544 DLFolder dlFolder, String[] groupPermissions,
545 String[] guestPermissions)
546 throws PortalException, SystemException {
547
548 resourceLocalService.addModelResources(
549 dlFolder.getCompanyId(), dlFolder.getGroupId(),
550 dlFolder.getUserId(), DLFolder.class.getName(),
551 dlFolder.getFolderId(), groupPermissions, guestPermissions);
552 }
553
554 protected void addFolderResources(
555 long folderId, boolean addGroupPermissions,
556 boolean addGuestPermissions)
557 throws PortalException, SystemException {
558
559 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
560
561 addFolderResources(dlFolder, addGroupPermissions, addGuestPermissions);
562 }
563
564 protected void addFolderResources(
565 long folderId, String[] groupPermissions, String[] guestPermissions)
566 throws PortalException, SystemException {
567
568 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
569
570 addFolderResources(dlFolder, groupPermissions, guestPermissions);
571 }
572
573 protected void deleteFolder(DLFolder dlFolder)
574 throws PortalException, SystemException {
575
576
577
578 List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
579 dlFolder.getGroupId(), dlFolder.getFolderId());
580
581 for (DLFolder curDLFolder : dlFolders) {
582 deleteFolder(curDLFolder);
583 }
584
585
586
587 dlFolderPersistence.remove(dlFolder);
588
589
590
591 resourceLocalService.deleteResource(
592 dlFolder.getCompanyId(), DLFolder.class.getName(),
593 ResourceConstants.SCOPE_INDIVIDUAL, dlFolder.getFolderId());
594
595
596
597 webDAVPropsLocalService.deleteWebDAVProps(
598 DLFolder.class.getName(), dlFolder.getFolderId());
599
600
601
602 dlFileEntryLocalService.deleteFileEntries(
603 dlFolder.getGroupId(), dlFolder.getFolderId());
604
605
606
607 dlFileEntryTypeLocalService.unsetFolderFileEntryTypes(
608 dlFolder.getFolderId());
609
610
611
612 expandoValueLocalService.deleteValues(
613 DLFolder.class.getName(), dlFolder.getFolderId());
614
615
616
617 dlAppHelperLocalService.deleteFolder(new LiferayFolder(dlFolder));
618
619
620
621 try {
622 DLStoreUtil.deleteDirectory(
623 dlFolder.getCompanyId(), dlFolder.getFolderId(),
624 StringPool.BLANK);
625 }
626 catch (NoSuchDirectoryException nsde) {
627 if (_log.isDebugEnabled()) {
628 _log.debug(nsde.getMessage());
629 }
630 }
631 }
632
633 protected long getParentFolderId(DLFolder dlFolder, long parentFolderId)
634 throws SystemException {
635
636 if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
637 return parentFolderId;
638 }
639
640 if (dlFolder.getFolderId() == parentFolderId) {
641 return dlFolder.getParentFolderId();
642 }
643 else {
644 DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
645 parentFolderId);
646
647 if ((parentDLFolder == null) ||
648 (dlFolder.getGroupId() != parentDLFolder.getGroupId())) {
649
650 return dlFolder.getParentFolderId();
651 }
652
653 List<Long> subfolderIds = new ArrayList<Long>();
654
655 getSubfolderIds(
656 subfolderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
657
658 if (subfolderIds.contains(parentFolderId)) {
659 return dlFolder.getParentFolderId();
660 }
661
662 return parentFolderId;
663 }
664 }
665
666 protected long getParentFolderId(long groupId, long parentFolderId)
667 throws SystemException {
668
669 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
670 DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
671 parentFolderId);
672
673 if ((parentDLFolder == null) ||
674 (groupId != parentDLFolder.getGroupId())) {
675
676 parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
677 }
678 }
679
680 return parentFolderId;
681 }
682
683 protected void validateFolder(
684 long folderId, long groupId, long parentFolderId, String name)
685 throws PortalException, SystemException {
686
687 validateFolderName(name);
688
689 try {
690 dlFileEntryLocalService.getFileEntry(groupId, parentFolderId, name);
691
692 throw new DuplicateFileException(name);
693 }
694 catch (NoSuchFileEntryException nsfee) {
695 }
696
697 DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
698 groupId, parentFolderId, name);
699
700 if ((dlFolder != null) && (dlFolder.getFolderId() != folderId)) {
701 throw new DuplicateFolderNameException(name);
702 }
703 }
704
705 protected void validateFolder(
706 long groupId, long parentFolderId, String name)
707 throws PortalException, SystemException {
708
709 long folderId = 0;
710
711 validateFolder(folderId, groupId, parentFolderId, name);
712 }
713
714 protected void validateFolderName(String name) throws PortalException {
715 if (!AssetUtil.isValidWord(name)) {
716 throw new FolderNameException();
717 }
718 }
719
720 private static Log _log = LogFactoryUtil.getLog(
721 DLFolderLocalServiceImpl.class);
722
723 }