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