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 BookmarksFolder getTrashContainer() {
113                    BookmarksFolder folder = null;
114    
115                    try {
116                            folder = getParentFolder();
117                    }
118                    catch (Exception e) {
119                            return null;
120                    }
121    
122                    while (folder != null) {
123                            if (folder.isInTrash()) {
124                                    return folder;
125                            }
126    
127                            try {
128                                    folder = folder.getParentFolder();
129                            }
130                            catch (Exception e) {
131                                    return null;
132                            }
133                    }
134    
135                    return null;
136            }
137    
138            @Override
139            public boolean isInTrashContainer() {
140                    if (getTrashContainer() != null) {
141                            return true;
142                    }
143                    else {
144                            return false;
145                    }
146            }
147    
148            @Override
149            public boolean isRoot() {
150                    if (getParentFolderId() ==
151                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
152    
153                            return true;
154                    }
155    
156                    return false;
157            }
158    
159            protected void buildTreePath(StringBundler sb, BookmarksFolder folder)
160                    throws PortalException, SystemException {
161    
162                    if (folder == null) {
163                            sb.append(StringPool.SLASH);
164                    }
165                    else {
166                            buildTreePath(sb, folder.getParentFolder());
167    
168                            sb.append(folder.getFolderId());
169                            sb.append(StringPool.SLASH);
170                    }
171            }
172    
173    }