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