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