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.journal.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.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portlet.journal.NoSuchFolderException;
022    import com.liferay.portlet.journal.model.JournalFolder;
023    import com.liferay.portlet.journal.model.JournalFolderConstants;
024    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Juan Fern??ndez
031     */
032    public class JournalFolderImpl extends JournalFolderBaseImpl {
033    
034            public JournalFolderImpl() {
035            }
036    
037            @Override
038            public String buildTreePath() throws PortalException, SystemException {
039                    StringBundler sb = new StringBundler();
040    
041                    buildTreePath(sb, this);
042    
043                    return sb.toString();
044            }
045    
046            @Override
047            public List<Long> getAncestorFolderIds()
048                    throws PortalException, SystemException {
049    
050                    List<Long> ancestorFolderIds = new ArrayList<Long>();
051    
052                    JournalFolder folder = this;
053    
054                    while (!folder.isRoot()) {
055                            try {
056                                    folder = folder.getParentFolder();
057    
058                                    ancestorFolderIds.add(folder.getFolderId());
059                            }
060                            catch (NoSuchFolderException nsfe) {
061                                    if (folder.isInTrash()) {
062                                            break;
063                                    }
064    
065                                    throw nsfe;
066                            }
067                    }
068    
069                    return ancestorFolderIds;
070            }
071    
072            @Override
073            public List<JournalFolder> getAncestors()
074                    throws PortalException, SystemException {
075    
076                    List<JournalFolder> ancestors = new ArrayList<JournalFolder>();
077    
078                    JournalFolder folder = this;
079    
080                    while (!folder.isRoot()) {
081                            folder = folder.getParentFolder();
082    
083                            ancestors.add(folder);
084                    }
085    
086                    return ancestors;
087            }
088    
089            @Override
090            public JournalFolder getParentFolder()
091                    throws PortalException, SystemException {
092    
093                    if (getParentFolderId() ==
094                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
095    
096                            return null;
097                    }
098    
099                    return JournalFolderLocalServiceUtil.getFolder(getParentFolderId());
100            }
101    
102            @Override
103            public JournalFolder getTrashContainer() {
104                    JournalFolder folder = null;
105    
106                    try {
107                            folder = getParentFolder();
108                    }
109                    catch (Exception e) {
110                            return null;
111                    }
112    
113                    while (folder != null) {
114                            if (folder.isInTrash()) {
115                                    return folder;
116                            }
117    
118                            try {
119                                    folder = folder.getParentFolder();
120                            }
121                            catch (Exception e) {
122                                    return null;
123                            }
124                    }
125    
126                    return null;
127            }
128    
129            @Override
130            public boolean isInTrashContainer() {
131                    if (getTrashContainer() != null) {
132                            return true;
133                    }
134                    else {
135                            return false;
136                    }
137            }
138    
139            @Override
140            public boolean isRoot() {
141                    if (getParentFolderId() ==
142                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
143    
144                            return true;
145                    }
146    
147                    return false;
148            }
149    
150            protected void buildTreePath(StringBundler sb, JournalFolder folder)
151                    throws PortalException, SystemException {
152    
153                    if (folder == null) {
154                            sb.append(StringPool.SLASH);
155                    }
156                    else {
157                            buildTreePath(sb, folder.getParentFolder());
158    
159                            sb.append(folder.getFolderId());
160                            sb.append(StringPool.SLASH);
161                    }
162            }
163    
164    }