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