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