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 boolean hasInheritableLock() {
138                    try {
139                            return DLFolderServiceUtil.hasInheritableLock(getFolderId());
140                    }
141                    catch (Exception e) {
142                    }
143    
144                    return false;
145            }
146    
147            @Override
148            public boolean hasLock() {
149                    try {
150                            return DLFolderServiceUtil.hasFolderLock(getFolderId());
151                    }
152                    catch (Exception e) {
153                    }
154    
155                    return false;
156            }
157    
158            @Override
159            public boolean isInHiddenFolder() {
160                    try {
161                            Repository repository = RepositoryLocalServiceUtil.getRepository(
162                                    getRepositoryId());
163    
164                            long dlFolderId = repository.getDlFolderId();
165    
166                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
167    
168                            return dlFolder.isHidden();
169                    }
170                    catch (Exception e) {
171                    }
172    
173                    return false;
174            }
175    
176            @Override
177            public boolean isLocked() {
178                    try {
179                            return DLFolderServiceUtil.isFolderLocked(getFolderId());
180                    }
181                    catch (Exception e) {
182                    }
183    
184                    return false;
185            }
186    
187            @Override
188            public boolean isRoot() {
189                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
190                            return true;
191                    }
192    
193                    return false;
194            }
195    
196    }