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