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