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.http;
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.core.api.NuxeoPrincipal;
032import org.nuxeo.ecm.platform.annotations.api.Annotation;
033import org.nuxeo.ecm.platform.annotations.api.AnnotationManager;
034import org.nuxeo.ecm.platform.annotations.api.AnnotationsService;
035import org.nuxeo.ecm.platform.relations.api.Graph;
036import org.nuxeo.ecm.platform.relations.api.RelationManager;
037import org.nuxeo.ecm.platform.relations.api.Statement;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @author Alexandre Russel
042 */
043public class AnnotationServiceFacade {
044
045    private static final String TRANSIENT_GRAPH_TYPE = "jena";
046
047    private AnnotationsService service;
048
049    private final AnnotationManager manager = new AnnotationManager();
050
051    public AnnotationServiceFacade() {
052    }
053
054    protected AnnotationsService getService() {
055        if (service == null) {
056            service = Framework.getService(AnnotationsService.class);
057        }
058        return service;
059    }
060
061    public void query(String uri, OutputStream outputStream, NuxeoPrincipal name) {
062        List<Annotation> annotations;
063        try {
064            annotations = getService().queryAnnotations(new URI(uri), name);
065        } catch (URISyntaxException e) {
066            throw new NuxeoException(e);
067        }
068        List<Statement> statements = new ArrayList<Statement>();
069        for (Annotation annotation : annotations) {
070            statements.addAll(annotation.getStatements());
071        }
072        RelationManager service = Framework.getService(RelationManager.class);
073        Graph graph = service.getTransientGraph(TRANSIENT_GRAPH_TYPE);
074        graph.add(statements);
075        try {
076            outputStream.write("<?xml version='1.0'?>\n".getBytes());
077        } catch (IOException e) {
078            throw new NuxeoException(e);
079        }
080        graph.write(outputStream, null, null);
081    }
082
083    public void getAnnotation(String annId, NuxeoPrincipal name, OutputStream os, String baseUrl) {
084        Annotation annotation = getService().getAnnotation(annId, name, baseUrl);
085        manager.writeAnnotation(os, annotation);
086    }
087
088    public void updateAnnotation(InputStream is, NuxeoPrincipal name, OutputStream outputStream, String baseUrl) {
089        Annotation annotation = manager.getAnnotation(is);
090        annotation = getService().updateAnnotation(annotation, name, baseUrl);
091        manager.writeAnnotation(outputStream, annotation);
092    }
093
094    public String getAnnotationBody(String id, NuxeoPrincipal name, String baseUrl) {
095        Annotation annotation = getService().getAnnotation(id, name, baseUrl);
096        return annotation.getBodyAsText();
097    }
098
099    public void createAnnotation(InputStream inputStream, NuxeoPrincipal name, OutputStream outputStream,
100            String baseUrl) {
101        Annotation annotation = manager.getAnnotation(inputStream);
102        annotation = getService().addAnnotation(annotation, name, baseUrl);
103        manager.writeAnnotation(outputStream, annotation);
104    }
105
106    public void delete(String annId, NuxeoPrincipal name, String baseUrl) {
107        Annotation annotation = getService().getAnnotation(annId, name, baseUrl);
108        getService().deleteAnnotation(annotation, name);
109    }
110
111    public void deleteFor(String uri, String annId, NuxeoPrincipal name, String baseUrl) {
112        try {
113            Annotation annotation = getService().getAnnotation(annId, name, baseUrl);
114            getService().deleteAnnotationFor(new URI(uri), annotation, name);
115        } catch (URISyntaxException e) {
116            throw new NuxeoException(e);
117        }
118    }
119
120}