001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.increment.BufferedIncrement;
021    import com.liferay.portal.kernel.increment.DateOverrideIncrement;
022    import com.liferay.portal.kernel.lock.ExpiredLockException;
023    import com.liferay.portal.kernel.lock.InvalidLockException;
024    import com.liferay.portal.kernel.lock.Lock;
025    import com.liferay.portal.kernel.lock.LockManagerUtil;
026    import com.liferay.portal.kernel.lock.NoSuchLockException;
027    import com.liferay.portal.kernel.repository.event.RepositoryEventTrigger;
028    import com.liferay.portal.kernel.repository.event.RepositoryEventType;
029    import com.liferay.portal.kernel.repository.model.Folder;
030    import com.liferay.portal.kernel.search.Indexable;
031    import com.liferay.portal.kernel.search.IndexableType;
032    import com.liferay.portal.kernel.search.Indexer;
033    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
034    import com.liferay.portal.kernel.systemevent.SystemEvent;
035    import com.liferay.portal.kernel.util.ObjectValuePair;
036    import com.liferay.portal.kernel.util.OrderByComparator;
037    import com.liferay.portal.kernel.util.ParamUtil;
038    import com.liferay.portal.kernel.util.StringPool;
039    import com.liferay.portal.kernel.util.TreeModelTasksAdapter;
040    import com.liferay.portal.kernel.util.TreePathUtil;
041    import com.liferay.portal.kernel.util.Validator;
042    import com.liferay.portal.kernel.workflow.WorkflowConstants;
043    import com.liferay.portal.model.Group;
044    import com.liferay.portal.model.Repository;
045    import com.liferay.portal.model.ResourceConstants;
046    import com.liferay.portal.model.SystemEventConstants;
047    import com.liferay.portal.model.TreeModel;
048    import com.liferay.portal.model.User;
049    import com.liferay.portal.model.WorkflowDefinitionLink;
050    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
051    import com.liferay.portal.service.ServiceContext;
052    import com.liferay.portal.service.permission.ModelPermissions;
053    import com.liferay.portal.util.RepositoryUtil;
054    import com.liferay.portlet.documentlibrary.DuplicateFileEntryException;
055    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
056    import com.liferay.portlet.documentlibrary.FolderNameException;
057    import com.liferay.portlet.documentlibrary.InvalidFolderException;
058    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
059    import com.liferay.portlet.documentlibrary.RequiredFileEntryTypeException;
060    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
061    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
062    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
063    import com.liferay.portlet.documentlibrary.model.DLFolder;
064    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
065    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
066    import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
067    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
068    import com.liferay.portlet.documentlibrary.util.DLValidatorUtil;
069    import com.liferay.portlet.documentlibrary.util.comparator.FolderIdComparator;
070    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
071    
072    import java.io.Serializable;
073    
074    import java.util.ArrayList;
075    import java.util.Collections;
076    import java.util.Date;
077    import java.util.HashSet;
078    import java.util.List;
079    import java.util.Map;
080    import java.util.Set;
081    
082    /**
083     * @author Brian Wing Shun Chan
084     * @author Alexander Chow
085     */
086    public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
087    
088            @Override
089            public DLFolder addFolder(
090                            long userId, long groupId, long repositoryId, boolean mountPoint,
091                            long parentFolderId, String name, String description,
092                            boolean hidden, ServiceContext serviceContext)
093                    throws PortalException {
094    
095                    // Folder
096    
097                    User user = userPersistence.findByPrimaryKey(userId);
098                    parentFolderId = getParentFolderId(
099                            groupId, repositoryId, parentFolderId);
100                    Date now = new Date();
101    
102                    validateFolder(groupId, parentFolderId, name);
103    
104                    long folderId = counterLocalService.increment();
105    
106                    DLFolder dlFolder = dlFolderPersistence.create(folderId);
107    
108                    dlFolder.setUuid(serviceContext.getUuid());
109                    dlFolder.setGroupId(groupId);
110                    dlFolder.setCompanyId(user.getCompanyId());
111                    dlFolder.setUserId(user.getUserId());
112                    dlFolder.setUserName(user.getFullName());
113                    dlFolder.setRepositoryId(repositoryId);
114                    dlFolder.setMountPoint(mountPoint);
115                    dlFolder.setParentFolderId(parentFolderId);
116                    dlFolder.setTreePath(dlFolder.buildTreePath());
117                    dlFolder.setName(name);
118                    dlFolder.setDescription(description);
119                    dlFolder.setLastPostDate(now);
120                    dlFolder.setHidden(hidden);
121                    dlFolder.setRestrictionType(DLFolderConstants.RESTRICTION_TYPE_INHERIT);
122                    dlFolder.setExpandoBridgeAttributes(serviceContext);
123    
124                    dlFolderPersistence.update(dlFolder);
125    
126                    // Resources
127    
128                    if (serviceContext.isAddGroupPermissions() ||
129                            serviceContext.isAddGuestPermissions()) {
130    
131                            addFolderResources(
132                                    dlFolder, serviceContext.isAddGroupPermissions(),
133                                    serviceContext.isAddGuestPermissions());
134                    }
135                    else {
136                            if (serviceContext.isDeriveDefaultPermissions()) {
137                                    serviceContext.deriveDefaultPermissions(
138                                            repositoryId, DLFolderConstants.getClassName());
139                            }
140    
141                            addFolderResources(dlFolder, serviceContext.getModelPermissions());
142                    }
143    
144                    // Parent folder
145    
146                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
147                            dlFolderLocalService.updateLastPostDate(parentFolderId, now);
148                    }
149    
150                    return dlFolder;
151            }
152    
153            /**
154             * @deprecated As of 6.2.0, replaced by more general {@link #addFolder(long,
155             *             long, long, boolean, long, String, String, boolean,
156             *             ServiceContext)}
157             */
158            @Deprecated
159            @Override
160            public DLFolder addFolder(
161                            long userId, long groupId, long repositoryId, boolean mountPoint,
162                            long parentFolderId, String name, String description,
163                            ServiceContext serviceContext)
164                    throws PortalException {
165    
166                    return addFolder(
167                            userId, groupId, repositoryId, mountPoint, parentFolderId, name,
168                            description, false, serviceContext);
169            }
170    
171            /**
172             * @deprecated As of 7.0.0, replaced by {@link #deleteAllByGroup(long)}
173             */
174            @Deprecated
175            @Override
176            public void deleteAll(long groupId) throws PortalException {
177                    deleteAllByGroup(groupId);
178            }
179    
180            @Override
181            public void deleteAllByGroup(long groupId) throws PortalException {
182                    Group group = groupLocalService.getGroup(groupId);
183    
184                    List<DLFolder> dlFolders = dlFolderPersistence.findByGroupId(groupId);
185    
186                    for (DLFolder dlFolder : dlFolders) {
187                            dlFolderLocalService.deleteFolder(dlFolder);
188                    }
189    
190                    dlFileEntryLocalService.deleteFileEntries(
191                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
192    
193                    dlFileEntryTypeLocalService.deleteFileEntryTypes(groupId);
194    
195                    dlFileShortcutLocalService.deleteFileShortcuts(
196                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
197    
198                    DLStoreUtil.deleteDirectory(
199                            group.getCompanyId(), groupId, StringPool.BLANK);
200            }
201    
202            @Override
203            public void deleteAllByRepository(long repositoryId)
204                    throws PortalException {
205    
206                    Repository repository = repositoryLocalService.fetchRepository(
207                            repositoryId);
208    
209                    long groupId = repositoryId;
210    
211                    if (repository != null) {
212                            groupId = repository.getGroupId();
213                    }
214    
215                    Group group = groupLocalService.getGroup(groupId);
216    
217                    RepositoryEventTrigger repositoryEventTrigger =
218                            RepositoryUtil.getRepositoryEventTrigger(repositoryId);
219    
220                    List<DLFolder> dlFolders = dlFolderPersistence.findByRepositoryId(
221                            repositoryId);
222    
223                    for (DLFolder dlFolder : dlFolders) {
224                            deleteFolderDependencies(dlFolder, true);
225    
226                            repositoryEventTrigger.trigger(
227                                    RepositoryEventType.Delete.class, Folder.class,
228                                    new LiferayFolder(dlFolder));
229                    }
230    
231                    if (repository != null) {
232                            dlFileEntryLocalService.deleteRepositoryFileEntries(
233                                    repository.getRepositoryId(), repository.getDlFolderId());
234                    }
235                    else {
236                            dlFileEntryLocalService.deleteFileEntries(
237                                    groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
238    
239                            dlFileShortcutLocalService.deleteFileShortcuts(
240                                    groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
241                    }
242    
243                    DLStoreUtil.deleteDirectory(
244                            group.getCompanyId(), repositoryId, StringPool.BLANK);
245            }
246    
247            @Indexable(type = IndexableType.DELETE)
248            @Override
249            @SystemEvent(
250                    action = SystemEventConstants.ACTION_SKIP,
251                    type = SystemEventConstants.TYPE_DELETE
252            )
253            public DLFolder deleteFolder(DLFolder dlFolder) throws PortalException {
254                    return deleteFolder(dlFolder, true);
255            }
256    
257            @Indexable(type = IndexableType.DELETE)
258            @Override
259            @SystemEvent(
260                    action = SystemEventConstants.ACTION_SKIP,
261                    type = SystemEventConstants.TYPE_DELETE
262            )
263            public DLFolder deleteFolder(
264                            DLFolder dlFolder, boolean includeTrashedEntries)
265                    throws PortalException {
266    
267                    deleteSubfolders(dlFolder, includeTrashedEntries);
268    
269                    deleteFolderDependencies(dlFolder, includeTrashedEntries);
270    
271                    return dlFolder;
272            }
273    
274            @Indexable(type = IndexableType.DELETE)
275            @Override
276            public DLFolder deleteFolder(long folderId) throws PortalException {
277                    return dlFolderLocalService.deleteFolder(folderId, true);
278            }
279    
280            @Indexable(type = IndexableType.DELETE)
281            @Override
282            public DLFolder deleteFolder(long folderId, boolean includeTrashedEntries)
283                    throws PortalException {
284    
285                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
286    
287                    return dlFolderLocalService.deleteFolder(
288                            dlFolder, includeTrashedEntries);
289            }
290    
291            @Indexable(type = IndexableType.DELETE)
292            @Override
293            public DLFolder deleteFolder(
294                            long userId, long folderId, boolean includeTrashedEntries)
295                    throws PortalException {
296    
297                    boolean hasLock = hasFolderLock(userId, folderId);
298    
299                    Lock lock = null;
300    
301                    if (!hasLock) {
302    
303                            // Lock
304    
305                            lock = lockFolder(
306                                    userId, folderId, null, false,
307                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
308                    }
309    
310                    try {
311                            return deleteFolder(folderId, includeTrashedEntries);
312                    }
313                    finally {
314                            if (!hasLock) {
315    
316                                    // Unlock
317    
318                                    unlockFolder(folderId, lock.getUuid());
319                            }
320                    }
321            }
322    
323            @Override
324            public DLFolder fetchFolder(long folderId) {
325                    return dlFolderPersistence.fetchByPrimaryKey(folderId);
326            }
327    
328            @Override
329            public DLFolder fetchFolder(
330                    long groupId, long parentFolderId, String name) {
331    
332                    return dlFolderPersistence.fetchByG_P_N(groupId, parentFolderId, name);
333            }
334    
335            @Override
336            public List<DLFolder> getCompanyFolders(
337                    long companyId, int start, int end) {
338    
339                    return dlFolderPersistence.findByCompanyId(companyId, start, end);
340            }
341    
342            @Override
343            public int getCompanyFoldersCount(long companyId) {
344                    return dlFolderPersistence.countByCompanyId(companyId);
345            }
346    
347            /**
348             * @deprecated As of 6.2.0, replaced by {@link
349             *             #getFileEntriesAndFileShortcuts(long, long, QueryDefinition)}
350             */
351            @Deprecated
352            @Override
353            public List<Object> getFileEntriesAndFileShortcuts(
354                    long groupId, long folderId, int status, int start, int end) {
355    
356                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
357                            status, start, end, null);
358    
359                    return getFileEntriesAndFileShortcuts(
360                            groupId, folderId, queryDefinition);
361            }
362    
363            @Override
364            public List<Object> getFileEntriesAndFileShortcuts(
365                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
366    
367                    return dlFolderFinder.findFE_FS_ByG_F(
368                            groupId, folderId, queryDefinition);
369            }
370    
371            /**
372             * @deprecated As of 6.2.0, replaced by {@link
373             *             #getFileEntriesAndFileShortcutsCount(long, long,
374             *             QueryDefinition)}
375             */
376            @Deprecated
377            @Override
378            public int getFileEntriesAndFileShortcutsCount(
379                    long groupId, long folderId, int status) {
380    
381                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
382    
383                    return getFileEntriesAndFileShortcutsCount(
384                            groupId, folderId, queryDefinition);
385            }
386    
387            @Override
388            public int getFileEntriesAndFileShortcutsCount(
389                    long groupId, long folderId, QueryDefinition<?> queryDefinition) {
390    
391                    return dlFolderFinder.countFE_FS_ByG_F(
392                            groupId, folderId, queryDefinition);
393            }
394    
395            @Override
396            public DLFolder getFolder(long folderId) throws PortalException {
397                    return dlFolderPersistence.findByPrimaryKey(folderId);
398            }
399    
400            @Override
401            public DLFolder getFolder(long groupId, long parentFolderId, String name)
402                    throws PortalException {
403    
404                    return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
405            }
406    
407            @Override
408            public long getFolderId(long companyId, long folderId) {
409                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
410    
411                            // Ensure folder exists and belongs to the proper company
412    
413                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
414    
415                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
416                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
417                            }
418                    }
419    
420                    return folderId;
421            }
422    
423            /**
424             * @deprecated As of 7.0.0, replaced by {@link #getGroupFolderIds(long,
425             *             long)}
426             */
427            @Deprecated
428            @Override
429            public List<Long> getFolderIds(long groupId, long parentFolderId) {
430                    return getGroupFolderIds(groupId, parentFolderId);
431            }
432    
433            @Override
434            public List<DLFolder> getFolders(long groupId, long parentFolderId) {
435                    return getFolders(groupId, parentFolderId, true);
436            }
437    
438            @Override
439            public List<DLFolder> getFolders(
440                    long groupId, long parentFolderId, boolean includeMountfolders) {
441    
442                    if (includeMountfolders) {
443                            return dlFolderPersistence.findByG_P(groupId, parentFolderId);
444                    }
445                    else {
446                            return dlFolderPersistence.findByG_M_P_H(
447                                    groupId, false, parentFolderId, false);
448                    }
449            }
450    
451            @Override
452            public List<DLFolder> getFolders(
453                    long groupId, long parentFolderId, boolean includeMountfolders,
454                    int start, int end, OrderByComparator<DLFolder> obc) {
455    
456                    if (includeMountfolders) {
457                            return dlFolderPersistence.findByG_P(
458                                    groupId, parentFolderId, start, end, obc);
459                    }
460                    else {
461                            return dlFolderPersistence.findByG_M_P_H(
462                                    groupId, false, parentFolderId, false, start, end, obc);
463                    }
464            }
465    
466            @Override
467            public List<DLFolder> getFolders(
468                    long groupId, long parentFolderId, int status,
469                    boolean includeMountfolders, int start, int end,
470                    OrderByComparator<DLFolder> obc) {
471    
472                    if (includeMountfolders) {
473                            return dlFolderPersistence.findByG_P_H_S(
474                                    groupId, parentFolderId, false, status, start, end, obc);
475                    }
476                    else {
477                            return dlFolderPersistence.findByG_M_P_H_S(
478                                    groupId, false, parentFolderId, false, status, start, end, obc);
479                    }
480            }
481    
482            @Override
483            public List<DLFolder> getFolders(
484                    long groupId, long parentFolderId, int start, int end,
485                    OrderByComparator<DLFolder> obc) {
486    
487                    return getFolders(groupId, parentFolderId, true, start, end, obc);
488            }
489    
490            /**
491             * @deprecated As of 6.2.0, replaced by {@link
492             *             #getFoldersAndFileEntriesAndFileShortcuts(long, long,
493             *             String[], boolean, QueryDefinition)}
494             */
495            @Deprecated
496            @Override
497            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
498                    long groupId, long folderId, int status, boolean includeMountFolders,
499                    int start, int end, OrderByComparator<?> obc) {
500    
501                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
502                            status, start, end, (OrderByComparator<Object>)obc);
503    
504                    return getFoldersAndFileEntriesAndFileShortcuts(
505                            groupId, folderId, null, includeMountFolders, queryDefinition);
506            }
507    
508            /**
509             * @deprecated As of 6.2.0, replaced by {@link
510             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
511             *             String[], boolean, QueryDefinition)}
512             */
513            @Deprecated
514            @Override
515            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
516                    long groupId, long folderId, int status, String[] mimeTypes,
517                    boolean includeMountFolders, int start, int end,
518                    OrderByComparator<?> obc) {
519    
520                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
521                            status, start, end, (OrderByComparator<Object>)obc);
522    
523                    return getFoldersAndFileEntriesAndFileShortcuts(
524                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
525            }
526    
527            @Override
528            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
529                    long groupId, long folderId, String[] mimeTypes,
530                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
531    
532                    return dlFolderFinder.findF_FE_FS_ByG_F_M_M(
533                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
534            }
535    
536            /**
537             * @deprecated As of 6.2.0, replaced by {@link
538             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
539             *             String[], boolean, QueryDefinition)}
540             */
541            @Deprecated
542            @Override
543            public int getFoldersAndFileEntriesAndFileShortcutsCount(
544                    long groupId, long folderId, int status, boolean includeMountFolders) {
545    
546                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
547    
548                    return getFoldersAndFileEntriesAndFileShortcutsCount(
549                            groupId, folderId, null, includeMountFolders, queryDefinition);
550            }
551    
552            /**
553             * @deprecated As of 6.2.0, replaced by {@link
554             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
555             *             String[], boolean, QueryDefinition)}
556             */
557            @Deprecated
558            @Override
559            public int getFoldersAndFileEntriesAndFileShortcutsCount(
560                    long groupId, long folderId, int status, String[] mimeTypes,
561                    boolean includeMountFolders) {
562    
563                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
564    
565                    return getFoldersAndFileEntriesAndFileShortcutsCount(
566                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
567            }
568    
569            @Override
570            public int getFoldersAndFileEntriesAndFileShortcutsCount(
571                    long groupId, long folderId, String[] mimeTypes,
572                    boolean includeMountFolders, QueryDefinition<?> queryDefinition) {
573    
574                    return dlFolderFinder.countF_FE_FS_ByG_F_M_M(
575                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
576            }
577    
578            @Override
579            public int getFoldersCount(long groupId, long parentFolderId) {
580                    return getFoldersCount(groupId, parentFolderId, true);
581            }
582    
583            @Override
584            public int getFoldersCount(
585                    long groupId, long parentFolderId, boolean includeMountfolders) {
586    
587                    if (includeMountfolders) {
588                            return dlFolderPersistence.countByG_P(groupId, parentFolderId);
589                    }
590                    else {
591                            return dlFolderPersistence.countByG_M_P_H(
592                                    groupId, false, parentFolderId, false);
593                    }
594            }
595    
596            @Override
597            public int getFoldersCount(
598                    long groupId, long parentFolderId, int status,
599                    boolean includeMountfolders) {
600    
601                    if (includeMountfolders) {
602                            return dlFolderPersistence.countByG_P_H_S(
603                                    groupId, parentFolderId, false, status);
604                    }
605                    else {
606                            return dlFolderPersistence.countByG_M_P_H_S(
607                                    groupId, false, parentFolderId, false, status);
608                    }
609            }
610    
611            @Override
612            public List<Long> getGroupFolderIds(long groupId, long parentFolderId) {
613                    List<Long> folderIds = new ArrayList<>();
614    
615                    folderIds.add(parentFolderId);
616    
617                    getGroupSubfolderIds(folderIds, groupId, parentFolderId);
618    
619                    return folderIds;
620            }
621    
622            @Override
623            public void getGroupSubfolderIds(
624                    List<Long> folderIds, long groupId, long folderId) {
625    
626                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
627                            groupId, folderId);
628    
629                    for (DLFolder dlFolder : dlFolders) {
630                            folderIds.add(dlFolder.getFolderId());
631    
632                            getGroupSubfolderIds(
633                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
634                    }
635            }
636    
637            @Override
638            public DLFolder getMountFolder(long repositoryId) throws PortalException {
639                    return dlFolderPersistence.findByR_M(repositoryId, true);
640            }
641    
642            @Override
643            public List<DLFolder> getMountFolders(
644                    long groupId, long parentFolderId, int start, int end,
645                    OrderByComparator<DLFolder> obc) {
646    
647                    return dlFolderPersistence.findByG_M_P_H(
648                            groupId, true, parentFolderId, false, start, end, obc);
649            }
650    
651            @Override
652            public int getMountFoldersCount(long groupId, long parentFolderId) {
653                    return dlFolderPersistence.countByG_M_P_H(
654                            groupId, true, parentFolderId, false);
655            }
656    
657            @Override
658            public List<DLFolder> getNoAssetFolders() {
659                    return dlFolderFinder.findF_ByNoAssets();
660            }
661    
662            @Override
663            public List<Long> getRepositoryFolderIds(
664                    long repositoryId, long parentFolderId) {
665    
666                    List<Long> folderIds = new ArrayList<>();
667    
668                    folderIds.add(parentFolderId);
669    
670                    getRepositorySubfolderIds(folderIds, repositoryId, parentFolderId);
671    
672                    return folderIds;
673            }
674    
675            @Override
676            public List<DLFolder> getRepositoryFolders(
677                    long repositoryId, int start, int end) {
678    
679                    return dlFolderPersistence.findByRepositoryId(repositoryId, start, end);
680            }
681    
682            @Override
683            public int getRepositoryFoldersCount(long repositoryId) {
684                    return dlFolderPersistence.countByRepositoryId(repositoryId);
685            }
686    
687            @Override
688            public void getRepositorySubfolderIds(
689                    List<Long> folderIds, long repositoryId, long folderId) {
690    
691                    List<DLFolder> dlFolders = dlFolderPersistence.findByR_P(
692                            repositoryId, folderId);
693    
694                    for (DLFolder dlFolder : dlFolders) {
695                            folderIds.add(dlFolder.getFolderId());
696    
697                            getRepositorySubfolderIds(
698                                    folderIds, dlFolder.getRepositoryId(), dlFolder.getFolderId());
699                    }
700            }
701    
702            /**
703             * @deprecated As of 7.0.0, replaced by {@link #getGroupSubfolderIds(List,
704             *             long, long)}
705             */
706            @Deprecated
707            @Override
708            public void getSubfolderIds(
709                    List<Long> folderIds, long groupId, long folderId) {
710    
711                    getGroupSubfolderIds(folderIds, groupId, folderId);
712            }
713    
714            @Override
715            public boolean hasFolderLock(long userId, long folderId) {
716                    return LockManagerUtil.hasLock(
717                            userId, DLFolder.class.getName(), folderId);
718            }
719    
720            @Override
721            public Lock lockFolder(long userId, long folderId) throws PortalException {
722                    return lockFolder(
723                            userId, folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
724            }
725    
726            @Override
727            public Lock lockFolder(
728                            long userId, long folderId, String owner, boolean inheritable,
729                            long expirationTime)
730                    throws PortalException {
731    
732                    if ((expirationTime <= 0) ||
733                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
734    
735                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
736                    }
737    
738                    return LockManagerUtil.lock(
739                            userId, DLFolder.class.getName(), folderId, owner, inheritable,
740                            expirationTime);
741            }
742    
743            @Indexable(type = IndexableType.REINDEX)
744            @Override
745            public DLFolder moveFolder(
746                            long userId, long folderId, long parentFolderId,
747                            ServiceContext serviceContext)
748                    throws PortalException {
749    
750                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
751    
752                    parentFolderId = getParentFolderId(dlFolder, parentFolderId);
753    
754                    if (dlFolder.getParentFolderId() == parentFolderId) {
755                            return dlFolder;
756                    }
757    
758                    boolean hasLock = hasFolderLock(userId, folderId);
759    
760                    Lock lock = null;
761    
762                    if (!hasLock) {
763    
764                            // Lock
765    
766                            lock = lockFolder(userId, folderId);
767                    }
768    
769                    try {
770                            validateFolder(
771                                    dlFolder.getFolderId(), dlFolder.getGroupId(), parentFolderId,
772                                    dlFolder.getName());
773    
774                            dlFolder.setParentFolderId(parentFolderId);
775                            dlFolder.setTreePath(dlFolder.buildTreePath());
776                            dlFolder.setExpandoBridgeAttributes(serviceContext);
777    
778                            dlFolderPersistence.update(dlFolder);
779    
780                            rebuildTree(
781                                    dlFolder.getCompanyId(), folderId, dlFolder.getTreePath(),
782                                    true);
783    
784                            return dlFolder;
785                    }
786                    finally {
787                            if (!hasLock) {
788    
789                                    // Unlock
790    
791                                    unlockFolder(folderId, lock.getUuid());
792                            }
793                    }
794            }
795    
796            @Override
797            public void rebuildTree(long companyId) throws PortalException {
798                    rebuildTree(
799                            companyId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
800                            StringPool.SLASH, false);
801            }
802    
803            @Override
804            public void rebuildTree(
805                            long companyId, long parentFolderId, String parentTreePath,
806                            final boolean reindex)
807                    throws PortalException {
808    
809                    TreePathUtil.rebuildTree(
810                            companyId, parentFolderId, parentTreePath,
811                            new TreeModelTasksAdapter<DLFolder>() {
812    
813                                    @Override
814                                    public List<DLFolder> findTreeModels(
815                                            long previousId, long companyId, long parentPrimaryKey,
816                                            int size) {
817    
818                                            return dlFolderPersistence.findByF_C_P_NotS(
819                                                    previousId, companyId, parentPrimaryKey,
820                                                    WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
821                                                    size, new FolderIdComparator(true));
822                                    }
823    
824                                    @Override
825                                    public void rebuildDependentModelsTreePaths(
826                                                    long parentPrimaryKey, String treePath)
827                                            throws PortalException {
828    
829                                            dlFileEntryLocalService.setTreePaths(
830                                                    parentPrimaryKey, treePath, false);
831                                            dlFileShortcutLocalService.setTreePaths(
832                                                    parentPrimaryKey, treePath);
833                                            dlFileVersionLocalService.setTreePaths(
834                                                    parentPrimaryKey, treePath);
835                                    }
836    
837                                    @Override
838                                    public void reindexTreeModels(List<TreeModel> treeModels)
839                                            throws PortalException {
840    
841                                            if (!reindex) {
842                                                    return;
843                                            }
844    
845                                            Indexer<DLFolder> indexer =
846                                                    IndexerRegistryUtil.nullSafeGetIndexer(DLFolder.class);
847    
848                                            for (TreeModel treeModel : treeModels) {
849                                                    DLFolder dlFolder = (DLFolder)treeModel;
850    
851                                                    indexer.reindex(dlFolder);
852                                            }
853                                    }
854    
855                            }
856                    );
857            }
858    
859            @Override
860            public void unlockFolder(
861                            long groupId, long parentFolderId, String name, String lockUuid)
862                    throws PortalException {
863    
864                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
865    
866                    unlockFolder(dlFolder.getFolderId(), lockUuid);
867            }
868    
869            @Override
870            public void unlockFolder(long folderId, String lockUuid)
871                    throws PortalException {
872    
873                    if (Validator.isNotNull(lockUuid)) {
874                            try {
875                                    Lock lock = LockManagerUtil.getLock(
876                                            DLFolder.class.getName(), folderId);
877    
878                                    if (!lockUuid.equals(lock.getUuid())) {
879                                            throw new InvalidLockException("UUIDs do not match");
880                                    }
881                            }
882                            catch (ExpiredLockException | NoSuchLockException e) {
883                            }
884                    }
885    
886                    LockManagerUtil.unlock(DLFolder.class.getName(), folderId);
887            }
888    
889            /**
890             * @deprecated As of 7.0.0, replaced by {@link #updateFolder(long, long,
891             *             String, String, long, List, int, ServiceContext)}
892             */
893            @Deprecated
894            @Override
895            public DLFolder updateFolder(
896                            long folderId, long parentFolderId, String name, String description,
897                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
898                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
899                    throws PortalException {
900    
901                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
902    
903                    if (overrideFileEntryTypes) {
904                            restrictionType =
905                                    DLFolderConstants.
906                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
907                    }
908    
909                    return updateFolder(
910                            folderId, name, description, defaultFileEntryTypeId,
911                            fileEntryTypeIds, restrictionType, serviceContext);
912            }
913    
914            @Indexable(type = IndexableType.REINDEX)
915            @Override
916            public DLFolder updateFolder(
917                            long folderId, long parentFolderId, String name, String description,
918                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
919                            int restrictionType, ServiceContext serviceContext)
920                    throws PortalException {
921    
922                    boolean hasLock = hasFolderLock(serviceContext.getUserId(), folderId);
923    
924                    Lock lock = null;
925    
926                    if (!hasLock) {
927    
928                            // Lock
929    
930                            lock = lockFolder(
931                                    serviceContext.getUserId(), folderId, null, false,
932                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
933                    }
934    
935                    try {
936    
937                            // File entry types
938    
939                            DLFolder dlFolder = null;
940    
941                            Set<Long> originalFileEntryTypeIds = new HashSet<>();
942    
943                            if (folderId > DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
944                                    originalFileEntryTypeIds = getFileEntryTypeIds(
945                                            dlFolderPersistence.getDLFileEntryTypes(folderId));
946    
947                                    dlFolder = dlFolderLocalService.updateFolderAndFileEntryTypes(
948                                            serviceContext.getUserId(), folderId, parentFolderId, name,
949                                            description, defaultFileEntryTypeId, fileEntryTypeIds,
950                                            restrictionType, serviceContext);
951    
952                                    dlFileEntryTypeLocalService.cascadeFileEntryTypes(
953                                            serviceContext.getUserId(), dlFolder);
954                            }
955    
956                            // Workflow definitions
957    
958                            List<ObjectValuePair<Long, String>> workflowDefinitionOVPs =
959                                    new ArrayList<>();
960    
961                            if (restrictionType ==
962                                            DLFolderConstants.
963                                                    RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) {
964    
965                                    workflowDefinitionOVPs.add(
966                                            new ObjectValuePair<Long, String>(
967                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
968                                                    StringPool.BLANK));
969    
970                                    for (long fileEntryTypeId : fileEntryTypeIds) {
971                                            String workflowDefinition = ParamUtil.getString(
972                                                    serviceContext, "workflowDefinition" + fileEntryTypeId);
973    
974                                            workflowDefinitionOVPs.add(
975                                                    new ObjectValuePair<Long, String>(
976                                                            fileEntryTypeId, workflowDefinition));
977                                    }
978                            }
979                            else if (restrictionType ==
980                                                    DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
981    
982                                    if (originalFileEntryTypeIds.isEmpty()) {
983                                            originalFileEntryTypeIds.add(
984                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
985                                    }
986    
987                                    for (long originalFileEntryTypeId : originalFileEntryTypeIds) {
988                                            workflowDefinitionOVPs.add(
989                                                    new ObjectValuePair<Long, String>(
990                                                            originalFileEntryTypeId, StringPool.BLANK));
991                                    }
992                            }
993                            else if (restrictionType ==
994                                                    DLFolderConstants.RESTRICTION_TYPE_WORKFLOW) {
995    
996                                    String workflowDefinition = ParamUtil.getString(
997                                            serviceContext,
998                                            "workflowDefinition" +
999                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
1000    
1001                                    workflowDefinitionOVPs.add(
1002                                            new ObjectValuePair<Long, String>(
1003                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
1004                                                    workflowDefinition));
1005    
1006                                    for (long originalFileEntryTypeId : originalFileEntryTypeIds) {
1007                                            workflowDefinitionOVPs.add(
1008                                                    new ObjectValuePair<Long, String>(
1009                                                            originalFileEntryTypeId, StringPool.BLANK));
1010                                    }
1011                            }
1012    
1013                            workflowDefinitionLinkLocalService.updateWorkflowDefinitionLinks(
1014                                    serviceContext.getUserId(), serviceContext.getCompanyId(),
1015                                    serviceContext.getScopeGroupId(), DLFolder.class.getName(),
1016                                    folderId, workflowDefinitionOVPs);
1017    
1018                            return dlFolder;
1019                    }
1020                    finally {
1021                            if (!hasLock) {
1022    
1023                                    // Unlock
1024    
1025                                    unlockFolder(folderId, lock.getUuid());
1026                            }
1027                    }
1028            }
1029    
1030            /**
1031             * @deprecated As of 7.0.0, replaced {@link #updateFolder(long, long,
1032             *             String, String, long, List, int, ServiceContext)}
1033             */
1034            @Deprecated
1035            @Override
1036            public DLFolder updateFolder(
1037                            long folderId, String name, String description,
1038                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
1039                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
1040                    throws PortalException {
1041    
1042                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
1043    
1044                    if (overrideFileEntryTypes) {
1045                            restrictionType =
1046                                    DLFolderConstants.
1047                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
1048                    }
1049    
1050                    return updateFolder(
1051                            serviceContext.getScopeGroupId(), folderId, name, description,
1052                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
1053                            serviceContext);
1054            }
1055    
1056            @Indexable(type = IndexableType.REINDEX)
1057            @Override
1058            public DLFolder updateFolder(
1059                            long folderId, String name, String description,
1060                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
1061                            int restrictionType, ServiceContext serviceContext)
1062                    throws PortalException {
1063    
1064                    long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1065    
1066                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1067                            DLFolder dlFolder = getDLFolder(folderId);
1068    
1069                            parentFolderId = dlFolder.getParentFolderId();
1070                    }
1071    
1072                    return updateFolder(
1073                            folderId, parentFolderId, name, description, defaultFileEntryTypeId,
1074                            fileEntryTypeIds, restrictionType, serviceContext);
1075            }
1076    
1077            /**
1078             * @deprecated As of 7.0.0, replaced by {@link #
1079             *             updateFolderAndFileEntryTypes(long, long, long, String,
1080             *             String, long, List, int, ServiceContext)}
1081             */
1082            @Deprecated
1083            @Override
1084            public DLFolder updateFolderAndFileEntryTypes(
1085                            long userId, long folderId, long parentFolderId, String name,
1086                            String description, long defaultFileEntryTypeId,
1087                            List<Long> fileEntryTypeIds, boolean overrideFileEntryTypes,
1088                            ServiceContext serviceContext)
1089                    throws PortalException {
1090    
1091                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
1092    
1093                    if (overrideFileEntryTypes) {
1094                            restrictionType =
1095                                    DLFolderConstants.
1096                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
1097                    }
1098    
1099                    return updateFolderAndFileEntryTypes(
1100                            userId, folderId, parentFolderId, name, description,
1101                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
1102                            serviceContext);
1103            }
1104    
1105            @Override
1106            public DLFolder updateFolderAndFileEntryTypes(
1107                            long userId, long folderId, long parentFolderId, String name,
1108                            String description, long defaultFileEntryTypeId,
1109                            List<Long> fileEntryTypeIds, int restrictionType,
1110                            ServiceContext serviceContext)
1111                    throws PortalException {
1112    
1113                    if ((restrictionType ==
1114                                    DLFolderConstants.
1115                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) &&
1116                            fileEntryTypeIds.isEmpty()) {
1117    
1118                            throw new RequiredFileEntryTypeException();
1119                    }
1120    
1121                    boolean hasLock = hasFolderLock(userId, folderId);
1122    
1123                    Lock lock = null;
1124    
1125                    if (!hasLock) {
1126    
1127                            // Lock
1128    
1129                            lock = lockFolder(
1130                                    userId, folderId, null, false,
1131                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
1132                    }
1133    
1134                    try {
1135    
1136                            // Folder
1137    
1138                            if (restrictionType == DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
1139                                    fileEntryTypeIds = Collections.emptyList();
1140                            }
1141    
1142                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1143    
1144                            parentFolderId = getParentFolderId(dlFolder, parentFolderId);
1145    
1146                            validateFolder(
1147                                    folderId, dlFolder.getGroupId(), parentFolderId, name);
1148    
1149                            long oldParentFolderId = dlFolder.getParentFolderId();
1150    
1151                            if (oldParentFolderId != parentFolderId) {
1152                                    dlFolder.setParentFolderId(parentFolderId);
1153                                    dlFolder.setTreePath(dlFolder.buildTreePath());
1154                            }
1155    
1156                            dlFolder.setName(name);
1157                            dlFolder.setDescription(description);
1158                            dlFolder.setExpandoBridgeAttributes(serviceContext);
1159                            dlFolder.setRestrictionType(restrictionType);
1160                            dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
1161    
1162                            dlFolderPersistence.update(dlFolder);
1163    
1164                            // File entry types
1165    
1166                            if (fileEntryTypeIds != null) {
1167                                    dlFileEntryTypeLocalService.updateFolderFileEntryTypes(
1168                                            dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
1169                                            serviceContext);
1170                            }
1171    
1172                            if (oldParentFolderId != parentFolderId) {
1173                                    rebuildTree(
1174                                            dlFolder.getCompanyId(), folderId, dlFolder.getTreePath(),
1175                                            true);
1176                            }
1177    
1178                            return dlFolder;
1179                    }
1180                    finally {
1181                            if (!hasLock) {
1182    
1183                                    // Unlock
1184    
1185                                    unlockFolder(folderId, lock.getUuid());
1186                            }
1187                    }
1188            }
1189    
1190            @BufferedIncrement(
1191                    configuration = "DLFolderEntry",
1192                    incrementClass = DateOverrideIncrement.class
1193            )
1194            @Override
1195            public void updateLastPostDate(long folderId, Date lastPostDate)
1196                    throws PortalException {
1197    
1198                    if (ExportImportThreadLocal.isImportInProcess() ||
1199                            (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) ||
1200                            (lastPostDate == null)) {
1201    
1202                            return;
1203                    }
1204    
1205                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1206    
1207                    if (lastPostDate.before(dlFolder.getLastPostDate())) {
1208                            return;
1209                    }
1210    
1211                    dlFolder.setModifiedDate(dlFolder.getModifiedDate());
1212                    dlFolder.setLastPostDate(lastPostDate);
1213    
1214                    dlFolderPersistence.update(dlFolder);
1215            }
1216    
1217            @Override
1218            public DLFolder updateStatus(
1219                            long userId, long folderId, int status,
1220                            Map<String, Serializable> workflowContext,
1221                            ServiceContext serviceContext)
1222                    throws PortalException {
1223    
1224                    // Folder
1225    
1226                    User user = userPersistence.findByPrimaryKey(userId);
1227    
1228                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1229    
1230                    int oldStatus = dlFolder.getStatus();
1231    
1232                    dlFolder.setStatus(status);
1233                    dlFolder.setStatusByUserId(user.getUserId());
1234                    dlFolder.setStatusByUserName(user.getFullName());
1235                    dlFolder.setStatusDate(new Date());
1236    
1237                    dlFolderPersistence.update(dlFolder);
1238    
1239                    // Asset
1240    
1241                    if (status == WorkflowConstants.STATUS_APPROVED) {
1242                            assetEntryLocalService.updateVisible(
1243                                    DLFolder.class.getName(), dlFolder.getFolderId(), true);
1244                    }
1245                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
1246                            assetEntryLocalService.updateVisible(
1247                                    DLFolder.class.getName(), dlFolder.getFolderId(), false);
1248                    }
1249    
1250                    // Indexer
1251    
1252                    if (((status == WorkflowConstants.STATUS_APPROVED) ||
1253                             (status == WorkflowConstants.STATUS_IN_TRASH) ||
1254                             (oldStatus == WorkflowConstants.STATUS_IN_TRASH)) &&
1255                            ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1256    
1257                            Indexer<DLFolder> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1258                                    DLFolder.class);
1259    
1260                            indexer.reindex(dlFolder);
1261                    }
1262    
1263                    return dlFolder;
1264            }
1265    
1266            protected void addFolderResources(
1267                            DLFolder dlFolder, boolean addGroupPermissions,
1268                            boolean addGuestPermissions)
1269                    throws PortalException {
1270    
1271                    resourceLocalService.addResources(
1272                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1273                            dlFolder.getUserId(), DLFolder.class.getName(),
1274                            dlFolder.getFolderId(), false, addGroupPermissions,
1275                            addGuestPermissions);
1276            }
1277    
1278            protected void addFolderResources(
1279                            DLFolder dlFolder, ModelPermissions modelPermissions)
1280                    throws PortalException {
1281    
1282                    resourceLocalService.addModelResources(
1283                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1284                            dlFolder.getUserId(), DLFolder.class.getName(),
1285                            dlFolder.getFolderId(), modelPermissions);
1286            }
1287    
1288            protected void addFolderResources(
1289                            long folderId, boolean addGroupPermissions,
1290                            boolean addGuestPermissions)
1291                    throws PortalException {
1292    
1293                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1294    
1295                    addFolderResources(dlFolder, addGroupPermissions, addGuestPermissions);
1296            }
1297    
1298            protected void addFolderResources(
1299                            long folderId, ModelPermissions modelPermissions)
1300                    throws PortalException {
1301    
1302                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1303    
1304                    addFolderResources(dlFolder, modelPermissions);
1305            }
1306    
1307            protected void deleteFolderDependencies(
1308                            DLFolder dlFolder, boolean includeTrashedEntries)
1309                    throws PortalException {
1310    
1311                    // Resources
1312    
1313                    resourceLocalService.deleteResource(
1314                            dlFolder.getCompanyId(), DLFolder.class.getName(),
1315                            ResourceConstants.SCOPE_INDIVIDUAL, dlFolder.getFolderId());
1316    
1317                    // WebDAVProps
1318    
1319                    webDAVPropsLocalService.deleteWebDAVProps(
1320                            DLFolder.class.getName(), dlFolder.getFolderId());
1321    
1322                    // File entries
1323    
1324                    dlFileEntryLocalService.deleteFileEntries(
1325                            dlFolder.getGroupId(), dlFolder.getFolderId(),
1326                            includeTrashedEntries);
1327    
1328                    // File entry types
1329    
1330                    List<Long> fileEntryTypeIds = new ArrayList<>();
1331    
1332                    for (DLFileEntryType dlFileEntryType :
1333                                    dlFileEntryTypeLocalService.getDLFolderDLFileEntryTypes(
1334                                            dlFolder.getFolderId())) {
1335    
1336                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
1337                    }
1338    
1339                    if (fileEntryTypeIds.isEmpty()) {
1340                            fileEntryTypeIds.add(
1341                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
1342                    }
1343    
1344                    dlFileEntryTypeLocalService.unsetFolderFileEntryTypes(
1345                            dlFolder.getFolderId());
1346    
1347                    // File shortcuts
1348    
1349                    dlFileShortcutLocalService.deleteFileShortcuts(
1350                            dlFolder.getGroupId(), dlFolder.getFolderId(),
1351                            includeTrashedEntries);
1352    
1353                    // Expando
1354    
1355                    expandoRowLocalService.deleteRows(dlFolder.getFolderId());
1356    
1357                    // Ratings
1358    
1359                    ratingsStatsLocalService.deleteStats(
1360                            DLFolder.class.getName(), dlFolder.getFolderId());
1361    
1362                    // Folder
1363    
1364                    dlFolderPersistence.remove(dlFolder);
1365    
1366                    // Directory
1367    
1368                    if (includeTrashedEntries) {
1369                            DLStoreUtil.deleteDirectory(
1370                                    dlFolder.getCompanyId(), dlFolder.getFolderId(),
1371                                    StringPool.BLANK);
1372                    }
1373    
1374                    // Workflow
1375    
1376                    for (long fileEntryTypeId : fileEntryTypeIds) {
1377                            WorkflowDefinitionLink workflowDefinitionLink =
1378                                    workflowDefinitionLinkLocalService.fetchWorkflowDefinitionLink(
1379                                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1380                                            DLFolder.class.getName(), dlFolder.getFolderId(),
1381                                            fileEntryTypeId);
1382    
1383                            if (workflowDefinitionLink != null) {
1384                                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
1385                                            workflowDefinitionLink);
1386                            }
1387                    }
1388            }
1389    
1390            protected void deleteSubfolders(
1391                            DLFolder dlFolder, boolean includeTrashedEntries)
1392                    throws PortalException {
1393    
1394                    RepositoryEventTrigger repositoryEventTrigger =
1395                            RepositoryUtil.getRepositoryEventTrigger(
1396                                    dlFolder.getRepositoryId());
1397    
1398                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
1399                            dlFolder.getGroupId(), dlFolder.getFolderId());
1400    
1401                    for (DLFolder curDLFolder : dlFolders) {
1402                            if (includeTrashedEntries || !curDLFolder.isInTrashExplicitly()) {
1403                                    repositoryEventTrigger.trigger(
1404                                            RepositoryEventType.Delete.class, Folder.class,
1405                                            new LiferayFolder(curDLFolder));
1406    
1407                                    dlFolderLocalService.deleteFolder(
1408                                            curDLFolder, includeTrashedEntries);
1409                            }
1410                    }
1411            }
1412    
1413            protected Set<Long> getFileEntryTypeIds(
1414                    List<DLFileEntryType> dlFileEntryTypes) {
1415    
1416                    Set<Long> fileEntryTypeIds = new HashSet<>();
1417    
1418                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
1419                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
1420                    }
1421    
1422                    return fileEntryTypeIds;
1423            }
1424    
1425            protected long getParentFolderId(DLFolder dlFolder, long parentFolderId)
1426                    throws PortalException {
1427    
1428                    parentFolderId = getParentFolderId(
1429                            dlFolder.getGroupId(), dlFolder.getRepositoryId(), parentFolderId);
1430    
1431                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1432                            return parentFolderId;
1433                    }
1434    
1435                    if (dlFolder.getFolderId() == parentFolderId) {
1436                            throw new InvalidFolderException(
1437                                    InvalidFolderException.CANNOT_MOVE_INTO_ITSELF, parentFolderId);
1438                    }
1439    
1440                    List<Long> subfolderIds = new ArrayList<>();
1441    
1442                    getGroupSubfolderIds(
1443                            subfolderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
1444    
1445                    if (subfolderIds.contains(parentFolderId)) {
1446                            throw new InvalidFolderException(
1447                                    InvalidFolderException.CANNOT_MOVE_INTO_CHILD_FOLDER,
1448                                    parentFolderId);
1449                    }
1450    
1451                    return parentFolderId;
1452            }
1453    
1454            protected long getParentFolderId(
1455                            long groupId, long repositoryId, long parentFolderId)
1456                    throws PortalException {
1457    
1458                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1459                            return parentFolderId;
1460                    }
1461    
1462                    DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(
1463                            parentFolderId);
1464    
1465                    if (parentDLFolder.getGroupId() != groupId) {
1466                            throw new NoSuchFolderException(
1467                                    String.format(
1468                                            "No folder exists with the primary key %s in group %s",
1469                                            parentFolderId, groupId));
1470                    }
1471    
1472                    if ((parentDLFolder.getRepositoryId() != repositoryId) &&
1473                            (parentDLFolder.getRepositoryId() != groupId)) {
1474    
1475                            Repository repository = repositoryLocalService.getRepository(
1476                                    repositoryId);
1477    
1478                            if (repository.getGroupId() != parentDLFolder.getGroupId()) {
1479                                    throw new NoSuchFolderException(
1480                                            String.format(
1481                                                    "No folder exists with the primary key %s in " +
1482                                                            "repository %s",
1483                                                    parentFolderId, repositoryId));
1484                            }
1485                    }
1486    
1487                    return parentDLFolder.getFolderId();
1488            }
1489    
1490            protected void validateFolder(
1491                            long folderId, long groupId, long parentFolderId, String name)
1492                    throws PortalException {
1493    
1494                    validateFolderName(name);
1495    
1496                    DLValidatorUtil.validateDirectoryName(name);
1497    
1498                    DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchFileEntry(
1499                            groupId, parentFolderId, name);
1500    
1501                    if (dlFileEntry != null) {
1502                            throw new DuplicateFileEntryException(name);
1503                    }
1504    
1505                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
1506                            groupId, parentFolderId, name);
1507    
1508                    if ((dlFolder != null) && (dlFolder.getFolderId() != folderId)) {
1509                            throw new DuplicateFolderNameException(name);
1510                    }
1511            }
1512    
1513            protected void validateFolder(
1514                            long groupId, long parentFolderId, String name)
1515                    throws PortalException {
1516    
1517                    long folderId = 0;
1518    
1519                    validateFolder(folderId, groupId, parentFolderId, name);
1520            }
1521    
1522            protected void validateFolderName(String folderName)
1523                    throws PortalException {
1524    
1525                    if (folderName.contains(StringPool.SLASH)) {
1526                            throw new FolderNameException(folderName);
1527                    }
1528            }
1529    
1530    }