001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.search.Indexable;
028    import com.liferay.portal.kernel.search.IndexableType;
029    import com.liferay.portal.kernel.search.Indexer;
030    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
031    import com.liferay.portal.kernel.systemevent.SystemEvent;
032    import com.liferay.portal.kernel.util.ObjectValuePair;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.ParamUtil;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.TreeModelFinder;
037    import com.liferay.portal.kernel.util.TreePathUtil;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.kernel.workflow.WorkflowConstants;
040    import com.liferay.portal.model.Group;
041    import com.liferay.portal.model.Lock;
042    import com.liferay.portal.model.ResourceConstants;
043    import com.liferay.portal.model.SystemEventConstants;
044    import com.liferay.portal.model.User;
045    import com.liferay.portal.model.WorkflowDefinitionLink;
046    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
047    import com.liferay.portal.service.ServiceContext;
048    import com.liferay.portlet.documentlibrary.DuplicateFileException;
049    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
050    import com.liferay.portlet.documentlibrary.FolderNameException;
051    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
052    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
053    import com.liferay.portlet.documentlibrary.RequiredFileEntryTypeException;
054    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
055    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
056    import com.liferay.portlet.documentlibrary.model.DLFolder;
057    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
058    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
059    import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
060    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
061    import com.liferay.portlet.documentlibrary.util.comparator.FolderIdComparator;
062    
063    import java.io.Serializable;
064    
065    import java.util.ArrayList;
066    import java.util.Collections;
067    import java.util.Date;
068    import java.util.List;
069    import java.util.Map;
070    
071    /**
072     * @author Brian Wing Shun Chan
073     * @author Alexander Chow
074     */
075    public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
076    
077            @Override
078            public DLFolder addFolder(
079                            long userId, long groupId, long repositoryId, boolean mountPoint,
080                            long parentFolderId, String name, String description,
081                            boolean hidden, ServiceContext serviceContext)
082                    throws PortalException, SystemException {
083    
084                    // Folder
085    
086                    User user = userPersistence.findByPrimaryKey(userId);
087                    parentFolderId = getParentFolderId(groupId, parentFolderId);
088                    Date now = new Date();
089    
090                    validateFolder(groupId, parentFolderId, name);
091    
092                    long folderId = counterLocalService.increment();
093    
094                    DLFolder dlFolder = dlFolderPersistence.create(folderId);
095    
096                    dlFolder.setUuid(serviceContext.getUuid());
097                    dlFolder.setGroupId(groupId);
098                    dlFolder.setCompanyId(user.getCompanyId());
099                    dlFolder.setUserId(user.getUserId());
100                    dlFolder.setUserName(user.getFullName());
101                    dlFolder.setCreateDate(serviceContext.getCreateDate(now));
102                    dlFolder.setModifiedDate(serviceContext.getModifiedDate(now));
103                    dlFolder.setRepositoryId(repositoryId);
104                    dlFolder.setMountPoint(mountPoint);
105                    dlFolder.setParentFolderId(parentFolderId);
106                    dlFolder.setTreePath(dlFolder.buildTreePath());
107                    dlFolder.setName(name);
108                    dlFolder.setDescription(description);
109                    dlFolder.setLastPostDate(now);
110                    dlFolder.setHidden(hidden);
111                    dlFolder.setOverrideFileEntryTypes(false);
112                    dlFolder.setExpandoBridgeAttributes(serviceContext);
113    
114                    dlFolderPersistence.update(dlFolder);
115    
116                    // Resources
117    
118                    if (serviceContext.isAddGroupPermissions() ||
119                            serviceContext.isAddGuestPermissions()) {
120    
121                            addFolderResources(
122                                    dlFolder, serviceContext.isAddGroupPermissions(),
123                                    serviceContext.isAddGuestPermissions());
124                    }
125                    else {
126                            if (serviceContext.isDeriveDefaultPermissions()) {
127                                    serviceContext.deriveDefaultPermissions(
128                                            repositoryId, DLFolderConstants.getClassName());
129                            }
130    
131                            addFolderResources(
132                                    dlFolder, serviceContext.getGroupPermissions(),
133                                    serviceContext.getGuestPermissions());
134                    }
135    
136                    // Parent folder
137    
138                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
139                            DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(
140                                    parentFolderId);
141    
142                            parentDLFolder.setLastPostDate(now);
143    
144                            dlFolderPersistence.update(parentDLFolder);
145                    }
146    
147                    // App helper
148    
149                    dlAppHelperLocalService.addFolder(
150                            userId, new LiferayFolder(dlFolder), serviceContext);
151    
152                    return dlFolder;
153            }
154    
155            /**
156             * @deprecated As of 6.2.0, replaced by more general {@link #addFolder(long,
157             *             long, long, boolean, long, String, String, boolean,
158             *             ServiceContext)}
159             */
160            @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, SystemException {
166    
167                    return addFolder(
168                            userId, groupId, repositoryId, mountPoint, parentFolderId, name,
169                            description, false, serviceContext);
170            }
171    
172            @Override
173            public void deleteAll(long groupId)
174                    throws PortalException, SystemException {
175    
176                    Group group = groupLocalService.getGroup(groupId);
177    
178                    List<DLFolder> dlFolders = dlFolderPersistence.findByGroupId(groupId);
179    
180                    for (DLFolder dlFolder : dlFolders) {
181                            dlFolderLocalService.deleteFolder(dlFolder);
182                    }
183    
184                    dlFileEntryLocalService.deleteFileEntries(
185                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
186    
187                    dlFileEntryTypeLocalService.deleteFileEntryTypes(groupId);
188    
189                    dlFileShortcutLocalService.deleteFileShortcuts(
190                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
191    
192                    try {
193                            DLStoreUtil.deleteDirectory(
194                                    group.getCompanyId(), groupId, StringPool.BLANK);
195                    }
196                    catch (NoSuchDirectoryException nsde) {
197                            if (_log.isDebugEnabled()) {
198                                    _log.debug(nsde.getMessage());
199                            }
200                    }
201            }
202    
203            @Indexable(type = IndexableType.DELETE)
204            @Override
205            @SystemEvent(
206                    action = SystemEventConstants.ACTION_SKIP, send = false,
207                    type = SystemEventConstants.TYPE_DELETE)
208            public DLFolder deleteFolder(DLFolder dlFolder)
209                    throws PortalException, SystemException {
210    
211                    return deleteFolder(dlFolder, true);
212            }
213    
214            @Indexable(type = IndexableType.DELETE)
215            @Override
216            @SystemEvent(
217                    action = SystemEventConstants.ACTION_SKIP, send = false,
218                    type = SystemEventConstants.TYPE_DELETE)
219            public DLFolder deleteFolder(
220                            DLFolder dlFolder, boolean includeTrashedEntries)
221                    throws PortalException, SystemException {
222    
223                    // Folders
224    
225                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
226                            dlFolder.getGroupId(), dlFolder.getFolderId());
227    
228                    for (DLFolder curDLFolder : dlFolders) {
229                            if (includeTrashedEntries || !curDLFolder.isInTrashExplicitly()) {
230                                    dlFolderLocalService.deleteFolder(
231                                            curDLFolder, includeTrashedEntries);
232                            }
233                    }
234    
235                    // Resources
236    
237                    resourceLocalService.deleteResource(
238                            dlFolder.getCompanyId(), DLFolder.class.getName(),
239                            ResourceConstants.SCOPE_INDIVIDUAL, dlFolder.getFolderId());
240    
241                    // WebDAVProps
242    
243                    webDAVPropsLocalService.deleteWebDAVProps(
244                            DLFolder.class.getName(), dlFolder.getFolderId());
245    
246                    // File entries
247    
248                    dlFileEntryLocalService.deleteFileEntries(
249                            dlFolder.getGroupId(), dlFolder.getFolderId(),
250                            includeTrashedEntries);
251    
252                    // File entry types
253    
254                    List<Long> fileEntryTypeIds = new ArrayList<Long>();
255    
256                    for (DLFileEntryType dlFileEntryType :
257                                    dlFileEntryTypeLocalService.getDLFolderDLFileEntryTypes(
258                                            dlFolder.getFolderId())) {
259    
260                            fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
261                    }
262    
263                    if (fileEntryTypeIds.isEmpty()) {
264                            fileEntryTypeIds.add(
265                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
266                    }
267    
268                    dlFileEntryTypeLocalService.unsetFolderFileEntryTypes(
269                            dlFolder.getFolderId());
270    
271                    // File shortcuts
272    
273                    dlFileShortcutLocalService.deleteFileShortcuts(
274                            dlFolder.getGroupId(), dlFolder.getFolderId(),
275                            includeTrashedEntries);
276    
277                    // Expando
278    
279                    expandoRowLocalService.deleteRows(dlFolder.getFolderId());
280    
281                    // App helper
282    
283                    dlAppHelperLocalService.deleteFolder(new LiferayFolder(dlFolder));
284    
285                    // Folder
286    
287                    dlFolderPersistence.remove(dlFolder);
288    
289                    // Directory
290    
291                    try {
292                            if (includeTrashedEntries) {
293                                    DLStoreUtil.deleteDirectory(
294                                            dlFolder.getCompanyId(), dlFolder.getFolderId(),
295                                            StringPool.BLANK);
296                            }
297                    }
298                    catch (NoSuchDirectoryException nsde) {
299                            if (_log.isDebugEnabled()) {
300                                    _log.debug(nsde.getMessage());
301                            }
302                    }
303    
304                    // Workflow
305    
306                    for (long fileEntryTypeId : fileEntryTypeIds) {
307                            WorkflowDefinitionLink workflowDefinitionLink = null;
308    
309                            try {
310                                    workflowDefinitionLink =
311                                            workflowDefinitionLinkLocalService.
312                                                    getWorkflowDefinitionLink(
313                                                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
314                                                            DLFolder.class.getName(), dlFolder.getFolderId(),
315                                                            fileEntryTypeId);
316                            }
317                            catch (NoSuchWorkflowDefinitionLinkException nswdle) {
318                                    continue;
319                            }
320    
321                            workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
322                                    workflowDefinitionLink);
323                    }
324    
325                    return dlFolder;
326            }
327    
328            @Indexable(type = IndexableType.DELETE)
329            @Override
330            public DLFolder deleteFolder(long folderId)
331                    throws PortalException, SystemException {
332    
333                    return dlFolderLocalService.deleteFolder(folderId, true);
334            }
335    
336            @Indexable(type = IndexableType.DELETE)
337            @Override
338            public DLFolder deleteFolder(long folderId, boolean includeTrashedEntries)
339                    throws PortalException, SystemException {
340    
341                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
342    
343                    return dlFolderLocalService.deleteFolder(
344                            dlFolder, includeTrashedEntries);
345            }
346    
347            @Indexable(type = IndexableType.DELETE)
348            @Override
349            public DLFolder deleteFolder(
350                            long userId, long folderId, boolean includeTrashedEntries)
351                    throws PortalException, SystemException {
352    
353                    boolean hasLock = hasFolderLock(userId, folderId);
354    
355                    Lock lock = null;
356    
357                    if (!hasLock) {
358    
359                            // Lock
360    
361                            lock = lockFolder(
362                                    userId, folderId, null, false,
363                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
364                    }
365    
366                    try {
367                            return deleteFolder(folderId, includeTrashedEntries);
368                    }
369                    finally {
370                            if (!hasLock) {
371    
372                                    // Unlock
373    
374                                    unlockFolder(folderId, lock.getUuid());
375                            }
376                    }
377            }
378    
379            @Override
380            public DLFolder fetchFolder(long folderId) throws SystemException {
381                    return dlFolderPersistence.fetchByPrimaryKey(folderId);
382            }
383    
384            @Override
385            public DLFolder fetchFolder(long groupId, long parentFolderId, String name)
386                    throws SystemException {
387    
388                    return dlFolderPersistence.fetchByG_P_N(groupId, parentFolderId, name);
389            }
390    
391            @Override
392            public List<DLFolder> getCompanyFolders(long companyId, int start, int end)
393                    throws SystemException {
394    
395                    return dlFolderPersistence.findByCompanyId(companyId, start, end);
396            }
397    
398            @Override
399            public int getCompanyFoldersCount(long companyId) throws SystemException {
400                    return dlFolderPersistence.countByCompanyId(companyId);
401            }
402    
403            /**
404             * @deprecated As of 6.2.0, replaced by {@link
405             *             #getFileEntriesAndFileShortcuts(long, long, QueryDefinition)}
406             */
407            @Override
408            public List<Object> getFileEntriesAndFileShortcuts(
409                            long groupId, long folderId, int status, int start, int end)
410                    throws SystemException {
411    
412                    QueryDefinition queryDefinition = new QueryDefinition(
413                            status, start, end, null);
414    
415                    return getFileEntriesAndFileShortcuts(
416                            groupId, folderId, queryDefinition);
417            }
418    
419            @Override
420            public List<Object> getFileEntriesAndFileShortcuts(
421                            long groupId, long folderId, QueryDefinition queryDefinition)
422                    throws SystemException {
423    
424                    return dlFolderFinder.findFE_FS_ByG_F(
425                            groupId, folderId, queryDefinition);
426            }
427    
428            /**
429             * @deprecated As of 6.2.0, replaced by {@link
430             *             #getFileEntriesAndFileShortcutsCount(long, long,
431             *             QueryDefinition)}
432             */
433            @Override
434            public int getFileEntriesAndFileShortcutsCount(
435                            long groupId, long folderId, int status)
436                    throws SystemException {
437    
438                    QueryDefinition queryDefinition = new QueryDefinition(status);
439    
440                    return getFileEntriesAndFileShortcutsCount(
441                            groupId, folderId, queryDefinition);
442            }
443    
444            @Override
445            public int getFileEntriesAndFileShortcutsCount(
446                            long groupId, long folderId, QueryDefinition queryDefinition)
447                    throws SystemException {
448    
449                    return dlFolderFinder.countFE_FS_ByG_F(
450                            groupId, folderId, queryDefinition);
451            }
452    
453            @Override
454            public DLFolder getFolder(long folderId)
455                    throws PortalException, SystemException {
456    
457                    return dlFolderPersistence.findByPrimaryKey(folderId);
458            }
459    
460            @Override
461            public DLFolder getFolder(long groupId, long parentFolderId, String name)
462                    throws PortalException, SystemException {
463    
464                    return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
465            }
466    
467            @Override
468            public long getFolderId(long companyId, long folderId)
469                    throws SystemException {
470    
471                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
472    
473                            // Ensure folder exists and belongs to the proper company
474    
475                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
476    
477                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
478                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
479                            }
480                    }
481    
482                    return folderId;
483            }
484    
485            @Override
486            public List<DLFolder> getFolders(long groupId, long parentFolderId)
487                    throws SystemException {
488    
489                    return getFolders(groupId, parentFolderId, true);
490            }
491    
492            @Override
493            public List<DLFolder> getFolders(
494                            long groupId, long parentFolderId, boolean includeMountfolders)
495                    throws SystemException {
496    
497                    if (includeMountfolders) {
498                            return dlFolderPersistence.findByG_P(groupId, parentFolderId);
499                    }
500                    else {
501                            return dlFolderPersistence.findByG_M_P_H(
502                                    groupId, false, parentFolderId, false);
503                    }
504            }
505    
506            @Override
507            public List<DLFolder> getFolders(
508                            long groupId, long parentFolderId, boolean includeMountfolders,
509                            int start, int end, OrderByComparator obc)
510                    throws SystemException {
511    
512                    if (includeMountfolders) {
513                            return dlFolderPersistence.findByG_P(
514                                    groupId, parentFolderId, start, end, obc);
515                    }
516                    else {
517                            return dlFolderPersistence.findByG_M_P_H(
518                                    groupId, false, parentFolderId, false, start, end, obc);
519                    }
520            }
521    
522            @Override
523            public List<DLFolder> getFolders(
524                            long groupId, long parentFolderId, int start, int end,
525                            OrderByComparator obc)
526                    throws SystemException {
527    
528                    return getFolders(groupId, parentFolderId, true, start, end, obc);
529            }
530    
531            /**
532             * @deprecated As of 6.2.0, replaced by {@link
533             *             #getFoldersAndFileEntriesAndFileShortcuts(long, long,
534             *             String[], boolean, QueryDefinition)}
535             */
536            @Override
537            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
538                            long groupId, long folderId, int status,
539                            boolean includeMountFolders, int start, int end,
540                            OrderByComparator obc)
541                    throws SystemException {
542    
543                    QueryDefinition queryDefinition = new QueryDefinition(
544                            status, start, end, obc);
545    
546                    return getFoldersAndFileEntriesAndFileShortcuts(
547                            groupId, folderId, null, includeMountFolders, queryDefinition);
548            }
549    
550            /**
551             * @deprecated As of 6.2.0, replaced by {@link
552             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
553             *             String[], boolean, QueryDefinition)}
554             */
555            @Override
556            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
557                            long groupId, long folderId, int status, String[] mimeTypes,
558                            boolean includeMountFolders, int start, int end,
559                            OrderByComparator obc)
560                    throws SystemException {
561    
562                    QueryDefinition queryDefinition = new QueryDefinition(
563                            status, start, end, obc);
564    
565                    return getFoldersAndFileEntriesAndFileShortcuts(
566                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
567            }
568    
569            @Override
570            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
571                            long groupId, long folderId, String[] mimeTypes,
572                            boolean includeMountFolders, QueryDefinition queryDefinition)
573                    throws SystemException {
574    
575                    return dlFolderFinder.findF_FE_FS_ByG_F_M_M(
576                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
577            }
578    
579            /**
580             * @deprecated As of 6.2.0, replaced by {@link
581             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
582             *             String[], boolean, QueryDefinition)}
583             */
584            @Override
585            public int getFoldersAndFileEntriesAndFileShortcutsCount(
586                            long groupId, long folderId, int status,
587                            boolean includeMountFolders)
588                    throws SystemException {
589    
590                    QueryDefinition queryDefinition = new QueryDefinition(status);
591    
592                    return getFoldersAndFileEntriesAndFileShortcutsCount(
593                            groupId, folderId, null, includeMountFolders, queryDefinition);
594            }
595    
596            /**
597             * @deprecated As of 6.2.0, replaced by {@link
598             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
599             *             String[], boolean, QueryDefinition)}
600             */
601            @Override
602            public int getFoldersAndFileEntriesAndFileShortcutsCount(
603                            long groupId, long folderId, int status, String[] mimeTypes,
604                            boolean includeMountFolders)
605                    throws SystemException {
606    
607                    QueryDefinition queryDefinition = new QueryDefinition(status);
608    
609                    return getFoldersAndFileEntriesAndFileShortcutsCount(
610                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
611            }
612    
613            @Override
614            public int getFoldersAndFileEntriesAndFileShortcutsCount(
615                            long groupId, long folderId, String[] mimeTypes,
616                            boolean includeMountFolders, QueryDefinition queryDefinition)
617                    throws SystemException {
618    
619                    return dlFolderFinder.countF_FE_FS_ByG_F_M_M(
620                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
621            }
622    
623            @Override
624            public int getFoldersCount(long groupId, long parentFolderId)
625                    throws SystemException {
626    
627                    return getFoldersCount(groupId, parentFolderId, true);
628            }
629    
630            @Override
631            public int getFoldersCount(
632                            long groupId, long parentFolderId, boolean includeMountfolders)
633                    throws SystemException {
634    
635                    if (includeMountfolders) {
636                            return dlFolderPersistence.countByG_P(groupId, parentFolderId);
637                    }
638                    else {
639                            return dlFolderPersistence.countByG_M_P_H(
640                                    groupId, false, parentFolderId, false);
641                    }
642            }
643    
644            @Override
645            public int getFoldersCount(
646                            long groupId, long parentFolderId, int status,
647                            boolean includeMountfolders)
648                    throws SystemException {
649    
650                    if (includeMountfolders) {
651                            return dlFolderPersistence.countByG_P_H_S(
652                                    groupId, parentFolderId, false, status);
653                    }
654                    else {
655                            return dlFolderPersistence.countByG_M_P_H_S(
656                                    groupId, false, parentFolderId, false, status);
657                    }
658            }
659    
660            @Override
661            public DLFolder getMountFolder(long repositoryId)
662                    throws PortalException, SystemException {
663    
664                    return dlFolderPersistence.findByRepositoryId(repositoryId);
665            }
666    
667            @Override
668            public List<DLFolder> getMountFolders(
669                            long groupId, long parentFolderId, int start, int end,
670                            OrderByComparator obc)
671                    throws SystemException {
672    
673                    return dlFolderPersistence.findByG_M_P_H(
674                            groupId, true, parentFolderId, false, start, end, obc);
675            }
676    
677            @Override
678            public int getMountFoldersCount(long groupId, long parentFolderId)
679                    throws SystemException {
680    
681                    return dlFolderPersistence.countByG_M_P_H(
682                            groupId, true, parentFolderId, false);
683            }
684    
685            @Override
686            public List<DLFolder> getNoAssetFolders() throws SystemException {
687                    return dlFolderFinder.findF_ByNoAssets();
688            }
689    
690            @Override
691            public void getSubfolderIds(
692                            List<Long> folderIds, long groupId, long folderId)
693                    throws SystemException {
694    
695                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
696                            groupId, folderId);
697    
698                    for (DLFolder dlFolder : dlFolders) {
699                            folderIds.add(dlFolder.getFolderId());
700    
701                            getSubfolderIds(
702                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
703                    }
704            }
705    
706            @Override
707            public boolean hasFolderLock(long userId, long folderId)
708                    throws SystemException {
709    
710                    return lockLocalService.hasLock(
711                            userId, DLFolder.class.getName(), folderId);
712            }
713    
714            @Override
715            public Lock lockFolder(long userId, long folderId)
716                    throws PortalException, SystemException {
717    
718                    return lockFolder(
719                            userId, folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
720            }
721    
722            @Override
723            public Lock lockFolder(
724                            long userId, long folderId, String owner, boolean inheritable,
725                            long expirationTime)
726                    throws PortalException, SystemException {
727    
728                    if ((expirationTime <= 0) ||
729                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
730    
731                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
732                    }
733    
734                    return lockLocalService.lock(
735                            userId, DLFolder.class.getName(), folderId, owner, inheritable,
736                            expirationTime);
737            }
738    
739            @Indexable(type = IndexableType.REINDEX)
740            @Override
741            public DLFolder moveFolder(
742                            long userId, long folderId, long parentFolderId,
743                            ServiceContext serviceContext)
744                    throws PortalException, SystemException {
745    
746                    boolean hasLock = hasFolderLock(userId, folderId);
747    
748                    Lock lock = null;
749    
750                    if (!hasLock) {
751    
752                            // Lock
753    
754                            lock = lockFolder(userId, folderId);
755                    }
756    
757                    try {
758                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
759    
760                            parentFolderId = getParentFolderId(dlFolder, parentFolderId);
761    
762                            validateFolder(
763                                    dlFolder.getFolderId(), dlFolder.getGroupId(), parentFolderId,
764                                    dlFolder.getName());
765    
766                            dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
767                            dlFolder.setParentFolderId(parentFolderId);
768                            dlFolder.setTreePath(dlFolder.buildTreePath());
769                            dlFolder.setExpandoBridgeAttributes(serviceContext);
770    
771                            dlFolderPersistence.update(dlFolder);
772    
773                            dlAppHelperLocalService.moveFolder(new LiferayFolder(dlFolder));
774    
775                            return dlFolder;
776                    }
777                    finally {
778                            if (!hasLock) {
779    
780                                    // Unlock
781    
782                                    unlockFolder(folderId, lock.getUuid());
783                            }
784                    }
785            }
786    
787            @Override
788            public void rebuildTree(long companyId) throws SystemException {
789                    TreePathUtil.rebuildTree(
790                            companyId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
791                            new TreeModelFinder<DLFolder>() {
792    
793                                    @Override
794                                    public List<DLFolder> findTreeModels(
795                                                    long previousId, long companyId, long parentPrimaryKey,
796                                                    int size)
797                                            throws SystemException {
798    
799                                            return dlFolderPersistence.findByF_C_P_NotS(
800                                                    previousId, companyId, parentPrimaryKey,
801                                                    WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
802                                                    size, new FolderIdComparator());
803                                    }
804    
805                            }
806                    );
807            }
808    
809            @Override
810            public void unlockFolder(
811                            long groupId, long parentFolderId, String name, String lockUuid)
812                    throws PortalException, SystemException {
813    
814                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
815    
816                    unlockFolder(dlFolder.getFolderId(), lockUuid);
817            }
818    
819            @Override
820            public void unlockFolder(long folderId, String lockUuid)
821                    throws PortalException, SystemException {
822    
823                    if (Validator.isNotNull(lockUuid)) {
824                            try {
825                                    Lock lock = lockLocalService.getLock(
826                                            DLFolder.class.getName(), folderId);
827    
828                                    if (!lockUuid.equals(lock.getUuid())) {
829                                            throw new InvalidLockException("UUIDs do not match");
830                                    }
831                            }
832                            catch (PortalException pe) {
833                                    if (pe instanceof ExpiredLockException ||
834                                            pe instanceof NoSuchLockException) {
835                                    }
836                                    else {
837                                            throw pe;
838                                    }
839                            }
840                    }
841    
842                    lockLocalService.unlock(DLFolder.class.getName(), folderId);
843            }
844    
845            @Indexable(type = IndexableType.REINDEX)
846            @Override
847            public DLFolder updateFolder(
848                            long folderId, long parentFolderId, String name, String description,
849                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
850                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
851                    throws PortalException, SystemException {
852    
853                    boolean hasLock = hasFolderLock(serviceContext.getUserId(), folderId);
854    
855                    Lock lock = null;
856    
857                    if (!hasLock) {
858    
859                            // Lock
860    
861                            lock = lockFolder(
862                                    serviceContext.getUserId(), folderId, null, false,
863                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
864                    }
865    
866                    try {
867    
868                            // File entry types
869    
870                            DLFolder dlFolder = null;
871    
872                            if (folderId > DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
873                                    dlFolder = dlFolderLocalService.updateFolderAndFileEntryTypes(
874                                            serviceContext.getUserId(), folderId, parentFolderId, name,
875                                            description, defaultFileEntryTypeId, fileEntryTypeIds,
876                                            overrideFileEntryTypes, serviceContext);
877    
878                                    dlFileEntryTypeLocalService.cascadeFileEntryTypes(
879                                            serviceContext.getUserId(), dlFolder);
880                            }
881    
882                            // Workflow definitions
883    
884                            List<ObjectValuePair<Long, String>> workflowDefinitionOVPs =
885                                    new ArrayList<ObjectValuePair<Long, String>>();
886    
887                            if (fileEntryTypeIds.isEmpty()) {
888                                    fileEntryTypeIds.add(
889                                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
890                            }
891                            else {
892                                    workflowDefinitionOVPs.add(
893                                            new ObjectValuePair<Long, String>(
894                                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
895                                                    StringPool.BLANK));
896                            }
897    
898                            for (long fileEntryTypeId : fileEntryTypeIds) {
899                                    String workflowDefinition = StringPool.BLANK;
900    
901                                    if (overrideFileEntryTypes ||
902                                            (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
903    
904                                            workflowDefinition = ParamUtil.getString(
905                                                    serviceContext, "workflowDefinition" + fileEntryTypeId);
906                                    }
907    
908                                    workflowDefinitionOVPs.add(
909                                            new ObjectValuePair<Long, String>(
910                                                    fileEntryTypeId, workflowDefinition));
911                            }
912    
913                            workflowDefinitionLinkLocalService.updateWorkflowDefinitionLinks(
914                                    serviceContext.getUserId(), serviceContext.getCompanyId(),
915                                    serviceContext.getScopeGroupId(), DLFolder.class.getName(),
916                                    folderId, workflowDefinitionOVPs);
917    
918                            return dlFolder;
919                    }
920                    finally {
921                            if (!hasLock) {
922    
923                                    // Unlock
924    
925                                    unlockFolder(folderId, lock.getUuid());
926                            }
927                    }
928            }
929    
930            @Indexable(type = IndexableType.REINDEX)
931            @Override
932            public DLFolder updateFolder(
933                            long folderId, String name, String description,
934                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
935                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
936                    throws PortalException, SystemException {
937    
938                    return updateFolder(
939                            folderId, folderId, name, description, defaultFileEntryTypeId,
940                            fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
941            }
942    
943            @Override
944            public DLFolder updateFolderAndFileEntryTypes(
945                            long userId, long folderId, long parentFolderId, String name,
946                            String description, long defaultFileEntryTypeId,
947                            List<Long> fileEntryTypeIds, boolean overrideFileEntryTypes,
948                            ServiceContext serviceContext)
949                    throws PortalException, SystemException {
950    
951                    if (overrideFileEntryTypes && fileEntryTypeIds.isEmpty()) {
952                            throw new RequiredFileEntryTypeException();
953                    }
954    
955                    boolean hasLock = hasFolderLock(userId, folderId);
956    
957                    Lock lock = null;
958    
959                    if (!hasLock) {
960    
961                            // Lock
962    
963                            lock = lockFolder(
964                                    userId, folderId, null, false,
965                                    DLFolderImpl.LOCK_EXPIRATION_TIME);
966                    }
967    
968                    try {
969    
970                            // Folder
971    
972                            if (!overrideFileEntryTypes) {
973                                    fileEntryTypeIds = Collections.emptyList();
974                            }
975    
976                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
977    
978                            parentFolderId = getParentFolderId(dlFolder, parentFolderId);
979    
980                            validateFolder(
981                                    folderId, dlFolder.getGroupId(), parentFolderId, name);
982    
983                            dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
984                            dlFolder.setParentFolderId(parentFolderId);
985                            dlFolder.setTreePath(dlFolder.buildTreePath());
986                            dlFolder.setName(name);
987                            dlFolder.setDescription(description);
988                            dlFolder.setExpandoBridgeAttributes(serviceContext);
989                            dlFolder.setOverrideFileEntryTypes(overrideFileEntryTypes);
990                            dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
991    
992                            dlFolderPersistence.update(dlFolder);
993    
994                            // File entry types
995    
996                            if (fileEntryTypeIds != null) {
997                                    dlFileEntryTypeLocalService.updateFolderFileEntryTypes(
998                                            dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
999                                            serviceContext);
1000                            }
1001    
1002                            // App helper
1003    
1004                            dlAppHelperLocalService.updateFolder(
1005                                    userId, new LiferayFolder(dlFolder), serviceContext);
1006    
1007                            return dlFolder;
1008                    }
1009                    finally {
1010                            if (!hasLock) {
1011    
1012                                    // Unlock
1013    
1014                                    unlockFolder(folderId, lock.getUuid());
1015                            }
1016                    }
1017            }
1018    
1019            /**
1020             * @deprecated As of 6.2.0
1021             */
1022            @Override
1023            public void updateLastPostDate(long folderId, Date lastPostDate)
1024                    throws PortalException, SystemException {
1025    
1026                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1027    
1028                    dlFolder.setLastPostDate(lastPostDate);
1029    
1030                    dlFolderPersistence.update(dlFolder);
1031            }
1032    
1033            @Override
1034            public DLFolder updateStatus(
1035                            long userId, long folderId, int status,
1036                            Map<String, Serializable> workflowContext,
1037                            ServiceContext serviceContext)
1038                    throws PortalException, SystemException {
1039    
1040                    // Folder
1041    
1042                    User user = userPersistence.findByPrimaryKey(userId);
1043    
1044                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1045    
1046                    int oldStatus = dlFolder.getStatus();
1047    
1048                    dlFolder.setStatus(status);
1049                    dlFolder.setStatusByUserId(user.getUserId());
1050                    dlFolder.setStatusByUserName(user.getFullName());
1051                    dlFolder.setStatusDate(new Date());
1052    
1053                    dlFolderPersistence.update(dlFolder);
1054    
1055                    // Asset
1056    
1057                    if (status == WorkflowConstants.STATUS_APPROVED) {
1058                            assetEntryLocalService.updateVisible(
1059                                    DLFolder.class.getName(), dlFolder.getFolderId(), true);
1060                    }
1061                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
1062                            assetEntryLocalService.updateVisible(
1063                                    DLFolder.class.getName(), dlFolder.getFolderId(), false);
1064                    }
1065    
1066                    // Indexer
1067    
1068                    if (((status == WorkflowConstants.STATUS_APPROVED) ||
1069                             (status == WorkflowConstants.STATUS_IN_TRASH) ||
1070                             (oldStatus == WorkflowConstants.STATUS_IN_TRASH)) &&
1071                            ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1072    
1073                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1074                                    DLFolderConstants.getClassName());
1075    
1076                            indexer.reindex(dlFolder);
1077                    }
1078    
1079                    return dlFolder;
1080            }
1081    
1082            protected void addFolderResources(
1083                            DLFolder dlFolder, boolean addGroupPermissions,
1084                            boolean addGuestPermissions)
1085                    throws PortalException, SystemException {
1086    
1087                    resourceLocalService.addResources(
1088                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1089                            dlFolder.getUserId(), DLFolder.class.getName(),
1090                            dlFolder.getFolderId(), false, addGroupPermissions,
1091                            addGuestPermissions);
1092            }
1093    
1094            protected void addFolderResources(
1095                            DLFolder dlFolder, String[] groupPermissions,
1096                            String[] guestPermissions)
1097                    throws PortalException, SystemException {
1098    
1099                    resourceLocalService.addModelResources(
1100                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
1101                            dlFolder.getUserId(), DLFolder.class.getName(),
1102                            dlFolder.getFolderId(), groupPermissions, guestPermissions);
1103            }
1104    
1105            protected void addFolderResources(
1106                            long folderId, boolean addGroupPermissions,
1107                            boolean addGuestPermissions)
1108                    throws PortalException, SystemException {
1109    
1110                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1111    
1112                    addFolderResources(dlFolder, addGroupPermissions, addGuestPermissions);
1113            }
1114    
1115            protected void addFolderResources(
1116                            long folderId, String[] groupPermissions, String[] guestPermissions)
1117                    throws PortalException, SystemException {
1118    
1119                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
1120    
1121                    addFolderResources(dlFolder, groupPermissions, guestPermissions);
1122            }
1123    
1124            protected long getParentFolderId(DLFolder dlFolder, long parentFolderId)
1125                    throws SystemException {
1126    
1127                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1128                            return parentFolderId;
1129                    }
1130    
1131                    if (dlFolder.getFolderId() == parentFolderId) {
1132                            return dlFolder.getParentFolderId();
1133                    }
1134    
1135                    DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
1136                            parentFolderId);
1137    
1138                    if ((parentDLFolder == null) ||
1139                            (dlFolder.getGroupId() != parentDLFolder.getGroupId())) {
1140    
1141                            return dlFolder.getParentFolderId();
1142                    }
1143    
1144                    List<Long> subfolderIds = new ArrayList<Long>();
1145    
1146                    getSubfolderIds(
1147                            subfolderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
1148    
1149                    if (subfolderIds.contains(parentFolderId)) {
1150                            return dlFolder.getParentFolderId();
1151                    }
1152    
1153                    return parentFolderId;
1154            }
1155    
1156            protected long getParentFolderId(long groupId, long parentFolderId)
1157                    throws SystemException {
1158    
1159                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1160                            DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
1161                                    parentFolderId);
1162    
1163                            if ((parentDLFolder == null) ||
1164                                    (groupId != parentDLFolder.getGroupId())) {
1165    
1166                                    parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1167                            }
1168                    }
1169    
1170                    return parentFolderId;
1171            }
1172    
1173            protected void validateFolder(
1174                            long folderId, long groupId, long parentFolderId, String name)
1175                    throws PortalException, SystemException {
1176    
1177                    validateFolderName(name);
1178    
1179                    DLStoreUtil.validateDirectoryName(name);
1180    
1181                    try {
1182                            dlFileEntryLocalService.getFileEntry(groupId, parentFolderId, name);
1183    
1184                            throw new DuplicateFileException(name);
1185                    }
1186                    catch (NoSuchFileEntryException nsfee) {
1187                    }
1188    
1189                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
1190                            groupId, parentFolderId, name);
1191    
1192                    if ((dlFolder != null) && (dlFolder.getFolderId() != folderId)) {
1193                            throw new DuplicateFolderNameException(name);
1194                    }
1195            }
1196    
1197            protected void validateFolder(
1198                            long groupId, long parentFolderId, String name)
1199                    throws PortalException, SystemException {
1200    
1201                    long folderId = 0;
1202    
1203                    validateFolder(folderId, groupId, parentFolderId, name);
1204            }
1205    
1206            protected void validateFolderName(String folderName)
1207                    throws PortalException {
1208    
1209                    if (folderName.contains(StringPool.SLASH)) {
1210                            throw new FolderNameException(folderName);
1211                    }
1212            }
1213    
1214            private static Log _log = LogFactoryUtil.getLog(
1215                    DLFolderLocalServiceImpl.class);
1216    
1217    }