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.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.LocalRepository;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.FileVersion;
024    import com.liferay.portal.kernel.repository.model.Folder;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.SortedArrayList;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.model.Repository;
029    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
031    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
032    import com.liferay.portal.service.RepositoryLocalService;
033    import com.liferay.portal.service.RepositoryService;
034    import com.liferay.portal.service.ResourceLocalService;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
037    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
038    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
040    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
041    import com.liferay.portlet.documentlibrary.model.DLFolder;
042    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
043    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
044    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
045    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
046    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
047    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
048    import com.liferay.portlet.documentlibrary.service.DLFolderService;
049    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
050    
051    import java.io.File;
052    import java.io.InputStream;
053    
054    import java.util.Map;
055    
056    /**
057     * @author Alexander Chow
058     */
059    public class LiferayLocalRepository
060            extends LiferayRepositoryBase implements LocalRepository {
061    
062            public LiferayLocalRepository(
063                    RepositoryLocalService repositoryLocalService,
064                    RepositoryService repositoryService,
065                    DLAppHelperLocalService dlAppHelperLocalService,
066                    DLFileEntryLocalService dlFileEntryLocalService,
067                    DLFileEntryService dlFileEntryService,
068                    DLFileVersionLocalService dlFileVersionLocalService,
069                    DLFileVersionService dlFileVersionService,
070                    DLFolderLocalService dlFolderLocalService,
071                    DLFolderService dlFolderService,
072                    ResourceLocalService resourceLocalService, long repositoryId) {
073    
074                    super(
075                            repositoryLocalService, repositoryService, dlAppHelperLocalService,
076                            dlFileEntryLocalService, dlFileEntryService,
077                            dlFileVersionLocalService, dlFileVersionService,
078                            dlFolderLocalService, dlFolderService, resourceLocalService,
079                            repositoryId);
080            }
081    
082            public LiferayLocalRepository(
083                    RepositoryLocalService repositoryLocalService,
084                    RepositoryService repositoryService,
085                    DLAppHelperLocalService dlAppHelperLocalService,
086                    DLFileEntryLocalService dlFileEntryLocalService,
087                    DLFileEntryService dlFileEntryService,
088                    DLFileVersionLocalService dlFileVersionLocalService,
089                    DLFileVersionService dlFileVersionService,
090                    DLFolderLocalService dlFolderLocalService,
091                    DLFolderService dlFolderService,
092                    ResourceLocalService resourceLocalService, long folderId,
093                    long fileEntryId, long fileVersionId) {
094    
095                    super(
096                            repositoryLocalService, repositoryService, dlAppHelperLocalService,
097                            dlFileEntryLocalService, dlFileEntryService,
098                            dlFileVersionLocalService, dlFileVersionService,
099                            dlFolderLocalService, dlFolderService, resourceLocalService,
100                            folderId, fileEntryId, fileVersionId);
101            }
102    
103            public FileEntry addFileEntry(
104                            long userId, long folderId, String sourceFileName, String mimeType,
105                            String title, String description, String changeLog, File file,
106                            ServiceContext serviceContext)
107                    throws PortalException, SystemException {
108    
109                    long fileEntryTypeId = ParamUtil.getLong(
110                            serviceContext, "fileEntryTypeId", -1L);
111                    Map<String, Fields> fieldsMap = getFieldsMap(
112                            serviceContext, fileEntryTypeId);
113                    long size = 0;
114    
115                    if (file != null) {
116                            size = file.length();
117                    }
118    
119                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
120                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
121                            sourceFileName, mimeType, title, description, changeLog,
122                            fileEntryTypeId, fieldsMap, file, null, size, serviceContext);
123    
124                    addFileEntryResources(dlFileEntry, serviceContext);
125    
126                    return new LiferayFileEntry(dlFileEntry);
127            }
128    
129            public FileEntry addFileEntry(
130                            long userId, long folderId, String sourceFileName, String mimeType,
131                            String title, String description, String changeLog, InputStream is,
132                            long size, ServiceContext serviceContext)
133                    throws PortalException, SystemException {
134    
135                    long fileEntryTypeId = ParamUtil.getLong(
136                            serviceContext, "fileEntryTypeId", -1L);
137                    Map<String, Fields> fieldsMap = getFieldsMap(
138                            serviceContext, fileEntryTypeId);
139    
140                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
141                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
142                            sourceFileName, mimeType, title, description, changeLog,
143                            fileEntryTypeId, fieldsMap, null, is, size, serviceContext);
144    
145                    addFileEntryResources(dlFileEntry, serviceContext);
146    
147                    return new LiferayFileEntry(dlFileEntry);
148            }
149    
150            public Folder addFolder(
151                            long userId, long parentFolderId, String title, String description,
152                            ServiceContext serviceContext)
153                    throws PortalException, SystemException {
154    
155                    boolean mountPoint = ParamUtil.getBoolean(serviceContext, "mountPoint");
156    
157                    DLFolder dlFolder = dlFolderLocalService.addFolder(
158                            userId, getGroupId(), getRepositoryId(), mountPoint,
159                            toFolderId(parentFolderId), title, description, false,
160                            serviceContext);
161    
162                    return new LiferayFolder(dlFolder);
163            }
164    
165            public void addRepository(
166                    long groupId, String name, String description, String portletKey,
167                    UnicodeProperties typeSettingsProperties) {
168            }
169    
170            public void deleteAll() throws PortalException, SystemException {
171                    dlFolderLocalService.deleteAll(getGroupId());
172            }
173    
174            public void deleteFileEntry(long fileEntryId)
175                    throws PortalException, SystemException {
176    
177                    dlFileEntryLocalService.deleteFileEntry(fileEntryId);
178            }
179    
180            public void deleteFolder(long folderId)
181                    throws PortalException, SystemException {
182    
183                    dlFolderLocalService.deleteFolder(folderId);
184            }
185    
186            public FileEntry getFileEntry(long fileEntryId)
187                    throws PortalException, SystemException {
188    
189                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
190                            fileEntryId);
191    
192                    return new LiferayFileEntry(dlFileEntry);
193            }
194    
195            public FileEntry getFileEntry(long folderId, String title)
196                    throws PortalException, SystemException {
197    
198                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
199                            getGroupId(), toFolderId(folderId), title);
200    
201                    return new LiferayFileEntry(dlFileEntry);
202            }
203    
204            public FileEntry getFileEntryByUuid(String uuid)
205                    throws PortalException, SystemException {
206    
207                    DLFileEntry dlFileEntry =
208                            dlFileEntryLocalService.getFileEntryByUuidAndGroupId(
209                                    uuid, getGroupId());
210    
211                    return new LiferayFileEntry(dlFileEntry);
212            }
213    
214            public FileVersion getFileVersion(long fileVersionId)
215                    throws PortalException, SystemException {
216    
217                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
218                            fileVersionId);
219    
220                    return new LiferayFileVersion(dlFileVersion);
221            }
222    
223            public Folder getFolder(long folderId)
224                    throws PortalException, SystemException {
225    
226                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
227    
228                    return new LiferayFolder(dlFolder);
229            }
230    
231            public Folder getFolder(long parentFolderId, String title)
232                    throws PortalException, SystemException {
233    
234                    DLFolder dlFolder = dlFolderLocalService.getFolder(
235                            getGroupId(), toFolderId(parentFolderId), title);
236    
237                    return new LiferayFolder(dlFolder);
238            }
239    
240            public FileEntry moveFileEntry(
241                            long userId, long fileEntryId, long newFolderId,
242                            ServiceContext serviceContext)
243                    throws PortalException, SystemException {
244    
245                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
246                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
247    
248                    return new LiferayFileEntry(dlFileEntry);
249            }
250    
251            public void updateAsset(
252                            long userId, FileEntry fileEntry, FileVersion fileVersion,
253                            long[] assetCategoryIds, String[] assetTagNames,
254                            long[] assetLinkEntryIds)
255                    throws PortalException, SystemException {
256    
257                    dlAppHelperLocalService.updateAsset(
258                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
259                            assetLinkEntryIds);
260            }
261    
262            public FileEntry updateFileEntry(
263                            long userId, long fileEntryId, String sourceFileName,
264                            String mimeType, String title, String description, String changeLog,
265                            boolean majorVersion, File file, ServiceContext serviceContext)
266                    throws PortalException, SystemException {
267    
268                    long fileEntryTypeId = ParamUtil.getLong(
269                            serviceContext, "fileEntryTypeId", -1L);
270                    Map<String, Fields> fieldsMap = getFieldsMap(
271                            serviceContext, fileEntryTypeId);
272                    long size = 0;
273    
274                    if (file != null) {
275                            size = file.length();
276                    }
277    
278                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
279                            userId, fileEntryId, sourceFileName, mimeType, title, description,
280                            changeLog, majorVersion, fileEntryTypeId, fieldsMap, file, null,
281                            size, serviceContext);
282    
283                    return new LiferayFileEntry(dlFileEntry);
284            }
285    
286            public FileEntry updateFileEntry(
287                            long userId, long fileEntryId, String sourceFileName,
288                            String mimeType, String title, String description, String changeLog,
289                            boolean majorVersion, InputStream is, long size,
290                            ServiceContext serviceContext)
291                    throws PortalException, SystemException {
292    
293                    long fileEntryTypeId = ParamUtil.getLong(
294                            serviceContext, "fileEntryTypeId", -1L);
295                    Map<String, Fields> fieldsMap = getFieldsMap(
296                            serviceContext, fileEntryTypeId);
297    
298                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
299                            userId, fileEntryId, sourceFileName, mimeType, title, description,
300                            changeLog, majorVersion, fileEntryTypeId, fieldsMap, null, is, size,
301                            serviceContext);
302    
303                    return new LiferayFileEntry(dlFileEntry);
304            }
305    
306            public Folder updateFolder(
307                            long folderId, long parentFolderId, String title,
308                            String description, ServiceContext serviceContext)
309                    throws PortalException, SystemException {
310    
311                    long defaultFileEntryTypeId = ParamUtil.getLong(
312                            serviceContext, "defaultFileEntryTypeId");
313                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
314                            serviceContext, "dlFileEntryTypesSearchContainerPrimaryKeys");
315                    boolean overrideFileEntryTypes = ParamUtil.getBoolean(
316                            serviceContext, "overrideFileEntryTypes");
317    
318                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
319                            toFolderId(folderId), toFolderId(parentFolderId), title,
320                            description, defaultFileEntryTypeId, fileEntryTypeIds,
321                            overrideFileEntryTypes, serviceContext);
322    
323                    return new LiferayFolder(dlFolder);
324            }
325    
326            public UnicodeProperties updateRepository(
327                    UnicodeProperties typeSettingsProperties) {
328    
329                    return typeSettingsProperties;
330            }
331    
332            @Override
333            protected void initByFileEntryId(long fileEntryId) {
334                    try {
335                            DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
336                                    fileEntryId);
337    
338                            initByRepositoryId(dlFileEntry.getRepositoryId());
339                    }
340                    catch (Exception e) {
341                            if (_log.isTraceEnabled()) {
342                                    if (e instanceof NoSuchFileEntryException) {
343                                            _log.trace(e.getMessage());
344                                    }
345                                    else {
346                                            _log.trace(e, e);
347                                    }
348                            }
349                    }
350            }
351    
352            @Override
353            protected void initByFileVersionId(long fileVersionId) {
354                    try {
355                            DLFileVersion dlFileVersion =
356                                    dlFileVersionLocalService.getFileVersion(fileVersionId);
357    
358                            initByRepositoryId(dlFileVersion.getRepositoryId());
359                    }
360                    catch (Exception e) {
361                            if (_log.isTraceEnabled()) {
362                                    if (e instanceof NoSuchFileVersionException) {
363                                            _log.trace(e.getMessage());
364                                    }
365                                    else {
366                                            _log.trace(e, e);
367                                    }
368                            }
369                    }
370            }
371    
372            @Override
373            protected void initByFolderId(long folderId) {
374                    try {
375                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
376    
377                            initByRepositoryId(dlFolder.getRepositoryId());
378                    }
379                    catch (Exception e) {
380                            if (_log.isTraceEnabled()) {
381                                    if (e instanceof NoSuchFolderException) {
382                                            _log.trace(e.getMessage());
383                                    }
384                                    else {
385                                            _log.trace(e, e);
386                                    }
387                            }
388                    }
389            }
390    
391            @Override
392            protected void initByRepositoryId(long repositoryId) {
393                    setGroupId(repositoryId);
394                    setRepositoryId(repositoryId);
395    
396                    try {
397                            Repository repository = repositoryLocalService.getRepository(
398                                    repositoryId);
399    
400                            setDlFolderId(repository.getDlFolderId());
401                            setGroupId(repository.getGroupId());
402                            setRepositoryId(repository.getRepositoryId());
403                    }
404                    catch (Exception e) {
405                    }
406            }
407    
408            private static Log _log = LogFactoryUtil.getLog(
409                    LiferayLocalRepository.class);
410    
411    }