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