001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.api;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025import java.net.URI;
026import java.net.URISyntaxException;
027import java.util.ArrayList;
028import java.util.List;
029
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.platform.relations.api.Graph;
032import org.nuxeo.ecm.platform.relations.api.Node;
033import org.nuxeo.ecm.platform.relations.api.RelationManager;
034import org.nuxeo.ecm.platform.relations.api.Resource;
035import org.nuxeo.ecm.platform.relations.api.Statement;
036import org.nuxeo.ecm.platform.relations.api.impl.ResourceImpl;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * @author Alexandre Russel
041 */
042public class AnnotationManager {
043
044    private static final String TRANSIENT_GRAPH_TYPE = "jena";
045
046    public void writeAnnotation(OutputStream os, Annotation annotation) {
047        Graph graph = getTransientGraph();
048        graph.add(annotation.getStatements());
049        try {
050            os.write("<?xml version='1.0'?>".getBytes());
051        } catch (IOException e) {
052            throw new NuxeoException(e);
053        }
054        graph.write(os, null, null);
055    }
056
057    public Annotation translateAnnotationFromRepo(UriResolver resolver, String baseUrl, Annotation annotation) {
058        List<Statement> results = new ArrayList<Statement>();
059        for (Statement statement : annotation.getStatements()) {
060            Node node = statement.getSubject();
061            if (node instanceof Resource) {
062                Resource resource = getTranslatedResource(resolver, baseUrl, node);
063                statement.setSubject(resource);
064            }
065            node = statement.getObject();
066            if (node instanceof Resource) {
067                Resource resource = getTranslatedResource(resolver, baseUrl, node);
068                statement.setObject(resource);
069            }
070            results.add(statement);
071        }
072        return getAnnotation(results);
073    }
074
075    private static Resource getTranslatedResource(UriResolver resolver, String baseUrl, Node node) {
076        String uri = ((Resource) node).getUri();
077        Resource resource = null;
078        try {
079            URI newUri = resolver.translateFromGraphURI(new URI(uri), baseUrl);
080            resource = new ResourceImpl(newUri.toString());
081        } catch (URISyntaxException e) {
082            throw new NuxeoException(e);
083        }
084        return resource;
085    }
086
087    public Annotation translateAnnotationToRepo(UriResolver resolver, Annotation annotation) {
088        List<Statement> results = new ArrayList<Statement>();
089        for (Statement statement : annotation.getStatements()) {
090            Node node = statement.getSubject();
091            if (node instanceof Resource) {
092                String uri = ((Resource) node).getUri();
093                URI u;
094                try {
095                    u = resolver.translateToGraphURI(new URI(uri));
096                } catch (URISyntaxException e) {
097                    throw new NuxeoException(e);
098                }
099                Resource resource = new ResourceImpl(u.toString());
100                statement.setSubject(resource);
101            }
102            node = statement.getObject();
103            if (node instanceof Resource) {
104                String uri = ((Resource) node).getUri();
105                URI u;
106                try {
107                    u = resolver.translateToGraphURI(new URI(uri));
108                } catch (URISyntaxException e) {
109                    throw new NuxeoException(e);
110                }
111                Resource resource = new ResourceImpl(u.toString());
112                statement.setObject(resource);
113            }
114            results.add(statement);
115        }
116        return getAnnotation(results);
117    }
118
119    public Annotation getAnnotation(List<Statement> statements) {
120        AnnotationImpl annotation = new AnnotationImpl();
121        Graph graph = getTransientGraph();
122        graph.add(statements);
123        annotation.setGraph(graph);
124        return annotation;
125    }
126
127    public Annotation getAnnotation(InputStream is) {
128        Graph graph = getTransientGraph();
129        graph.read(is, null, null);
130        AnnotationImpl annotation = new AnnotationImpl();
131        annotation.setGraph(graph);
132        return annotation;
133    }
134
135    public Annotation getAnnotation(String is) {
136        Graph graph = getTransientGraph();
137        graph.read(is, null, null);
138        AnnotationImpl annotation = new AnnotationImpl();
139        annotation.setGraph(graph);
140        return annotation;
141    }
142
143    private static Graph getTransientGraph() {
144        return Framework.getService(RelationManager.class).getTransientGraph(TRANSIENT_GRAPH_TYPE);
145    }
146
147}