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