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