001    /**
002     * Copyright (c) 2000-2012 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.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            public List<Long> getAncestorFolderIds()
042                    throws PortalException, SystemException {
043    
044                    List<Long> ancestorFolderIds = new ArrayList<Long>();
045    
046                    DLFolder folder = this;
047    
048                    while (!folder.isRoot()) {
049                            try {
050                                    folder = folder.getParentFolder();
051    
052                                    ancestorFolderIds.add(folder.getFolderId());
053                            }
054                            catch (NoSuchFolderException nsfe) {
055                                    if (folder.isInTrash()) {
056                                            break;
057                                    }
058    
059                                    throw nsfe;
060                            }
061                    }
062    
063                    return ancestorFolderIds;
064            }
065    
066            public List<DLFolder> getAncestors()
067                    throws PortalException, SystemException {
068    
069                    List<DLFolder> ancestors = new ArrayList<DLFolder>();
070    
071                    DLFolder folder = this;
072    
073                    while (!folder.isRoot()) {
074                            try {
075                                    folder = folder.getParentFolder();
076    
077                                    ancestors.add(folder);
078                            }
079                            catch (NoSuchFolderException nsfe) {
080                                    if (folder.isInTrash()) {
081                                            break;
082                                    }
083    
084                                    throw nsfe;
085                            }
086                    }
087    
088                    return ancestors;
089            }
090    
091            public DLFolder getParentFolder() throws PortalException, SystemException {
092                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
093                            return null;
094                    }
095    
096                    return DLFolderLocalServiceUtil.getFolder(getParentFolderId());
097            }
098    
099            public String getPath() throws PortalException, SystemException {
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            public String[] getPathArray() throws PortalException, SystemException {
115                    String path = getPath();
116    
117                    // Remove leading /
118    
119                    path = path.substring(1);
120    
121                    return StringUtil.split(path, CharPool.SLASH);
122            }
123    
124            public DLFolder getTrashContainer() {
125                    DLFolder dlFolder = null;
126    
127                    try {
128                            dlFolder = getParentFolder();
129                    }
130                    catch (Exception e) {
131                            return null;
132                    }
133    
134                    while (dlFolder != null) {
135                            if (dlFolder.isInTrash()) {
136                                    return dlFolder;
137                            }
138    
139                            try {
140                                    dlFolder = dlFolder.getParentFolder();
141                            }
142                            catch (Exception e) {
143                                    return null;
144                            }
145                    }
146    
147                    return null;
148            }
149    
150            public boolean hasInheritableLock() {
151                    try {
152                            return DLFolderServiceUtil.hasInheritableLock(getFolderId());
153                    }
154                    catch (Exception e) {
155                    }
156    
157                    return false;
158            }
159    
160            public boolean hasLock() {
161                    try {
162                            return DLFolderServiceUtil.hasFolderLock(getFolderId());
163                    }
164                    catch (Exception e) {
165                    }
166    
167                    return false;
168            }
169    
170            public boolean isInHiddenFolder() {
171                    try {
172                            Repository repository = RepositoryLocalServiceUtil.getRepository(
173                                    getRepositoryId());
174    
175                            long dlFolderId = repository.getDlFolderId();
176    
177                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
178    
179                            return dlFolder.isHidden();
180                    }
181                    catch (Exception e) {
182                    }
183    
184                    return false;
185            }
186    
187            public boolean isInTrashContainer() {
188                    if (getTrashContainer() != null) {
189                            return true;
190                    }
191                    else {
192                            return false;
193                    }
194            }
195    
196            public boolean isLocked() {
197                    try {
198                            return DLFolderServiceUtil.isFolderLocked(getFolderId());
199                    }
200                    catch (Exception e) {
201                    }
202    
203                    return false;
204            }
205    
206            public boolean isRoot() {
207                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
208                            return true;
209                    }
210    
211                    return false;
212            }
213    
214    }