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