001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.api;
021
022import java.io.Serializable;
023import java.net.URI;
024import java.net.URISyntaxException;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.platform.relations.api.Graph;
031import org.nuxeo.ecm.platform.relations.api.Literal;
032import org.nuxeo.ecm.platform.relations.api.Node;
033import org.nuxeo.ecm.platform.relations.api.QueryResult;
034import org.nuxeo.ecm.platform.relations.api.Resource;
035import org.nuxeo.ecm.platform.relations.api.Statement;
036import org.nuxeo.ecm.platform.relations.api.impl.LiteralImpl;
037import org.nuxeo.ecm.platform.relations.api.impl.ResourceImpl;
038import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl;
039
040public class AnnotationImpl implements Annotation, Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    public Graph getGraph() {
045        return graph;
046    }
047
048    public void setGraph(Graph graph) {
049        this.graph = graph;
050    }
051
052    private Graph graph;
053
054    @Override
055    public Resource getSubject() {
056        QueryResult result = graph.query("SELECT ?s WHERE {?s ?p ?o}", "sparql",
057                null);
058        List<Map<String, Node>> results = result.getResults();
059        Node node = results.get(0).get("s");
060        return node.isBlank() ? null : (Resource) node;
061    }
062
063    @Override
064    public void setBody(Statement body) {
065        graph.add(body);
066    }
067
068    @Override
069    public URI getAnnotates() {
070        QueryResult result = graph.query("SELECT ?o WHERE {?s <" + AnnotationsConstants.A_ANNOTATES + "> ?o}",
071                "sparql", null);
072        List<Map<String, Node>> results = result.getResults();
073        if (results.isEmpty()) {
074            return null;
075        }
076        Node node = results.get(0).get("o");
077        try {
078            return node.isBlank() ? null : new URI(((Resource) node).getUri());
079        } catch (URISyntaxException e) {
080            throw new NuxeoException(e);
081        }
082    }
083
084    @Override
085    public URI getBody() {
086        QueryResult result = graph.query("SELECT ?o WHERE {?s <" + AnnotationsConstants.A_BODY + "> ?o}", "sparql",
087                null);
088        List<Map<String, Node>> results = result.getResults();
089        if (results.isEmpty()) {
090            return null;
091        }
092        Node node = results.get(0).get("o");
093        try {
094            return node.isBlank() ? null : new URI(((Resource) node).getUri());
095        } catch (URISyntaxException e) {
096            throw new NuxeoException(e);
097        }
098    }
099
100    @Override
101    public String getBodyAsText() {
102        QueryResult result = graph.query("SELECT ?o WHERE {?s <" + AnnotationsConstants.A_BODY + "> ?o}", "sparql",
103                null);
104        List<Map<String, Node>> results = result.getResults();
105        if (results.isEmpty()) {
106            return null;
107        }
108        Node node = results.get(0).get("o");
109        if (node.isLiteral()) {
110            Literal literal = (Literal) node;
111            return literal.getValue();
112        }
113        if (node.isResource()) {
114            Resource resource = (Resource) node;
115            return resource.getUri();
116        }
117        return null;
118    }
119
120    @Override
121    public void setBodyText(String text) {
122        QueryResult result = graph.query("SELECT ?s WHERE {?s <" + AnnotationsConstants.A_BODY + "> ?o}", "sparql",
123                null);
124        Node s = result.getResults().get(0).get("s");
125        Resource p = new ResourceImpl(AnnotationsConstants.A_BODY);
126        graph.remove(graph.getStatements(new StatementImpl(s, p, null)));
127        Literal o = new LiteralImpl(text);
128        Statement newStatement = new StatementImpl(s, p, o);
129        graph.add(newStatement);
130    }
131
132    @Override
133    public String getContext() {
134        QueryResult result = graph.query("SELECT ?o WHERE {?s <" + AnnotationsConstants.A_CONTEXT + "> ?o}", "sparql",
135                null);
136        List<Map<String, Node>> results = result.getResults();
137        if (results.isEmpty()) {
138            return null;
139        }
140        Node node = results.get(0).get("o");
141        return node.isBlank() ? null : ((Literal) node).getValue();
142    }
143
144    @Override
145    public void setContext(Statement context) {
146        graph.add(context);
147    }
148
149    @Override
150    public List<Statement> getStatements() {
151        return graph.getStatements();
152    }
153
154    @Override
155    public void setStatements(List<Statement> statements) {
156        graph.add(statements);
157    }
158
159    @Override
160    public void setSubject(Resource resource) {
161        List<Statement> statements = new ArrayList<Statement>();
162        for (Statement statement : graph.getStatements()) {
163            statement.setSubject(resource);
164            statements.add(statement);
165        }
166        graph.clear();
167        graph.add(statements);
168    }
169
170    @Override
171    public void setAnnotates(Statement statement) {
172        graph.add(statement);
173    }
174
175    @Override
176    public String getCreator() {
177        QueryResult result = graph.query("SELECT ?o WHERE {?s <" + AnnotationsConstants.D_CREATOR + "> ?o .}",
178                "sparql", null);
179        if (result.getCount() == 0) {
180            return null;
181        }
182        Node node = result.getResults().get(0).get("o");
183        return node.isBlank() ? null : ((Literal) node).getValue();
184    }
185
186    @Override
187    public void addMetadata(String predicate, String value) {
188        Statement statement = new StatementImpl(getSubject(), new ResourceImpl(predicate), new LiteralImpl(value));
189        graph.add(statement);
190    }
191
192    @Override
193    public String getId() {
194        Resource subject = getSubject();
195        String uri = subject.getUri().toString();
196        return uri.substring(uri.lastIndexOf(":"));
197    }
198}