001    /**
002     * Copyright (c) 2000-2012 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.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ObjectValuePair;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portlet.asset.util.AssetUtil;
033    import com.liferay.portlet.documentlibrary.DuplicateFileException;
034    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
035    import com.liferay.portlet.documentlibrary.FolderNameException;
036    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
037    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
039    import com.liferay.portlet.documentlibrary.model.DLFolder;
040    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
041    import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
042    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
043    import com.liferay.portlet.trash.util.TrashUtil;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.Date;
050    import java.util.List;
051    import java.util.Map;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Alexander Chow
056     */
057    public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
058    
059            public DLFolder addFolder(
060                            long userId, long groupId, long repositoryId, boolean mountPoint,
061                            long parentFolderId, String name, String description,
062                            boolean hidden, ServiceContext serviceContext)
063                    throws PortalException, SystemException {
064    
065                    // Folder
066    
067                    User user = userPersistence.findByPrimaryKey(userId);
068                    parentFolderId = getParentFolderId(groupId, parentFolderId);
069                    Date now = new Date();
070    
071                    validateFolder(groupId, parentFolderId, name);
072    
073                    long folderId = counterLocalService.increment();
074    
075                    DLFolder dlFolder = dlFolderPersistence.create(folderId);
076    
077                    dlFolder.setUuid(serviceContext.getUuid());
078                    dlFolder.setGroupId(groupId);
079                    dlFolder.setCompanyId(user.getCompanyId());
080                    dlFolder.setUserId(user.getUserId());
081                    dlFolder.setCreateDate(serviceContext.getCreateDate(now));
082                    dlFolder.setModifiedDate(serviceContext.getModifiedDate(now));
083                    dlFolder.setRepositoryId(repositoryId);
084                    dlFolder.setMountPoint(mountPoint);
085                    dlFolder.setParentFolderId(parentFolderId);
086                    dlFolder.setName(name);
087                    dlFolder.setDescription(description);
088                    dlFolder.setHidden(hidden);
089                    dlFolder.setOverrideFileEntryTypes(false);
090                    dlFolder.setExpandoBridgeAttributes(serviceContext);
091    
092                    dlFolderPersistence.update(dlFolder);
093    
094                    // Resources
095    
096                    if (serviceContext.isAddGroupPermissions() ||
097                            serviceContext.isAddGuestPermissions()) {
098    
099                            addFolderResources(
100                                    dlFolder, serviceContext.isAddGroupPermissions(),
101                                    serviceContext.isAddGuestPermissions());
102                    }
103                    else {
104                            if (serviceContext.isDeriveDefaultPermissions()) {
105                                    serviceContext.deriveDefaultPermissions(
106                                            repositoryId, DLFolderConstants.getClassName());
107                            }
108    
109                            addFolderResources(
110                                    dlFolder, serviceContext.getGroupPermissions(),
111                                    serviceContext.getGuestPermissions());
112                    }
113    
114                    // Parent folder
115    
116                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
117                            DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(
118                                    parentFolderId);
119    
120                            parentDLFolder.setLastPostDate(now);
121    
122                            dlFolderPersistence.update(parentDLFolder);
123                    }
124    
125                    // App helper
126    
127                    dlAppHelperLocalService.addFolder(
128                            new LiferayFolder(dlFolder), serviceContext);
129    
130                    return dlFolder;
131            }
132    
133            /**
134             * @deprecated As of 6.2, replaced by more general {@link #addFolder(long,
135             *             long, long, boolean, long, String, String, boolean,
136             *             ServiceContext)}
137             */
138            public DLFolder addFolder(
139                            long userId, long groupId, long repositoryId, boolean mountPoint,
140                            long parentFolderId, String name, String description,
141                            ServiceContext serviceContext)
142                    throws PortalException, SystemException {
143    
144                    return addFolder(
145                            userId, groupId, repositoryId, mountPoint, parentFolderId, name,
146                            description, false, serviceContext);
147            }
148    
149            public void deleteAll(long groupId)
150                    throws PortalException, SystemException {
151    
152                    Group group = groupLocalService.getGroup(groupId);
153    
154                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
155                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
156    
157                    for (DLFolder dlFolder : dlFolders) {
158                            deleteFolder(dlFolder);
159                    }
160    
161                    dlFileEntryLocalService.deleteFileEntries(
162                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
163    
164                    dlFileShortcutLocalService.deleteFileShortcuts(
165                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
166    
167                    try {
168                            DLStoreUtil.deleteDirectory(
169                                    group.getCompanyId(), groupId, StringPool.BLANK);
170                    }
171                    catch (NoSuchDirectoryException nsde) {
172                            if (_log.isDebugEnabled()) {
173                                    _log.debug(nsde.getMessage());
174                            }
175                    }
176            }
177    
178            public void deleteFolder(long folderId)
179                    throws PortalException, SystemException {
180    
181                    deleteFolder(folderId, true);
182            }
183    
184            public void deleteFolder(long folderId, boolean includeTrashedEntries)
185                    throws PortalException, SystemException {
186    
187                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
188    
189                    deleteFolder(dlFolder, includeTrashedEntries);
190            }
191    
192            public DLFolder fetchFolder(long groupId, long parentFolderId, String name)
193                    throws SystemException {
194    
195                    return dlFolderPersistence.fetchByG_P_N(groupId, parentFolderId, name);
196            }
197    
198            public List<DLFolder> getCompanyFolders(long companyId, int start, int end)
199                    throws SystemException {
200    
201                    return dlFolderPersistence.findByCompanyId(companyId, start, end);
202            }
203    
204            public int getCompanyFoldersCount(long companyId) throws SystemException {
205                    return dlFolderPersistence.countByCompanyId(companyId);
206            }
207    
208            /**
209             * @deprecated Replaced by {@link #getFileEntriesAndFileShortcuts(long,
210             *             long, QueryDefinition)}
211             */
212            public List<Object> getFileEntriesAndFileShortcuts(
213                            long groupId, long folderId, int status, int start, int end)
214                    throws SystemException {
215    
216                    QueryDefinition queryDefinition = new QueryDefinition(
217                            status, start, end, null);
218    
219                    return getFileEntriesAndFileShortcuts(
220                            groupId, folderId, queryDefinition);
221            }
222    
223            public List<Object> getFileEntriesAndFileShortcuts(
224                            long groupId, long folderId, QueryDefinition queryDefinition)
225                    throws SystemException {
226    
227                    return dlFolderFinder.findFE_FS_ByG_F(
228                            groupId, folderId, queryDefinition);
229            }
230    
231            /**
232             * @deprecated Replaced by {@link #getFileEntriesAndFileShortcutsCount(long,
233             *             long, QueryDefinition)}
234             */
235            public int getFileEntriesAndFileShortcutsCount(
236                            long groupId, long folderId, int status)
237                    throws SystemException {
238    
239                    QueryDefinition queryDefinition = new QueryDefinition(status);
240    
241                    return getFileEntriesAndFileShortcutsCount(
242                            groupId, folderId, queryDefinition);
243            }
244    
245            public int getFileEntriesAndFileShortcutsCount(
246                            long groupId, long folderId, QueryDefinition queryDefinition)
247                    throws SystemException {
248    
249                    return dlFolderFinder.countFE_FS_ByG_F(
250                            groupId, folderId, queryDefinition);
251            }
252    
253            public DLFolder getFolder(long folderId)
254                    throws PortalException, SystemException {
255    
256                    return dlFolderPersistence.findByPrimaryKey(folderId);
257            }
258    
259            public DLFolder getFolder(long groupId, long parentFolderId, String name)
260                    throws PortalException, SystemException {
261    
262                    return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
263            }
264    
265            public long getFolderId(long companyId, long folderId)
266                    throws SystemException {
267    
268                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
269    
270                            // Ensure folder exists and belongs to the proper company
271    
272                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
273    
274                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
275                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
276                            }
277                    }
278    
279                    return folderId;
280            }
281    
282            public List<DLFolder> getFolders(long groupId, long parentFolderId)
283                    throws SystemException {
284    
285                    return getFolders(groupId, parentFolderId, true);
286            }
287    
288            public List<DLFolder> getFolders(
289                            long groupId, long parentFolderId, boolean includeMountfolders)
290                    throws SystemException {
291    
292                    if (includeMountfolders) {
293                            return dlFolderPersistence.findByG_P(groupId, parentFolderId);
294                    }
295                    else {
296                            return dlFolderPersistence.findByG_M_P(
297                                    groupId, false, parentFolderId);
298                    }
299            }
300    
301            public List<DLFolder> getFolders(
302                            long groupId, long parentFolderId, boolean includeMountfolders,
303                            int start, int end, OrderByComparator obc)
304                    throws SystemException {
305    
306                    if (includeMountfolders) {
307                            return dlFolderPersistence.findByG_P(
308                                    groupId, parentFolderId, start, end, obc);
309                    }
310                    else {
311                            return dlFolderPersistence.findByG_M_P(
312                                    groupId, false, parentFolderId, start, end, obc);
313                    }
314            }
315    
316            public List<DLFolder> getFolders(
317                            long groupId, long parentFolderId, int start, int end,
318                            OrderByComparator obc)
319                    throws SystemException {
320    
321                    return getFolders(groupId, parentFolderId, true, start, end, obc);
322            }
323    
324            /**
325             * @deprecated Replaced by {@link
326             *             #getFoldersAndFileEntriesAndFileShortcuts(long, long,
327             *             String[], boolean, QueryDefinition)}
328             */
329            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
330                            long groupId, long folderId, int status,
331                            boolean includeMountFolders, int start, int end,
332                            OrderByComparator obc)
333                    throws SystemException {
334    
335                    QueryDefinition queryDefinition = new QueryDefinition(
336                            status, start, end, obc);
337    
338                    return getFoldersAndFileEntriesAndFileShortcuts(
339                            groupId, folderId, null, includeMountFolders, queryDefinition);
340            }
341    
342            /**
343             * @deprecated Replaced by {@link
344             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
345             *             String[], boolean, QueryDefinition)}
346             */
347            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
348                            long groupId, long folderId, int status, String[] mimeTypes,
349                            boolean includeMountFolders, int start, int end,
350                            OrderByComparator obc)
351                    throws SystemException {
352    
353                    QueryDefinition queryDefinition = new QueryDefinition(
354                            status, start, end, obc);
355    
356                    return getFoldersAndFileEntriesAndFileShortcuts(
357                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
358            }
359    
360            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
361                            long groupId, long folderId, String[] mimeTypes,
362                            boolean includeMountFolders, QueryDefinition queryDefinition)
363                    throws SystemException {
364    
365                    return dlFolderFinder.findF_FE_FS_ByG_F_M_M(
366                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
367            }
368    
369            /**
370             * @deprecated Replaced by {@link
371             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
372             *             String[], boolean, QueryDefinition)}
373             */
374            public int getFoldersAndFileEntriesAndFileShortcutsCount(
375                            long groupId, long folderId, int status,
376                            boolean includeMountFolders)
377                    throws SystemException {
378    
379                    QueryDefinition queryDefinition = new QueryDefinition(status);
380    
381                    return getFoldersAndFileEntriesAndFileShortcutsCount(
382                            groupId, folderId, null, includeMountFolders, queryDefinition);
383            }
384    
385            /**
386             * @deprecated Replaced by {@link
387             *             #getFoldersAndFileEntriesAndFileShortcutsCount(long, long,
388             *             String[], boolean, QueryDefinition)}
389             */
390            public int getFoldersAndFileEntriesAndFileShortcutsCount(
391                            long groupId, long folderId, int status, String[] mimeTypes,
392                            boolean includeMountFolders)
393                    throws SystemException {
394    
395                    QueryDefinition queryDefinition = new QueryDefinition(status);
396    
397                    return getFoldersAndFileEntriesAndFileShortcutsCount(
398                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
399            }
400    
401            public int getFoldersAndFileEntriesAndFileShortcutsCount(
402                            long groupId, long folderId, String[] mimeTypes,
403                            boolean includeMountFolders, QueryDefinition queryDefinition)
404                    throws SystemException {
405    
406                    return dlFolderFinder.countF_FE_FS_ByG_F_M_M(
407                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
408            }
409    
410            public int getFoldersCount(long groupId, long parentFolderId)
411                    throws SystemException {
412    
413                    return getFoldersCount(groupId, parentFolderId, true);
414            }
415    
416            public int getFoldersCount(
417                            long groupId, long parentFolderId, boolean includeMountfolders)
418                    throws SystemException {
419    
420                    if (includeMountfolders) {
421                            return dlFolderPersistence.countByG_P(groupId, parentFolderId);
422                    }
423                    else {
424                            return dlFolderPersistence.countByG_M_P(
425                                    groupId, false, parentFolderId);
426                    }
427            }
428    
429            public DLFolder getMountFolder(long repositoryId)
430                    throws PortalException, SystemException {
431    
432                    return dlFolderPersistence.findByRepositoryId(repositoryId);
433            }
434    
435            public List<DLFolder> getMountFolders(
436                            long groupId, long parentFolderId, int start, int end,
437                            OrderByComparator obc)
438                    throws SystemException {
439    
440                    return dlFolderPersistence.findByG_M_P(
441                            groupId, true, parentFolderId, start, end, obc);
442            }
443    
444            public int getMountFoldersCount(long groupId, long parentFolderId)
445                    throws SystemException {
446    
447                    return dlFolderPersistence.countByG_M_P(groupId, true, parentFolderId);
448            }
449    
450            public void getSubfolderIds(
451                            List<Long> folderIds, long groupId, long folderId)
452                    throws SystemException {
453    
454                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
455                            groupId, folderId);
456    
457                    for (DLFolder dlFolder : dlFolders) {
458                            folderIds.add(dlFolder.getFolderId());
459    
460                            getSubfolderIds(
461                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
462                    }
463            }
464    
465            public DLFolder moveFolder(
466                            long folderId, long parentFolderId, ServiceContext serviceContext)
467                    throws PortalException, SystemException {
468    
469                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
470    
471                    parentFolderId = getParentFolderId(dlFolder, parentFolderId);
472    
473                    validateFolder(
474                            dlFolder.getFolderId(), dlFolder.getGroupId(), parentFolderId,
475                            dlFolder.getName());
476    
477                    dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
478                    dlFolder.setParentFolderId(parentFolderId);
479                    dlFolder.setExpandoBridgeAttributes(serviceContext);
480    
481                    dlFolderPersistence.update(dlFolder);
482    
483                    dlAppHelperLocalService.moveFolder(new LiferayFolder(dlFolder));
484    
485                    return dlFolder;
486            }
487    
488            public DLFolder updateFolder(
489                            long folderId, long parentFolderId, String name, String description,
490                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
491                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
492                    throws PortalException, SystemException {
493    
494                    // File entry types
495    
496                    DLFolder dlFolder = null;
497    
498                    if (folderId > DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
499                            dlFolder = dlFolderLocalService.updateFolderAndFileEntryTypes(
500                                    folderId, parentFolderId, name, description,
501                                    defaultFileEntryTypeId, fileEntryTypeIds,
502                                    overrideFileEntryTypes, serviceContext);
503    
504                            dlFileEntryTypeLocalService.cascadeFileEntryTypes(
505                                    serviceContext.getUserId(), dlFolder);
506                    }
507    
508                    // Workflow definitions
509    
510                    List<ObjectValuePair<Long, String>> workflowDefinitionOVPs =
511                            new ArrayList<ObjectValuePair<Long, String>>();
512    
513                    if (fileEntryTypeIds.isEmpty()) {
514                            fileEntryTypeIds.add(
515                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);
516                    }
517                    else {
518                            workflowDefinitionOVPs.add(
519                                    new ObjectValuePair<Long, String>(
520                                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL,
521                                            StringPool.BLANK));
522                    }
523    
524                    for (long fileEntryTypeId : fileEntryTypeIds) {
525                            String workflowDefinition = ParamUtil.getString(
526                                    serviceContext, "workflowDefinition" + fileEntryTypeId);
527    
528                            workflowDefinitionOVPs.add(
529                                    new ObjectValuePair<Long, String>(
530                                            fileEntryTypeId, workflowDefinition));
531                    }
532    
533                    workflowDefinitionLinkLocalService.updateWorkflowDefinitionLinks(
534                            serviceContext.getUserId(), serviceContext.getCompanyId(),
535                            serviceContext.getScopeGroupId(), DLFolder.class.getName(),
536                            folderId, workflowDefinitionOVPs);
537    
538                    return dlFolder;
539            }
540    
541            public DLFolder updateFolder(
542                            long folderId, String name, String description,
543                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
544                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
545                    throws PortalException, SystemException {
546    
547                    return updateFolder(
548                            folderId, folderId, name, description, defaultFileEntryTypeId,
549                            fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
550            }
551    
552            public DLFolder updateFolderAndFileEntryTypes(
553                            long folderId, long parentFolderId, String name, String description,
554                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
555                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
556                    throws PortalException, SystemException {
557    
558                    // Folder
559    
560                    if (!overrideFileEntryTypes) {
561                            fileEntryTypeIds = Collections.emptyList();
562                    }
563    
564                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
565    
566                    parentFolderId = getParentFolderId(dlFolder, parentFolderId);
567    
568                    validateFolder(folderId, dlFolder.getGroupId(), parentFolderId, name);
569    
570                    dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
571                    dlFolder.setParentFolderId(parentFolderId);
572                    dlFolder.setName(name);
573                    dlFolder.setDescription(description);
574                    dlFolder.setExpandoBridgeAttributes(serviceContext);
575                    dlFolder.setOverrideFileEntryTypes(overrideFileEntryTypes);
576                    dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
577    
578                    dlFolderPersistence.update(dlFolder);
579    
580                    // File entry types
581    
582                    if (fileEntryTypeIds != null) {
583                            dlFileEntryTypeLocalService.updateFolderFileEntryTypes(
584                                    dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
585                                    serviceContext);
586                    }
587    
588                    // App helper
589    
590                    dlAppHelperLocalService.updateFolder(
591                            new LiferayFolder(dlFolder), serviceContext);
592    
593                    return dlFolder;
594            }
595    
596            public void updateLastPostDate(long folderId, Date lastPostDate)
597                    throws PortalException, SystemException {
598    
599                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
600    
601                    dlFolder.setLastPostDate(lastPostDate);
602    
603                    dlFolderPersistence.update(dlFolder);
604            }
605    
606            public DLFolder updateStatus(
607                            long userId, long folderId, int status,
608                            Map<String, Serializable> workflowContext,
609                            ServiceContext serviceContext)
610                    throws PortalException, SystemException {
611    
612                    // Folder
613    
614                    User user = userPersistence.findByPrimaryKey(userId);
615    
616                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
617    
618                    if (dlFolder.isInTrash() &&
619                            (status == WorkflowConstants.STATUS_APPROVED)) {
620    
621                            dlFolder.setName(TrashUtil.stripTrashNamespace(dlFolder.getName()));
622                    }
623    
624                    dlFolder.setStatus(status);
625                    dlFolder.setStatusByUserId(user.getUserId());
626                    dlFolder.setStatusByUserName(user.getFullName());
627                    dlFolder.setStatusDate(new Date());
628    
629                    dlFolderPersistence.update(dlFolder);
630    
631                    // Folders, file entries, and file shortcuts
632    
633                    QueryDefinition queryDefinition = new QueryDefinition(
634                            WorkflowConstants.STATUS_ANY);
635    
636                    List<Object> foldersAndFileEntriesAndFileShortcuts =
637                            getFoldersAndFileEntriesAndFileShortcuts(
638                                    dlFolder.getGroupId(), folderId, null, false, queryDefinition);
639    
640                    dlAppHelperLocalService.updateStatuses(
641                            user, foldersAndFileEntriesAndFileShortcuts, status);
642    
643                    // Trash
644    
645                    if (status == WorkflowConstants.STATUS_IN_TRASH) {
646                            trashEntryLocalService.addTrashEntry(
647                                    userId, dlFolder.getGroupId(), DLFolderConstants.getClassName(),
648                                    dlFolder.getFolderId(), WorkflowConstants.STATUS_APPROVED, null,
649                                    null);
650                    }
651    
652                    return dlFolder;
653            }
654    
655            protected void addFolderResources(
656                            DLFolder dlFolder, boolean addGroupPermissions,
657                            boolean addGuestPermissions)
658                    throws PortalException, SystemException {
659    
660                    resourceLocalService.addResources(
661                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
662                            dlFolder.getUserId(), DLFolder.class.getName(),
663                            dlFolder.getFolderId(), false, addGroupPermissions,
664                            addGuestPermissions);
665            }
666    
667            protected void addFolderResources(
668                            DLFolder dlFolder, String[] groupPermissions,
669                            String[] guestPermissions)
670                    throws PortalException, SystemException {
671    
672                    resourceLocalService.addModelResources(
673                            dlFolder.getCompanyId(), dlFolder.getGroupId(),
674                            dlFolder.getUserId(), DLFolder.class.getName(),
675                            dlFolder.getFolderId(), groupPermissions, guestPermissions);
676            }
677    
678            protected void addFolderResources(
679                            long folderId, boolean addGroupPermissions,
680                            boolean addGuestPermissions)
681                    throws PortalException, SystemException {
682    
683                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
684    
685                    addFolderResources(dlFolder, addGroupPermissions, addGuestPermissions);
686            }
687    
688            protected void addFolderResources(
689                            long folderId, String[] groupPermissions, String[] guestPermissions)
690                    throws PortalException, SystemException {
691    
692                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
693    
694                    addFolderResources(dlFolder, groupPermissions, guestPermissions);
695            }
696    
697            protected void deleteFolder(DLFolder dlFolder)
698                    throws PortalException, SystemException {
699    
700                    deleteFolder(dlFolder, true);
701            }
702    
703            protected void deleteFolder(
704                            DLFolder dlFolder, boolean includeTrashedEntries)
705                    throws PortalException, SystemException {
706    
707                    // Folders
708    
709                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
710                            dlFolder.getGroupId(), dlFolder.getFolderId());
711    
712                    for (DLFolder curDLFolder : dlFolders) {
713                            if (includeTrashedEntries || !curDLFolder.isInTrash()) {
714                                    deleteFolder(curDLFolder, includeTrashedEntries);
715                            }
716                    }
717    
718                    // Resources
719    
720                    resourceLocalService.deleteResource(
721                            dlFolder.getCompanyId(), DLFolder.class.getName(),
722                            ResourceConstants.SCOPE_INDIVIDUAL, dlFolder.getFolderId());
723    
724                    // WebDAVProps
725    
726                    webDAVPropsLocalService.deleteWebDAVProps(
727                            DLFolder.class.getName(), dlFolder.getFolderId());
728    
729                    // File entries
730    
731                    dlFileEntryLocalService.deleteFileEntries(
732                            dlFolder.getGroupId(), dlFolder.getFolderId(),
733                            includeTrashedEntries);
734    
735                    // File entry types
736    
737                    dlFileEntryTypeLocalService.unsetFolderFileEntryTypes(
738                            dlFolder.getFolderId());
739    
740                    // File shortcuts
741    
742                    dlFileShortcutLocalService.deleteFileShortcuts(
743                            dlFolder.getGroupId(), dlFolder.getFolderId(),
744                            includeTrashedEntries);
745    
746                    // Expando
747    
748                    expandoValueLocalService.deleteValues(
749                            DLFolder.class.getName(), dlFolder.getFolderId());
750    
751                    // App helper
752    
753                    dlAppHelperLocalService.deleteFolder(new LiferayFolder(dlFolder));
754    
755                    // Folder
756    
757                    dlFolderPersistence.remove(dlFolder);
758    
759                    // Directory
760    
761                    try {
762                            DLStoreUtil.deleteDirectory(
763                                    dlFolder.getCompanyId(), dlFolder.getFolderId(),
764                                    StringPool.BLANK);
765                    }
766                    catch (NoSuchDirectoryException nsde) {
767                            if (_log.isDebugEnabled()) {
768                                    _log.debug(nsde.getMessage());
769                            }
770                    }
771            }
772    
773            protected long getParentFolderId(DLFolder dlFolder, long parentFolderId)
774                    throws SystemException {
775    
776                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
777                            return parentFolderId;
778                    }
779    
780                    if (dlFolder.getFolderId() == parentFolderId) {
781                            return dlFolder.getParentFolderId();
782                    }
783                    else {
784                            DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
785                                    parentFolderId);
786    
787                            if ((parentDLFolder == null) ||
788                                    (dlFolder.getGroupId() != parentDLFolder.getGroupId())) {
789    
790                                    return dlFolder.getParentFolderId();
791                            }
792    
793                            List<Long> subfolderIds = new ArrayList<Long>();
794    
795                            getSubfolderIds(
796                                    subfolderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
797    
798                            if (subfolderIds.contains(parentFolderId)) {
799                                    return dlFolder.getParentFolderId();
800                            }
801    
802                            return parentFolderId;
803                    }
804            }
805    
806            protected long getParentFolderId(long groupId, long parentFolderId)
807                    throws SystemException {
808    
809                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
810                            DLFolder parentDLFolder = dlFolderPersistence.fetchByPrimaryKey(
811                                    parentFolderId);
812    
813                            if ((parentDLFolder == null) ||
814                                    (groupId != parentDLFolder.getGroupId())) {
815    
816                                    parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
817                            }
818                    }
819    
820                    return parentFolderId;
821            }
822    
823            protected void validateFolder(
824                            long folderId, long groupId, long parentFolderId, String name)
825                    throws PortalException, SystemException {
826    
827                    validateFolderName(name);
828    
829                    try {
830                            dlFileEntryLocalService.getFileEntry(groupId, parentFolderId, name);
831    
832                            throw new DuplicateFileException(name);
833                    }
834                    catch (NoSuchFileEntryException nsfee) {
835                    }
836    
837                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
838                            groupId, parentFolderId, name);
839    
840                    if ((dlFolder != null) && (dlFolder.getFolderId() != folderId)) {
841                            throw new DuplicateFolderNameException(name);
842                    }
843            }
844    
845            protected void validateFolder(
846                            long groupId, long parentFolderId, String name)
847                    throws PortalException, SystemException {
848    
849                    long folderId = 0;
850    
851                    validateFolder(folderId, groupId, parentFolderId, name);
852            }
853    
854            protected void validateFolderName(String name) throws PortalException {
855                    if (!AssetUtil.isValidWord(name)) {
856                            throw new FolderNameException();
857                    }
858            }
859    
860            private static Log _log = LogFactoryUtil.getLog(
861                    DLFolderLocalServiceImpl.class);
862    
863    }