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