001/* 002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: DocumentModelResourceAdapter.java 28924 2008-01-10 14:04:05Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.relations.adapters; 021 022import java.io.Serializable; 023import java.util.Map; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.CoreInstance; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentLocation; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentNotFoundException; 032import org.nuxeo.ecm.core.api.DocumentRef; 033import org.nuxeo.ecm.core.api.IdRef; 034import org.nuxeo.ecm.core.api.repository.RepositoryManager; 035import org.nuxeo.ecm.platform.relations.api.QNameResource; 036import org.nuxeo.ecm.platform.relations.api.Resource; 037import org.nuxeo.ecm.platform.relations.api.impl.AbstractResourceAdapter; 038import org.nuxeo.ecm.platform.relations.api.impl.QNameResourceImpl; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * Resource adapter using the document model id. 043 * 044 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 045 */ 046public class DocumentModelResourceAdapter extends AbstractResourceAdapter implements Serializable { 047 048 private static final Log log = LogFactory.getLog(DocumentModelResourceAdapter.class); 049 050 private static final long serialVersionUID = -5307418102496342779L; 051 052 @Override 053 public Serializable getResourceRepresentation(Resource resource, Map<String, Object> context) { 054 Serializable object = null; 055 if (resource.isQNameResource()) { 056 CoreSession session = null; 057 boolean sessionOpened = false; 058 try { 059 String repoName; 060 String uid; 061 String localName = ((QNameResource) resource).getLocalName(); 062 int index = localName.indexOf('/'); 063 if (index == -1) { 064 // BBB for when repository name was not included in the 065 // local name 066 RepositoryManager mgr = Framework.getLocalService(RepositoryManager.class); 067 repoName = mgr.getDefaultRepositoryName(); 068 uid = localName; 069 } else { 070 repoName = localName.substring(0, index); 071 uid = localName.substring(index + 1); 072 } 073 DocumentRef ref = new IdRef(uid); 074 075 if (context != null) { 076 session = (CoreSession) context.get(CORE_SESSION_CONTEXT_KEY); 077 if (!session.getRepositoryName().equals(repoName)) { 078 // let's open one 079 session = null; 080 } 081 } 082 if (session == null) { 083 // open one 084 session = CoreInstance.openCoreSession(repoName); 085 sessionOpened = true; 086 if (log.isDebugEnabled()) { 087 log.debug(String.format("Opened a new session '%s' with id %s", repoName, 088 session.getSessionId())); 089 } 090 } 091 if (!session.exists(ref)) { 092 return null; 093 } 094 object = session.getDocument(ref); 095 } catch (DocumentNotFoundException e) { 096 log.warn("Cannot get resource: " + resource, e); 097 } finally { 098 if (sessionOpened) { 099 session.close(); 100 } 101 } 102 } 103 return object; 104 } 105 106 @Override 107 public Resource getResource(Serializable object, Map<String, Object> context) { 108 if (object instanceof DocumentModel) { 109 DocumentModel doc = (DocumentModel) object; 110 String localName = doc.getRepositoryName() + '/' + doc.getId(); 111 return new QNameResourceImpl(namespace, localName); 112 } else if (object instanceof DocumentLocation) { 113 DocumentLocation docLoc = (DocumentLocation) object; 114 String localName = docLoc.getServerName() + '/' + docLoc.getIdRef().toString(); 115 return new QNameResourceImpl(namespace, localName); 116 } else { 117 throw new IllegalArgumentException(String.format("cannot build resource for '%s'", object)); 118 } 119 } 120 121 @Override 122 public Class<?> getKlass() { 123 return DocumentModel.class; 124 } 125 126}