1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portlet.documentlibrary.model.DLFolder;
22  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
23  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * <a href="DLFolderImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class DLFolderImpl extends DLFolderModelImpl implements DLFolder {
34  
35      public DLFolderImpl() {
36      }
37  
38      public List<DLFolder> getAncestors()
39          throws PortalException, SystemException {
40  
41          List<DLFolder> ancestors = new ArrayList<DLFolder>();
42  
43          DLFolder folder = this;
44  
45          while (true) {
46              if (!folder.isRoot()) {
47                  folder = folder.getParentFolder();
48  
49                  ancestors.add(folder);
50              }
51              else {
52                  break;
53              }
54          }
55  
56          return ancestors;
57      }
58  
59      public DLFolder getParentFolder()
60          throws PortalException, SystemException {
61  
62          if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
63              return null;
64          }
65  
66          return DLFolderLocalServiceUtil.getFolder(getParentFolderId());
67      }
68  
69      public String getPath() throws PortalException, SystemException {
70          StringBuilder sb = new StringBuilder();
71  
72          DLFolder folder = this;
73  
74          while (true) {
75              sb.insert(0, folder.getName());
76              sb.insert(0, StringPool.SLASH);
77  
78              if (folder.getParentFolderId() !=
79                      DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
80  
81                  folder = DLFolderLocalServiceUtil.getFolder(
82                      folder.getParentFolderId());
83              }
84              else {
85                  break;
86              }
87          }
88  
89          return sb.toString();
90      }
91  
92      public String[] getPathArray() throws PortalException, SystemException {
93          String path = getPath();
94  
95          // Remove leading /
96  
97          path = path.substring(1, path.length());
98  
99          return StringUtil.split(path, StringPool.SLASH);
100     }
101 
102     public boolean isRoot() {
103         if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
104             return true;
105         }
106         else {
107             return false;
108         }
109     }
110 
111 }