001    /**
002     * Copyright (c) 2000-present 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.bookmarks.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portlet.bookmarks.NoSuchFolderException;
019    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
020    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
021    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class BookmarksFolderImpl extends BookmarksFolderBaseImpl {
030    
031            public BookmarksFolderImpl() {
032            }
033    
034            @Override
035            public List<Long> getAncestorFolderIds() throws PortalException {
036                    List<Long> ancestorFolderIds = new ArrayList<Long>();
037    
038                    BookmarksFolder folder = this;
039    
040                    while (!folder.isRoot()) {
041                            try {
042                                    folder = folder.getParentFolder();
043    
044                                    ancestorFolderIds.add(folder.getFolderId());
045                            }
046                            catch (NoSuchFolderException nsfe) {
047                                    if (folder.isInTrash()) {
048                                            break;
049                                    }
050    
051                                    throw nsfe;
052                            }
053                    }
054    
055                    return ancestorFolderIds;
056            }
057    
058            @Override
059            public List<BookmarksFolder> getAncestors() throws PortalException {
060                    List<BookmarksFolder> ancestors = new ArrayList<BookmarksFolder>();
061    
062                    BookmarksFolder folder = this;
063    
064                    while (!folder.isRoot()) {
065                            try {
066                                    folder = folder.getParentFolder();
067    
068                                    ancestors.add(folder);
069                            }
070                            catch (NoSuchFolderException nsfe) {
071                                    if (folder.isInTrash()) {
072                                            break;
073                                    }
074    
075                                    throw nsfe;
076                            }
077                    }
078    
079                    return ancestors;
080            }
081    
082            @Override
083            public BookmarksFolder getParentFolder() throws PortalException {
084                    if (getParentFolderId() ==
085                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
086    
087                            return null;
088                    }
089    
090                    return BookmarksFolderLocalServiceUtil.getFolder(getParentFolderId());
091            }
092    
093            @Override
094            public boolean isRoot() {
095                    if (getParentFolderId() ==
096                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
097    
098                            return true;
099                    }
100    
101                    return false;
102            }
103    
104    }