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.OrderByComparator;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.SortedArrayList;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.model.Repository;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
031    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
032    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
033    import com.liferay.portal.service.RepositoryLocalService;
034    import com.liferay.portal.service.RepositoryService;
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.List;
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                    DLFileVersionLocalService dlFileVersionLocalService,
070                    DLFileVersionService dlFileVersionService,
071                    DLFolderLocalService dlFolderLocalService,
072                    DLFolderService dlFolderService, long repositoryId) {
073    
074                    super(
075                            repositoryLocalService, repositoryService, dlAppHelperLocalService,
076                            dlFileEntryLocalService, dlFileEntryService,
077                            dlFileVersionLocalService, dlFileVersionService,
078                            dlFolderLocalService, dlFolderService, repositoryId);
079            }
080    
081            public LiferayLocalRepository(
082                    RepositoryLocalService repositoryLocalService,
083                    RepositoryService repositoryService,
084                    DLAppHelperLocalService dlAppHelperLocalService,
085                    DLFileEntryLocalService dlFileEntryLocalService,
086                    DLFileEntryService dlFileEntryService,
087                    DLFileVersionLocalService dlFileVersionLocalService,
088                    DLFileVersionService dlFileVersionService,
089                    DLFolderLocalService dlFolderLocalService,
090                    DLFolderService dlFolderService, long folderId, long fileEntryId,
091                    long fileVersionId) {
092    
093                    super(
094                            repositoryLocalService, repositoryService, dlAppHelperLocalService,
095                            dlFileEntryLocalService, dlFileEntryService,
096                            dlFileVersionLocalService, dlFileVersionService,
097                            dlFolderLocalService, dlFolderService, folderId, fileEntryId,
098                            fileVersionId);
099            }
100    
101            public FileEntry addFileEntry(
102                            long userId, long folderId, String sourceFileName, String mimeType,
103                            String title, String description, String changeLog, File file,
104                            ServiceContext serviceContext)
105                    throws PortalException, SystemException {
106    
107                    long fileEntryTypeId = ParamUtil.getLong(
108                            serviceContext, "fileEntryTypeId", -1L);
109                    Map<String, Fields> fieldsMap = getFieldsMap(
110                            serviceContext, fileEntryTypeId);
111                    long size = 0;
112    
113                    if (file != null) {
114                            size = file.length();
115                    }
116    
117                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
118                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
119                            sourceFileName, mimeType, title, description, changeLog,
120                            fileEntryTypeId, fieldsMap, file, null, size, serviceContext);
121    
122                    addFileEntryResources(dlFileEntry, serviceContext);
123    
124                    return new LiferayFileEntry(dlFileEntry);
125            }
126    
127            public FileEntry addFileEntry(
128                            long userId, long folderId, String sourceFileName, String mimeType,
129                            String title, String description, String changeLog, InputStream is,
130                            long size, ServiceContext serviceContext)
131                    throws PortalException, SystemException {
132    
133                    long fileEntryTypeId = ParamUtil.getLong(
134                            serviceContext, "fileEntryTypeId", -1L);
135                    Map<String, Fields> fieldsMap = getFieldsMap(
136                            serviceContext, fileEntryTypeId);
137    
138                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
139                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
140                            sourceFileName, mimeType, title, description, changeLog,
141                            fileEntryTypeId, fieldsMap, null, is, size, serviceContext);
142    
143                    addFileEntryResources(dlFileEntry, serviceContext);
144    
145                    return new LiferayFileEntry(dlFileEntry);
146            }
147    
148            public Folder addFolder(
149                            long userId, long parentFolderId, String title, String description,
150                            ServiceContext serviceContext)
151                    throws PortalException, SystemException {
152    
153                    boolean mountPoint = ParamUtil.getBoolean(serviceContext, "mountPoint");
154    
155                    DLFolder dlFolder = dlFolderLocalService.addFolder(
156                            userId, getGroupId(), getRepositoryId(), mountPoint,
157                            toFolderId(parentFolderId), title, description, serviceContext);
158    
159                    return new LiferayFolder(dlFolder);
160            }
161    
162            public void addRepository(
163                    long groupId, String name, String description, String portletKey,
164                    UnicodeProperties typeSettingsProperties) {
165            }
166    
167            public void deleteAll() throws PortalException, SystemException {
168                    dlFolderLocalService.deleteAll(getGroupId());
169            }
170    
171            public void deleteFileEntry(long fileEntryId)
172                    throws PortalException, SystemException {
173    
174                    dlFileEntryLocalService.deleteFileEntry(fileEntryId);
175            }
176    
177            public void deleteFolder(long folderId)
178                    throws PortalException, SystemException {
179    
180                    dlFolderLocalService.deleteFolder(folderId);
181            }
182    
183            public List<FileEntry> getFileEntries(
184                            long folderId, int start, int end, OrderByComparator obc)
185                    throws SystemException {
186    
187                    List<DLFileEntry> dlFileEntries =
188                            dlFileEntryLocalService.getFileEntries(
189                                    getGroupId(), toFolderId(folderId), start, end, obc);
190    
191                    return toFileEntries(dlFileEntries);
192            }
193    
194            public List<Object> getFileEntriesAndFileShortcuts(
195                            long folderId, int status, int start, int end)
196                    throws SystemException {
197    
198                    List<Object> dlFileEntriesAndFileShortcuts =
199                            dlFolderLocalService.getFileEntriesAndFileShortcuts(
200                                    getGroupId(), toFolderId(folderId), status, start, end);
201    
202                    return toFileEntriesAndFolders(dlFileEntriesAndFileShortcuts);
203            }
204    
205            public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
206                    throws SystemException {
207    
208                    return dlFolderLocalService.getFileEntriesAndFileShortcutsCount(
209                            getGroupId(), toFolderId(folderId), status);
210            }
211    
212            public int getFileEntriesCount(long folderId) throws SystemException {
213                    return dlFileEntryLocalService.getFileEntriesCount(
214                            getGroupId(), toFolderId(folderId));
215            }
216    
217            public FileEntry getFileEntry(long fileEntryId)
218                    throws PortalException, SystemException {
219    
220                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
221                            fileEntryId);
222    
223                    return new LiferayFileEntry(dlFileEntry);
224            }
225    
226            public FileEntry getFileEntry(long folderId, String title)
227                    throws PortalException, SystemException {
228    
229                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
230                            getGroupId(), toFolderId(folderId), title);
231    
232                    return new LiferayFileEntry(dlFileEntry);
233            }
234    
235            public FileEntry getFileEntryByUuid(String uuid)
236                    throws PortalException, SystemException {
237    
238                    DLFileEntry dlFileEntry =
239                            dlFileEntryLocalService.getFileEntryByUuidAndGroupId(
240                                    uuid, getGroupId());
241    
242                    return new LiferayFileEntry(dlFileEntry);
243            }
244    
245            public FileVersion getFileVersion(long fileVersionId)
246                    throws PortalException, SystemException {
247    
248                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
249                            fileVersionId);
250    
251                    return new LiferayFileVersion(dlFileVersion);
252            }
253    
254            public Folder getFolder(long folderId)
255                    throws PortalException, SystemException {
256    
257                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
258    
259                    return new LiferayFolder(dlFolder);
260            }
261    
262            public Folder getFolder(long parentFolderId, String title)
263                    throws PortalException, SystemException {
264    
265                    DLFolder dlFolder = dlFolderLocalService.getFolder(
266                            getGroupId(), toFolderId(parentFolderId), title);
267    
268                    return new LiferayFolder(dlFolder);
269            }
270    
271            public List<Folder> getFolders(
272                            long parentFolderId, boolean includeMountfolders, int start,
273                            int end, OrderByComparator obc)
274                    throws SystemException {
275    
276                    List<DLFolder> dlFolders = dlFolderLocalService.getFolders(
277                            getGroupId(), toFolderId(parentFolderId), includeMountfolders,
278                            start, end, obc);
279    
280                    return toFolders(dlFolders);
281            }
282    
283            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
284                            long folderId, int status, boolean includeMountFolders, int start,
285                            int end, OrderByComparator obc)
286                    throws SystemException {
287    
288                    List<Object> dlFoldersAndFileEntriesAndFileShortcuts =
289                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
290                                    getGroupId(), toFolderId(folderId), status, includeMountFolders,
291                                    start, end, obc);
292    
293                    return toFileEntriesAndFolders(dlFoldersAndFileEntriesAndFileShortcuts);
294            }
295    
296            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
297                            long folderId, int status, String[] mimeTypes,
298                            boolean includeMountFolders, int start, int end,
299                            OrderByComparator obc)
300                    throws SystemException {
301    
302                    List<Object> dlFoldersAndFileEntriesAndFileShortcuts =
303                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
304                                    getGroupId(), toFolderId(folderId), status, mimeTypes,
305                                    includeMountFolders, start, end, obc);
306    
307                    return toFileEntriesAndFolders(dlFoldersAndFileEntriesAndFileShortcuts);
308            }
309    
310            public int getFoldersAndFileEntriesAndFileShortcutsCount(
311                            long folderId, int status, boolean includeMountFolders)
312                    throws SystemException {
313    
314                    return dlFolderLocalService.
315                            getFoldersAndFileEntriesAndFileShortcutsCount(
316                                    getGroupId(), toFolderId(folderId), status,
317                                    includeMountFolders);
318            }
319    
320            public int getFoldersAndFileEntriesAndFileShortcutsCount(
321                            long folderId, int status, String[] mimeTypes,
322                            boolean includeMountFolders)
323                    throws SystemException {
324    
325                    return dlFolderLocalService.
326                            getFoldersAndFileEntriesAndFileShortcutsCount(
327                                    getGroupId(), toFolderId(folderId), status, mimeTypes,
328                                    includeMountFolders);
329            }
330    
331            public int getFoldersCount(long parentFolderId, boolean includeMountfolders)
332                    throws SystemException {
333    
334                    return dlFolderLocalService.getFoldersCount(
335                            getGroupId(), toFolderId(parentFolderId), includeMountfolders);
336            }
337    
338            public int getFoldersFileEntriesCount(List<Long> folderIds, int status)
339                    throws SystemException {
340    
341                    return dlFolderLocalService.getFoldersFileEntriesCount(
342                            getGroupId(), toFolderIds(folderIds), status);
343            }
344    
345            public List<Folder> getMountFolders(
346                            long parentFolderId, int start, int end, OrderByComparator obc)
347                    throws SystemException {
348    
349                    List<DLFolder> dlFolders = dlFolderLocalService.getMountFolders(
350                            getGroupId(), toFolderId(parentFolderId), start, end, obc);
351    
352                    return toFolders(dlFolders);
353            }
354    
355            public int getMountFoldersCount(long parentFolderId)
356                    throws SystemException {
357    
358                    return dlFolderLocalService.getMountFoldersCount(
359                            getGroupId(), toFolderId(parentFolderId));
360            }
361    
362            public FileEntry moveFileEntry(
363                            long userId, long fileEntryId, long newFolderId,
364                            ServiceContext serviceContext)
365                    throws PortalException, SystemException {
366    
367                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
368                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
369    
370                    return new LiferayFileEntry(dlFileEntry);
371            }
372    
373            public void updateAsset(
374                            long userId, FileEntry fileEntry, FileVersion fileVersion,
375                            long[] assetCategoryIds, String[] assetTagNames,
376                            long[] assetLinkEntryIds)
377                    throws PortalException, SystemException {
378    
379                    dlAppHelperLocalService.updateAsset(
380                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
381                            assetLinkEntryIds);
382            }
383    
384            public FileEntry updateFileEntry(
385                            long userId, long fileEntryId, String sourceFileName,
386                            String mimeType, String title, String description, String changeLog,
387                            boolean majorVersion, File file, ServiceContext serviceContext)
388                    throws PortalException, SystemException {
389    
390                    long fileEntryTypeId = ParamUtil.getLong(
391                            serviceContext, "fileEntryTypeId", -1L);
392                    Map<String, Fields> fieldsMap = getFieldsMap(
393                            serviceContext, fileEntryTypeId);
394                    long size = 0;
395    
396                    if (file != null) {
397                            size = file.length();
398                    }
399    
400                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
401                            userId, fileEntryId, sourceFileName, mimeType, title, description,
402                            changeLog, majorVersion, fileEntryTypeId, fieldsMap, file, null,
403                            size, serviceContext);
404    
405                    return new LiferayFileEntry(dlFileEntry);
406            }
407    
408            public FileEntry updateFileEntry(
409                            long userId, long fileEntryId, String sourceFileName,
410                            String mimeType, String title, String description, String changeLog,
411                            boolean majorVersion, InputStream is, long size,
412                            ServiceContext serviceContext)
413                    throws PortalException, SystemException {
414    
415                    long fileEntryTypeId = ParamUtil.getLong(
416                            serviceContext, "fileEntryTypeId", -1L);
417                    Map<String, Fields> fieldsMap = getFieldsMap(
418                            serviceContext, fileEntryTypeId);
419    
420                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
421                            userId, fileEntryId, sourceFileName, mimeType, title, description,
422                            changeLog, majorVersion, fileEntryTypeId, fieldsMap, null, is, size,
423                            serviceContext);
424    
425                    return new LiferayFileEntry(dlFileEntry);
426            }
427    
428            public Folder updateFolder(
429                            long folderId, long parentFolderId, String title,
430                            String description, ServiceContext serviceContext)
431                    throws PortalException, SystemException {
432    
433                    long defaultFileEntryTypeId = ParamUtil.getLong(
434                            serviceContext, "defaultFileEntryTypeId");
435                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
436                            serviceContext, "fileEntryTypeSearchContainerPrimaryKeys");
437                    boolean overrideFileEntryTypes = ParamUtil.getBoolean(
438                            serviceContext, "overrideFileEntryTypes");
439    
440                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
441                            toFolderId(folderId), toFolderId(parentFolderId), title,
442                            description, defaultFileEntryTypeId, fileEntryTypeIds,
443                            overrideFileEntryTypes, serviceContext);
444    
445                    return new LiferayFolder(dlFolder);
446            }
447    
448            public UnicodeProperties updateRepository(
449                    UnicodeProperties typeSettingsProperties) {
450    
451                    return typeSettingsProperties;
452            }
453    
454            @Override
455            protected void initByFileEntryId(long fileEntryId) {
456                    try {
457                            DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
458                                    fileEntryId);
459    
460                            initByRepositoryId(dlFileEntry.getRepositoryId());
461                    }
462                    catch (Exception e) {
463                            if (_log.isTraceEnabled()) {
464                                    if (e instanceof NoSuchFileEntryException) {
465                                            _log.trace(e.getMessage());
466                                    }
467                                    else {
468                                            _log.trace(e, e);
469                                    }
470                            }
471                    }
472            }
473    
474            @Override
475            protected void initByFileVersionId(long fileVersionId) {
476                    try {
477                            DLFileVersion dlFileVersion =
478                                    dlFileVersionLocalService.getFileVersion(fileVersionId);
479    
480                            initByRepositoryId(dlFileVersion.getRepositoryId());
481                    }
482                    catch (Exception e) {
483                            if (_log.isTraceEnabled()) {
484                                    if (e instanceof NoSuchFileVersionException) {
485                                            _log.trace(e.getMessage());
486                                    }
487                                    else {
488                                            _log.trace(e, e);
489                                    }
490                            }
491                    }
492            }
493    
494            @Override
495            protected void initByFolderId(long folderId) {
496                    try {
497                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
498    
499                            initByRepositoryId(dlFolder.getRepositoryId());
500                    }
501                    catch (Exception e) {
502                            if (_log.isTraceEnabled()) {
503                                    if (e instanceof NoSuchFolderException) {
504                                            _log.trace(e.getMessage());
505                                    }
506                                    else {
507                                            _log.trace(e, e);
508                                    }
509                            }
510                    }
511            }
512    
513            @Override
514            protected void initByRepositoryId(long repositoryId) {
515                    setGroupId(repositoryId);
516                    setRepositoryId(repositoryId);
517    
518                    try {
519                            Repository repository = repositoryLocalService.getRepository(
520                                    repositoryId);
521    
522                            setDlFolderId(repository.getDlFolderId());
523                            setGroupId(repository.getGroupId());
524                            setRepositoryId(repository.getRepositoryId());
525                    }
526                    catch (Exception e) {
527                    }
528            }
529    
530            private static Log _log = LogFactoryUtil.getLog(
531                    LiferayLocalRepository.class);
532    
533    }