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.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
022    import com.liferay.portal.repository.liferayrepository.util.LiferayBase;
023    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
027    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
028    
029    import java.io.InputStream;
030    
031    import java.util.List;
032    
033    /**
034     * @author Alexander Chow
035     */
036    public class FileEntryUtil extends LiferayBase {
037    
038            public static FileEntry fetchByPrimaryKey(long fileEntryId)
039                    throws SystemException {
040    
041                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByPrimaryKey(
042                            fileEntryId);
043    
044                    if (dlFileEntry == null) {
045                            return null;
046                    }
047    
048                    return new LiferayFileEntry(dlFileEntry);
049            }
050    
051            public static FileEntry fetchByR_F_T(
052                            long repositoryId, long folderId, String title)
053                    throws SystemException {
054    
055                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByG_F_T(
056                            repositoryId, folderId, title);
057    
058                    if (dlFileEntry == null) {
059                            return null;
060                    }
061    
062                    return new LiferayFileEntry(dlFileEntry);
063            }
064    
065            public static FileEntry fetchByUUID_R(String uuid, long repositoryId)
066                    throws SystemException {
067    
068                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByUUID_G(
069                            uuid, repositoryId);
070    
071                    if (dlFileEntry == null) {
072                            return null;
073                    }
074    
075                    return new LiferayFileEntry(dlFileEntry);
076            }
077    
078            public static List<FileEntry> findByR_F(long repositoryId, long folderId)
079                    throws SystemException {
080    
081                    List<DLFileEntry> dlFileEntries = DLFileEntryUtil.findByG_F(
082                            repositoryId, folderId);
083    
084                    return _instance.toFileEntries(dlFileEntries);
085            }
086    
087            public static FileEntry findByR_F_T(
088                            long repositoryId, long folderId, String title)
089                    throws NoSuchFileEntryException, SystemException {
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, SystemException {
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            private static FileEntryUtil _instance = new FileEntryUtil();
117    
118    }