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.portal.service.LockLocalServiceUtil;
22  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
23  import com.liferay.portlet.documentlibrary.model.DLFolder;
24  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
25  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * <a href="DLFolderImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class DLFolderImpl extends DLFolderModelImpl implements DLFolder {
36  
37      public DLFolderImpl() {
38      }
39  
40      public List<DLFolder> getAncestors()
41          throws PortalException, SystemException {
42  
43          List<DLFolder> ancestors = new ArrayList<DLFolder>();
44  
45          DLFolder folder = this;
46  
47          while (true) {
48              if (!folder.isRoot()) {
49                  folder = folder.getParentFolder();
50  
51                  ancestors.add(folder);
52              }
53              else {
54                  break;
55              }
56          }
57  
58          return ancestors;
59      }
60  
61      public DLFolder getParentFolder()
62          throws PortalException, SystemException {
63  
64          if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
65              return null;
66          }
67  
68          return DLFolderLocalServiceUtil.getFolder(getParentFolderId());
69      }
70  
71      public String getPath() throws PortalException, SystemException {
72          StringBuilder sb = new StringBuilder();
73  
74          DLFolder folder = this;
75  
76          while (true) {
77              sb.insert(0, folder.getName());
78              sb.insert(0, StringPool.SLASH);
79  
80              if (folder.getParentFolderId() !=
81                      DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
82  
83                  folder = DLFolderLocalServiceUtil.getFolder(
84                      folder.getParentFolderId());
85              }
86              else {
87                  break;
88              }
89          }
90  
91          return sb.toString();
92      }
93  
94      public String[] getPathArray() throws PortalException, SystemException {
95          String path = getPath();
96  
97          // Remove leading /
98  
99          path = path.substring(1, path.length());
100 
101         return StringUtil.split(path, StringPool.SLASH);
102     }
103 
104     public boolean hasLock(long userId) {
105         try {
106             return LockLocalServiceUtil.hasLock(
107                 userId, DLFileEntry.class.getName(), getFolderId());
108         }
109         catch (Exception e) {
110         }
111 
112         return false;
113     }
114 
115     public boolean isLocked() {
116         try {
117             return LockLocalServiceUtil.isLocked(
118                 DLFolder.class.getName(), getFolderId());
119         }
120         catch (Exception e) {
121         }
122 
123         return false;
124     }
125 
126     public boolean isRoot() {
127         if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
128             return true;
129         }
130         else {
131             return false;
132         }
133     }
134 
135 }