001    /**
002     * Copyright (c) 2000-present 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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.Repository;
022    import com.liferay.portal.service.RepositoryLocalServiceUtil;
023    import com.liferay.portlet.documentlibrary.exception.NoSuchFolderException;
024    import com.liferay.portlet.documentlibrary.model.DLFolder;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
028    import com.liferay.portlet.exportimport.lar.StagedModelType;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DLFolderImpl extends DLFolderBaseImpl {
037    
038            @Override
039            public List<Long> getAncestorFolderIds() throws PortalException {
040                    List<Long> ancestorFolderIds = new ArrayList<>();
041    
042                    DLFolder folder = this;
043    
044                    while (!folder.isRoot()) {
045                            try {
046                                    folder = folder.getParentFolder();
047    
048                                    ancestorFolderIds.add(folder.getFolderId());
049                            }
050                            catch (NoSuchFolderException nsfe) {
051                                    if (folder.isInTrash()) {
052                                            break;
053                                    }
054    
055                                    throw nsfe;
056                            }
057                    }
058    
059                    return ancestorFolderIds;
060            }
061    
062            @Override
063            public List<DLFolder> getAncestors() throws PortalException {
064                    List<DLFolder> ancestors = new ArrayList<>();
065    
066                    DLFolder folder = this;
067    
068                    while (!folder.isRoot()) {
069                            try {
070                                    folder = folder.getParentFolder();
071    
072                                    ancestors.add(folder);
073                            }
074                            catch (NoSuchFolderException nsfe) {
075                                    if (folder.isInTrash()) {
076                                            break;
077                                    }
078    
079                                    throw nsfe;
080                            }
081                    }
082    
083                    return ancestors;
084            }
085    
086            @Override
087            public DLFolder getParentFolder() throws PortalException {
088                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
089                            return null;
090                    }
091    
092                    return DLFolderLocalServiceUtil.getFolder(getParentFolderId());
093            }
094    
095            @Override
096            public String getPath() throws PortalException {
097                    StringBuilder sb = new StringBuilder();
098    
099                    DLFolder folder = this;
100    
101                    while (folder != null) {
102                            sb.insert(0, folder.getName());
103                            sb.insert(0, StringPool.SLASH);
104    
105                            folder = folder.getParentFolder();
106                    }
107    
108                    return sb.toString();
109            }
110    
111            @Override
112            public String[] getPathArray() throws PortalException {
113                    String path = getPath();
114    
115                    // Remove leading /
116    
117                    path = path.substring(1);
118    
119                    return StringUtil.split(path, CharPool.SLASH);
120            }
121    
122            @Override
123            public StagedModelType getStagedModelType() {
124                    return new StagedModelType(DLFolderConstants.getClassName());
125            }
126    
127            @Override
128            public boolean hasInheritableLock() {
129                    try {
130                            return DLFolderLocalServiceUtil.hasInheritableLock(getFolderId());
131                    }
132                    catch (Exception e) {
133                    }
134    
135                    return false;
136            }
137    
138            @Override
139            public boolean hasLock() {
140                    try {
141                            return DLFolderServiceUtil.hasFolderLock(getFolderId());
142                    }
143                    catch (Exception e) {
144                    }
145    
146                    return false;
147            }
148    
149            @Override
150            public boolean isInHiddenFolder() {
151                    try {
152                            Repository repository = RepositoryLocalServiceUtil.getRepository(
153                                    getRepositoryId());
154    
155                            long dlFolderId = repository.getDlFolderId();
156    
157                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
158    
159                            return dlFolder.isHidden();
160                    }
161                    catch (Exception e) {
162                    }
163    
164                    return false;
165            }
166    
167            @Override
168            public boolean isLocked() {
169                    try {
170                            return DLFolderServiceUtil.isFolderLocked(getFolderId());
171                    }
172                    catch (Exception e) {
173                    }
174    
175                    return false;
176            }
177    
178            @Override
179            public boolean isRoot() {
180                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
181                            return true;
182                    }
183    
184                    return false;
185            }
186    
187    }