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 folderId, String sourceFileName, String mimeType, String title,
064                            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                                    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 6.2.0, replaced by {@link #checkInFileEntry(long,
095             *             String, ServiceContext)}
096             */
097            @Deprecated
098            @Override
099            public void checkInFileEntry(long fileEntryId, String lockUuid)
100                    throws PortalException {
101    
102                    checkInFileEntry(fileEntryId, lockUuid, new ServiceContext());
103            }
104    
105            @Override
106            public void deleteFileEntry(long folderId, String title)
107                    throws PortalException {
108    
109                    FileEntry fileEntry = getFileEntry(folderId, title);
110    
111                    deleteFileEntry(fileEntry.getFileEntryId());
112            }
113    
114            @Override
115            public void deleteFileVersion(long fileEntryId, String version) {
116                    throw new UnsupportedOperationException();
117            }
118    
119            @Override
120            public void deleteFolder(long parentFolderId, String name)
121                    throws PortalException {
122    
123                    Folder folder = getFolder(parentFolderId, name);
124    
125                    deleteFolder(folder.getFolderId());
126            }
127    
128            @Override
129            public <T extends Capability> T getCapability(Class<T> capabilityClass) {
130                    throw new IllegalArgumentException(
131                            String.format(
132                                    "Capability %s is not supported by repository %s",
133                                    capabilityClass.getName(), getRepositoryId()));
134            }
135    
136            public long getCompanyId() {
137                    return _companyId;
138            }
139    
140            @Override
141            public List<Object> getFileEntriesAndFileShortcuts(
142                            long folderId, int status, int start, int end)
143                    throws PortalException {
144    
145                    return new ArrayList<Object>(
146                            getFileEntries(folderId, start, end, null));
147            }
148    
149            @Override
150            public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
151                    throws PortalException {
152    
153                    return getFileEntriesCount(folderId);
154            }
155    
156            @Override
157            public int getFileEntriesAndFileShortcutsCount(
158                            long folderId, int status, String[] mimeTypes)
159                    throws PortalException {
160    
161                    return getFileEntriesCount(folderId, mimeTypes);
162            }
163    
164            @Override
165            public List<Folder> getFolders(
166                            long parentFolderId, int status, boolean includeMountfolders,
167                            int start, int end, OrderByComparator<Folder> obc)
168                    throws PortalException {
169    
170                    return getFolders(parentFolderId, includeMountfolders, start, end, obc);
171            }
172    
173            public abstract List<Object> getFoldersAndFileEntries(
174                    long folderId, int start, int end, OrderByComparator<?> obc);
175    
176            public abstract List<Object> getFoldersAndFileEntries(
177                            long folderId, String[] mimeTypes, int start, int end,
178                            OrderByComparator<?> obc)
179                    throws PortalException;
180    
181            @Override
182            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
183                    long folderId, int status, boolean includeMountFolders, int start,
184                    int end, OrderByComparator<?> obc) {
185    
186                    return getFoldersAndFileEntries(folderId, start, end, obc);
187            }
188    
189            @Override
190            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
191                            long folderId, int status, String[] mimeTypes,
192                            boolean includeMountFolders, int start, int end,
193                            OrderByComparator<?> obc)
194                    throws PortalException {
195    
196                    return getFoldersAndFileEntries(folderId, mimeTypes, start, end, obc);
197            }
198    
199            @Override
200            public int getFoldersAndFileEntriesAndFileShortcutsCount(
201                    long folderId, int status, boolean includeMountFolders) {
202    
203                    return getFoldersAndFileEntriesCount(folderId);
204            }
205    
206            @Override
207            public int getFoldersAndFileEntriesAndFileShortcutsCount(
208                            long folderId, int status, String[] mimeTypes,
209                            boolean includeMountFolders)
210                    throws PortalException {
211    
212                    return getFoldersAndFileEntriesCount(folderId, mimeTypes);
213            }
214    
215            public abstract int getFoldersAndFileEntriesCount(long folderId);
216    
217            public abstract int getFoldersAndFileEntriesCount(
218                            long folderId, String[] mimeTypes)
219                    throws PortalException;
220    
221            @Override
222            public int getFoldersCount(
223                            long parentFolderId, int status, boolean includeMountfolders)
224                    throws PortalException {
225    
226                    return getFoldersCount(parentFolderId, includeMountfolders);
227            }
228    
229            public long getGroupId() {
230                    return _groupId;
231            }
232    
233            @Override
234            public LocalRepository getLocalRepository() {
235                    return _localRepository;
236            }
237    
238            public Object[] getRepositoryEntryIds(String objectId)
239                    throws PortalException {
240    
241                    boolean newRepositoryEntry = false;
242    
243                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.fetchByR_M(
244                            getRepositoryId(), objectId);
245    
246                    if (repositoryEntry == null) {
247                            repositoryEntry = repositoryEntryLocalService.addRepositoryEntry(
248                                    PrincipalThreadLocal.getUserId(), getGroupId(),
249                                    getRepositoryId(), objectId, new ServiceContext());
250    
251                            newRepositoryEntry = true;
252                    }
253    
254                    return new Object[] {
255                            repositoryEntry.getRepositoryEntryId(), repositoryEntry.getUuid(),
256                            newRepositoryEntry
257                    };
258            }
259    
260            @Override
261            public List<FileEntry> getRepositoryFileEntries(
262                            long userId, long rootFolderId, int start, int end,
263                            OrderByComparator<FileEntry> obc)
264                    throws PortalException {
265    
266                    return getFileEntries(rootFolderId, start, end, obc);
267            }
268    
269            @Override
270            public List<FileEntry> getRepositoryFileEntries(
271                            long userId, long rootFolderId, String[] mimeTypes, int status,
272                            int start, int end, OrderByComparator<FileEntry> obc)
273                    throws PortalException {
274    
275                    return getFileEntries(rootFolderId, mimeTypes, start, end, obc);
276            }
277    
278            @Override
279            public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
280                    throws PortalException {
281    
282                    return getFileEntriesCount(rootFolderId);
283            }
284    
285            @Override
286            public int getRepositoryFileEntriesCount(
287                            long userId, long rootFolderId, String[] mimeTypes, int status)
288                    throws PortalException {
289    
290                    return getFileEntriesCount(rootFolderId, mimeTypes);
291            }
292    
293            @Override
294            public long getRepositoryId() {
295                    return _repositoryId;
296            }
297    
298            public UnicodeProperties getTypeSettingsProperties() {
299                    return _typeSettingsProperties;
300            }
301    
302            @Override
303            public abstract void initRepository() throws PortalException;
304    
305            @Override
306            public <T extends Capability> boolean isCapabilityProvided(
307                    Class<T> capabilityClass) {
308    
309                    return false;
310            }
311    
312            /**
313             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
314             *             ServiceContext)}
315             */
316            @Deprecated
317            @Override
318            public Lock lockFileEntry(long fileEntryId) throws PortalException {
319                    checkOutFileEntry(fileEntryId, new ServiceContext());
320    
321                    FileEntry fileEntry = getFileEntry(fileEntryId);
322    
323                    return fileEntry.getLock();
324            }
325    
326            /**
327             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
328             *             String, long, ServiceContext)}
329             */
330            @Deprecated
331            @Override
332            public Lock lockFileEntry(
333                            long fileEntryId, String owner, long expirationTime)
334                    throws PortalException {
335    
336                    FileEntry fileEntry = checkOutFileEntry(
337                            fileEntryId, owner, expirationTime, new ServiceContext());
338    
339                    return fileEntry.getLock();
340            }
341    
342            @Override
343            public Hits search(SearchContext searchContext) throws SearchException {
344                    searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
345    
346                    BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
347                            searchContext);
348    
349                    return search(searchContext, fullQuery);
350            }
351    
352            @Override
353            public void setAssetEntryLocalService(
354                    AssetEntryLocalService assetEntryLocalService) {
355    
356                    this.assetEntryLocalService = assetEntryLocalService;
357            }
358    
359            @Override
360            public void setCompanyId(long companyId) {
361                    _companyId = companyId;
362            }
363    
364            @Override
365            public void setCompanyLocalService(
366                    CompanyLocalService companyLocalService) {
367    
368                    this.companyLocalService = companyLocalService;
369            }
370    
371            @Override
372            public void setDLAppHelperLocalService(
373                    DLAppHelperLocalService dlAppHelperLocalService) {
374    
375                    this.dlAppHelperLocalService = dlAppHelperLocalService;
376            }
377    
378            @Override
379            public void setGroupId(long groupId) {
380                    _groupId = groupId;
381            }
382    
383            @Override
384            public void setRepositoryEntryLocalService(
385                    RepositoryEntryLocalService repositoryEntryLocalService) {
386    
387                    this.repositoryEntryLocalService = repositoryEntryLocalService;
388            }
389    
390            @Override
391            public void setRepositoryId(long repositoryId) {
392                    _repositoryId = repositoryId;
393            }
394    
395            @Override
396            public void setTypeSettingsProperties(
397                    UnicodeProperties typeSettingsProperties) {
398    
399                    _typeSettingsProperties = typeSettingsProperties;
400            }
401    
402            @Override
403            public void setUserLocalService(UserLocalService userLocalService) {
404                    this.userLocalService = userLocalService;
405            }
406    
407            @Override
408            public void unlockFolder(long parentFolderId, String name, String lockUuid)
409                    throws PortalException {
410    
411                    Folder folder = getFolder(parentFolderId, name);
412    
413                    unlockFolder(folder.getFolderId(), lockUuid);
414            }
415    
416            @Override
417            public FileEntry updateFileEntry(
418                            long fileEntryId, String sourceFileName, String mimeType,
419                            String title, String description, String changeLog,
420                            boolean majorVersion, File file, ServiceContext serviceContext)
421                    throws PortalException {
422    
423                    InputStream is = null;
424                    long size = 0;
425    
426                    try {
427                            is = new FileInputStream(file);
428                            size = file.length();
429    
430                            return updateFileEntry(
431                                    fileEntryId, sourceFileName, mimeType, title, description,
432                                    changeLog, majorVersion, is, size, serviceContext);
433                    }
434                    catch (IOException ioe) {
435                            throw new SystemException(ioe);
436                    }
437                    finally {
438                            if (is != null) {
439                                    try {
440                                            is.close();
441                                    }
442                                    catch (IOException ioe) {
443                                    }
444                            }
445                    }
446            }
447    
448            @Override
449            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid) {
450                    throw new UnsupportedOperationException();
451            }
452    
453            protected void clearManualCheckInRequired(
454                            long fileEntryId, ServiceContext serviceContext)
455                    throws NoSuchRepositoryEntryException {
456    
457                    boolean webDAVCheckInMode = GetterUtil.getBoolean(
458                            serviceContext.getAttribute(DL.WEBDAV_CHECK_IN_MODE));
459    
460                    if (webDAVCheckInMode) {
461                            return;
462                    }
463    
464                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
465                            fileEntryId);
466    
467                    boolean manualCheckInRequired =
468                            repositoryEntry.getManualCheckInRequired();
469    
470                    if (!manualCheckInRequired) {
471                            return;
472                    }
473    
474                    repositoryEntry.setManualCheckInRequired(false);
475    
476                    RepositoryEntryUtil.update(repositoryEntry);
477            }
478    
479            protected void setManualCheckInRequired(
480                            long fileEntryId, ServiceContext serviceContext)
481                    throws NoSuchRepositoryEntryException {
482    
483                    boolean manualCheckInRequired = GetterUtil.getBoolean(
484                            serviceContext.getAttribute(DL.MANUAL_CHECK_IN_REQUIRED));
485    
486                    if (!manualCheckInRequired) {
487                            return;
488                    }
489    
490                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
491                            fileEntryId);
492    
493                    repositoryEntry.setManualCheckInRequired(manualCheckInRequired);
494    
495                    RepositoryEntryUtil.update(repositoryEntry);
496            }
497    
498            protected AssetEntryLocalService assetEntryLocalService;
499            protected CompanyLocalService companyLocalService;
500            protected DLAppHelperLocalService dlAppHelperLocalService;
501            protected RepositoryEntryLocalService repositoryEntryLocalService;
502            protected UserLocalService userLocalService;
503    
504            private long _companyId;
505            private long _groupId;
506            private final LocalRepository _localRepository =
507                    new DefaultLocalRepositoryImpl(this);
508            private long _repositoryId;
509            private UnicodeProperties _typeSettingsProperties;
510    
511    }