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