001    /**
002     * Copyright (c) 2000-present 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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.repository.InvalidRepositoryIdException;
023    import com.liferay.portal.kernel.repository.LocalRepository;
024    import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
025    import com.liferay.portal.kernel.repository.model.FileEntry;
026    import com.liferay.portal.kernel.repository.model.Folder;
027    import com.liferay.portal.kernel.repository.util.RepositoryTrashUtil;
028    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
029    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntryThreadLocal;
030    import com.liferay.portal.kernel.util.ContentTypes;
031    import com.liferay.portal.kernel.util.FileUtil;
032    import com.liferay.portal.kernel.util.HtmlUtil;
033    import com.liferay.portal.kernel.util.HttpUtil;
034    import com.liferay.portal.kernel.util.MimeTypesUtil;
035    import com.liferay.portal.kernel.util.ObjectValuePair;
036    import com.liferay.portal.kernel.util.OrderByComparator;
037    import com.liferay.portal.kernel.util.StringBundler;
038    import com.liferay.portal.kernel.util.StringPool;
039    import com.liferay.portal.kernel.util.UnicodeProperties;
040    import com.liferay.portal.kernel.util.Validator;
041    import com.liferay.portal.model.Group;
042    import com.liferay.portal.model.Repository;
043    import com.liferay.portal.model.User;
044    import com.liferay.portal.repository.portletrepository.PortletRepository;
045    import com.liferay.portal.service.GroupLocalServiceUtil;
046    import com.liferay.portal.service.RepositoryLocalServiceUtil;
047    import com.liferay.portal.service.ServiceContext;
048    import com.liferay.portal.service.UserLocalServiceUtil;
049    import com.liferay.portal.theme.ThemeDisplay;
050    import com.liferay.portal.util.PortalUtil;
051    import com.liferay.portal.webserver.WebServerServlet;
052    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
053    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
054    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
055    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
056    import com.liferay.portlet.trash.util.TrashUtil;
057    
058    import java.io.File;
059    import java.io.IOException;
060    import java.io.InputStream;
061    
062    import java.util.List;
063    
064    /**
065     * @author Eudaldo Alonso
066     * @author Alexander Chow
067     */
068    @DoPrivileged
069    public class PortletFileRepositoryImpl implements PortletFileRepository {
070    
071            @Override
072            public void addPortletFileEntries(
073                            long groupId, long userId, String className, long classPK,
074                            String portletId, long folderId,
075                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
076                    throws PortalException {
077    
078                    for (int i = 0; i < inputStreamOVPs.size(); i++) {
079                            ObjectValuePair<String, InputStream> inputStreamOVP =
080                                    inputStreamOVPs.get(i);
081    
082                            InputStream inputStream = inputStreamOVP.getValue();
083                            String fileName = inputStreamOVP.getKey();
084    
085                            addPortletFileEntry(
086                                    groupId, userId, className, classPK, portletId, folderId,
087                                    inputStream, fileName, StringPool.BLANK, true);
088                    }
089            }
090    
091            @Override
092            public FileEntry addPortletFileEntry(
093                            long groupId, long userId, String className, long classPK,
094                            String portletId, long folderId, File file, String fileName,
095                            String mimeType, boolean indexingEnabled)
096                    throws PortalException {
097    
098                    if (Validator.isNull(fileName)) {
099                            return null;
100                    }
101    
102                    ServiceContext serviceContext = new ServiceContext();
103    
104                    serviceContext.setAddGroupPermissions(true);
105                    serviceContext.setAddGuestPermissions(true);
106    
107                    Repository repository = addPortletRepository(
108                            groupId, portletId, serviceContext);
109    
110                    serviceContext.setAttribute("className", className);
111                    serviceContext.setAttribute("classPK", String.valueOf(classPK));
112                    serviceContext.setIndexingEnabled(indexingEnabled);
113    
114                    if (Validator.isNull(mimeType) ||
115                            mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
116    
117                            mimeType = MimeTypesUtil.getContentType(file, fileName);
118                    }
119    
120                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
121    
122                    try {
123                            DLAppHelperThreadLocal.setEnabled(false);
124    
125                            LocalRepository localRepository =
126                                    RepositoryProviderUtil.getLocalRepository(
127                                            repository.getRepositoryId());
128    
129                            return localRepository.addFileEntry(
130                                    userId, folderId, fileName, mimeType, fileName,
131                                    StringPool.BLANK, StringPool.BLANK, file, serviceContext);
132                    }
133                    finally {
134                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
135                    }
136            }
137    
138            @Override
139            public FileEntry addPortletFileEntry(
140                            long groupId, long userId, String className, long classPK,
141                            String portletId, long folderId, InputStream inputStream,
142                            String fileName, String mimeType, boolean indexingEnabled)
143                    throws PortalException {
144    
145                    if (inputStream == null) {
146                            return null;
147                    }
148    
149                    File file = null;
150    
151                    try {
152                            file = FileUtil.createTempFile(inputStream);
153    
154                            return addPortletFileEntry(
155                                    groupId, userId, className, classPK, portletId, folderId, file,
156                                    fileName, mimeType, indexingEnabled);
157                    }
158                    catch (IOException ioe) {
159                            throw new SystemException("Unable to write temporary file", ioe);
160                    }
161                    finally {
162                            FileUtil.delete(file);
163                    }
164            }
165    
166            @Override
167            public Folder addPortletFolder(
168                            long userId, long repositoryId, long parentFolderId,
169                            String folderName, ServiceContext serviceContext)
170                    throws PortalException {
171    
172                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
173    
174                    LocalRepository localRepository =
175                            RepositoryProviderUtil.getLocalRepository(repositoryId);
176    
177                    try {
178                            DLAppHelperThreadLocal.setEnabled(false);
179    
180                            return localRepository.getFolder(parentFolderId, folderName);
181                    }
182                    catch (NoSuchFolderException nsfe) {
183                            return localRepository.addFolder(
184                                    userId, parentFolderId, folderName, StringPool.BLANK,
185                                    serviceContext);
186                    }
187                    finally {
188                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
189                    }
190            }
191    
192            @Override
193            public Folder addPortletFolder(
194                            long groupId, long userId, String portletId, long parentFolderId,
195                            String folderName, ServiceContext serviceContext)
196                    throws PortalException {
197    
198                    Repository repository = addPortletRepository(
199                            groupId, portletId, serviceContext);
200    
201                    return addPortletFolder(
202                            userId, repository.getRepositoryId(), parentFolderId, folderName,
203                            serviceContext);
204            }
205    
206            @Override
207            public Repository addPortletRepository(
208                            long groupId, String portletId, ServiceContext serviceContext)
209                    throws PortalException {
210    
211                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
212                            groupId, portletId);
213    
214                    if (repository != null) {
215                            return repository;
216                    }
217    
218                    Group group = GroupLocalServiceUtil.getGroup(groupId);
219    
220                    User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
221    
222                    long classNameId = PortalUtil.getClassNameId(
223                            PortletRepository.class.getName());
224    
225                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
226    
227                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
228    
229                    try {
230                            DLAppHelperThreadLocal.setEnabled(false);
231    
232                            return RepositoryLocalServiceUtil.addRepository(
233                                    user.getUserId(), groupId, classNameId,
234                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
235                                    StringPool.BLANK, portletId, typeSettingsProperties, true,
236                                    serviceContext);
237                    }
238                    finally {
239                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
240                    }
241            }
242    
243            /**
244             * @deprecated As of 7.0.0, replaced by {@link #deletePortletFolder}
245             */
246            @Deprecated
247            @Override
248            public void deleteFolder(long folderId) throws PortalException {
249                    deletePortletFolder(folderId);
250            }
251    
252            @Override
253            public void deletePortletFileEntries(long groupId, long folderId)
254                    throws PortalException {
255    
256                    LocalRepository localRepository =
257                            RepositoryProviderUtil.getLocalRepository(groupId);
258    
259                    List<FileEntry> fileEntries = localRepository.getFileEntries(
260                            folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
261    
262                    for (FileEntry fileEntry : fileEntries) {
263                            deletePortletFileEntry(fileEntry.getFileEntryId());
264                    }
265            }
266    
267            @Override
268            public void deletePortletFileEntries(
269                            long groupId, long folderId, int status)
270                    throws PortalException {
271    
272                    LocalRepository localRepository =
273                            RepositoryProviderUtil.getLocalRepository(groupId);
274    
275                    List<FileEntry> fileEntries = localRepository.getFileEntries(
276                            folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
277    
278                    for (FileEntry fileEntry : fileEntries) {
279                            deletePortletFileEntry(fileEntry.getFileEntryId());
280                    }
281            }
282    
283            @Override
284            public void deletePortletFileEntry(long fileEntryId)
285                    throws PortalException {
286    
287                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
288    
289                    try {
290                            DLAppHelperThreadLocal.setEnabled(false);
291    
292                            SystemEventHierarchyEntryThreadLocal.push(FileEntry.class);
293    
294                            LocalRepository localRepository =
295                                    RepositoryProviderUtil.getFileEntryLocalRepository(fileEntryId);
296    
297                            localRepository.deleteFileEntry(fileEntryId);
298                    }
299                    catch (InvalidRepositoryIdException irie) {
300                            if (_log.isWarnEnabled()) {
301                                    _log.warn(irie, irie);
302                            }
303                    }
304                    finally {
305                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
306    
307                            SystemEventHierarchyEntryThreadLocal.pop(FileEntry.class);
308                    }
309            }
310    
311            @Override
312            public void deletePortletFileEntry(
313                            long groupId, long folderId, String fileName)
314                    throws PortalException {
315    
316                    LocalRepository localRepository =
317                            RepositoryProviderUtil.getLocalRepository(groupId);
318    
319                    FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
320    
321                    deletePortletFileEntry(fileEntry.getFileEntryId());
322            }
323    
324            @Override
325            public void deletePortletFolder(long folderId) throws PortalException {
326                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
327    
328                    try {
329                            DLAppHelperThreadLocal.setEnabled(false);
330    
331                            SystemEventHierarchyEntryThreadLocal.push(Folder.class);
332    
333                            LocalRepository localRepository =
334                                    RepositoryProviderUtil.getFolderLocalRepository(folderId);
335    
336                            localRepository.deleteFolder(folderId);
337                    }
338                    catch (InvalidRepositoryIdException irie) {
339                            if (_log.isWarnEnabled()) {
340                                    _log.warn(irie, irie);
341                            }
342                    }
343                    finally {
344                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
345    
346                            SystemEventHierarchyEntryThreadLocal.pop(Folder.class);
347                    }
348            }
349    
350            @Override
351            public void deletePortletRepository(long groupId, String portletId)
352                    throws PortalException {
353    
354                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
355                            groupId, portletId);
356    
357                    if (repository != null) {
358                            RepositoryLocalServiceUtil.deleteRepository(
359                                    repository.getRepositoryId());
360                    }
361            }
362    
363            @Override
364            public Repository fetchPortletRepository(long groupId, String portletId) {
365                    return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
366            }
367    
368            @Override
369            public String getDownloadPortletFileEntryURL(
370                    ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString) {
371    
372                    return getDownloadPortletFileEntryURL(
373                            themeDisplay, fileEntry, queryString, true);
374            }
375    
376            @Override
377            public String getDownloadPortletFileEntryURL(
378                    ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString,
379                    boolean absoluteURL) {
380    
381                    String portletFileEntryURL = getPortletFileEntryURL(
382                            themeDisplay, fileEntry, queryString, absoluteURL);
383    
384                    return HttpUtil.addParameter(portletFileEntryURL, "download", true);
385            }
386    
387            @Override
388            public List<FileEntry> getPortletFileEntries(long groupId, long folderId)
389                    throws PortalException {
390    
391                    LocalRepository localRepository =
392                            RepositoryProviderUtil.getLocalRepository(groupId);
393    
394                    return localRepository.getFileEntries(
395                            folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
396            }
397    
398            @Override
399            public List<FileEntry> getPortletFileEntries(
400                            long groupId, long folderId, int status)
401                    throws PortalException {
402    
403                    return getPortletFileEntries(
404                            groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
405                            null);
406            }
407    
408            @Override
409            public List<FileEntry> getPortletFileEntries(
410                            long groupId, long folderId, int status, int start, int end,
411                            OrderByComparator<FileEntry> obc)
412                    throws PortalException {
413    
414                    LocalRepository localRepository =
415                            RepositoryProviderUtil.getLocalRepository(groupId);
416    
417                    return localRepository.getFileEntries(
418                            folderId, status, start, end, obc);
419            }
420    
421            @Override
422            public List<FileEntry> getPortletFileEntries(
423                            long groupId, long folderId, OrderByComparator<FileEntry> obc)
424                    throws PortalException {
425    
426                    LocalRepository localRepository =
427                            RepositoryProviderUtil.getLocalRepository(groupId);
428    
429                    return localRepository.getFileEntries(
430                            folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, obc);
431            }
432    
433            @Override
434            public int getPortletFileEntriesCount(long groupId, long folderId)
435                    throws PortalException {
436    
437                    LocalRepository localRepository =
438                            RepositoryProviderUtil.getLocalRepository(groupId);
439    
440                    return localRepository.getFileEntriesCount(folderId);
441            }
442    
443            @Override
444            public int getPortletFileEntriesCount(
445                            long groupId, long folderId, int status)
446                    throws PortalException {
447    
448                    LocalRepository localRepository =
449                            RepositoryProviderUtil.getLocalRepository(groupId);
450    
451                    return localRepository.getFileEntriesCount(folderId, status);
452            }
453    
454            @Override
455            public FileEntry getPortletFileEntry(long fileEntryId)
456                    throws PortalException {
457    
458                    try {
459                            LocalRepository localRepository =
460                                    RepositoryProviderUtil.getFileEntryLocalRepository(fileEntryId);
461    
462                            return localRepository.getFileEntry(fileEntryId);
463                    }
464                    catch (InvalidRepositoryIdException irid) {
465                            throw new NoSuchFileEntryException(irid);
466                    }
467            }
468    
469            @Override
470            public FileEntry getPortletFileEntry(
471                            long groupId, long folderId, String fileName)
472                    throws PortalException {
473    
474                    LocalRepository localRepository =
475                            RepositoryProviderUtil.getLocalRepository(groupId);
476    
477                    return localRepository.getFileEntry(folderId, fileName);
478            }
479    
480            @Override
481            public FileEntry getPortletFileEntry(String uuid, long groupId)
482                    throws PortalException {
483    
484                    LocalRepository localRepository =
485                            RepositoryProviderUtil.getLocalRepository(groupId);
486    
487                    return localRepository.getFileEntryByUuid(uuid);
488            }
489    
490            @Override
491            public String getPortletFileEntryURL(
492                    ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString) {
493    
494                    return getPortletFileEntryURL(
495                            themeDisplay, fileEntry, queryString, true);
496            }
497    
498            @Override
499            public String getPortletFileEntryURL(
500                    ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString,
501                    boolean absoluteURL) {
502    
503                    StringBundler sb = new StringBundler(10);
504    
505                    if (themeDisplay != null) {
506                            if (absoluteURL) {
507                                    sb.append(themeDisplay.getPortalURL());
508                            }
509                    }
510    
511                    sb.append(PortalUtil.getPathContext());
512                    sb.append("/documents/");
513                    sb.append(WebServerServlet.PATH_PORTLET_FILE_ENTRY);
514                    sb.append(StringPool.SLASH);
515                    sb.append(fileEntry.getGroupId());
516                    sb.append(StringPool.SLASH);
517    
518                    String title = fileEntry.getTitle();
519    
520                    if (fileEntry.isInTrash()) {
521                            title = TrashUtil.getOriginalTitle(fileEntry.getTitle());
522                    }
523    
524                    sb.append(HttpUtil.encodeURL(HtmlUtil.unescape(title)));
525    
526                    sb.append(StringPool.SLASH);
527                    sb.append(HttpUtil.encodeURL(fileEntry.getUuid()));
528    
529                    if (Validator.isNotNull(queryString)) {
530                            sb.append(StringPool.QUESTION);
531                            sb.append(queryString);
532                    }
533    
534                    String portletFileEntryURL = sb.toString();
535    
536                    if ((themeDisplay != null) && themeDisplay.isAddSessionIdToURL()) {
537                            return PortalUtil.getURLWithSessionId(
538                                    portletFileEntryURL, themeDisplay.getSessionId());
539                    }
540    
541                    return portletFileEntryURL;
542            }
543    
544            @Override
545            public Folder getPortletFolder(long folderId) throws PortalException {
546                    try {
547                            LocalRepository localRepository =
548                                    RepositoryProviderUtil.getFolderLocalRepository(folderId);
549    
550                            return localRepository.getFolder(folderId);
551                    }
552                    catch (InvalidRepositoryIdException irid) {
553                            throw new NoSuchFolderException(irid);
554                    }
555            }
556    
557            @Override
558            public Folder getPortletFolder(
559                            long repositoryId, long parentFolderId, String folderName)
560                    throws PortalException {
561    
562                    LocalRepository localRepository =
563                            RepositoryProviderUtil.getLocalRepository(repositoryId);
564    
565                    return localRepository.getFolder(parentFolderId, folderName);
566            }
567    
568            @Override
569            public Repository getPortletRepository(long groupId, String portletId)
570                    throws PortalException {
571    
572                    return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
573            }
574    
575            @Override
576            public FileEntry movePortletFileEntryToTrash(long userId, long fileEntryId)
577                    throws PortalException {
578    
579                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
580    
581                    try {
582                            DLAppHelperThreadLocal.setEnabled(false);
583    
584                            LocalRepository localRepository =
585                                    RepositoryProviderUtil.getFileEntryLocalRepository(fileEntryId);
586    
587                            return RepositoryTrashUtil.moveFileEntryToTrash(
588                                    userId, localRepository.getRepositoryId(), fileEntryId);
589                    }
590                    finally {
591                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
592                    }
593            }
594    
595            @Override
596            public FileEntry movePortletFileEntryToTrash(
597                            long groupId, long userId, long folderId, String fileName)
598                    throws PortalException {
599    
600                    LocalRepository localRepository =
601                            RepositoryProviderUtil.getLocalRepository(groupId);
602    
603                    FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
604    
605                    return movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
606            }
607    
608            @Override
609            public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
610                    throws PortalException {
611    
612                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
613    
614                    try {
615                            DLAppHelperThreadLocal.setEnabled(false);
616    
617                            LocalRepository localRepository =
618                                    RepositoryProviderUtil.getFileEntryLocalRepository(fileEntryId);
619    
620                            RepositoryTrashUtil.restoreFileEntryFromTrash(
621                                    userId, localRepository.getRepositoryId(), fileEntryId);
622                    }
623                    finally {
624                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
625                    }
626            }
627    
628            @Override
629            public void restorePortletFileEntryFromTrash(
630                            long groupId, long userId, long folderId, String fileName)
631                    throws PortalException {
632    
633                    LocalRepository localRepository =
634                            RepositoryProviderUtil.getLocalRepository(groupId);
635    
636                    FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
637    
638                    restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
639            }
640    
641            private static final Log _log = LogFactoryUtil.getLog(
642                    PortletFileRepositoryImpl.class);
643    
644    }