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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.Repository;
026    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
027    import com.liferay.portal.service.RepositoryLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
029    import com.liferay.portlet.documentlibrary.model.DLFolder;
030    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
031    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DLFileShortcutImpl extends DLFileShortcutBaseImpl {
037    
038            public DLFileShortcutImpl() {
039            }
040    
041            @Override
042            public String buildTreePath() throws PortalException, SystemException {
043                    StringBundler sb = new StringBundler();
044    
045                    buildTreePath(sb, getDLFolder());
046    
047                    return sb.toString();
048            }
049    
050            @Override
051            public DLFolder getDLFolder() throws PortalException, SystemException {
052                    Folder folder = getFolder();
053    
054                    return (DLFolder)folder.getModel();
055            }
056    
057            @Override
058            public Folder getFolder() throws PortalException, SystemException {
059                    if (getFolderId() <= 0) {
060                            return new LiferayFolder(new DLFolderImpl());
061                    }
062    
063                    return DLAppLocalServiceUtil.getFolder(getFolderId());
064            }
065    
066            @Override
067            public String getToTitle() {
068                    String toTitle = null;
069    
070                    try {
071                            FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
072                                    getToFileEntryId());
073    
074                            toTitle = fileEntry.getTitle();
075                    }
076                    catch (Exception e) {
077                            _log.error(e, e);
078                    }
079    
080                    return toTitle;
081            }
082    
083            @Override
084            public DLFolder getTrashContainer()
085                    throws PortalException, SystemException {
086    
087                    DLFolder dlFolder = null;
088    
089                    try {
090                            dlFolder = getDLFolder();
091                    }
092                    catch (NoSuchFolderException nsfe) {
093                            return null;
094                    }
095    
096                    if (dlFolder.isInTrash()) {
097                            return dlFolder;
098                    }
099    
100                    return dlFolder.getTrashContainer();
101            }
102    
103            @Override
104            public boolean isInHiddenFolder() {
105                    try {
106                            long repositoryId = getRepositoryId();
107    
108                            Repository repository = RepositoryLocalServiceUtil.getRepository(
109                                    repositoryId);
110    
111                            long dlFolderId = repository.getDlFolderId();
112    
113                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);
114    
115                            return dlFolder.isHidden();
116                    }
117                    catch (Exception e) {
118                    }
119    
120                    return false;
121            }
122    
123            @Override
124            public boolean isInTrashContainer() {
125                    try {
126                            if (getTrashContainer() != null) {
127                                    return true;
128                            }
129                    }
130                    catch (Exception e) {
131                    }
132    
133                    return false;
134            }
135    
136            protected void buildTreePath(StringBundler sb, DLFolder dlFolder)
137                    throws PortalException, SystemException {
138    
139                    if (dlFolder == null) {
140                            sb.append(StringPool.SLASH);
141                    }
142                    else {
143                            buildTreePath(sb, dlFolder.getParentFolder());
144    
145                            sb.append(dlFolder.getFolderId());
146                            sb.append(StringPool.SLASH);
147                    }
148            }
149    
150            private static Log _log = LogFactoryUtil.getLog(DLFileShortcutImpl.class);
151    
152    }