001/* 002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: DocumentModelResourceAdapter.java 28924 2008-01-10 14:04:05Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.relations.adapters; 023 024import java.io.Serializable; 025import java.util.Map; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.ecm.core.api.CloseableCoreSession; 030import org.nuxeo.ecm.core.api.CoreInstance; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentLocation; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentNotFoundException; 035import org.nuxeo.ecm.core.api.DocumentRef; 036import org.nuxeo.ecm.core.api.IdRef; 037import org.nuxeo.ecm.core.api.repository.RepositoryManager; 038import org.nuxeo.ecm.platform.relations.api.QNameResource; 039import org.nuxeo.ecm.platform.relations.api.Resource; 040import org.nuxeo.ecm.platform.relations.api.impl.AbstractResourceAdapter; 041import org.nuxeo.ecm.platform.relations.api.impl.QNameResourceImpl; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * Resource adapter using the document model id. 046 * 047 * @author <a href="mailto:[email protected]">Anahide Tchertchian</a> 048 */ 049public class DocumentModelResourceAdapter extends AbstractResourceAdapter implements Serializable { 050 051 private static final Log log = LogFactory.getLog(DocumentModelResourceAdapter.class); 052 053 private static final long serialVersionUID = -5307418102496342779L; 054 055 @Override 056 public Serializable getResourceRepresentation(Resource resource, Map<String, Object> context) { 057 Serializable object = null; 058 if (resource.isQNameResource()) { 059 CoreSession session = null; 060 boolean sessionOpened = false; 061 try { 062 String repoName; 063 String uid; 064 String localName = ((QNameResource) resource).getLocalName(); 065 int index = localName.indexOf('/'); 066 if (index == -1) { 067 // BBB for when repository name was not included in the 068 // local name 069 RepositoryManager mgr = Framework.getService(RepositoryManager.class); 070 repoName = mgr.getDefaultRepositoryName(); 071 uid = localName; 072 } else { 073 repoName = localName.substring(0, index); 074 uid = localName.substring(index + 1); 075 } 076 DocumentRef ref = new IdRef(uid); 077 078 if (context != null) { 079 session = (CoreSession) context.get(CORE_SESSION_CONTEXT_KEY); 080 if (!session.getRepositoryName().equals(repoName)) { 081 // let's open one 082 session = null; 083 } 084 } 085 if (session == null) { 086 // open one 087 session = CoreInstance.openCoreSession(repoName); 088 sessionOpened = true; 089 if (log.isDebugEnabled()) { 090 log.debug(String.format("Opened a new session '%s' with id %s", repoName, 091 session.getSessionId())); 092 } 093 } 094 if (!session.exists(ref)) { 095 return null; 096 } 097 object = session.getDocument(ref); 098 } catch (DocumentNotFoundException e) { 099 log.warn("Cannot get resource: " + resource, e); 100 } finally { 101 if (sessionOpened) { 102 ((CloseableCoreSession) session).close(); 103 } 104 } 105 } 106 return object; 107 } 108 109 @Override 110 public Resource getResource(Serializable object, Map<String, Object> context) { 111 if (object instanceof DocumentModel) { 112 DocumentModel doc = (DocumentModel) object; 113 String localName = doc.getRepositoryName() + '/' + doc.getId(); 114 return new QNameResourceImpl(namespace, localName); 115 } else if (object instanceof DocumentLocation) { 116 DocumentLocation docLoc = (DocumentLocation) object; 117 String localName = docLoc.getServerName() + '/' + docLoc.getIdRef().toString(); 118 return new QNameResourceImpl(namespace, localName); 119 } else { 120 throw new IllegalArgumentException(String.format("cannot build resource for '%s'", object)); 121 } 122 } 123 124 @Override 125 public Class<?> getKlass() { 126 return DocumentModel.class; 127 } 128 129}