Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 42:aa2bb7ac04d9
more work on resources and targets.
author | casties |
---|---|
date | Wed, 26 Sep 2012 16:12:46 +0200 |
parents | 03e0f7574224 |
children | d1bef7952bec |
comparison
equal
deleted
inserted
replaced
41:5d4260344db5 | 42:aa2bb7ac04d9 |
---|---|
23 import de.mpiwg.itgroup.annotations.Actor; | 23 import de.mpiwg.itgroup.annotations.Actor; |
24 import de.mpiwg.itgroup.annotations.Annotation; | 24 import de.mpiwg.itgroup.annotations.Annotation; |
25 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes; | 25 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes; |
26 import de.mpiwg.itgroup.annotations.Group; | 26 import de.mpiwg.itgroup.annotations.Group; |
27 import de.mpiwg.itgroup.annotations.Person; | 27 import de.mpiwg.itgroup.annotations.Person; |
28 import de.mpiwg.itgroup.annotations.Resource; | |
28 import de.mpiwg.itgroup.annotations.Tag; | 29 import de.mpiwg.itgroup.annotations.Tag; |
30 import de.mpiwg.itgroup.annotations.Target; | |
31 import de.mpiwg.itgroup.annotations.Uri; | |
29 | 32 |
30 /** | 33 /** |
31 * Neo4J based Annotation store. | 34 * Neo4J based Annotation store. |
32 * | 35 * |
33 * @author casties | 36 * @author casties |
89 if (tagUri == null) return null; | 92 if (tagUri == null) return null; |
90 Node person = getNodeIndex(NodeTypes.TAG).get("uri", tagUri).getSingle(); | 93 Node person = getNodeIndex(NodeTypes.TAG).get("uri", tagUri).getSingle(); |
91 return person; | 94 return person; |
92 } | 95 } |
93 | 96 |
94 public List<Actor> getActors(String key, String query, NodeTypes type) { | 97 /** |
95 ArrayList<Actor> actors = new ArrayList<Actor>(); | 98 * Returns the Node with the given key and value. |
99 * Key has to be indexed. | |
100 * | |
101 * @param key | |
102 * @param value | |
103 * @param type | |
104 * @return | |
105 */ | |
106 public Node getNodeFromIndex(String key, String value, NodeTypes type) { | |
107 if (key == null || value == null) return null; | |
108 Node person = getNodeIndex(type).get(key, value).getSingle(); | |
109 return person; | |
110 } | |
111 | |
112 /** | |
113 * Returns list of Actors of given type (Group or Person). | |
114 * Key has to be indexed. | |
115 * | |
116 * @param key | |
117 * @param query | |
118 * @param type | |
119 * @return | |
120 */ | |
121 @SuppressWarnings("unchecked") | |
122 public <T extends Actor> List<T> getActors(String key, String query, NodeTypes type) { | |
123 ArrayList<T> actors = new ArrayList<T>(); | |
96 Index<Node> idx = getNodeIndex(type); | 124 Index<Node> idx = getNodeIndex(type); |
97 if (key == null) { | 125 if (key == null) { |
98 key = "uri"; | 126 key = "uri"; |
99 query = "*"; | 127 query = "*"; |
100 } | 128 } |
101 IndexHits<Node> actorNodes = idx.query(key, query); | 129 IndexHits<Node> actorNodes = idx.query(key, query); |
102 for (Node actorNode : actorNodes) { | 130 for (Node actorNode : actorNodes) { |
103 Actor actor = createActorFromNode(actorNode); | 131 Actor actor = createActorFromNode(actorNode); |
104 actors.add(actor); | 132 actors.add((T) actor); |
105 } | 133 } |
106 return actors; | 134 return actors; |
107 } | 135 } |
108 | 136 |
109 /** | 137 /** |
110 * Returns List of Groups. | 138 * Returns list of groups. Key has to be indexed. |
111 * Key has to be indexed. | |
112 * | |
113 * @param key | 139 * @param key |
114 * @param query | 140 * @param query |
115 * @return | 141 * @return |
116 */ | 142 */ |
117 public List<Group> getGroups(String key, String query) { | 143 public List<Group> getGroups(String key, String query) { |
118 ArrayList<Group> groups = new ArrayList<Group>(); | 144 List<Group> groups = getActors(key, query, NodeTypes.GROUP); |
119 Index<Node> idx = getNodeIndex(NodeTypes.GROUP); | 145 return groups; |
146 } | |
147 | |
148 /** | |
149 * Returns list of Persons. Key has to be indexed. | |
150 * @param key | |
151 * @param query | |
152 * @return | |
153 */ | |
154 public List<Person> getPersons(String key, String query) { | |
155 List<Person> persons = getActors(key, query, NodeTypes.PERSON); | |
156 return persons; | |
157 } | |
158 | |
159 /** | |
160 * Returns list of uri-like objects of given type (Target or Resource). | |
161 * Key has to be indexed. | |
162 * | |
163 * @param key | |
164 * @param query | |
165 * @param type | |
166 * @return | |
167 */ | |
168 @SuppressWarnings("unchecked") | |
169 public <T extends Uri> List<T> getUris(String key, String query, NodeTypes type) { | |
170 ArrayList<T> uris = new ArrayList<T>(); | |
171 Index<Node> idx = getNodeIndex(type); | |
120 if (key == null) { | 172 if (key == null) { |
121 key = "uri"; | 173 key = "uri"; |
122 query = "*"; | 174 query = "*"; |
123 } | 175 } |
124 IndexHits<Node> groupNodes = idx.query(key, query); | 176 IndexHits<Node> actorNodes = idx.query(key, query); |
125 for (Node groupNode : groupNodes) { | 177 for (Node actorNode : actorNodes) { |
126 Actor group = createActorFromNode(groupNode); | 178 Uri uri = createUriFromNode(actorNode); |
127 groups.add((Group) group); | 179 uris.add((T) uri); |
128 } | 180 } |
129 return groups; | 181 return uris; |
130 } | 182 } |
131 | 183 |
184 | |
185 /** | |
186 * Returns list of Targets. Key has to be indexed. | |
187 * @param key | |
188 * @param query | |
189 * @return | |
190 */ | |
191 public List<Target> getTargets(String key, String query) { | |
192 List<Target> targets = getUris(key, query, NodeTypes.TARGET); | |
193 return targets; | |
194 } | |
195 | |
196 /** | |
197 * Returns list of Resources. Key has to be indexed. | |
198 * @param key | |
199 * @param query | |
200 * @return | |
201 */ | |
202 public List<Resource> getResources(String key, String query) { | |
203 List<Resource> targets = getUris(key, query, NodeTypes.RESOURCE); | |
204 return targets; | |
205 } | |
206 | |
132 /** | 207 /** |
133 * Returns List of Annotations. | 208 * Returns List of Annotations. |
134 * Key has to be indexed. | 209 * Key has to be indexed. |
135 * | 210 * |
136 * @param key | 211 * @param key |
487 String uri = (String) tagNode.getProperty("uri", null); | 562 String uri = (String) tagNode.getProperty("uri", null); |
488 String id = (String) tagNode.getProperty("id", null); | 563 String id = (String) tagNode.getProperty("id", null); |
489 | 564 |
490 return new Tag(id, uri, name); | 565 return new Tag(id, uri, name); |
491 | 566 |
567 } | |
568 | |
569 protected Uri createUriFromNode(Node uriNode) { | |
570 if (uriNode == null) return null; | |
571 String uri = (String) uriNode.getProperty("uri", null); | |
572 String type = (String) uriNode.getProperty("TYPE", null); | |
573 if (type != null && type.equals("TARGET")) { | |
574 return new Target(uri); | |
575 } else if (type != null && type.equals("RESOURCE")) { | |
576 return new Resource(uri); | |
577 } | |
578 return null; | |
492 } | 579 } |
493 | 580 |
494 /** | 581 /** |
495 * Store a new annotation in the store or update an existing one. Returns | 582 * Store a new annotation in the store or update an existing one. Returns |
496 * the stored annotation. | 583 * the stored annotation. |
978 | 1065 |
979 } | 1066 } |
980 return ret; | 1067 return ret; |
981 } | 1068 } |
982 | 1069 |
1070 public List<Annotation> getAnnotationsByResource(String resourceUri) { | |
1071 ArrayList<Annotation> ret = new ArrayList<Annotation>(); | |
1072 Node tag = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE); | |
1073 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES); | |
1074 for (Relationship rel : rels) { | |
1075 Node node = rel.getStartNode(); | |
1076 ret.add(createAnnotationFromNode(node)); | |
1077 } | |
1078 return ret; | |
1079 } | |
1080 | |
983 } | 1081 } |