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.portletfilerepository;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.ObjectValuePair;
026    import com.liferay.portal.kernel.util.OrderByComparator;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.kernel.util.UnmodifiableList;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.Repository;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
035    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
036    import com.liferay.portal.service.GroupLocalServiceUtil;
037    import com.liferay.portal.service.RepositoryLocalServiceUtil;
038    import com.liferay.portal.service.ServiceContext;
039    import com.liferay.portal.service.UserLocalServiceUtil;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
042    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
043    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
044    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
045    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
046    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
047    
048    import java.io.File;
049    import java.io.IOException;
050    import java.io.InputStream;
051    
052    import java.util.ArrayList;
053    import java.util.List;
054    
055    /**
056     * @author Eudaldo Alonso
057     * @author Alexander Chow
058     */
059    @DoPrivileged
060    public class PortletFileRepositoryImpl implements PortletFileRepository {
061    
062            public void addPortletFileEntries(
063                            long groupId, long userId, String className, long classPK,
064                            String portletId, long folderId,
065                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
066                    throws PortalException, SystemException {
067    
068                    for (int i = 0; i < inputStreamOVPs.size(); i++) {
069                            ObjectValuePair<String, InputStream> inputStreamOVP =
070                                    inputStreamOVPs.get(i);
071    
072                            InputStream inputStream = inputStreamOVP.getValue();
073                            String fileName = inputStreamOVP.getKey();
074    
075                            File file = null;
076    
077                            try {
078                                    file = FileUtil.createTempFile(inputStream);
079    
080                                    String mimeType = MimeTypesUtil.getContentType(file, fileName);
081    
082                                    addPortletFileEntry(
083                                            groupId, userId, className, classPK, portletId, folderId,
084                                            file, fileName, mimeType);
085                            }
086                            catch (IOException ioe) {
087                                    throw new SystemException(
088                                            "Unable to write temporary file", ioe);
089                            }
090                            finally {
091                                    FileUtil.delete(file);
092                            }
093                    }
094            }
095    
096            public FileEntry addPortletFileEntry(
097                            long groupId, long userId, String className, long classPK,
098                            String portletId, long folderId, File file, String fileName,
099                            String mimeType)
100                    throws PortalException, SystemException {
101    
102                    if (Validator.isNull(fileName)) {
103                            return null;
104                    }
105    
106                    ServiceContext serviceContext = new ServiceContext();
107    
108                    serviceContext.setAddGroupPermissions(true);
109                    serviceContext.setAddGuestPermissions(true);
110    
111                    Repository repository = addPortletRepository(
112                            groupId, portletId, serviceContext);
113    
114                    serviceContext.setAttribute("className", className);
115                    serviceContext.setAttribute("classPK", String.valueOf(classPK));
116    
117                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
118    
119                    try {
120                            DLAppHelperThreadLocal.setEnabled(false);
121    
122                            return DLAppLocalServiceUtil.addFileEntry(
123                                    userId, repository.getRepositoryId(), folderId, fileName,
124                                    mimeType, fileName, StringPool.BLANK, StringPool.BLANK, file,
125                                    serviceContext);
126                    }
127                    finally {
128                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
129                    }
130            }
131    
132            public FileEntry addPortletFileEntry(
133                            long groupId, long userId, String className, long classPK,
134                            String portletId, long folderId, InputStream inputStream,
135                            String fileName, String mimeType)
136                    throws PortalException, SystemException {
137    
138                    if (inputStream == null) {
139                            return null;
140                    }
141    
142                    byte[] bytes = null;
143    
144                    try {
145                            bytes = FileUtil.getBytes(inputStream, -1, false);
146                    }
147                    catch (IOException ioe) {
148                            return null;
149                    }
150    
151                    ServiceContext serviceContext = new ServiceContext();
152    
153                    serviceContext.setAddGroupPermissions(true);
154                    serviceContext.setAddGuestPermissions(true);
155    
156                    Repository repository = addPortletRepository(
157                            groupId, portletId, serviceContext);
158    
159                    serviceContext.setAttribute("className", className);
160                    serviceContext.setAttribute("classPK", String.valueOf(classPK));
161    
162                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
163    
164                    try {
165                            DLAppHelperThreadLocal.setEnabled(false);
166    
167                            return DLAppLocalServiceUtil.addFileEntry(
168                                    userId, repository.getRepositoryId(), folderId, fileName,
169                                    mimeType, fileName, StringPool.BLANK, StringPool.BLANK, bytes,
170                                    serviceContext);
171                    }
172                    finally {
173                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
174                    }
175            }
176    
177            public Folder addPortletFolder(
178                            long userId, long repositoryId, long parentFolderId,
179                            String folderName, ServiceContext serviceContext)
180                    throws PortalException, SystemException {
181    
182                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
183    
184                    try {
185                            DLAppHelperThreadLocal.setEnabled(false);
186    
187                            return DLAppLocalServiceUtil.getFolder(
188                                    repositoryId, parentFolderId, folderName);
189                    }
190                    catch (NoSuchFolderException nsfe) {
191                            return DLAppLocalServiceUtil.addFolder(
192                                    userId, repositoryId, parentFolderId, folderName,
193                                    StringPool.BLANK, serviceContext);
194                    }
195                    finally {
196                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
197                    }
198            }
199    
200            public Repository addPortletRepository(
201                            long groupId, String portletId, ServiceContext serviceContext)
202                    throws PortalException, SystemException {
203    
204                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
205                            groupId, portletId);
206    
207                    if (repository != null) {
208                            return repository;
209                    }
210    
211                    Group group = GroupLocalServiceUtil.getGroup(groupId);
212    
213                    User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
214    
215                    long classNameId = PortalUtil.getClassNameId(
216                            LiferayRepository.class.getName());
217    
218                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
219    
220                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
221    
222                    try {
223                            DLAppHelperThreadLocal.setEnabled(false);
224    
225                            return RepositoryLocalServiceUtil.addRepository(
226                                    user.getUserId(), groupId, classNameId,
227                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
228                                    StringPool.BLANK, portletId, typeSettingsProperties, true,
229                                    serviceContext);
230                    }
231                    finally {
232                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
233                    }
234            }
235    
236            public void deleteFolder(long folderId)
237                    throws PortalException, SystemException {
238    
239                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
240    
241                    try {
242                            DLAppHelperThreadLocal.setEnabled(false);
243    
244                            DLAppLocalServiceUtil.deleteFolder(folderId);
245                    }
246                    finally {
247                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
248                    }
249            }
250    
251            public void deletePortletFileEntries(long groupId, long folderId)
252                    throws PortalException, SystemException {
253    
254                    List<DLFileEntry> dlFileEntries =
255                            DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId);
256    
257                    for (DLFileEntry dlFileEntry : dlFileEntries) {
258                            deletePortletFileEntry(dlFileEntry.getFileEntryId());
259                    }
260            }
261    
262            public void deletePortletFileEntries(
263                            long groupId, long folderId, int status)
264                    throws PortalException, SystemException {
265    
266                    List<DLFileEntry> dlFileEntries =
267                            DLFileEntryLocalServiceUtil.getFileEntries(
268                                    groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
269                                    null);
270    
271                    for (DLFileEntry dlFileEntry : dlFileEntries) {
272                            deletePortletFileEntry(dlFileEntry.getFileEntryId());
273                    }
274            }
275    
276            public void deletePortletFileEntry(long fileEntryId)
277                    throws PortalException, SystemException {
278    
279                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
280    
281                    try {
282                            DLAppHelperThreadLocal.setEnabled(false);
283    
284                            DLAppLocalServiceUtil.deleteFileEntry(fileEntryId);
285                    }
286                    finally {
287                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
288                    }
289            }
290    
291            public void deletePortletFileEntry(
292                            long groupId, long folderId, String fileName)
293                    throws PortalException, SystemException {
294    
295                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
296                            groupId, folderId, fileName);
297    
298                    deletePortletFileEntry(fileEntry.getFileEntryId());
299            }
300    
301            public void deletePortletRepository(long groupId, String portletId)
302                    throws PortalException, SystemException {
303    
304                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
305                            groupId, portletId);
306    
307                    if (repository != null) {
308                            RepositoryLocalServiceUtil.deleteRepository(
309                                    repository.getRepositoryId());
310                    }
311            }
312    
313            public Repository fetchPortletRepository(long groupId, String portletId)
314                    throws SystemException {
315    
316                    return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
317            }
318    
319            public List<FileEntry> getPortletFileEntries(long groupId, long folderId)
320                    throws SystemException {
321    
322                    return toFileEntries(
323                            DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId));
324            }
325    
326            public List<FileEntry> getPortletFileEntries(
327                            long groupId, long folderId, int status)
328                    throws SystemException {
329    
330                    return getPortletFileEntries(
331                            groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
332                            null);
333            }
334    
335            public List<FileEntry> getPortletFileEntries(
336                            long groupId, long folderId, int status, int start, int end,
337                            OrderByComparator obc)
338                    throws SystemException {
339    
340                    return toFileEntries(
341                            DLFileEntryLocalServiceUtil.getFileEntries(
342                                    groupId, folderId, status, start, end, obc));
343            }
344    
345            public int getPortletFileEntriesCount(long groupId, long folderId)
346                    throws SystemException {
347    
348                    return DLFileEntryLocalServiceUtil.getFileEntriesCount(
349                            groupId, folderId);
350            }
351    
352            public int getPortletFileEntriesCount(
353                            long groupId, long folderId, int status)
354                    throws SystemException {
355    
356                    return DLFileEntryLocalServiceUtil.getFileEntriesCount(
357                            groupId, folderId, status);
358            }
359    
360            public FileEntry getPortletFileEntry(long fileEntryId)
361                    throws PortalException, SystemException {
362    
363                    return DLAppLocalServiceUtil.getFileEntry(fileEntryId);
364            }
365    
366            public FileEntry getPortletFileEntry(
367                            long groupId, long folderId, String fileName)
368                    throws PortalException, SystemException {
369    
370                    return DLAppLocalServiceUtil.getFileEntry(groupId, folderId, fileName);
371            }
372    
373            public Folder getPortletFolder(long folderId)
374                    throws PortalException, SystemException {
375    
376                    return DLAppLocalServiceUtil.getFolder(folderId);
377            }
378    
379            public Folder getPortletFolder(
380                            long repositoryId, long parentFolderId, String folderName)
381                    throws PortalException, SystemException {
382    
383                    return DLAppLocalServiceUtil.getFolder(
384                            repositoryId, parentFolderId, folderName);
385            }
386    
387            public Repository getPortletRepository(long groupId, String portletId)
388                    throws PortalException, SystemException {
389    
390                    return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
391            }
392    
393            public void movePortletFileEntryToTrash(long userId, long fileEntryId)
394                    throws PortalException, SystemException {
395    
396                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
397    
398                    try {
399                            DLAppHelperThreadLocal.setEnabled(false);
400    
401                            DLAppLocalServiceUtil.moveFileEntryToTrash(userId, fileEntryId);
402                    }
403                    finally {
404                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
405                    }
406            }
407    
408            public void movePortletFileEntryToTrash(
409                            long groupId, long userId, long folderId, String fileName)
410                    throws PortalException, SystemException {
411    
412                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
413                            groupId, folderId, fileName);
414    
415                    movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
416            }
417    
418            public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
419                    throws PortalException, SystemException {
420    
421                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
422    
423                    try {
424                            DLAppHelperThreadLocal.setEnabled(false);
425    
426                            DLAppLocalServiceUtil.restoreFileEntryFromTrash(
427                                    userId, fileEntryId);
428                    }
429                    finally {
430                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
431                    }
432            }
433    
434            public void restorePortletFileEntryFromTrash(
435                            long groupId, long userId, long folderId, String fileName)
436                    throws PortalException, SystemException {
437    
438                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
439                            groupId, folderId, fileName);
440    
441                    restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
442            }
443    
444            /**
445             * @see {@link
446             *      com.liferay.portal.repository.liferayrepository.util.LiferayBase#toFileEntries}
447             */
448            protected List<FileEntry> toFileEntries(List<DLFileEntry> dlFileEntries) {
449                    List<FileEntry> fileEntries = new ArrayList<FileEntry>(
450                            dlFileEntries.size());
451    
452                    for (DLFileEntry dlFileEntry : dlFileEntries) {
453                            FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
454    
455                            fileEntries.add(fileEntry);
456                    }
457    
458                    if (dlFileEntries instanceof UnmodifiableList) {
459                            return new UnmodifiableList<FileEntry>(fileEntries);
460                    }
461                    else {
462                            return fileEntries;
463                    }
464            }
465    
466    }