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.kernel.repository;
016    
017    import com.liferay.portal.exception.NoSuchRepositoryEntryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.capabilities.Capability;
021    import com.liferay.portal.kernel.repository.capabilities.CapabilityProvider;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.repository.search.RepositorySearchQueryBuilderUtil;
025    import com.liferay.portal.kernel.search.BooleanQuery;
026    import com.liferay.portal.kernel.search.Hits;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.SearchEngineHelper;
029    import com.liferay.portal.kernel.search.SearchException;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.UnicodeProperties;
033    import com.liferay.portal.model.RepositoryEntry;
034    import com.liferay.portal.security.auth.PrincipalThreadLocal;
035    import com.liferay.portal.service.CompanyLocalService;
036    import com.liferay.portal.service.RepositoryEntryLocalService;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.UserLocalService;
039    import com.liferay.portal.service.persistence.RepositoryEntryUtil;
040    import com.liferay.portlet.asset.service.AssetEntryLocalService;
041    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
042    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
043    import com.liferay.portlet.documentlibrary.util.DL;
044    
045    import java.io.File;
046    import java.io.FileInputStream;
047    import java.io.IOException;
048    import java.io.InputStream;
049    
050    import java.util.List;
051    
052    /**
053     * Third-party repository implementations should extend from this class.
054     *
055     * @author Alexander Chow
056     */
057    public abstract class BaseRepositoryImpl
058            implements BaseRepository, CapabilityProvider {
059    
060            @Override
061            public FileEntry addFileEntry(
062                            long userId, long folderId, String sourceFileName, String mimeType,
063                            String title, String description, String changeLog, File file,
064                            ServiceContext serviceContext)
065                    throws PortalException {
066    
067                    InputStream is = null;
068                    long size = 0;
069    
070                    try {
071                            is = new FileInputStream(file);
072                            size = file.length();
073    
074                            return addFileEntry(
075                                    userId, folderId, sourceFileName, mimeType, title, description,
076                                    changeLog, is, size, serviceContext);
077                    }
078                    catch (IOException ioe) {
079                            throw new SystemException(ioe);
080                    }
081                    finally {
082                            if (is != null) {
083                                    try {
084                                            is.close();
085                                    }
086                                    catch (IOException ioe) {
087                                    }
088                            }
089                    }
090            }
091    
092            /**
093             * @deprecated As of 7.0.0, see {@link #addFileEntry(long, long, String,
094             *             String, String, String, String, File, ServiceContext)}
095             */
096            @Deprecated
097            @Override
098            public FileEntry addFileEntry(
099                            long folderId, String sourceFileName, String mimeType, String title,
100                            String description, String changeLog, File file,
101                            ServiceContext serviceContext)
102                    throws PortalException {
103    
104                    return addFileEntry(
105                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
106                                    getUserId(),
107                            folderId, sourceFileName, mimeType, title, description, changeLog,
108                            file, serviceContext);
109            }
110    
111            /**
112             * @deprecated As of 7.0.0, see {@link #addFileEntry(long, long, String,
113             *             String, String, String, String, InputStream, long,
114             *             ServiceContext)}
115             */
116            @Deprecated
117            @Override
118            public FileEntry addFileEntry(
119                            long folderId, String sourceFileName, String mimeType, String title,
120                            String description, String changeLog, InputStream is, long size,
121                            ServiceContext serviceContext)
122                    throws PortalException {
123    
124                    return addFileEntry(
125                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
126                                    getUserId(),
127                            sourceFileName, mimeType, title, description, changeLog, is, size,
128                            serviceContext);
129            }
130    
131            @Override
132            public abstract Folder addFolder(
133                            long userId, long parentFolderId, String name, String description,
134                            ServiceContext serviceContext)
135                    throws PortalException;
136    
137            /**
138             * @deprecated As of 7.0.0, replaced by {@link #addFolder(long, long,
139             *             String, String, ServiceContext)}
140             */
141            @Deprecated
142            @Override
143            public Folder addFolder(
144                            long parentFolderId, String name, String description,
145                            ServiceContext serviceContext)
146                    throws PortalException {
147    
148                    return addFolder(
149                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
150                                    getUserId(),
151                            name, description, serviceContext);
152            }
153    
154            /**
155             * @deprecated As of 7.0.0, replaced by {@link #checkInFileEntry(long, long,
156             *             boolean, String, ServiceContext)}
157             */
158            @Deprecated
159            @Override
160            public void checkInFileEntry(
161                            long fileEntryId, boolean major, String changeLog,
162                            ServiceContext serviceContext)
163                    throws PortalException {
164    
165                    checkInFileEntry(
166                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
167                                    getUserId(),
168                            fileEntryId, major, changeLog, serviceContext);
169            }
170    
171            /**
172             * @deprecated As of 6.2.0, replaced by {@link #checkInFileEntry(long,
173             *             String, ServiceContext)}
174             */
175            @Deprecated
176            @Override
177            public void checkInFileEntry(long fileEntryId, String lockUuid)
178                    throws PortalException {
179    
180                    checkInFileEntry(fileEntryId, lockUuid, new ServiceContext());
181            }
182    
183            /**
184             * @deprecated As of 7.0.0, replaced by {@link #checkInFileEntry(long, long,
185             *             String, ServiceContext)}
186             */
187            @Deprecated
188            @Override
189            public void checkInFileEntry(
190                            long fileEntryId, String lockUuid, ServiceContext serviceContext)
191                    throws PortalException {
192    
193                    checkInFileEntry(
194                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
195                                    getUserId(),
196                            fileEntryId, lockUuid, serviceContext);
197            }
198    
199            @Override
200            public abstract FileEntry checkOutFileEntry(
201                            long fileEntryId, ServiceContext serviceContext)
202                    throws PortalException;
203    
204            @Override
205            public abstract FileEntry checkOutFileEntry(
206                            long fileEntryId, String owner, long expirationTime,
207                            ServiceContext serviceContext)
208                    throws PortalException;
209    
210            /**
211             * @deprecated As of 7.0.0, replaced by {@link #copyFileEntry(long, long,
212             *             long, long, ServiceContext)}
213             */
214            @Deprecated
215            @Override
216            public FileEntry copyFileEntry(
217                            long groupId, long fileEntryId, long destFolderId,
218                            ServiceContext serviceContext)
219                    throws PortalException {
220    
221                    return copyFileEntry(
222                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
223                                    getUserId(),
224                            groupId, fileEntryId, destFolderId, serviceContext);
225            }
226    
227            @Override
228            public void deleteAll() {
229                    throw new UnsupportedOperationException();
230            }
231    
232            @Override
233            public void deleteFileEntry(long folderId, String title)
234                    throws PortalException {
235    
236                    FileEntry fileEntry = getFileEntry(folderId, title);
237    
238                    deleteFileEntry(fileEntry.getFileEntryId());
239            }
240    
241            @Override
242            public void deleteFileVersion(long fileEntryId, String version) {
243                    throw new UnsupportedOperationException();
244            }
245    
246            @Override
247            public void deleteFolder(long parentFolderId, String name)
248                    throws PortalException {
249    
250                    Folder folder = getFolder(parentFolderId, name);
251    
252                    deleteFolder(folder.getFolderId());
253            }
254    
255            @Override
256            public <T extends Capability> T getCapability(Class<T> capabilityClass) {
257                    throw new IllegalArgumentException(
258                            String.format(
259                                    "Capability %s is not supported by repository %s",
260                                    capabilityClass.getName(), getRepositoryId()));
261            }
262    
263            public long getCompanyId() {
264                    return _companyId;
265            }
266    
267            @Override
268            @SuppressWarnings("rawtypes")
269            public List<com.liferay.portal.kernel.repository.model.RepositoryEntry>
270                            getFileEntriesAndFileShortcuts(
271                                    long folderId, int status, int start, int end)
272                    throws PortalException {
273    
274                    return (List)getFileEntries(folderId, start, end, null);
275            }
276    
277            @Override
278            public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
279                    throws PortalException {
280    
281                    return getFileEntriesCount(folderId);
282            }
283    
284            @Override
285            public int getFileEntriesAndFileShortcutsCount(
286                            long folderId, int status, String[] mimeTypes)
287                    throws PortalException {
288    
289                    return getFileEntriesCount(folderId, mimeTypes);
290            }
291    
292            @Override
293            public List<Folder> getFolders(
294                            long parentFolderId, int status, boolean includeMountfolders,
295                            int start, int end, OrderByComparator<Folder> obc)
296                    throws PortalException {
297    
298                    return getFolders(parentFolderId, includeMountfolders, start, end, obc);
299            }
300    
301            public abstract List<Object> getFoldersAndFileEntries(
302                    long folderId, int start, int end, OrderByComparator<?> obc);
303    
304            public abstract List<Object> getFoldersAndFileEntries(
305                            long folderId, String[] mimeTypes, int start, int end,
306                            OrderByComparator<?> obc)
307                    throws PortalException;
308    
309            @Override
310            @SuppressWarnings("rawtypes")
311            public List<com.liferay.portal.kernel.repository.model.RepositoryEntry>
312                    getFoldersAndFileEntriesAndFileShortcuts(
313                            long folderId, int status, boolean includeMountFolders, int start,
314                            int end, OrderByComparator<?> obc) {
315    
316                    return (List)getFoldersAndFileEntries(folderId, start, end, obc);
317            }
318    
319            @Override
320            @SuppressWarnings("rawtypes")
321            public List<com.liferay.portal.kernel.repository.model.RepositoryEntry>
322                            getFoldersAndFileEntriesAndFileShortcuts(
323                                    long folderId, int status, String[] mimeTypes,
324                                    boolean includeMountFolders, int start, int end,
325                                    OrderByComparator<?> obc)
326                    throws PortalException {
327    
328                    return (List)getFoldersAndFileEntries(
329                            folderId, mimeTypes, start, end, obc);
330            }
331    
332            @Override
333            public int getFoldersAndFileEntriesAndFileShortcutsCount(
334                    long folderId, int status, boolean includeMountFolders) {
335    
336                    return getFoldersAndFileEntriesCount(folderId);
337            }
338    
339            @Override
340            public int getFoldersAndFileEntriesAndFileShortcutsCount(
341                            long folderId, int status, String[] mimeTypes,
342                            boolean includeMountFolders)
343                    throws PortalException {
344    
345                    return getFoldersAndFileEntriesCount(folderId, mimeTypes);
346            }
347    
348            public abstract int getFoldersAndFileEntriesCount(long folderId);
349    
350            public abstract int getFoldersAndFileEntriesCount(
351                            long folderId, String[] mimeTypes)
352                    throws PortalException;
353    
354            @Override
355            public int getFoldersCount(
356                            long parentFolderId, int status, boolean includeMountfolders)
357                    throws PortalException {
358    
359                    return getFoldersCount(parentFolderId, includeMountfolders);
360            }
361    
362            public long getGroupId() {
363                    return _groupId;
364            }
365    
366            @Override
367            public LocalRepository getLocalRepository() {
368                    return _localRepository;
369            }
370    
371            /**
372             * @deprecated As of 7.0.0, replaced by {@link #getRepositoryEntry(String)}
373             */
374            @Deprecated
375            public Object[] getRepositoryEntryIds(String objectId)
376                    throws PortalException {
377    
378                    RepositoryEntry repositoryEntry =
379                            repositoryEntryLocalService.getRepositoryEntry(
380                                    PrincipalThreadLocal.getUserId(), getGroupId(),
381                                    getRepositoryId(), objectId);
382    
383                    return new Object[] {
384                            repositoryEntry.getRepositoryEntryId(), repositoryEntry.getUuid(),
385                            false
386                    };
387            }
388    
389            @Override
390            public List<FileEntry> getRepositoryFileEntries(
391                            long userId, long rootFolderId, int start, int end,
392                            OrderByComparator<FileEntry> obc)
393                    throws PortalException {
394    
395                    return getFileEntries(rootFolderId, start, end, obc);
396            }
397    
398            @Override
399            public List<FileEntry> getRepositoryFileEntries(
400                            long userId, long rootFolderId, String[] mimeTypes, int status,
401                            int start, int end, OrderByComparator<FileEntry> obc)
402                    throws PortalException {
403    
404                    return getFileEntries(rootFolderId, mimeTypes, start, end, obc);
405            }
406    
407            @Override
408            public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
409                    throws PortalException {
410    
411                    return getFileEntriesCount(rootFolderId);
412            }
413    
414            @Override
415            public int getRepositoryFileEntriesCount(
416                            long userId, long rootFolderId, String[] mimeTypes, int status)
417                    throws PortalException {
418    
419                    return getFileEntriesCount(rootFolderId, mimeTypes);
420            }
421    
422            @Override
423            public long getRepositoryId() {
424                    return _repositoryId;
425            }
426    
427            @Deprecated
428            @Override
429            public String[] getSupportedConfigurations() {
430                    return _SUPPORTED_CONFIGURATIONS;
431            }
432    
433            @Deprecated
434            @Override
435            public String[][] getSupportedParameters() {
436                    return _SUPPORTED_PARAMETERS;
437            }
438    
439            public UnicodeProperties getTypeSettingsProperties() {
440                    return _typeSettingsProperties;
441            }
442    
443            @Override
444            public abstract void initRepository() throws PortalException;
445    
446            @Override
447            public <T extends Capability> boolean isCapabilityProvided(
448                    Class<T> capabilityClass) {
449    
450                    return false;
451            }
452    
453            /**
454             * @deprecated As of 7.0.0, replaced by {@link #moveFileEntry(long, long,
455             *             long, ServiceContext)}
456             */
457            @Deprecated
458            @Override
459            public FileEntry moveFileEntry(
460                            long fileEntryId, long newFolderId, ServiceContext serviceContext)
461                    throws PortalException {
462    
463                    return moveFileEntry(
464                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
465                                    getUserId(),
466                            fileEntryId, newFolderId, serviceContext);
467            }
468    
469            /**
470             * @deprecated As of 7.0.0, replaced by {@link #moveFolder(long, long, long,
471             *             ServiceContext)}
472             */
473            @Deprecated
474            @Override
475            public Folder moveFolder(
476                            long folderId, long newParentFolderId,
477                            ServiceContext serviceContext)
478                    throws PortalException {
479    
480                    return moveFolder(
481                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
482                                    getUserId(),
483                            folderId, newParentFolderId, serviceContext);
484            }
485    
486            /**
487             * @deprecated As of 7.0.0, replaced by {@link #revertFileEntry(long, long,
488             *             String, ServiceContext)}
489             */
490            @Deprecated
491            @Override
492            public void revertFileEntry(
493                            long fileEntryId, String version, ServiceContext serviceContext)
494                    throws PortalException {
495    
496                    revertFileEntry(
497                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
498                                    getUserId(),
499                            fileEntryId, version, serviceContext);
500            }
501    
502            @Override
503            public Hits search(SearchContext searchContext) throws SearchException {
504                    searchContext.setSearchEngineId(SearchEngineHelper.GENERIC_ENGINE_ID);
505    
506                    BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
507                            searchContext);
508    
509                    return search(searchContext, fullQuery);
510            }
511    
512            @Override
513            public void setAssetEntryLocalService(
514                    AssetEntryLocalService assetEntryLocalService) {
515    
516                    this.assetEntryLocalService = assetEntryLocalService;
517            }
518    
519            @Override
520            public void setCompanyId(long companyId) {
521                    _companyId = companyId;
522            }
523    
524            @Override
525            public void setCompanyLocalService(
526                    CompanyLocalService companyLocalService) {
527    
528                    this.companyLocalService = companyLocalService;
529            }
530    
531            @Override
532            public void setDLAppHelperLocalService(
533                    DLAppHelperLocalService dlAppHelperLocalService) {
534    
535                    this.dlAppHelperLocalService = dlAppHelperLocalService;
536            }
537    
538            @Override
539            public void setDLFolderLocalService(
540                    DLFolderLocalService dlFolderLocalService) {
541    
542                    this.dlFolderLocalService = dlFolderLocalService;
543            }
544    
545            @Override
546            public void setGroupId(long groupId) {
547                    _groupId = groupId;
548            }
549    
550            @Override
551            public void setRepositoryEntryLocalService(
552                    RepositoryEntryLocalService repositoryEntryLocalService) {
553    
554                    this.repositoryEntryLocalService = repositoryEntryLocalService;
555            }
556    
557            @Override
558            public void setRepositoryId(long repositoryId) {
559                    _repositoryId = repositoryId;
560            }
561    
562            @Override
563            public void setTypeSettingsProperties(
564                    UnicodeProperties typeSettingsProperties) {
565    
566                    _typeSettingsProperties = typeSettingsProperties;
567            }
568    
569            @Override
570            public void setUserLocalService(UserLocalService userLocalService) {
571                    this.userLocalService = userLocalService;
572            }
573    
574            @Override
575            public void unlockFolder(long parentFolderId, String name, String lockUuid)
576                    throws PortalException {
577    
578                    Folder folder = getFolder(parentFolderId, name);
579    
580                    unlockFolder(folder.getFolderId(), lockUuid);
581            }
582    
583            @Override
584            public FileEntry updateFileEntry(
585                            long userId, long fileEntryId, String sourceFileName,
586                            String mimeType, String title, String description, String changeLog,
587                            boolean majorVersion, File file, ServiceContext serviceContext)
588                    throws PortalException {
589    
590                    InputStream is = null;
591                    long size = 0;
592    
593                    try {
594                            is = new FileInputStream(file);
595                            size = file.length();
596    
597                            return updateFileEntry(
598                                    userId, fileEntryId, sourceFileName, mimeType, title,
599                                    description, changeLog, majorVersion, is, size, serviceContext);
600                    }
601                    catch (IOException ioe) {
602                            throw new SystemException(ioe);
603                    }
604                    finally {
605                            if (is != null) {
606                                    try {
607                                            is.close();
608                                    }
609                                    catch (IOException ioe) {
610                                    }
611                            }
612                    }
613            }
614    
615            /**
616             * @deprecated As of 7.0.0, replaced by {@link #updateFileEntry(long, long,
617             *             String, String, String, String, String, boolean, InputStream,
618             *             long, ServiceContext)}
619             */
620            @Deprecated
621            @Override
622            public FileEntry updateFileEntry(
623                            long fileEntryId, String sourceFileName, String mimeType,
624                            String title, String description, String changeLog,
625                            boolean majorVersion, File file, ServiceContext serviceContext)
626                    throws PortalException {
627    
628                    return updateFileEntry(
629                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
630                                    getUserId(),
631                            fileEntryId, sourceFileName, mimeType, title, description,
632                            changeLog, majorVersion, file, serviceContext);
633            }
634    
635            /**
636             * @deprecated As of 7.0.0, replaced by {@link #updateFileEntry(long, long,
637             *             String, String, String, String, String, boolean, File,
638             *             ServiceContext)}
639             */
640            @Deprecated
641            @Override
642            public FileEntry updateFileEntry(
643                            long fileEntryId, String sourceFileName, String mimeType,
644                            String title, String description, String changeLog,
645                            boolean majorVersion, InputStream is, long size,
646                            ServiceContext serviceContext)
647                    throws PortalException {
648    
649                    return updateFileEntry(
650                            com.liferay.portal.kernel.repository.util.RepositoryUserUtil.
651                                    getUserId(),
652                            fileEntryId, sourceFileName, mimeType, title, description,
653                            changeLog, majorVersion, is, size, serviceContext);
654            }
655    
656            @Override
657            public Folder updateFolder(
658                    long folderId, long parentFolderId, String name, String description,
659                    ServiceContext serviceContext) {
660    
661                    throw new UnsupportedOperationException();
662            }
663    
664            @Override
665            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid) {
666                    throw new UnsupportedOperationException();
667            }
668    
669            protected void clearManualCheckInRequired(
670                            long fileEntryId, ServiceContext serviceContext)
671                    throws NoSuchRepositoryEntryException {
672    
673                    boolean webDAVCheckInMode = GetterUtil.getBoolean(
674                            serviceContext.getAttribute(DL.WEBDAV_CHECK_IN_MODE));
675    
676                    if (webDAVCheckInMode) {
677                            return;
678                    }
679    
680                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
681                            fileEntryId);
682    
683                    boolean manualCheckInRequired =
684                            repositoryEntry.getManualCheckInRequired();
685    
686                    if (!manualCheckInRequired) {
687                            return;
688                    }
689    
690                    repositoryEntry.setManualCheckInRequired(false);
691    
692                    RepositoryEntryUtil.update(repositoryEntry);
693            }
694    
695            protected RepositoryEntry getRepositoryEntry(String objectId)
696                    throws PortalException {
697    
698                    return repositoryEntryLocalService.getRepositoryEntry(
699                            PrincipalThreadLocal.getUserId(), getGroupId(), getRepositoryId(),
700                            objectId);
701            }
702    
703            protected void setManualCheckInRequired(
704                            long fileEntryId, ServiceContext serviceContext)
705                    throws NoSuchRepositoryEntryException {
706    
707                    boolean manualCheckInRequired = GetterUtil.getBoolean(
708                            serviceContext.getAttribute(DL.MANUAL_CHECK_IN_REQUIRED));
709    
710                    if (!manualCheckInRequired) {
711                            return;
712                    }
713    
714                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
715                            fileEntryId);
716    
717                    repositoryEntry.setManualCheckInRequired(manualCheckInRequired);
718    
719                    RepositoryEntryUtil.update(repositoryEntry);
720            }
721    
722            protected AssetEntryLocalService assetEntryLocalService;
723            protected CompanyLocalService companyLocalService;
724            protected DLAppHelperLocalService dlAppHelperLocalService;
725            protected DLFolderLocalService dlFolderLocalService;
726            protected RepositoryEntryLocalService repositoryEntryLocalService;
727            protected UserLocalService userLocalService;
728    
729            private static final String[] _SUPPORTED_CONFIGURATIONS = {};
730    
731            private static final String[][] _SUPPORTED_PARAMETERS = {};
732    
733            private long _companyId;
734            private long _groupId;
735            private final LocalRepository _localRepository =
736                    new DefaultLocalRepositoryImpl(this);
737            private long _repositoryId;
738            private UnicodeProperties _typeSettingsProperties;
739    
740    }