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