001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.repository;
016    
017    import com.liferay.counter.service.CounterLocalService;
018    import com.liferay.portal.NoSuchRepositoryEntryException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.repository.search.RepositorySearchQueryBuilderUtil;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.Hits;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.search.SearchException;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.UnicodeProperties;
032    import com.liferay.portal.model.Lock;
033    import com.liferay.portal.model.RepositoryEntry;
034    import com.liferay.portal.service.CompanyLocalService;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.UserLocalService;
037    import com.liferay.portal.service.persistence.RepositoryEntryUtil;
038    import com.liferay.portlet.asset.service.AssetEntryLocalService;
039    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
040    import com.liferay.portlet.documentlibrary.util.DL;
041    
042    import java.io.File;
043    import java.io.FileInputStream;
044    import java.io.IOException;
045    import java.io.InputStream;
046    
047    import java.util.ArrayList;
048    import java.util.List;
049    
050    /**
051     * Third-party repository implementations should extend from this class.
052     *
053     * @author Alexander Chow
054     */
055    public abstract class BaseRepositoryImpl implements BaseRepository {
056    
057            public FileEntry addFileEntry(
058                            long folderId, String sourceFileName, String mimeType, String title,
059                            String description, String changeLog, File file,
060                            ServiceContext serviceContext)
061                    throws PortalException, SystemException {
062    
063                    InputStream is = null;
064                    long size = 0;
065    
066                    try {
067                            is = new FileInputStream(file);
068                            size = file.length();
069    
070                            return addFileEntry(
071                                    folderId, sourceFileName, mimeType, title, description,
072                                    changeLog, is, size, serviceContext);
073                    }
074                    catch (IOException ioe) {
075                            throw new SystemException(ioe);
076                    }
077                    finally {
078                            if (is != null) {
079                                    try {
080                                            is.close();
081                                    }
082                                    catch (IOException ioe) {
083                                    }
084                            }
085                    }
086            }
087    
088            /**
089             * @deprecated As of 6.2.0, replaced by {@link #checkInFileEntry(long,
090             *             String, ServiceContext)}
091             */
092            public void checkInFileEntry(long fileEntryId, String lockUuid)
093                    throws PortalException, SystemException {
094    
095                    checkInFileEntry(fileEntryId, lockUuid, new ServiceContext());
096            }
097    
098            public void deleteFileEntry(long folderId, String title)
099                    throws PortalException, SystemException {
100    
101                    FileEntry fileEntry = getFileEntry(folderId, title);
102    
103                    deleteFileEntry(fileEntry.getFileEntryId());
104            }
105    
106            public void deleteFileVersion(long fileEntryId, String version) {
107                    throw new UnsupportedOperationException();
108            }
109    
110            public void deleteFolder(long parentFolderId, String title)
111                    throws PortalException, SystemException {
112    
113                    Folder folder = getFolder(parentFolderId, title);
114    
115                    deleteFolder(folder.getFolderId());
116            }
117    
118            public long getCompanyId() {
119                    return _companyId;
120            }
121    
122            public List<Object> getFileEntriesAndFileShortcuts(
123                            long folderId, int status, int start, int end)
124                    throws PortalException, SystemException {
125    
126                    return new ArrayList<Object>(
127                            getFileEntries(folderId, start, end, null));
128            }
129    
130            public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
131                    throws SystemException {
132    
133                    return getFileEntriesCount(folderId);
134            }
135    
136            public int getFileEntriesAndFileShortcutsCount(
137                            long folderId, int status, String[] mimeTypes)
138                    throws PortalException, SystemException {
139    
140                    return getFileEntriesCount(folderId, mimeTypes);
141            }
142    
143            public List<Folder> getFolders(
144                            long parentFolderId, int status, boolean includeMountfolders,
145                            int start, int end, OrderByComparator obc)
146                    throws PortalException, SystemException {
147    
148                    return getFolders(parentFolderId, includeMountfolders, start, end, obc);
149            }
150    
151            public abstract List<Object> getFoldersAndFileEntries(
152                            long folderId, int start, int end, OrderByComparator obc)
153                    throws SystemException;
154    
155            public abstract List<Object> getFoldersAndFileEntries(
156                            long folderId, String[] mimeTypes, int start, int end,
157                            OrderByComparator obc)
158                    throws PortalException, SystemException;
159    
160            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
161                            long folderId, int status, boolean includeMountFolders, int start,
162                            int end, OrderByComparator obc)
163                    throws SystemException {
164    
165                    return getFoldersAndFileEntries(folderId, start, end, obc);
166            }
167    
168            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
169                            long folderId, int status, String[] mimeTypes,
170                            boolean includeMountFolders, int start, int end,
171                            OrderByComparator obc)
172                    throws PortalException, SystemException {
173    
174                    return getFoldersAndFileEntries(folderId, mimeTypes, start, end, obc);
175            }
176    
177            public int getFoldersAndFileEntriesAndFileShortcutsCount(
178                            long folderId, int status, boolean includeMountFolders)
179                    throws SystemException {
180    
181                    return getFoldersAndFileEntriesCount(folderId);
182            }
183    
184            public int getFoldersAndFileEntriesAndFileShortcutsCount(
185                            long folderId, int status, String[] mimeTypes,
186                            boolean includeMountFolders)
187                    throws PortalException, SystemException {
188    
189                    return getFoldersAndFileEntriesCount(folderId, mimeTypes);
190            }
191    
192            public abstract int getFoldersAndFileEntriesCount(long folderId)
193                    throws SystemException;
194    
195            public abstract int getFoldersAndFileEntriesCount(
196                            long folderId, String[] mimeTypes)
197                    throws PortalException, SystemException;
198    
199            public int getFoldersCount(
200                            long parentFolderId, int status, boolean includeMountfolders)
201                    throws PortalException, SystemException {
202    
203                    return getFoldersCount(parentFolderId, includeMountfolders);
204            }
205    
206            public long getGroupId() {
207                    return _groupId;
208            }
209    
210            public LocalRepository getLocalRepository() {
211                    return _localRepository;
212            }
213    
214            public Object[] getRepositoryEntryIds(String objectId)
215                    throws SystemException {
216    
217                    boolean newRepositoryEntry = false;
218    
219                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.fetchByR_M(
220                            getRepositoryId(), objectId);
221    
222                    if (repositoryEntry == null) {
223                            long repositoryEntryId = counterLocalService.increment();
224    
225                            repositoryEntry = RepositoryEntryUtil.create(repositoryEntryId);
226    
227                            repositoryEntry.setGroupId(getGroupId());
228                            repositoryEntry.setRepositoryId(getRepositoryId());
229                            repositoryEntry.setMappedId(objectId);
230    
231                            RepositoryEntryUtil.update(repositoryEntry);
232    
233                            newRepositoryEntry = true;
234                    }
235    
236                    return new Object[] {
237                            repositoryEntry.getRepositoryEntryId(), repositoryEntry.getUuid(),
238                            newRepositoryEntry
239                    };
240            }
241    
242            public List<FileEntry> getRepositoryFileEntries(
243                            long userId, long rootFolderId, int start, int end,
244                            OrderByComparator obc)
245                    throws PortalException, SystemException {
246    
247                    return getFileEntries(rootFolderId, start, end, obc);
248            }
249    
250            public List<FileEntry> getRepositoryFileEntries(
251                            long userId, long rootFolderId, String[] mimeTypes, int status,
252                            int start, int end, OrderByComparator obc)
253                    throws PortalException, SystemException {
254    
255                    return getFileEntries(rootFolderId, mimeTypes, start, end, obc);
256            }
257    
258            public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
259                    throws SystemException {
260    
261                    return getFileEntriesCount(rootFolderId);
262            }
263    
264            public int getRepositoryFileEntriesCount(
265                            long userId, long rootFolderId, String[] mimeTypes, int status)
266                    throws PortalException, SystemException {
267    
268                    return getFileEntriesCount(rootFolderId, mimeTypes);
269            }
270    
271            public long getRepositoryId() {
272                    return _repositoryId;
273            }
274    
275            public UnicodeProperties getTypeSettingsProperties() {
276                    return _typeSettingsProperties;
277            }
278    
279            public abstract void initRepository()
280                    throws PortalException, SystemException;
281    
282            /**
283             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
284             *             ServiceContext)}
285             */
286            public Lock lockFileEntry(long fileEntryId)
287                    throws PortalException, SystemException {
288    
289                    checkOutFileEntry(fileEntryId, new ServiceContext());
290    
291                    FileEntry fileEntry = getFileEntry(fileEntryId);
292    
293                    return fileEntry.getLock();
294            }
295    
296            /**
297             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
298             *             String, long, ServiceContext)}
299             */
300            public Lock lockFileEntry(
301                            long fileEntryId, String owner, long expirationTime)
302                    throws PortalException, SystemException {
303    
304                    FileEntry fileEntry = checkOutFileEntry(
305                            fileEntryId, owner, expirationTime, new ServiceContext());
306    
307                    return fileEntry.getLock();
308            }
309    
310            public Hits search(SearchContext searchContext) throws SearchException {
311                    searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
312    
313                    BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
314                            searchContext);
315    
316                    return search(searchContext, fullQuery);
317            }
318    
319            public void setAssetEntryLocalService(
320                    AssetEntryLocalService assetEntryLocalService) {
321    
322                    this.assetEntryLocalService = assetEntryLocalService;
323            }
324    
325            public void setCompanyId(long companyId) {
326                    _companyId = companyId;
327            }
328    
329            public void setCompanyLocalService(
330                    CompanyLocalService companyLocalService) {
331    
332                    this.companyLocalService = companyLocalService;
333            }
334    
335            public void setCounterLocalService(
336                    CounterLocalService counterLocalService) {
337    
338                    this.counterLocalService = counterLocalService;
339            }
340    
341            public void setDLAppHelperLocalService(
342                    DLAppHelperLocalService dlAppHelperLocalService) {
343    
344                    this.dlAppHelperLocalService = dlAppHelperLocalService;
345            }
346    
347            public void setGroupId(long groupId) {
348                    _groupId = groupId;
349            }
350    
351            public void setRepositoryId(long repositoryId) {
352                    _repositoryId = repositoryId;
353            }
354    
355            public void setTypeSettingsProperties(
356                    UnicodeProperties typeSettingsProperties) {
357    
358                    _typeSettingsProperties = typeSettingsProperties;
359            }
360    
361            public void setUserLocalService(UserLocalService userLocalService) {
362                    this.userLocalService = userLocalService;
363            }
364    
365            public void unlockFolder(long parentFolderId, String title, String lockUuid)
366                    throws PortalException, SystemException {
367    
368                    Folder folder = getFolder(parentFolderId, title);
369    
370                    unlockFolder(folder.getFolderId(), lockUuid);
371            }
372    
373            public FileEntry updateFileEntry(
374                            long fileEntryId, String sourceFileName, String mimeType,
375                            String title, String description, String changeLog,
376                            boolean majorVersion, File file, ServiceContext serviceContext)
377                    throws PortalException, SystemException {
378    
379                    InputStream is = null;
380                    long size = 0;
381    
382                    try {
383                            is = new FileInputStream(file);
384                            size = file.length();
385    
386                            return updateFileEntry(
387                                    fileEntryId, sourceFileName, mimeType, title, description,
388                                    changeLog, majorVersion, is, size, serviceContext);
389                    }
390                    catch (IOException ioe) {
391                            throw new SystemException(ioe);
392                    }
393                    finally {
394                            if (is != null) {
395                                    try {
396                                            is.close();
397                                    }
398                                    catch (IOException ioe) {
399                                    }
400                            }
401                    }
402            }
403    
404            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid) {
405                    throw new UnsupportedOperationException();
406            }
407    
408            protected void clearManualCheckInRequired(
409                            long fileEntryId, ServiceContext serviceContext)
410                    throws NoSuchRepositoryEntryException, SystemException {
411    
412                    boolean webDAVCheckInMode = GetterUtil.getBoolean(
413                            serviceContext.getAttribute(DL.WEBDAV_CHECK_IN_MODE));
414    
415                    if (webDAVCheckInMode) {
416                            return;
417                    }
418    
419                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
420                            fileEntryId);
421    
422                    boolean manualCheckInRequired =
423                            repositoryEntry.getManualCheckInRequired();
424    
425                    if (!manualCheckInRequired) {
426                            return;
427                    }
428    
429                    repositoryEntry.setManualCheckInRequired(false);
430    
431                    RepositoryEntryUtil.update(repositoryEntry);
432            }
433    
434            protected void setManualCheckInRequired(
435                            long fileEntryId, ServiceContext serviceContext)
436                    throws NoSuchRepositoryEntryException, SystemException {
437    
438                    boolean manualCheckInRequired = GetterUtil.getBoolean(
439                            serviceContext.getAttribute(DL.MANUAL_CHECK_IN_REQUIRED));
440    
441                    if (!manualCheckInRequired) {
442                            return;
443                    }
444    
445                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.findByPrimaryKey(
446                            fileEntryId);
447    
448                    repositoryEntry.setManualCheckInRequired(manualCheckInRequired);
449    
450                    RepositoryEntryUtil.update(repositoryEntry);
451            }
452    
453            protected AssetEntryLocalService assetEntryLocalService;
454            protected CompanyLocalService companyLocalService;
455            protected CounterLocalService counterLocalService;
456            protected DLAppHelperLocalService dlAppHelperLocalService;
457            protected UserLocalService userLocalService;
458    
459            private long _companyId;
460            private long _groupId;
461            private LocalRepository _localRepository = new DefaultLocalRepositoryImpl(
462                    this);
463            private long _repositoryId;
464            private UnicodeProperties _typeSettingsProperties;
465    
466    }