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