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.portal.kernel.util;
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.kernel.repository.model.Folder;
021    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
025    
026    import java.io.File;
027    import java.io.InputStream;
028    
029    import java.util.List;
030    
031    /**
032     * @author Sergio González
033     * @author Matthew Kong
034     * @author Alexander Chow
035     */
036    public class TempFileUtil {
037    
038            public static FileEntry addTempFile(
039                            long groupId, long userId, String fileName, String tempFolderName,
040                            File file, String mimeType)
041                    throws PortalException, SystemException {
042    
043                    long folderId = getTempFolderId(groupId, userId, tempFolderName);
044    
045                    return PortletFileRepositoryUtil.addPortletFileEntry(
046                            groupId, userId, StringPool.BLANK, 0, PortletKeys.DOCUMENT_LIBRARY,
047                            folderId, file, fileName, mimeType);
048            }
049    
050            public static FileEntry addTempFile(
051                            long groupId, long userId, String fileName, String tempFolderName,
052                            InputStream inputStream, String mimeType)
053                    throws PortalException, SystemException {
054    
055                    long folderId = getTempFolderId(groupId, userId, tempFolderName);
056    
057                    return PortletFileRepositoryUtil.addPortletFileEntry(
058                            groupId, userId, StringPool.BLANK, 0, PortletKeys.DOCUMENT_LIBRARY,
059                            folderId, inputStream, fileName, mimeType);
060            }
061    
062            public static void deleteTempFile(long fileEntryId)
063                    throws PortalException, SystemException {
064    
065                    PortletFileRepositoryUtil.deletePortletFileEntry(fileEntryId);
066            }
067    
068            public static void deleteTempFile(
069                            long groupId, long userId, String fileName, String tempFolderName)
070                    throws PortalException, SystemException {
071    
072                    long folderId = getTempFolderId(groupId, userId, tempFolderName);
073    
074                    PortletFileRepositoryUtil.deletePortletFileEntry(
075                            groupId, folderId, fileName);
076            }
077    
078            public static FileEntry getTempFile(
079                            long groupId, long userId, String fileName, String tempFolderName)
080                    throws PortalException, SystemException {
081    
082                    long folderId = getTempFolderId(groupId, userId, tempFolderName);
083    
084                    return PortletFileRepositoryUtil.getPortletFileEntry(
085                            groupId, folderId, fileName);
086            }
087    
088            public static String[] getTempFileEntryNames(
089                            long groupId, long userId, String tempFolderName)
090                    throws PortalException, SystemException {
091    
092                    long folderId = getTempFolderId(groupId, userId, tempFolderName);
093    
094                    List<FileEntry> fileEntries =
095                            PortletFileRepositoryUtil.getPortletFileEntries(groupId, folderId);
096    
097                    String[] fileEntryNames = new String[fileEntries.size()];
098    
099                    for (int i = 0; i < fileEntries.size(); i++) {
100                            FileEntry fileEntry = fileEntries.get(i);
101    
102                            fileEntryNames[i] = fileEntry.getTitle();
103                    }
104    
105                    return fileEntryNames;
106            }
107    
108            protected static long getTempFolderId(
109                            long groupId, long userId, String tempFolderName)
110                    throws PortalException, SystemException {
111    
112                    ServiceContext serviceContext = new ServiceContext();
113    
114                    serviceContext.setAddGroupPermissions(true);
115                    serviceContext.setAddGuestPermissions(true);
116    
117                    long repositoryId = PortletFileRepositoryUtil.getPortletRepositoryId(
118                            groupId, PortletKeys.DOCUMENT_LIBRARY, serviceContext);
119    
120                    Folder userFolder = PortletFileRepositoryUtil.getPortletFolder(
121                            userId, repositoryId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
122                            String.valueOf(userId), serviceContext);
123    
124                    Folder tempFolder = PortletFileRepositoryUtil.getPortletFolder(
125                            userId, repositoryId, userFolder.getFolderId(), tempFolderName,
126                            serviceContext);
127    
128                    return tempFolder.getFolderId();
129            }
130    
131    }