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