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.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portlet.bookmarks.NoSuchFolderException;
022    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
023    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
024    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
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 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                    BookmarksFolder 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<BookmarksFolder> getAncestors()
074                    throws PortalException, SystemException {
075    
076                    List<BookmarksFolder> ancestors = new ArrayList<BookmarksFolder>();
077    
078                    BookmarksFolder folder = this;
079    
080                    while (!folder.isRoot()) {
081                            try {
082                                    folder = folder.getParentFolder();
083    
084                                    ancestors.add(folder);
085                            }
086                            catch (NoSuchFolderException nsfe) {
087                                    if (folder.isInTrash()) {
088                                            break;
089                                    }
090    
091                                    throw nsfe;
092                            }
093                    }
094    
095                    return ancestors;
096            }
097    
098            @Override
099            public BookmarksFolder getParentFolder()
100                    throws PortalException, SystemException {
101    
102                    if (getParentFolderId() ==
103                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
104    
105                            return null;
106                    }
107    
108                    return BookmarksFolderLocalServiceUtil.getFolder(getParentFolderId());
109            }
110    
111            @Override
112            public boolean isRoot() {
113                    if (getParentFolderId() ==
114                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
115    
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122            protected void buildTreePath(StringBundler sb, BookmarksFolder folder)
123                    throws PortalException, SystemException {
124    
125                    if (folder == null) {
126                            sb.append(StringPool.SLASH);
127                    }
128                    else {
129                            buildTreePath(sb, folder.getParentFolder());
130    
131                            sb.append(folder.getFolderId());
132                            sb.append(StringPool.SLASH);
133                    }
134            }
135    
136    }