001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.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.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.service.LockLocalServiceUtil;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
023    import com.liferay.portlet.documentlibrary.model.DLFolder;
024    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
025    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class DLFolderImpl extends DLFolderModelImpl implements DLFolder {
034    
035            public DLFolderImpl() {
036            }
037    
038            public List<DLFolder> getAncestors()
039                    throws PortalException, SystemException {
040    
041                    List<DLFolder> ancestors = new ArrayList<DLFolder>();
042    
043                    DLFolder folder = this;
044    
045                    while (true) {
046                            if (!folder.isRoot()) {
047                                    folder = folder.getParentFolder();
048    
049                                    ancestors.add(folder);
050                            }
051                            else {
052                                    break;
053                            }
054                    }
055    
056                    return ancestors;
057            }
058    
059            public DLFolder getParentFolder()
060                    throws PortalException, SystemException {
061    
062                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
063                            return null;
064                    }
065    
066                    return DLFolderLocalServiceUtil.getFolder(getParentFolderId());
067            }
068    
069            public String getPath() throws PortalException, SystemException {
070                    StringBuilder sb = new StringBuilder();
071    
072                    DLFolder folder = this;
073    
074                    while (true) {
075                            sb.insert(0, folder.getName());
076                            sb.insert(0, StringPool.SLASH);
077    
078                            if (folder.getParentFolderId() !=
079                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
080    
081                                    folder = DLFolderLocalServiceUtil.getFolder(
082                                            folder.getParentFolderId());
083                            }
084                            else {
085                                    break;
086                            }
087                    }
088    
089                    return sb.toString();
090            }
091    
092            public String[] getPathArray() throws PortalException, SystemException {
093                    String path = getPath();
094    
095                    // Remove leading /
096    
097                    path = path.substring(1, path.length());
098    
099                    return StringUtil.split(path, StringPool.SLASH);
100            }
101    
102            public boolean hasLock(long userId) {
103                    try {
104                            return LockLocalServiceUtil.hasLock(
105                                    userId, DLFileEntry.class.getName(), getFolderId());
106                    }
107                    catch (Exception e) {
108                    }
109    
110                    return false;
111            }
112    
113            public boolean isLocked() {
114                    try {
115                            return LockLocalServiceUtil.isLocked(
116                                    DLFolder.class.getName(), getFolderId());
117                    }
118                    catch (Exception e) {
119                    }
120    
121                    return false;
122            }
123    
124            public boolean isRoot() {
125                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
126                            return true;
127                    }
128                    else {
129                            return false;
130                    }
131            }
132    
133    }