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(), repositoryEntry.getUuid()
202                    };
203            }
204    
205            public List<FileEntry> getRepositoryFileEntries(
206                            long userId, long rootFolderId, int start, int end,
207                            OrderByComparator obc)
208                    throws SystemException {
209    
210                    return getFileEntries(rootFolderId, start, end, obc);
211            }
212    
213            public List<FileEntry> getRepositoryFileEntries(
214                            long userId, long rootFolderId, String[] mimeTypes, int status,
215                            int start, int end, OrderByComparator obc)
216                    throws PortalException, SystemException {
217    
218                    return getFileEntries(rootFolderId, mimeTypes, start, end, obc);
219            }
220    
221            public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
222                    throws SystemException {
223    
224                    return getFileEntriesCount(rootFolderId);
225            }
226    
227            public int getRepositoryFileEntriesCount(
228                            long userId, long rootFolderId, String[] mimeTypes, int status)
229                    throws PortalException, SystemException {
230    
231                    return getFileEntriesCount(rootFolderId, mimeTypes);
232            }
233    
234            public long getRepositoryId() {
235                    return _repositoryId;
236            }
237    
238            public UnicodeProperties getTypeSettingsProperties() {
239                    return _typeSettingsProperties;
240            }
241    
242            public abstract void initRepository()
243                    throws PortalException, SystemException;
244    
245            public Lock lockFileEntry(long fileEntryId) {
246                    throw new UnsupportedOperationException();
247            }
248    
249            public Lock lockFileEntry(
250                    long fileEntryId, String owner, long expirationTime) {
251    
252                    throw new UnsupportedOperationException();
253            }
254    
255            public Hits search(SearchContext searchContext) throws SearchException {
256                    searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
257    
258                    BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
259                            searchContext);
260    
261                    return search(searchContext, fullQuery);
262            }
263    
264            public void setAssetEntryLocalService(
265                    AssetEntryLocalService assetEntryLocalService) {
266    
267                    this.assetEntryLocalService = assetEntryLocalService;
268            }
269    
270            public void setCompanyId(long companyId) {
271                    _companyId = companyId;
272            }
273    
274            public void setCompanyLocalService(
275                    CompanyLocalService companyLocalService) {
276    
277                    this.companyLocalService = companyLocalService;
278            }
279    
280            public void setCounterLocalService(
281                    CounterLocalService counterLocalService) {
282    
283                    this.counterLocalService = counterLocalService;
284            }
285    
286            public void setDLAppHelperLocalService(
287                    DLAppHelperLocalService dlAppHelperLocalService) {
288    
289                    this.dlAppHelperLocalService = dlAppHelperLocalService;
290            }
291    
292            public void setGroupId(long groupId) {
293                    _groupId = groupId;
294            }
295    
296            public void setRepositoryId(long repositoryId) {
297                    _repositoryId = repositoryId;
298            }
299    
300            public void setTypeSettingsProperties(
301                    UnicodeProperties typeSettingsProperties) {
302    
303                    _typeSettingsProperties = typeSettingsProperties;
304            }
305    
306            public void setUserLocalService(UserLocalService userLocalService) {
307                    this.userLocalService = userLocalService;
308            }
309    
310            public void unlockFileEntry(long fileEntryId) {
311                    throw new UnsupportedOperationException();
312            }
313    
314            public void unlockFileEntry(long fileEntryId, String lockUuid) {
315                    throw new UnsupportedOperationException();
316            }
317    
318            public void unlockFolder(long parentFolderId, String title, String lockUuid)
319                    throws PortalException, SystemException {
320    
321                    Folder folder = getFolder(parentFolderId, title);
322    
323                    unlockFolder(folder.getFolderId(), lockUuid);
324            }
325    
326            public FileEntry updateFileEntry(
327                            long fileEntryId, String sourceFileName, String mimeType,
328                            String title, String description, String changeLog,
329                            boolean majorVersion, File file, ServiceContext serviceContext)
330                    throws PortalException, SystemException {
331    
332                    InputStream is = null;
333                    long size = 0;
334    
335                    try {
336                            is = new FileInputStream(file);
337                            size = file.length();
338    
339                            return updateFileEntry(
340                                    fileEntryId, sourceFileName, mimeType, title, description,
341                                    changeLog, majorVersion, is, size, serviceContext);
342                    }
343                    catch (IOException ioe) {
344                            throw new SystemException(ioe);
345                    }
346                    finally {
347                            if (is != null) {
348                                    try {
349                                            is.close();
350                                    }
351                                    catch (IOException ioe) {
352                                    }
353                            }
354                    }
355            }
356    
357            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid) {
358                    throw new UnsupportedOperationException();
359            }
360    
361            protected AssetEntryLocalService assetEntryLocalService;
362            protected CompanyLocalService companyLocalService;
363            protected CounterLocalService counterLocalService;
364            protected DLAppHelperLocalService dlAppHelperLocalService;
365            protected UserLocalService userLocalService;
366    
367            private long _companyId;
368            private long _groupId;
369            private LocalRepository _localRepository = new DefaultLocalRepositoryImpl(
370                    this);
371            private long _repositoryId;
372            private UnicodeProperties _typeSettingsProperties;
373    
374    }