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