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