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