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