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