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.external;
016    
017    import com.liferay.asset.kernel.service.AssetEntryLocalService;
018    import com.liferay.document.library.kernel.service.DLAppHelperLocalService;
019    import com.liferay.document.library.kernel.service.DLFolderLocalService;
020    import com.liferay.exportimport.kernel.lar.ExportImportThreadLocal;
021    import com.liferay.portal.kernel.bean.BeanReference;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.model.ClassName;
024    import com.liferay.portal.kernel.repository.BaseRepository;
025    import com.liferay.portal.kernel.repository.LocalRepository;
026    import com.liferay.portal.kernel.repository.Repository;
027    import com.liferay.portal.kernel.repository.RepositoryException;
028    import com.liferay.portal.kernel.repository.RepositoryFactory;
029    import com.liferay.portal.kernel.service.ClassNameLocalService;
030    import com.liferay.portal.kernel.service.CompanyLocalService;
031    import com.liferay.portal.kernel.service.RepositoryEntryLocalService;
032    import com.liferay.portal.kernel.service.RepositoryLocalService;
033    import com.liferay.portal.kernel.service.UserLocalService;
034    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
035    import com.liferay.portal.repository.util.ExternalRepositoryFactoryUtil;
036    
037    /**
038     * @author Adolfo Pérez
039     */
040    public class LegacyExternalRepositoryFactory implements RepositoryFactory {
041    
042            @Override
043            public LocalRepository createLocalRepository(long repositoryId)
044                    throws PortalException {
045    
046                    BaseRepository baseRepository = createBaseRepository(repositoryId);
047    
048                    return baseRepository.getLocalRepository();
049            }
050    
051            @Override
052            public Repository createRepository(long repositoryId)
053                    throws PortalException {
054    
055                    return createBaseRepository(repositoryId);
056            }
057    
058            protected BaseRepository createBaseRepository(long repositoryId)
059                    throws PortalException {
060    
061                    long classNameId = getRepositoryClassNameId(repositoryId);
062    
063                    return createExternalRepositoryImpl(repositoryId, classNameId);
064            }
065    
066            protected BaseRepository createExternalRepositoryImpl(
067                            long repositoryId, long classNameId)
068                    throws PortalException {
069    
070                    BaseRepository baseRepository = null;
071    
072                    com.liferay.portal.kernel.model.Repository repository = null;
073    
074                    try {
075                            repository = _repositoryLocalService.getRepository(repositoryId);
076    
077                            ClassName className = _classNameLocalService.getClassName(
078                                    classNameId);
079    
080                            String repositoryImplClassName = className.getValue();
081    
082                            baseRepository = ExternalRepositoryFactoryUtil.getInstance(
083                                    repositoryImplClassName);
084                    }
085                    catch (Exception e) {
086                            throw new RepositoryException(
087                                    "Unable to find a valid repository for class name ID " +
088                                            classNameId,
089                                    e);
090                    }
091    
092                    setupRepository(repositoryId, repository, baseRepository);
093    
094                    if (!ExportImportThreadLocal.isImportInProcess()) {
095                            baseRepository.initRepository();
096                    }
097    
098                    return baseRepository;
099            }
100    
101            protected long getRepositoryClassNameId(long repositoryId) {
102                    com.liferay.portal.kernel.model.Repository repository =
103                            _repositoryLocalService.fetchRepository(repositoryId);
104    
105                    if (repository != null) {
106                            return repository.getClassNameId();
107                    }
108    
109                    return _classNameLocalService.getClassNameId(
110                            LiferayRepository.class.getName());
111            }
112    
113            protected void setupRepository(
114                    long repositoryId,
115                    com.liferay.portal.kernel.model.Repository repository,
116                    BaseRepository baseRepository) {
117    
118                    baseRepository.setAssetEntryLocalService(_assetEntryLocalService);
119                    baseRepository.setCompanyId(repository.getCompanyId());
120                    baseRepository.setCompanyLocalService(_companyLocalService);
121                    baseRepository.setDLAppHelperLocalService(_dlAppHelperLocalService);
122                    baseRepository.setDLFolderLocalService(_dlFolderLocalService);
123                    baseRepository.setGroupId(repository.getGroupId());
124                    baseRepository.setRepositoryEntryLocalService(
125                            _repositoryEntryLocalService);
126                    baseRepository.setRepositoryId(repositoryId);
127                    baseRepository.setTypeSettingsProperties(
128                            repository.getTypeSettingsProperties());
129                    baseRepository.setUserLocalService(_userLocalService);
130            }
131    
132            @BeanReference(type = AssetEntryLocalService.class)
133            private AssetEntryLocalService _assetEntryLocalService;
134    
135            @BeanReference(type = ClassNameLocalService.class)
136            private ClassNameLocalService _classNameLocalService;
137    
138            @BeanReference(type = CompanyLocalService.class)
139            private CompanyLocalService _companyLocalService;
140    
141            @BeanReference(type = DLAppHelperLocalService.class)
142            private DLAppHelperLocalService _dlAppHelperLocalService;
143    
144            @BeanReference(type = DLFolderLocalService.class)
145            private DLFolderLocalService _dlFolderLocalService;
146    
147            @BeanReference(type = RepositoryEntryLocalService.class)
148            private RepositoryEntryLocalService _repositoryEntryLocalService;
149    
150            @BeanReference(type = RepositoryLocalService.class)
151            private RepositoryLocalService _repositoryLocalService;
152    
153            @BeanReference(type = UserLocalService.class)
154            private UserLocalService _userLocalService;
155    
156    }