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.CoreInstance; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentLocation; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentNotFoundException; 034import org.nuxeo.ecm.core.api.DocumentRef; 035import org.nuxeo.ecm.core.api.IdRef; 036import org.nuxeo.ecm.core.api.repository.RepositoryManager; 037import org.nuxeo.ecm.platform.relations.api.QNameResource; 038import org.nuxeo.ecm.platform.relations.api.Resource; 039import org.nuxeo.ecm.platform.relations.api.impl.AbstractResourceAdapter; 040import org.nuxeo.ecm.platform.relations.api.impl.QNameResourceImpl; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Resource adapter using the document model id. 045 * 046 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 047 */ 048public class DocumentModelResourceAdapter extends AbstractResourceAdapter implements Serializable { 049 050 private static final Log log = LogFactory.getLog(DocumentModelResourceAdapter.class); 051 052 private static final long serialVersionUID = -5307418102496342779L; 053 054 @Override 055 public Serializable getResourceRepresentation(Resource resource, Map<String, Object> context) { 056 Serializable object = null; 057 if (resource.isQNameResource()) { 058 CoreSession session = null; 059 boolean sessionOpened = false; 060 try { 061 String repoName; 062 String uid; 063 String localName = ((QNameResource) resource).getLocalName(); 064 int index = localName.indexOf('/'); 065 if (index == -1) { 066 // BBB for when repository name was not included in the 067 // local name 068 RepositoryManager mgr = Framework.getLocalService(RepositoryManager.class); 069 repoName = mgr.getDefaultRepositoryName(); 070 uid = localName; 071 } else { 072 repoName = localName.substring(0, index); 073 uid = localName.substring(index + 1); 074 } 075 DocumentRef ref = new IdRef(uid); 076 077 if (context != null) { 078 session = (CoreSession) context.get(CORE_SESSION_CONTEXT_KEY); 079 if (!session.getRepositoryName().equals(repoName)) { 080 // let's open one 081 session = null; 082 } 083 } 084 if (session == null) { 085 // open one 086 session = CoreInstance.openCoreSession(repoName); 087 sessionOpened = true; 088 if (log.isDebugEnabled()) { 089 log.debug(String.format("Opened a new session '%s' with id %s", repoName, 090 session.getSessionId())); 091 } 092 } 093 if (!session.exists(ref)) { 094 return null; 095 } 096 object = session.getDocument(ref); 097 } catch (DocumentNotFoundException e) { 098 log.warn("Cannot get resource: " + resource, e); 099 } finally { 100 if (sessionOpened) { 101 session.close(); 102 } 103 } 104 } 105 return object; 106 } 107 108 @Override 109 public Resource getResource(Serializable object, Map<String, Object> context) { 110 if (object instanceof DocumentModel) { 111 DocumentModel doc = (DocumentModel) object; 112 String localName = doc.getRepositoryName() + '/' + doc.getId(); 113 return new QNameResourceImpl(namespace, localName); 114 } else if (object instanceof DocumentLocation) { 115 DocumentLocation docLoc = (DocumentLocation) object; 116 String localName = docLoc.getServerName() + '/' + docLoc.getIdRef().toString(); 117 return new QNameResourceImpl(namespace, localName); 118 } else { 119 throw new IllegalArgumentException(String.format("cannot build resource for '%s'", object)); 120 } 121 } 122 123 @Override 124 public Class<?> getKlass() { 125 return DocumentModel.class; 126 } 127 128}