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