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.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.BaseRepository;
020    import com.liferay.portal.kernel.repository.LocalRepository;
021    import com.liferay.portal.kernel.repository.Repository;
022    import com.liferay.portal.kernel.repository.RepositoryException;
023    import com.liferay.portal.kernel.repository.RepositoryFactory;
024    import com.liferay.portal.model.ClassName;
025    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
026    import com.liferay.portal.repository.util.ExternalRepositoryFactoryUtil;
027    import com.liferay.portal.service.ClassNameLocalService;
028    import com.liferay.portal.service.CompanyLocalService;
029    import com.liferay.portal.service.RepositoryEntryLocalService;
030    import com.liferay.portal.service.RepositoryLocalService;
031    import com.liferay.portal.service.UserLocalService;
032    import com.liferay.portlet.asset.service.AssetEntryLocalService;
033    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
034    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
035    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
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.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.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, com.liferay.portal.model.Repository repository,
115                    BaseRepository baseRepository) {
116    
117                    baseRepository.setAssetEntryLocalService(_assetEntryLocalService);
118                    baseRepository.setCompanyId(repository.getCompanyId());
119                    baseRepository.setCompanyLocalService(_companyLocalService);
120                    baseRepository.setDLAppHelperLocalService(_dlAppHelperLocalService);
121                    baseRepository.setDLFolderLocalService(_dlFolderLocalService);
122                    baseRepository.setGroupId(repository.getGroupId());
123                    baseRepository.setRepositoryEntryLocalService(
124                            _repositoryEntryLocalService);
125                    baseRepository.setRepositoryId(repositoryId);
126                    baseRepository.setTypeSettingsProperties(
127                            repository.getTypeSettingsProperties());
128                    baseRepository.setUserLocalService(_userLocalService);
129            }
130    
131            @BeanReference(type = AssetEntryLocalService.class)
132            private AssetEntryLocalService _assetEntryLocalService;
133    
134            @BeanReference(type = ClassNameLocalService.class)
135            private ClassNameLocalService _classNameLocalService;
136    
137            @BeanReference(type = CompanyLocalService.class)
138            private CompanyLocalService _companyLocalService;
139    
140            @BeanReference(type = DLAppHelperLocalService.class)
141            private DLAppHelperLocalService _dlAppHelperLocalService;
142    
143            @BeanReference(type = DLFolderLocalService.class)
144            private DLFolderLocalService _dlFolderLocalService;
145    
146            @BeanReference(type = RepositoryEntryLocalService.class)
147            private RepositoryEntryLocalService _repositoryEntryLocalService;
148    
149            @BeanReference(type = RepositoryLocalService.class)
150            private RepositoryLocalService _repositoryLocalService;
151    
152            @BeanReference(type = UserLocalService.class)
153            private UserLocalService _userLocalService;
154    
155    }