001    /**
002     * Copyright (c) 2000-2012 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            public Object getSession(long repositoryId)
039                    throws PortalException, SystemException {
040    
041                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
042    
043                    return cmisRepository.getSession();
044            }
045    
046            public FileEntry toFileEntry(long repositoryId, Object object)
047                    throws PortalException, SystemException {
048    
049                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
050    
051                    Document document = (Document)object;
052    
053                    return cmisRepository.toFileEntry(document);
054            }
055    
056            public FileVersion toFileVersion(long repositoryId, Object object)
057                    throws PortalException, SystemException {
058    
059                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
060    
061                    Document document = (Document)object;
062    
063                    return cmisRepository.toFileVersion(document);
064            }
065    
066            public Folder toFolder(long repositoryId, Object object)
067                    throws PortalException, SystemException {
068    
069                    CMISRepository cmisRepository = getCmisRepository(repositoryId);
070    
071                    org.apache.chemistry.opencmis.client.api.Folder cmisFolder =
072                            (org.apache.chemistry.opencmis.client.api.Folder)object;
073    
074                    return cmisRepository.toFolder(cmisFolder);
075            }
076    
077            protected CMISRepository getCmisRepository(long repositoryId)
078                    throws PortalException, SystemException {
079    
080                    Repository repositoryImpl = repositoryLocalService.getRepositoryImpl(
081                            repositoryId);
082    
083                    CMISRepositoryHandler cmisRepositoryHandler = null;
084    
085                    if (repositoryImpl instanceof CMISRepositoryHandler) {
086                            cmisRepositoryHandler = (CMISRepositoryHandler)repositoryImpl;
087                    }
088                    else if (repositoryImpl instanceof BaseRepositoryProxyBean) {
089                            BaseRepositoryProxyBean baseRepositoryProxyBean =
090                                    (BaseRepositoryProxyBean)repositoryImpl;
091    
092                            ClassLoaderBeanHandler classLoaderBeanHandler =
093                                    (ClassLoaderBeanHandler)ProxyUtil.getInvocationHandler(
094                                            baseRepositoryProxyBean.getProxyBean());
095    
096                            Object bean = classLoaderBeanHandler.getBean();
097    
098                            if (bean instanceof CMISRepositoryHandler) {
099                                    cmisRepositoryHandler = (CMISRepositoryHandler)bean;
100                            }
101                    }
102    
103                    CMISRepository cmisRepository =
104                            (CMISRepository)cmisRepositoryHandler.getCmisRepository();
105    
106                    return cmisRepository;
107            }
108    
109    }