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