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