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