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.exception.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_FN(
049                    long repositoryId, long folderId, String fileName) {
050    
051                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByG_F_FN(
052                            repositoryId, folderId, fileName);
053    
054                    if (dlFileEntry == null) {
055                            return null;
056                    }
057    
058                    return new LiferayFileEntry(dlFileEntry);
059            }
060    
061            public static FileEntry fetchByR_F_T(
062                    long repositoryId, long folderId, String title) {
063    
064                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByG_F_T(
065                            repositoryId, folderId, title);
066    
067                    if (dlFileEntry == null) {
068                            return null;
069                    }
070    
071                    return new LiferayFileEntry(dlFileEntry);
072            }
073    
074            public static FileEntry fetchByUUID_R(String uuid, long repositoryId) {
075                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByUUID_G(
076                            uuid, repositoryId);
077    
078                    if (dlFileEntry == null) {
079                            return null;
080                    }
081    
082                    return new LiferayFileEntry(dlFileEntry);
083            }
084    
085            /**
086             * @deprecated As of 7.0.0, with no direct replacement
087             */
088            @Deprecated
089            public static List<FileEntry> findByR_F(long repositoryId, long folderId) {
090                    List<DLFileEntry> dlFileEntries = DLFileEntryUtil.findByG_F(
091                            repositoryId, folderId);
092    
093                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
094            }
095    
096            /**
097             * @deprecated As of 7.0.0, with no direct replacement
098             */
099            @Deprecated
100            public static FileEntry findByR_F_T(
101                            long repositoryId, long folderId, String title)
102                    throws NoSuchFileEntryException {
103    
104                    DLFileEntry dlFileEntry = DLFileEntryUtil.findByG_F_T(
105                            repositoryId, folderId, title);
106    
107                    return new LiferayFileEntry(dlFileEntry);
108            }
109    
110            public static InputStream getContentStream(FileEntry fileEntry)
111                    throws PortalException {
112    
113                    long repositoryId = DLFolderConstants.getDataRepositoryId(
114                            fileEntry.getRepositoryId(), fileEntry.getFolderId());
115    
116                    String name = ((DLFileEntry)fileEntry.getModel()).getName();
117    
118                    InputStream is = DLStoreUtil.getFileAsStream(
119                            fileEntry.getCompanyId(), repositoryId, name,
120                            fileEntry.getVersion());
121    
122                    if (is == null) {
123                            is = new UnsyncByteArrayInputStream(new byte[0]);
124                    }
125    
126                    return is;
127            }
128    
129    }