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