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