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 boolean isRoot() {
104                    if (getParentFolderId() ==
105                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
106    
107                            return true;
108                    }
109    
110                    return false;
111            }
112    
113            protected void buildTreePath(StringBundler sb, JournalFolder folder)
114                    throws PortalException, SystemException {
115    
116                    if (folder == null) {
117                            sb.append(StringPool.SLASH);
118                    }
119                    else {
120                            buildTreePath(sb, folder.getParentFolder());
121    
122                            sb.append(folder.getFolderId());
123                            sb.append(StringPool.SLASH);
124                    }
125            }
126    
127    }