001    /**
002     * Copyright (c) 2000-present 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.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
021    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
023    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
024    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
025    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
026    import com.liferay.portlet.documentlibrary.util.RepositoryModelUtil;
027    
028    import java.io.InputStream;
029    
030    import java.util.List;
031    
032    /**
033     * @author Alexander Chow
034     */
035    public class FileEntryUtil {
036    
037            public static FileEntry fetchByPrimaryKey(long fileEntryId) {
038                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByPrimaryKey(
039                            fileEntryId);
040    
041                    if (dlFileEntry == null) {
042                            return null;
043                    }
044    
045                    return new LiferayFileEntry(dlFileEntry);
046            }
047    
048            public static FileEntry fetchByR_F_T(
049                    long repositoryId, long folderId, String title) {
050    
051                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByG_F_T(
052                            repositoryId, folderId, title);
053    
054                    if (dlFileEntry == null) {
055                            return null;
056                    }
057    
058                    return new LiferayFileEntry(dlFileEntry);
059            }
060    
061            public static FileEntry fetchByUUID_R(String uuid, long repositoryId) {
062                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByUUID_G(
063                            uuid, repositoryId);
064    
065                    if (dlFileEntry == null) {
066                            return null;
067                    }
068    
069                    return new LiferayFileEntry(dlFileEntry);
070            }
071    
072            /**
073             * @deprecated As of 7.0.0, with no direct replacement
074             */
075            @Deprecated
076            public static List<FileEntry> findByR_F(long repositoryId, long folderId) {
077                    List<DLFileEntry> dlFileEntries = DLFileEntryUtil.findByG_F(
078                            repositoryId, folderId);
079    
080                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
081            }
082    
083            /**
084             * @deprecated As of 7.0.0, with no direct replacement
085             */
086            @Deprecated
087            public static FileEntry findByR_F_T(
088                            long repositoryId, long folderId, String title)
089                    throws NoSuchFileEntryException {
090    
091                    DLFileEntry dlFileEntry = DLFileEntryUtil.findByG_F_T(
092                            repositoryId, folderId, title);
093    
094                    return new LiferayFileEntry(dlFileEntry);
095            }
096    
097            public static InputStream getContentStream(FileEntry fileEntry)
098                    throws PortalException {
099    
100                    long repositoryId = DLFolderConstants.getDataRepositoryId(
101                            fileEntry.getRepositoryId(), fileEntry.getFolderId());
102    
103                    String name = ((DLFileEntry)fileEntry.getModel()).getName();
104    
105                    InputStream is = DLStoreUtil.getFileAsStream(
106                            fileEntry.getCompanyId(), repositoryId, name,
107                            fileEntry.getVersion());
108    
109                    if (is == null) {
110                            is = new UnsyncByteArrayInputStream(new byte[0]);
111                    }
112    
113                    return is;
114            }
115    
116    }