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 @SuppressWarnings("resource") // session closed only if we opened it 055 @Override 056 public Serializable getResourceRepresentation(Resource resource, Map<String, Object> context) { 057 Serializable object = null; 058 if (resource.isQNameResource()) { 059 try { 060 String repoName; 061 String uid; 062 String localName = ((QNameResource) resource).getLocalName(); 063 int index = localName.indexOf('/'); 064 if (index == -1) { 065 // BBB for when repository name was not included in the 066 // local name 067 RepositoryManager mgr = Framework.getService(RepositoryManager.class); 068 repoName = mgr.getDefaultRepositoryName(); 069 uid = localName; 070 } else { 071 repoName = localName.substring(0, index); 072 uid = localName.substring(index + 1); 073 } 074 DocumentRef ref = new IdRef(uid); 075 076 CoreSession session = null; 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.getCoreSession(repoName); 087 if (log.isDebugEnabled()) { 088 log.debug(String.format("Opened a new session '%s' with id %s", repoName, session)); 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 } 098 } 099 return object; 100 } 101 102 @Override 103 public Resource getResource(Serializable object, Map<String, Object> context) { 104 if (object instanceof DocumentModel) { 105 DocumentModel doc = (DocumentModel) object; 106 String localName = doc.getRepositoryName() + '/' + doc.getId(); 107 return new QNameResourceImpl(namespace, localName); 108 } else if (object instanceof DocumentLocation) { 109 DocumentLocation docLoc = (DocumentLocation) object; 110 String localName = docLoc.getServerName() + '/' + docLoc.getIdRef().toString(); 111 return new QNameResourceImpl(namespace, localName); 112 } else { 113 throw new IllegalArgumentException(String.format("cannot build resource for '%s'", object)); 114 } 115 } 116 117 @Override 118 public Class<?> getKlass() { 119 return DocumentModel.class; 120 } 121 122}