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 (status == WorkflowConstants.STATUS_ANY) {
602                            return getFoldersCount(
603                                    groupId, parentFolderId, includeMountfolders);
604                    }
605                    else if (includeMountfolders) {
606                            return dlFolderPersistence.countByG_P_H_S(
607                                    groupId, parentFolderId, false, status);
608                    }
609                    else {
610                            return dlFolderPersistence.countByG_M_P_H_S(
611                                    groupId, false, parentFolderId, false, status);
612                    }
613            }
614    
615            @Override
616            public List<Long> getGroupFolderIds(long groupId, long parentFolderId) {
617                    List<Long> folderIds = new ArrayList<>();
618    
619                    folderIds.add(parentFolderId);
620    
621                    getGroupSubfolderIds(folderIds, groupId, parentFolderId);
622    
623                    return folderIds;
624            }
625    
626            @Override
627            public void getGroupSubfolderIds(
628                    List<Long> folderIds, long groupId, long folderId) {
629    
630                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
631                            groupId, folderId);
632    
633                    for (DLFolder dlFolder : dlFolders) {
634                            folderIds.add(dlFolder.getFolderId());
635    
636                            getGroupSubfolderIds(
637                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
638                    }
639            }
640    
641            @Override
642            public DLFolder getMountFolder(long repositoryId) throws PortalException {
643                    return dlFolderPersistence.findByR_M(repositoryId, true);
644            }
645    
646            @Override
647            public List<DLFolder> getMountFolders(
648                    long groupId, long parentFolderId, int start, int end,
649                    OrderByComparator<DLFolder> obc) {
650    
651                    return dlFolderPersistence.findByG_M_P_H(
652                            groupId, true, parentFolderId, false, start, end, obc);
653            }
654    
655            @Override
656            public int getMountFoldersCount(long groupId, long parentFolderId) {
657                    return dlFolderPersistence.countByG_M_P_H(
658                            groupId, true, parentFolderId, false);
659            }
660    
661            @Override
662            public List<DLFolder> getNoAssetFolders() {
663                    return dlFolderFinder.findF_ByNoAssets();
664            }
665    
666            @Override
667            public List<Long> getRepositoryFolderIds(
668                    long repositoryId, long parentFolderId) {
669    
670                    List<Long> folderIds = new ArrayList<>();
671    
672                    folderIds.add(parentFolderId);
673    
674                    getRepositorySubfolderIds(folderIds, repositoryId, parentFolderId);
675    
676                    return folderIds;
677            }
678    
679            @Override
680            public List<DLFolder> getRepositoryFolders(
681                    long repositoryId, int start, int end) {
682    
683                    return dlFolderPersistence.findByRepositoryId(repositoryId, start, end);
684            }
685    
686            @Override
687            public int getRepositoryFoldersCount(long repositoryId) {
688                    return dlFolderPersistence.countByRepositoryId(repositoryId);
689            }
690    
691            @Override
692            public void getRepositorySubfolderIds(
693                    List<Long> folderIds, long repositoryId, long folderId) {
694    
695                    List<DLFolder> dlFolders = dlFolderPersistence.findByR_P(
696                            repositoryId, folderId);
697    
698                    for (DLFolder dlFolder : dlFolders) {
699                            folderIds.add(dlFolder.getFolderId());
700    
701                            getRepositorySubfolderIds(
702                                    folderIds, dlFolder.getRepositoryId(), dlFolder.getFolderId());
703                    }
704            }
705    
706            /**
707             * @deprecated As of 7.0.0, replaced by {@link #getGroupSubfolderIds(List,
708             *             long, long)}
709             */
710            @Deprecated
711            @Override
712            public void getSubfolderIds(
713                    List<Long> folderIds, long groupId, long folderId) {
714    
715                    getGroupSubfolderIds(folderIds, groupId, folderId);
716            }
717    
718            @Override
719            public boolean hasFolderLock(long userId, long folderId) {
720                    return LockManagerUtil.hasLock(
721                            userId, DLFolder.class.getName(), folderId);
722            }
723    
724            @Override
725            public Lock lockFolder(long userId, long folderId) throws PortalException {
726                    return lockFolder(
727                            userId, folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
728            }
729    
730            @Override
731            public Lock lockFolder(
732                            long userId, long folderId, String owner, boolean inheritable,
733                            long expirationTime)
734                    throws PortalException {
735    
736                    if ((expirationTime <= 0) ||
737                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
738    
739                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
740                    }
741    
742                    return LockManagerUtil.lock(
743                            userId, DLFolder.class.getName(), folderId, owner, inheritable,
744                            expirationTime);
745            }
746    
747            @Indexable(type = IndexableType.REINDEX)
748            @Override
749            public DLFolder moveFolder(
750                            long userId, long folderId, long parentFolderId,
751                            ServiceContext serviceContext)
752                    throws PortalException {
753    
754                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
755    
756                    parentFolderId = getParentFolderId(dlFolder, parentFolderId);
757    
758                    if (dlFolder.getParentFolderId() == parentFolderId) {
759                            return dlFolder;
760                    }
761    
762                    boolean hasLock = hasFolderLock(userId, folderId);
763    
764                    Lock lock = null;
765    
766                    if (!hasLock) {
767    
768                            // Lock
769    
770                            lock = lockFolder(userId, folderId);
771                    }
772    
773                    try {
774                            validateFolder(
775                                    dlFolder.getFolderId(), dlFolder.getGroupId(), parentFolderId,
776                                    dlFolder.getName());
777    
778                            dlFolder.setParentFolderId(parentFolderId);
779                            dlFolder.setTreePath(dlFolder.buildTreePath());
780                            dlFolder.setExpandoBridgeAttributes(serviceContext);
781    
782                            dlFolderPersistence.update(dlFolder);
783    
784                            rebuildTree(
785                                    dlFolder.getCompanyId(), folderId, dlFolder.getTreePath(),
786                                    true);
787    
788                            return dlFolder;
789                    }
790                    finally {
791                            if (!hasLock) {
792    
793                                    // Unlock
794    
795                                    unlockFolder(folderId, lock.getUuid());
796                            }
797                    }
798            }
799    
800            @Override
801            public void rebuildTree(long companyId) throws PortalException {
802                    rebuildTree(
803                            companyId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
804                            StringPool.SLASH, false);
805            }
806    
807            @Override
808            public void rebuildTree(
809                            long companyId, long parentFolderId, String parentTreePath,
810                            final boolean reindex)
811                    throws PortalException {
812    
813                    TreePathUtil.rebuildTree(
814                            companyId, parentFolderId, parentTreePath,
815                            new TreeModelTasksAdapter<DLFolder>() {
816    
817                                    @Override
818                                    public List<DLFolder> findTreeModels(
819                                            long previousId, long companyId, long parentPrimaryKey,
820                                            int size) {
821    
822                                            return dlFolderPersistence.findByF_C_P_NotS(
823                                                    previousId, companyId, parentPrimaryKey,
824                                                    WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
825                                                    size, new FolderIdComparator(true));
826                                    }
827    
828                                    @Override
829                                    public void rebuildDependentModelsTreePaths(
830                                                    long parentPrimaryKey, String treePath)
831                                            throws PortalException {
832    
833                                            dlFileEntryLocalService.setTreePaths(
834                                                    parentPrimaryKey, treePath, false);
835                                            dlFileShortcutLocalService.setTreePaths(
836                                                    parentPrimaryKey, treePath);
837                                            dlFileVersionLocalService.setTreePaths(
838                                                    parentPrimaryKey, treePath);
839                                    }
840    
841                                    @Override
842                                    public void reindexTreeModels(List<TreeModel> treeModels)
843                                            throws PortalException {
844    
845                                            if (!reindex) {
846                                                    return;
847                                            }
848    
849                                            Indexer<DLFolder> indexer =
850                                                    IndexerRegistryUtil.nullSafeGetIndexer(DLFolder.class);
851    
852                                            for (TreeModel treeModel : treeModels) {
853                                                    DLFolder dlFolder = (DLFolder)treeModel;
854    
855                                                    indexer.reindex(dlFolder);
856                                            }
857                                    }
858    
859                            }
860                    );
861            }
862    
863            @Override
864            public void unlockFolder(
865                            long groupId, long parentFolderId, String name, String lockUuid)
866                    throws PortalException {
867    
868                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
869    
870                    unlockFolder(dlFolder.getFolderId(), lockUuid);
871            }
872    
873            @Override
874            public void unlockFolder(long folderId, String lockUuid)
875                    throws PortalException {
876    
877                    if (Validator.isNotNull(lockUuid)) {
878                            try {
879                                    Lock lock = LockManagerUtil.getLock(
880                                            DLFolder.class.getName(), folderId);
881    
882                                    if (!lockUuid.equals(lock.getUuid())) {
883                                            throw new InvalidLockException("UUIDs do not match");
884                                    }
885                            }
886                            catch (ExpiredLockException | NoSuchLockException e) {
887                            }
888                    }
889    
890                    LockManagerUtil.unlock(DLFolder.class.getName(), folderId);
891            }
892    
893            /**
894             * @deprecated As of 7.0.0, replaced by {@link #updateFolder(long, long,
895             *             String, String, long, List, int, ServiceContext)}
896             */
897            @Deprecated
898            @Override
899            public DLFolder updateFolder(
900                            long folderId, long parentFolderId, String name, String description,
901                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
902                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
903                    throws PortalException {
904    
905                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
906    
907                    if (overrideFileEntryTypes) {
908                            restrictionType =
909                                    DLFolderConstants.
910                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
911                    }
912    
913                    return updateFolder(
914                            folderId, name, description, defaultFileEntryTypeId,
915                            fileEntryTypeIds, restrictionType, serviceContext);
916            }
917    
918            @Indexable(type = IndexableType.REINDEX)
919            @Override
920            public DLFolder updateFolder(
921                            long folderId, long parentFolderId, String name, String description,
922                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
923                            int restrictionType, ServiceContext serviceContext)
924                    throws PortalException {
925    
926                    boolean hasLock = hasFolderLock(serviceContext.getUserId(), folderId);
927    
928                    Lock lock = null;
929    
930                    if (!hasLock) {
931    
932                            // Lock
933    
934                            lock = lockFolder(
935                                    serviceContext.getUserId(), folderId, null, false,
936                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
937                    }
938    
939                    try {
940    
941                            // File entry types
942    
943                            DLFolder dlFolder = null;
944    
945                            Set<Long> originalFileEntryTypeIds = new HashSet<>();
946    
947                            if (folderId > DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
948                                    originalFileEntryTypeIds = getFileEntryTypeIds(
949                                            dlFolderPersistence.getDLFileEntryTypes(folderId));
950    
951                                    dlFolder = dlFolderLocalService.updateFolderAndFileEntryTypes(
952                                            serviceContext.getUserId(), folderId, parentFolderId, name,
953                                            description, defaultFileEntryTypeId, fileEntryTypeIds,
954                                            restrictionType, serviceContext);
955    
956                                    dlFileEntryTypeLocalService.cascadeFileEntryTypes(
957                                            serviceContext.getUserId(), dlFolder);
958                            }
959    
960                            // Workflow definitions
961    
962                            List<ObjectValuePair<Long, String>> workflowDefinitionOVPs =
963                                    new ArrayList<>();
964    
965                            if (restrictionType ==
966                                            DLFolderConstants.
967                                                    RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) {
968    
969                                    workflowDefinitionOVPs.add(
970                                            new ObjectValuePair<Long, String>(
971                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
972                                                    StringPool.BLANK));
973    
974                                    for (long fileEntryTypeId : fileEntryTypeIds) {
975                                            String workflowDefinition = ParamUtil.getString(
976                                                    serviceContext, "workflowDefinition" + fileEntryTypeId);
977    
978                                            workflowDefinitionOVPs.add(
979                                                    new ObjectValuePair<Long, String>(
980                                                            fileEntryTypeId, workflowDefinition));
981                                    }
982                            }
983                            else if (restrictionType ==
984                                                    DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
985    
986                                    if (originalFileEntryTypeIds.isEmpty()) {
987                                            originalFileEntryTypeIds.add(
988                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
989                                    }
990    
991                                    for (long originalFileEntryTypeId : originalFileEntryTypeIds) {
992                                            workflowDefinitionOVPs.add(
993                                                    new ObjectValuePair<Long, String>(
994                                                            originalFileEntryTypeId, StringPool.BLANK));
995                                    }
996                            }
997                            else if (restrictionType ==
998                                                    DLFolderConstants.RESTRICTION_TYPE_WORKFLOW) {
999    
1000                                    String workflowDefinition = ParamUtil.getString(
1001                                            serviceContext,
1002                                            "workflowDefinition" +
1003                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
1004    
1005                                    workflowDefinitionOVPs.add(
1006                                            new ObjectValuePair<Long, String>(
1007                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
1008                                                    workflowDefinition));
1009    
1010                                    for (long originalFileEntryTypeId : originalFileEntryTypeIds) {
1011                                            workflowDefinitionOVPs.add(
1012                                                    new ObjectValuePair<Long, String>(
1013                                                            originalFileEntryTypeId, StringPool.BLANK));
1014                                    }
1015                            }
1016    
1017                            workflowDefinitionLinkLocalService.updateWorkflowDefinitionLinks(
1018                                    serviceContext.getUserId(), serviceContext.getCompanyId(),
1019                                    serviceContext.getScopeGroupId(), DLFolder.class.getName(),
1020                                    folderId, workflowDefinitionOVPs);
1021    
1022                            return dlFolder;
1023                    }
1024                    finally {
1025                            if (!hasLock) {
1026    
1027                                    // Unlock
1028    
1029                                    unlockFolder(folderId, lock.getUuid());
1030                            }
1031                    }
1032            }
1033    
1034            /**
1035             * @deprecated As of 7.0.0, replaced {@link #updateFolder(long, long,
1036             *             String, String, long, List, int, ServiceContext)}
1037             */
1038            @Deprecated
1039            @Override
1040            public DLFolder updateFolder(
1041                            long folderId, String name, String description,
1042                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
1043                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
1044                    throws PortalException {
1045    
1046                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
1047    
1048                    if (overrideFileEntryTypes) {
1049                            restrictionType =
1050                                    DLFolderConstants.
1051                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
1052                    }
1053    
1054                    return updateFolder(
1055                            serviceContext.getScopeGroupId(), folderId, name, description,
1056                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
1057                            serviceContext);
1058            }
1059    
1060            @Indexable(type = IndexableType.REINDEX)
1061            @Override
1062            public DLFolder updateFolder(
1063                            long folderId, String name, String description,
1064                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
1065                            int restrictionType, ServiceContext serviceContext)
1066                    throws PortalException {
1067    
1068                    long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1069    
1070                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1071                            DLFolder dlFolder = getDLFolder(folderId);
1072    
1073                            parentFolderId = dlFolder.getParentFolderId();
1074                    }
1075    
1076                    return updateFolder(
1077                            folderId, parentFolderId, name, description, defaultFileEntryTypeId,
1078                            fileEntryTypeIds, restrictionType, serviceContext);
1079            }
1080    
1081            /**
1082             * @deprecated As of 7.0.0, replaced by {@link #
1083             *             updateFolderAndFileEntryTypes(long, long, long, String,
1084             *             String, long, List, int, ServiceContext)}
1085             */
1086            @Deprecated
1087            @Override
1088            public DLFolder updateFolderAndFileEntryTypes(
1089                            long userId, long folderId, long parentFolderId, String name,
1090                            String description, long defaultFileEntryTypeId,
1091                            List<Long> fileEntryTypeIds, boolean overrideFileEntryTypes,
1092                            ServiceContext serviceContext)
1093                    throws PortalException {
1094    
1095                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
1096    
1097                    if (overrideFileEntryTypes) {
1098                            restrictionType =
1099                                    DLFolderConstants.
1100                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
1101                    }
1102    
1103                    return updateFolderAndFileEntryTypes(
1104                            userId, folderId, parentFolderId, name, description,
1105                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
1106                            serviceContext);
1107            }
1108    
1109            @Override
1110            public DLFolder updateFolderAndFileEntryTypes(
1111                            long userId, long folderId, long parentFolderId, String name,
1112                            String description, long defaultFileEntryTypeId,
1113                            List<Long> fileEntryTypeIds, int restrictionType,
1114                            ServiceContext serviceContext)
1115                    throws PortalException {
1116    
1117                    if ((restrictionType ==
1118                                    DLFolderConstants.
1119                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW) &&
1120                            fileEntryTypeIds.isEmpty()) {
1121    
1122                            throw new RequiredFileEntryTypeException();
1123                    }
1124    
1125                    boolean hasLock = hasFolderLock(userId, folderId);
1126    
1127                    Lock lock = null;
1128    
1129                    if (!hasLock) {
1130    
1131                            // Lock
1132    
1133                            lock = lockFolder(
1134                                    userId, folderId, null, false,
1135                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
1136                    }
1137    
1138                    try {
1139    
1140                            // Folder
1141    
1142                            if (restrictionType == DLFolderConstants.RESTRICTION_TYPE_INHERIT) {
1143                                    fileEntryTypeIds = Collections.emptyList();
1144                            }
1145    
1146                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1147    
1148                            parentFolderId = getParentFolderId(dlFolder, parentFolderId);
1149    
1150                            validateFolder(
1151                                    folderId, dlFolder.getGroupId(), parentFolderId, name);
1152    
1153                            long oldParentFolderId = dlFolder.getParentFolderId();
1154    
1155                            if (oldParentFolderId != parentFolderId) {
1156                                    dlFolder.setParentFolderId(parentFolderId);
1157                                    dlFolder.setTreePath(dlFolder.buildTreePath());
1158                            }
1159    
1160                            dlFolder.setName(name);
1161                            dlFolder.setDescription(description);
1162                            dlFolder.setExpandoBridgeAttributes(serviceContext);
1163                            dlFolder.setRestrictionType(restrictionType);
1164                            dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
1165    
1166                            dlFolderPersistence.update(dlFolder);
1167    
1168                            // File entry types
1169    
1170                            if (fileEntryTypeIds != null) {
1171                                    dlFileEntryTypeLocalService.updateFolderFileEntryTypes(
1172                                            dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
1173                                            serviceContext);
1174                            }
1175    
1176                            if (oldParentFolderId != parentFolderId) {
1177                                    rebuildTree(
1178                                            dlFolder.getCompanyId(), folderId, dlFolder.getTreePath(),
1179                                            true);
1180                            }
1181    
1182                            return dlFolder;
1183                    }
1184                    finally {
1185                            if (!hasLock) {
1186    
1187                                    // Unlock
1188    
1189                                    unlockFolder(folderId, lock.getUuid());
1190                            }
1191                    }
1192            }
1193    
1194            @BufferedIncrement(
1195                    configuration = "DLFolderEntry",
1196                    incrementClass = DateOverrideIncrement.class
1197            )
1198            @Override
1199            public void updateLastPostDate(long folderId, Date lastPostDate)
1200                    throws PortalException {
1201    
1202                    if (ExportImportThreadLocal.isImportInProcess() ||
1203                            (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) ||
1204                            (lastPostDate == null)) {
1205    
1206                            return;
1207                    }
1208    
1209                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1210    
1211                    if (lastPostDate.before(dlFolder.getLastPostDate())) {
1212                            return;
1213                    }
1214    
1215                    dlFolder.setModifiedDate(dlFolder.getModifiedDate());
1216                    dlFolder.setLastPostDate(lastPostDate);
1217    
1218                    dlFolderPersistence.update(dlFolder);
1219            }
1220    
1221            @Override
1222            public DLFolder updateStatus(
1223                            long userId, long folderId, int status,
1224                            Map<String, Serializable> workflowContext,
1225                            ServiceContext serviceContext)
1226                    throws PortalException {
1227    
1228                    // Folder
1229    
1230                    User user = userPersistence.findByPrimaryKey(userId);
1231    
1232                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1233    
1234                    int oldStatus = dlFolder.getStatus();
1235    
1236                    dlFolder.setStatus(status);
1237                    dlFolder.setStatusByUserId(user.getUserId());
1238                    dlFolder.setStatusByUserName(user.getFullName());
1239                    dlFolder.setStatusDate(new Date());
1240    
1241                    dlFolderPersistence.update(dlFolder);
1242    
1243                    // Asset
1244    
1245                    if (status == WorkflowConstants.STATUS_APPROVED) {
1246                            assetEntryLocalService.updateVisible(
1247                                    DLFolder.class.getName(), dlFolder.getFolderId(), true);
1248                    }
1249                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
1250                            assetEntryLocalService.updateVisible(
1251                                    DLFolder.class.getName(), dlFolder.getFolderId(), false);
1252                    }
1253    
1254                    // Indexer
1255    
1256                    if (((status == WorkflowConstants.STATUS_APPROVED) ||
1257                             (status == WorkflowConstants.STATUS_IN_TRASH) ||
1258                             (oldStatus == WorkflowConstants.STATUS_IN_TRASH)) &&
1259                            ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1260    
1261                            Indexer<DLFolder> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1262                                    DLFolder.class);
1263    
1264                            indexer.reindex(dlFolder);
1265                    }
1266    
1267                    return dlFolder;
1268            }
1269    
1270            protected void addFolderResources(
1271                            DLFolder dlFolder, boolean addGroupPermissions,
1272                            boolean addGuestPermissions)
1273                    throws PortalException {
1274    
1275                    resourceLocalService.addResources(
1276                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1277                            dlFolder.getUserId(), DLFolder.class.getName(),
1278                            dlFolder.getFolderId(), false, addGroupPermissions,
1279                            addGuestPermissions);
1280            }
1281    
1282            protected void addFolderResources(
1283                            DLFolder dlFolder, ModelPermissions modelPermissions)
1284                    throws PortalException {
1285    
1286                    resourceLocalService.addModelResources(
1287                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1288                            dlFolder.getUserId(), DLFolder.class.getName(),
1289                            dlFolder.getFolderId(), modelPermissions);
1290            }
1291    
1292            protected void addFolderResources(
1293                            long folderId, boolean addGroupPermissions,
1294                            boolean addGuestPermissions)
1295                    throws PortalException {
1296    
1297                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1298    
1299                    addFolderResources(dlFolder, addGroupPermissions, addGuestPermissions);
1300            }
1301    
1302            protected void addFolderResources(
1303                            long folderId, ModelPermissions modelPermissions)
1304                    throws PortalException {
1305    
1306                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1307    
1308                    addFolderResources(dlFolder, modelPermissions);
1309            }
1310    
1311            protected void deleteFolderDependencies(
1312                            DLFolder dlFolder, boolean includeTrashedEntries)
1313                    throws PortalException {
1314    
1315                    // Resources
1316    
1317                    resourceLocalService.deleteResource(
1318                            dlFolder.getCompanyId(), DLFolder.class.getName(),
1319                            ResourceConstants.SCOPE_INDIVIDUAL, dlFolder.getFolderId());
1320    
1321                    // WebDAVProps
1322    
1323                    webDAVPropsLocalService.deleteWebDAVProps(
1324                            DLFolder.class.getName(), dlFolder.getFolderId());
1325    
1326                    // File entries
1327    
1328                    dlFileEntryLocalService.deleteFileEntries(
1329                            dlFolder.getGroupId(), dlFolder.getFolderId(),
1330                            includeTrashedEntries);
1331    
1332                    // File entry types
1333    
1334                    List<Long> fileEntryTypeIds = new ArrayList<>();
1335    
1336                    for (DLFileEntryType dlFileEntryType :
1337                                    dlFileEntryTypeLocalService.getDLFolderDLFileEntryTypes(
1338                                            dlFolder.getFolderId())) {
1339    
1340                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
1341                    }
1342    
1343                    if (fileEntryTypeIds.isEmpty()) {
1344                            fileEntryTypeIds.add(
1345                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
1346                    }
1347    
1348                    dlFileEntryTypeLocalService.unsetFolderFileEntryTypes(
1349                            dlFolder.getFolderId());
1350    
1351                    // File shortcuts
1352    
1353                    dlFileShortcutLocalService.deleteFileShortcuts(
1354                            dlFolder.getGroupId(), dlFolder.getFolderId(),
1355                            includeTrashedEntries);
1356    
1357                    // Expando
1358    
1359                    expandoRowLocalService.deleteRows(dlFolder.getFolderId());
1360    
1361                    // Ratings
1362    
1363                    ratingsStatsLocalService.deleteStats(
1364                            DLFolder.class.getName(), dlFolder.getFolderId());
1365    
1366                    // Folder
1367    
1368                    dlFolderPersistence.remove(dlFolder);
1369    
1370                    // Directory
1371    
1372                    if (includeTrashedEntries) {
1373                            DLStoreUtil.deleteDirectory(
1374                                    dlFolder.getCompanyId(), dlFolder.getFolderId(),
1375                                    StringPool.BLANK);
1376                    }
1377    
1378                    // Workflow
1379    
1380                    for (long fileEntryTypeId : fileEntryTypeIds) {
1381                            WorkflowDefinitionLink workflowDefinitionLink =
1382                                    workflowDefinitionLinkLocalService.fetchWorkflowDefinitionLink(
1383                                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1384                                            DLFolder.class.getName(), dlFolder.getFolderId(),
1385                                            fileEntryTypeId);
1386    
1387                            if (workflowDefinitionLink != null) {
1388                                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
1389                                            workflowDefinitionLink);
1390                            }
1391                    }
1392            }
1393    
1394            protected void deleteSubfolders(
1395                            DLFolder dlFolder, boolean includeTrashedEntries)
1396                    throws PortalException {
1397    
1398                    RepositoryEventTrigger repositoryEventTrigger =
1399                            RepositoryUtil.getRepositoryEventTrigger(
1400                                    dlFolder.getRepositoryId());
1401    
1402                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
1403                            dlFolder.getGroupId(), dlFolder.getFolderId());
1404    
1405                    for (DLFolder curDLFolder : dlFolders) {
1406                            if (includeTrashedEntries || !curDLFolder.isInTrashExplicitly()) {
1407                                    repositoryEventTrigger.trigger(
1408                                            RepositoryEventType.Delete.class, Folder.class,
1409                                            new LiferayFolder(curDLFolder));
1410    
1411                                    dlFolderLocalService.deleteFolder(
1412                                            curDLFolder, includeTrashedEntries);
1413                            }
1414                    }
1415            }
1416    
1417            protected Set<Long> getFileEntryTypeIds(
1418                    List<DLFileEntryType> dlFileEntryTypes) {
1419    
1420                    Set<Long> fileEntryTypeIds = new HashSet<>();
1421    
1422                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
1423                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
1424                    }
1425    
1426                    return fileEntryTypeIds;
1427            }
1428    
1429            protected long getParentFolderId(DLFolder dlFolder, long parentFolderId)
1430                    throws PortalException {
1431    
1432                    parentFolderId = getParentFolderId(
1433                            dlFolder.getGroupId(), dlFolder.getRepositoryId(), parentFolderId);
1434    
1435                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1436                            return parentFolderId;
1437                    }
1438    
1439                    if (dlFolder.getFolderId() == parentFolderId) {
1440                            throw new InvalidFolderException(
1441                                    InvalidFolderException.CANNOT_MOVE_INTO_ITSELF, parentFolderId);
1442                    }
1443    
1444                    List<Long> subfolderIds = new ArrayList<>();
1445    
1446                    getGroupSubfolderIds(
1447                            subfolderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
1448    
1449                    if (subfolderIds.contains(parentFolderId)) {
1450                            throw new InvalidFolderException(
1451                                    InvalidFolderException.CANNOT_MOVE_INTO_CHILD_FOLDER,
1452                                    parentFolderId);
1453                    }
1454    
1455                    return parentFolderId;
1456            }
1457    
1458            protected long getParentFolderId(
1459                            long groupId, long repositoryId, long parentFolderId)
1460                    throws PortalException {
1461    
1462                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1463                            return parentFolderId;
1464                    }
1465    
1466                    DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(
1467                            parentFolderId);
1468    
1469                    if (parentDLFolder.getGroupId() != groupId) {
1470                            throw new NoSuchFolderException(
1471                                    String.format(
1472                                            "No folder exists with the primary key %s in group %s",
1473                                            parentFolderId, groupId));
1474                    }
1475    
1476                    if ((parentDLFolder.getRepositoryId() != repositoryId) &&
1477                            (parentDLFolder.getRepositoryId() != groupId)) {
1478    
1479                            Repository repository = repositoryLocalService.getRepository(
1480                                    repositoryId);
1481    
1482                            if (repository.getGroupId() != parentDLFolder.getGroupId()) {
1483                                    throw new NoSuchFolderException(
1484                                            String.format(
1485                                                    "No folder exists with the primary key %s in " +
1486                                                            "repository %s",
1487                                                    parentFolderId, repositoryId));
1488                            }
1489                    }
1490    
1491                    return parentDLFolder.getFolderId();
1492            }
1493    
1494            protected void validateFolder(
1495                            long folderId, long groupId, long parentFolderId, String name)
1496                    throws PortalException {
1497    
1498                    validateFolderName(name);
1499    
1500                    DLValidatorUtil.validateDirectoryName(name);
1501    
1502                    DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchFileEntry(
1503                            groupId, parentFolderId, name);
1504    
1505                    if (dlFileEntry != null) {
1506                            throw new DuplicateFileEntryException(name);
1507                    }
1508    
1509                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
1510                            groupId, parentFolderId, name);
1511    
1512                    if ((dlFolder != null) && (dlFolder.getFolderId() != folderId)) {
1513                            throw new DuplicateFolderNameException(name);
1514                    }
1515            }
1516    
1517            protected void validateFolder(
1518                            long groupId, long parentFolderId, String name)
1519                    throws PortalException {
1520    
1521                    long folderId = 0;
1522    
1523                    validateFolder(folderId, groupId, parentFolderId, name);
1524            }
1525    
1526            protected void validateFolderName(String folderName)
1527                    throws PortalException {
1528    
1529                    if (folderName.contains(StringPool.SLASH)) {
1530                            throw new FolderNameException(folderName);
1531                    }
1532            }
1533    
1534    }