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