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