001    /**
002     * Copyright (c) 2000-present 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.capabilities.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.DocumentRepository;
020    import com.liferay.portal.kernel.repository.LocalRepository;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
030    
031    import java.io.Serializable;
032    
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Iv??n Zaera
038     */
039    public class DLFileEntryServiceAdapter {
040    
041            public static DLFileEntryServiceAdapter create(
042                    DocumentRepository documentRepository) {
043    
044                    if (documentRepository instanceof LocalRepository) {
045                            return new DLFileEntryServiceAdapter(
046                                    DLFileEntryLocalServiceUtil.getService());
047                    }
048    
049                    return new DLFileEntryServiceAdapter(
050                            DLFileEntryLocalServiceUtil.getService(),
051                            DLFileEntryServiceUtil.getService());
052            }
053    
054            public DLFileEntryServiceAdapter(
055                    DLFileEntryLocalService dlFileEntryLocalService) {
056    
057                    this(dlFileEntryLocalService, null);
058            }
059    
060            public DLFileEntryServiceAdapter(
061                    DLFileEntryLocalService dlFileEntryLocalService,
062                    DLFileEntryService dlFileEntryService) {
063    
064                    _dlFileEntryLocalService = dlFileEntryLocalService;
065                    _dlFileEntryService = dlFileEntryService;
066            }
067    
068            public DLFileEntry fetchDLFileEntryByImageId(long imageId)
069                    throws PortalException {
070    
071                    DLFileEntry dlFileEntry = null;
072    
073                    if (_dlFileEntryService != null) {
074                            dlFileEntry = _dlFileEntryService.fetchFileEntryByImageId(imageId);
075                    }
076                    else {
077                            dlFileEntry = _dlFileEntryLocalService.fetchFileEntryByAnyImageId(
078                                    imageId);
079                    }
080    
081                    return dlFileEntry;
082            }
083    
084            public ActionableDynamicQuery getActionableDynamicQuery()
085                    throws PortalException {
086    
087                    if (_dlFileEntryService != null) {
088                            throw new PrincipalException("DL file entry service is not null");
089                    }
090    
091                    return _dlFileEntryLocalService.getActionableDynamicQuery();
092            }
093    
094            public DLFileEntry getDLFileEntry(long fileEntryId) throws PortalException {
095                    DLFileEntry dlFileEntry = null;
096    
097                    if (_dlFileEntryService != null) {
098                            dlFileEntry = _dlFileEntryService.getFileEntry(fileEntryId);
099                    }
100                    else {
101                            dlFileEntry = _dlFileEntryLocalService.getFileEntry(fileEntryId);
102                    }
103    
104                    return dlFileEntry;
105            }
106    
107            public List<DLFileEntry> getGroupFileEntries(
108                            long groupId, int userId, long repositoryId, long folderId,
109                            int start, int end, OrderByComparator<DLFileEntry> obc)
110                    throws PortalException {
111    
112                    List<DLFileEntry> dlFileEntries = null;
113    
114                    if (_dlFileEntryService != null) {
115                            dlFileEntries = _dlFileEntryService.getGroupFileEntries(
116                                    groupId, userId, repositoryId, folderId, null,
117                                    WorkflowConstants.STATUS_ANY, start, end, obc);
118                    }
119                    else {
120                            dlFileEntries = _dlFileEntryLocalService.getGroupFileEntries(
121                                    groupId, userId, repositoryId, folderId, start, end, obc);
122                    }
123    
124                    return dlFileEntries;
125            }
126    
127            public boolean isKeepFileVersionLabel(
128                            long fileEntryId, ServiceContext serviceContext)
129                    throws PortalException {
130    
131                    if (_dlFileEntryService != null) {
132                            return _dlFileEntryService.isKeepFileVersionLabel(
133                                    fileEntryId, serviceContext);
134                    }
135                    else {
136                            return _dlFileEntryLocalService.isKeepFileVersionLabel(
137                                    fileEntryId, serviceContext);
138                    }
139            }
140    
141            public DLFileEntry updateStatus(
142                            long userId, long fileVersionId, int status,
143                            ServiceContext serviceContext,
144                            Map<String, Serializable> workflowContext)
145                    throws PortalException {
146    
147                    DLFileEntry dlFileEntry = null;
148    
149                    if (_dlFileEntryService != null) {
150                            dlFileEntry = _dlFileEntryService.updateStatus(
151                                    userId, fileVersionId, status, serviceContext, workflowContext);
152                    }
153                    else {
154                            dlFileEntry = _dlFileEntryLocalService.updateStatus(
155                                    userId, fileVersionId, status, serviceContext, workflowContext);
156                    }
157    
158                    return dlFileEntry;
159            }
160    
161            private final DLFileEntryLocalService _dlFileEntryLocalService;
162            private final DLFileEntryService _dlFileEntryService;
163    
164    }