001    /**
002     * Copyright (c) 2000-2011 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.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.repository.search.RepositorySearchQueryBuilderUtil;
023    import com.liferay.portal.kernel.search.BooleanQuery;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.SearchEngineUtil;
027    import com.liferay.portal.kernel.search.SearchException;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.UnicodeProperties;
030    import com.liferay.portal.model.RepositoryEntry;
031    import com.liferay.portal.service.CompanyLocalService;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.UserLocalService;
034    import com.liferay.portal.service.persistence.RepositoryEntryUtil;
035    import com.liferay.portlet.asset.service.AssetEntryLocalService;
036    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
037    
038    import java.io.File;
039    import java.io.FileInputStream;
040    import java.io.IOException;
041    import java.io.InputStream;
042    
043    import java.util.ArrayList;
044    import java.util.List;
045    
046    /**
047     * Third-party repository implementations should extend from this class.
048     *
049     * @author Alexander Chow
050     */
051    public abstract class BaseRepositoryImpl implements BaseRepository {
052    
053            public FileEntry addFileEntry(
054                            long folderId, String sourceFileName, String mimeType, String title,
055                            String description, String changeLog, File file,
056                            ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    InputStream is = null;
060                    long size = 0;
061    
062                    try {
063                            is = new FileInputStream(file);
064                            size = file.length();
065    
066                            return addFileEntry(
067                                    folderId, sourceFileName, mimeType, title, description,
068                                    changeLog, is, size, serviceContext);
069                    }
070                    catch (IOException ioe) {
071                            throw new SystemException(ioe);
072                    }
073                    finally {
074                            if (is != null) {
075                                    try {
076                                            is.close();
077                                    }
078                                    catch (IOException ioe) {
079                                    }
080                            }
081                    }
082            }
083    
084            public void deleteFileEntry(long folderId, String title)
085                    throws PortalException, SystemException {
086    
087                    FileEntry fileEntry = getFileEntry(folderId, title);
088    
089                    deleteFileEntry(fileEntry.getFileEntryId());
090            }
091    
092            public void deleteFolder(long parentFolderId, String title)
093                    throws PortalException, SystemException {
094    
095                    Folder folder = getFolder(parentFolderId, title);
096    
097                    deleteFolder(folder.getFolderId());
098            }
099    
100            public long getCompanyId() {
101                    return _companyId;
102            }
103    
104            public List<Object> getFileEntriesAndFileShortcuts(
105                            long folderId, int status, int start, int end)
106                    throws SystemException {
107    
108                    return new ArrayList<Object>(
109                            getFileEntries(folderId, start, end, null));
110            }
111    
112            public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
113                    throws SystemException {
114    
115                    return getFileEntriesCount(folderId);
116            }
117    
118            public abstract List<Object> getFoldersAndFileEntries(
119                            long folderId, int start, int end, OrderByComparator obc)
120                    throws SystemException;
121    
122            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
123                            long folderId, int status, boolean includeMountFolders, int start,
124                            int end, OrderByComparator obc)
125                    throws SystemException {
126    
127                    return getFoldersAndFileEntries(folderId, start, end, obc);
128            }
129    
130            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
131                            long folderId, int status, String[] mimeTypes,
132                            boolean includeMountFolders, int start, int end,
133                            OrderByComparator obc)
134                    throws SystemException {
135    
136                    return getFoldersAndFileEntries(folderId, start, end, obc);
137            }
138    
139            public int getFoldersAndFileEntriesAndFileShortcutsCount(
140                            long folderId, int status, boolean includeMountFolders)
141                    throws SystemException {
142    
143                    return getFoldersAndFileEntriesCount(folderId);
144            }
145    
146            public int getFoldersAndFileEntriesAndFileShortcutsCount(
147                            long folderId, int status, String[] mimeTypes,
148                            boolean includeMountFolders)
149                    throws SystemException {
150    
151                    return getFoldersAndFileEntriesCount(folderId);
152            }
153    
154            public abstract int getFoldersAndFileEntriesCount(long folderId)
155                    throws SystemException;
156    
157            public long getGroupId() {
158                    return _groupId;
159            }
160    
161            public LocalRepository getLocalRepository() {
162                    return _localRepository;
163            }
164    
165            public Object[] getRepositoryEntryIds(String objectId)
166                    throws SystemException {
167    
168                    RepositoryEntry repositoryEntry = RepositoryEntryUtil.fetchByR_M(
169                            getRepositoryId(), objectId);
170    
171                    if (repositoryEntry == null) {
172                            long repositoryEntryId = counterLocalService.increment();
173    
174                            repositoryEntry = RepositoryEntryUtil.create(repositoryEntryId);
175    
176                            repositoryEntry.setGroupId(getGroupId());
177                            repositoryEntry.setRepositoryId(getRepositoryId());
178                            repositoryEntry.setMappedId(objectId);
179    
180                            RepositoryEntryUtil.update(repositoryEntry, false);
181                    }
182    
183                    return new Object[] {
184                            repositoryEntry.getRepositoryEntryId(),
185                            repositoryEntry.getUuid()
186                    };
187            }
188    
189            public List<FileEntry> getRepositoryFileEntries(
190                            long userId, long rootFolderId, int start, int end,
191                            OrderByComparator obc)
192                    throws SystemException {
193    
194                    return getFileEntries(rootFolderId, start, end, obc);
195            }
196    
197            public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
198                    throws SystemException {
199    
200                    return getFileEntriesCount(rootFolderId);
201            }
202    
203            public long getRepositoryId() {
204                    return _repositoryId;
205            }
206    
207            public UnicodeProperties getTypeSettingsProperties() {
208                    return _typeSettingsProperties;
209            }
210    
211            public abstract void initRepository()
212                    throws PortalException, SystemException;
213    
214            public Hits search(SearchContext searchContext) throws SearchException {
215                    searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
216    
217                    BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
218                            searchContext);
219    
220                    return search(searchContext, fullQuery);
221            }
222    
223            public void setAssetEntryLocalService(
224                    AssetEntryLocalService assetEntryLocalService) {
225    
226                    this.assetEntryLocalService = assetEntryLocalService;
227            }
228    
229            public void setCompanyId(long companyId) {
230                    _companyId = companyId;
231            }
232    
233            public void setCompanyLocalService(
234                    CompanyLocalService companyLocalService) {
235    
236                    this.companyLocalService = companyLocalService;
237            }
238    
239            public void setCounterLocalService(
240                    CounterLocalService counterLocalService) {
241    
242                    this.counterLocalService = counterLocalService;
243            }
244    
245            public void setDLAppHelperLocalService(
246                    DLAppHelperLocalService dlAppHelperLocalService) {
247    
248                    this.dlAppHelperLocalService = dlAppHelperLocalService;
249            }
250    
251            public void setGroupId(long groupId) {
252                    _groupId = groupId;
253            }
254    
255            public void setRepositoryId(long repositoryId) {
256                    _repositoryId = repositoryId;
257            }
258    
259            public void setTypeSettingsProperties(
260                    UnicodeProperties typeSettingsProperties) {
261    
262                    _typeSettingsProperties = typeSettingsProperties;
263            }
264    
265            public void setUserLocalService(UserLocalService userLocalService) {
266                    this.userLocalService = userLocalService;
267            }
268    
269            public void unlockFolder(long parentFolderId, String title, String lockUuid)
270                    throws PortalException, SystemException {
271    
272                    Folder folder = getFolder(parentFolderId, title);
273    
274                    unlockFolder(folder.getFolderId(), lockUuid);
275            }
276    
277            public FileEntry updateFileEntry(
278                            long fileEntryId, String sourceFileName, String mimeType,
279                            String title, String description, String changeLog,
280                            boolean majorVersion, File file, ServiceContext serviceContext)
281                    throws PortalException, SystemException {
282    
283                    InputStream is = null;
284                    long size = 0;
285    
286                    try {
287                            is = new FileInputStream(file);
288                            size = file.length();
289    
290                            return updateFileEntry(
291                                    fileEntryId, sourceFileName, mimeType, title, description,
292                                    changeLog, majorVersion, is, size, serviceContext);
293                    }
294                    catch (IOException ioe) {
295                            throw new SystemException(ioe);
296                    }
297                    finally {
298                            if (is != null) {
299                                    try {
300                                            is.close();
301                                    }
302                                    catch (IOException ioe) {
303                                    }
304                            }
305                    }
306            }
307    
308            protected AssetEntryLocalService assetEntryLocalService;
309            protected CompanyLocalService companyLocalService;
310            protected CounterLocalService counterLocalService;
311            protected DLAppHelperLocalService dlAppHelperLocalService;
312            protected UserLocalService userLocalService;
313    
314            private long _companyId;
315            private long _groupId;
316            private LocalRepository _localRepository = new DefaultLocalRepositoryImpl(
317                    this);
318            private long _repositoryId;
319            private UnicodeProperties _typeSettingsProperties;
320    
321    }