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.exception.PortalException;
018    import com.liferay.portal.kernel.repository.DocumentRepository;
019    import com.liferay.portal.kernel.repository.LocalRepository;
020    import com.liferay.portal.model.Repository;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.RepositoryLocalService;
023    import com.liferay.portal.service.RepositoryLocalServiceUtil;
024    import com.liferay.portal.service.RepositoryService;
025    import com.liferay.portal.service.RepositoryServiceUtil;
026    
027    /**
028     * @author Iv??n Zaera
029     */
030    public class RepositoryServiceAdapter {
031    
032            public static RepositoryServiceAdapter create(
033                    DocumentRepository documentRepository) {
034    
035                    if (documentRepository instanceof LocalRepository) {
036                            return new RepositoryServiceAdapter(
037                                    RepositoryLocalServiceUtil.getService());
038                    }
039    
040                    return new RepositoryServiceAdapter(
041                            RepositoryLocalServiceUtil.getService(),
042                            RepositoryServiceUtil.getService());
043            }
044    
045            public RepositoryServiceAdapter(
046                    RepositoryLocalService repositoryLocalService) {
047    
048                    this(repositoryLocalService, null);
049            }
050    
051            public RepositoryServiceAdapter(
052                    RepositoryLocalService repositoryLocalService,
053                    RepositoryService repositoryService) {
054    
055                    _repositoryLocalService = repositoryLocalService;
056                    _repositoryService = repositoryService;
057            }
058    
059            public Repository fetchRepository(long repositoryId)
060                    throws PortalException {
061    
062                    Repository repository = null;
063    
064                    if (_repositoryService != null) {
065                            repository = _repositoryLocalService.fetchRepository(repositoryId);
066    
067                            if (repository != null) {
068                                    repository = _repositoryService.getRepository(repositoryId);
069                            }
070                    }
071                    else {
072                            repository = _repositoryLocalService.fetchRepository(repositoryId);
073                    }
074    
075                    return repository;
076            }
077    
078            public Repository getRepository(long repositoryId) throws PortalException {
079                    Repository repository = null;
080    
081                    if (_repositoryService != null) {
082                            repository = _repositoryService.getRepository(repositoryId);
083                    }
084                    else {
085                            repository = _repositoryLocalService.getRepository(repositoryId);
086                    }
087    
088                    return repository;
089            }
090    
091            public Repository updateRepository(Repository repository)
092                    throws PrincipalException {
093    
094                    if (_repositoryService != null) {
095                            throw new PrincipalException("Repository service is not null");
096                    }
097    
098                    return _repositoryLocalService.updateRepository(repository);
099            }
100    
101            private final RepositoryLocalService _repositoryLocalService;
102            private final RepositoryService _repositoryService;
103    
104    }