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