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