annotate src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 10:90911b2da322

more work on permissions...
author casties
date Thu, 12 Jul 2012 17:01:32 +0200
parents b2bfc3bc9ba8
children bc90aaeb925d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
1 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
2 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
3 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.neo4j;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
5
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
6 import java.util.ArrayList;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
7 import java.util.Calendar;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
8 import java.util.List;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
9
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
10 import org.apache.log4j.Logger;
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
11 import org.neo4j.graphdb.Direction;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
12 import org.neo4j.graphdb.GraphDatabaseService;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
13 import org.neo4j.graphdb.Node;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
14 import org.neo4j.graphdb.Relationship;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
15 import org.neo4j.graphdb.RelationshipType;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
16 import org.neo4j.graphdb.Transaction;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
17 import org.neo4j.graphdb.index.Index;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
18 import org.neo4j.graphdb.index.IndexHits;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
19
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
20 import de.mpiwg.itgroup.annotations.Actor;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
21 import de.mpiwg.itgroup.annotations.Annotation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
22 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
23 import de.mpiwg.itgroup.annotations.Person;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
24
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
25 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
26 * @author casties
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
27 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
28 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
29 public class AnnotationStore {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
30
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
31 protected static Logger logger = Logger.getLogger(AnnotationStore.class);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
32
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
33 protected GraphDatabaseService graphDb;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
34
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
35 public static enum NodeTypes {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
36 ANNOTATION, PERSON, TARGET
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
37 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
38
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
39 protected List<Index<Node>> nodeIndexes;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
40
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
41 public static enum RelationTypes implements RelationshipType {
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
42 ANNOTATES, CREATED, PERMITS
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
43 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
44
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
45 public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/";
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
46
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
47 public AnnotationStore(GraphDatabaseService graphDb) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
48 super();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
49 this.graphDb = graphDb;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
50 nodeIndexes = new ArrayList<Index<Node>>(3);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
51 // List.set(enum.ordinal(), val) seems not to work.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
52 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
53 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
54 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
55 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
56
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
57 protected Index<Node> getNodeIndex(NodeTypes type) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
58 return nodeIndexes.get(type.ordinal());
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
59 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
60
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
61 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
62 * Returns the Annotation with the given id.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
63 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
64 * @param id
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
65 * @return
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
66 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
67 public Annotation getAnnotationById(String id) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
68 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
69 Annotation annot = createAnnotationFromNode(annotNode);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
70 return annot;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
71 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
72
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
73 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
74 * Returns an Annotation object from an annotation-Node.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
75 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
76 * @param annotNode
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
77 * @return
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
78 */
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
79 public Annotation createAnnotationFromNode(Node annotNode) {
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
80 Annotation annot = new Annotation();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
81 annot.setUri((String) annotNode.getProperty("id", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
82 annot.setBodyText((String) annotNode.getProperty("bodyText", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
83 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
84 // get annotation target from relation
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
85 Iterable<Relationship> targetRels = annotNode
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
86 .getRelationships(RelationTypes.ANNOTATES);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
87 for (Relationship targetRel : targetRels) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
88 Node target = targetRel.getEndNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
89 annot.setTargetBaseUri((String) target.getProperty("uri", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
90 // just the first one
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
91 break;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
92 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
93 annot.setTargetFragment((String) annotNode.getProperty(
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
94 "targetFragment", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
95 String ft = (String) annotNode.getProperty("fragmentType", null);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
96 if (ft != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
97 annot.setFragmentType(FragmentTypes.valueOf(ft));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
98 }
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
99 // get creator from relation
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
100 Iterable<Relationship> creatorRels = annotNode
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
101 .getRelationships(RelationTypes.CREATED);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
102 for (Relationship creatorRel : creatorRels) {
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
103 Node creatorNode = creatorRel.getStartNode();
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
104 String uri = (String) creatorNode.getProperty("uri", null);
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
105 String name = (String) creatorNode.getProperty("name", null);
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
106 Actor creator = new Person(uri, name);
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 8
diff changeset
107 annot.setCreator(creator);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
108 // just the first one
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
109 break;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
110 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
111 annot.setCreated((String) annotNode.getProperty("created", null));
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
112 return annot;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
113 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
114
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
115 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
116 * Store a new annotation in the store or update an existing one. Returns
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
117 * the stored annotation.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
118 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
119 * @param annot
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
120 * @return
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
121 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
122 public Annotation storeAnnotation(Annotation annot) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
123 Node annotNode = null;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
124 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
125 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
126 /*
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
127 * create or get the annotation
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
128 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
129 String id = annot.getUri();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
130 if (id == null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
131 id = createRessourceURI("annot:");
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
132 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
133 annotNode = getOrCreateAnnotationNode(id);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
134
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
135 /*
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
136 * the annotation body
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
137 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
138 String bodyText = annot.getBodyText();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
139 if (bodyText != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
140 annotNode.setProperty("bodyText", bodyText);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
141 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
142 String bodyUri = annot.getBodyUri();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
143 if (bodyUri != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
144 annotNode.setProperty("bodyUri", bodyUri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
145 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
146
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
147 /*
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
148 * the annotation target
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
149 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
150 String targetBaseUri = annot.getTargetBaseUri();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
151 if (targetBaseUri != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
152 Node target = getOrCreateTargetNode(targetBaseUri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
153 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
154 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
155
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
156 /*
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
157 * The fragment part of the annotation target.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
158 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
159 String targetFragment = annot.getTargetFragment();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
160 FragmentTypes fragmentType = annot.getFragmentType();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
161 if (targetFragment != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
162 annotNode.setProperty("targetFragment", targetFragment);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
163 annotNode.setProperty("fragmentType", fragmentType.name());
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
164 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
165
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
166 /*
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
167 * The creator of this annotation.
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
168 */
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
169 Actor creator = annot.getCreator();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
170 if (creator != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
171 Node creatorNode = getOrCreatePersonNode(creator);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
172 getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
173 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
174
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
175 /*
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
176 * The creation date of this annotation.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
177 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
178 String created = annot.getCreated();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
179 if (created != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
180 annotNode.setProperty("created", created);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
181 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
182
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
183 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
184 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
185 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
186 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
187
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
188 // re-read and return annotation
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
189 Annotation storedAnnot = createAnnotationFromNode(annotNode);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
190 return storedAnnot;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
191 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
192
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
193 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
194 * Deletes the annotation with the given id.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
195 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
196 * @param id
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
197 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
198 public void deleteById(String id) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
199 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
200 if (annotNode != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
201 // delete related objects
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
202 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
203 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
204 for (Relationship rel : annotNode.getRelationships()) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
205 // delete relation and the related node if it has no other relations
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
206 Node other = rel.getOtherNode(annotNode);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
207 rel.delete();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
208 if (! other.hasRelationship()) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
209 deleteNode(other);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
210 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
211 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
212 if (! annotNode.hasRelationship()) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
213 deleteNode(annotNode);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
214 } else {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
215 logger.error("deleteById: unable to delete: Node still has relations.");
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
216 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
217 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
218 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
219 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
220 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
221 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
222 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
223
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
224 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
225 * Returns all annotations with the given uri and/or user.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
226 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
227 * @param uri
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
228 * @param userUri
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
229 * @param limit
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
230 * @param offset
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
231 * @return
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
232 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
233 public List<Annotation> searchByUriUser(String targetUri, String userUri,
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
234 String limit, String offset) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
235 List<Annotation> annotations = new ArrayList<Annotation>();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
236 if (targetUri != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
237 // there should be only one
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
238 Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
239 if (target != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
240 Iterable<Relationship> relations = target
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
241 .getRelationships(RelationTypes.ANNOTATES);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
242 for (Relationship relation : relations) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
243 Node ann = relation.getStartNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
244 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
245 Annotation annot = createAnnotationFromNode(ann);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
246 annotations.add(annot);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
247 } else {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
248 logger.error("ANNOTATES relation does not start with ANNOTATION: "
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
249 + ann);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
250 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
251 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
252 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
253 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
254 if (userUri != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
255 // there should be only one
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
256 Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
257 if (person != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
258 Iterable<Relationship> relations = person
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
259 .getRelationships(RelationTypes.CREATED);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
260 for (Relationship relation : relations) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
261 Node ann = relation.getEndNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
262 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
8
c3cc6a41dd1c under construction
casties
parents: 6
diff changeset
263 Annotation annot = createAnnotationFromNode(ann);
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
264 annotations.add(annot);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
265 } else {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
266 logger.error("CREATED relation does not end with ANNOTATION: "
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
267 + ann);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
268 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
269 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
270 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
271 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
272 return annotations;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
273 }
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
274
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
275 protected Relationship getOrCreateRelation(Node start,
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
276 RelationshipType type, Node end) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
277 if (start.hasRelationship()) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
278 // there are relations
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
279 Iterable<Relationship> rels = start.getRelationships(type,
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
280 Direction.OUTGOING);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
281 for (Relationship rel : rels) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
282 if (rel.getEndNode().equals(end)) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
283 // relation exists
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
284 return rel;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
285 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
286 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
287 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
288 // create new one
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
289 Relationship rel;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
290 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
291 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
292 rel = start.createRelationshipTo(end, type);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
293 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
294 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
295 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
296 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
297 return rel;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
298 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
299
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
300 protected Node getOrCreateAnnotationNode(String id) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
301 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
302 IndexHits<Node> annotations = idx.get("id", id);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
303 Node annotation = annotations.getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
304 if (annotation == null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
305 // does not exist yet
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
306 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
307 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
308 annotation = graphDb.createNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
309 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name());
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
310 annotation.setProperty("id", id);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
311 idx.add(annotation, "id", id);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
312 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
313 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
314 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
315 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
316 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
317 return annotation;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
318 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
319
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
320 protected Node getOrCreateTargetNode(String uri) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
321 Index<Node> idx = getNodeIndex(NodeTypes.TARGET);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
322 IndexHits<Node> targets = idx.get("uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
323 Node target = targets.getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
324 if (target == null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
325 // does not exist yet
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
326 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
327 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
328 target = graphDb.createNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
329 target.setProperty("TYPE", NodeTypes.TARGET.name());
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
330 target.setProperty("uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
331 idx.add(target, "uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
332 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
333 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
334 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
335 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
336 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
337 return target;
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
338 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
339
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
340 protected Node getOrCreatePersonNode(Actor actor) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
341 /*
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
342 // Person is identified by URI
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
343 Index<Node> idx = getNodeIndex(NodeTypes.PERSON);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
344 IndexHits<Node> persons = idx.get("uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
345 Node person = persons.getSingle();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
346 if (person == null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
347 // does not exist yet
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
348 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
349 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
350 person = graphDb.createNode();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
351 person.setProperty("TYPE", NodeTypes.PERSON.name());
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
352 person.setProperty("uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
353 idx.add(person, "uri", uri);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
354 if (name != null) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
355 person.setProperty("name", name);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
356 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
357 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
358 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
359 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
360 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
361 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
362 return person;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
363 */
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
364 return null;
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
365 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
366
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
367 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
368 * Unindexes and deletes given Node if it has no relations.
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
369 * @param node
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
370 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
371 protected void deleteNode(Node node) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
372 Transaction tx = graphDb.beginTx();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
373 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
374 if (node.hasRelationship()) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
375 logger.error("deleteNode: unable to delete: Node still has relations.");
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
376 } else {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
377 String ts = (String) node.getProperty("TYPE", null);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
378 try {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
379 NodeTypes type = NodeTypes.valueOf(ts);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
380 getNodeIndex(type).remove(node);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
381 } catch (Exception e) {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
382 logger.error("deleteNode: unable to get TYPE of node: "+node);
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
383 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
384 node.delete();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
385 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
386 tx.success();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
387 } finally {
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
388 tx.finish();
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
389 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
390 }
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
391
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
392 /**
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
393 * Erzeuge eine urn aus der aktuellen Zeit in millis
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
394 *
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
395 * @return
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
396 */
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
397 private String createRessourceURI(String prefix) {
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
398
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
399 Calendar cal = Calendar.getInstance();
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
400
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
401 long time = cal.getTimeInMillis();
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
402
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
403 return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
404
6
6dfbe2400f64 delete annotation should work now.
casties
parents: 5
diff changeset
405 }
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
406
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
407 }