001    /**
002     * Copyright (c) 2000-2013 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.service.impl;
016    
017    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.Repository;
021    import com.liferay.portal.kernel.repository.cmis.CMISRepositoryHandler;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.FileVersion;
024    import com.liferay.portal.kernel.repository.model.Folder;
025    import com.liferay.portal.kernel.util.ProxyUtil;
026    import com.liferay.portal.repository.cmis.CMISRepository;
027    import com.liferay.portal.repository.proxy.BaseRepositoryProxyBean;
028    import com.liferay.portal.service.base.CMISRepositoryLocalServiceBaseImpl;
029    
030    import org.apache.chemistry.opencmis.client.api.Document;
031    
032    /**
033     * @author Alexander Chow
034     */
035    public class CMISRepositoryLocalServiceImpl
036            extends CMISRepositoryLocalServiceBaseImpl {
037    
038            @Override
039            public Object getSession(long repositoryId)
040                    throws PortalException, SystemException {
041    
042                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
043    
044                    return cmisRepository.getSession();
045            }
046    
047            @Override
048            public FileEntry toFileEntry(long repositoryId, Object object)
049                    throws PortalException, SystemException {
050    
051                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
052    
053                    Document document = (Document)object;
054    
055                    return cmisRepository.toFileEntry(document);
056            }
057    
058            @Override
059            public FileVersion toFileVersion(long repositoryId, Object object)
060                    throws PortalException, SystemException {
061    
062                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
063    
064                    Document document = (Document)object;
065    
066                    return cmisRepository.toFileVersion(document);
067            }
068    
069            @Override
070            public Folder toFolder(long repositoryId, Object object)
071                    throws PortalException, SystemException {
072    
073                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
074    
075                    org.apache.chemistry.opencmis.client.api.Folder cmisFolder =
076                            (org.apache.chemistry.opencmis.client.api.Folder)object;
077    
078                    return cmisRepository.toFolder(cmisFolder);
079            }
080    
081            protected CMISRepository getCmisRepository(long repositoryId)
082                    throws PortalException, SystemException {
083    
084                    Repository repositoryImpl = repositoryLocalService.getRepositoryImpl(
085                            repositoryId);
086    
087                    CMISRepositoryHandler cmisRepositoryHandler = null;
088    
089                    if (repositoryImpl instanceof CMISRepositoryHandler) {
090                            cmisRepositoryHandler = (CMISRepositoryHandler)repositoryImpl;
091                    }
092                    else if (repositoryImpl instanceof BaseRepositoryProxyBean) {
093                            BaseRepositoryProxyBean baseRepositoryProxyBean =
094                                    (BaseRepositoryProxyBean)repositoryImpl;
095    
096                            ClassLoaderBeanHandler classLoaderBeanHandler =
097                                    (ClassLoaderBeanHandler)ProxyUtil.getInvocationHandler(
098                                            baseRepositoryProxyBean.getProxyBean());
099    
100                            Object bean = classLoaderBeanHandler.getBean();
101    
102                            if (bean instanceof CMISRepositoryHandler) {
103                                    cmisRepositoryHandler = (CMISRepositoryHandler)bean;
104                            }
105                    }
106    
107                    CMISRepository cmisRepository =
108                            (CMISRepository)cmisRepositoryHandler.getCmisRepository();
109    
110                    return cmisRepository;
111            }
112    
113    }