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