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