comparison src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 59:e2f86ef9b871

make annotation uri in store configurable. fix npe with no tags.
author casties
date Tue, 20 Nov 2012 19:16:43 +0100
parents 0e00bf8e27fb
children 9f8c9611848a
comparison
equal deleted inserted replaced
58:f5c0e6df7e88 59:e2f86ef9b871
53 53
54 public static enum RelationTypes implements RelationshipType { 54 public static enum RelationTypes implements RelationshipType {
55 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF, HAS_TAG, PART_OF 55 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF, HAS_TAG, PART_OF
56 } 56 }
57 57
58 public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/"; 58 public static String ANNOTATION_URI_PREFIX = "";
59 59
60 public AnnotationStore(GraphDatabaseService graphDb) { 60 public AnnotationStore(GraphDatabaseService graphDb) {
61 super(); 61 super();
62 this.graphDb = graphDb; 62 this.graphDb = graphDb;
63 nodeIndexes = new ArrayList<Index<Node>>(5); 63 nodeIndexes = new ArrayList<Index<Node>>(5);
77 /** 77 /**
78 * @param userUri 78 * @param userUri
79 * @return 79 * @return
80 */ 80 */
81 public Node getPersonNodeByUri(String userUri) { 81 public Node getPersonNodeByUri(String userUri) {
82 if (userUri == null) return null; 82 return getNodeFromIndex("uri", userUri, NodeTypes.PERSON);
83 Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
84 return person;
85 } 83 }
86 84
87 /** 85 /**
88 * @param tagUri 86 * @param tagUri
89 * @return 87 * @return
90 */ 88 */
91 public Node getTagNodeByUri(String tagUri) { 89 public Node getTagNodeByUri(String tagUri) {
92 if (tagUri == null) return null; 90 return getNodeFromIndex("uri", tagUri, NodeTypes.TAG);
93 Node person = getNodeIndex(NodeTypes.TAG).get("uri", tagUri).getSingle();
94 return person;
95 } 91 }
96 92
97 /** 93 /**
98 * @param resourceUri 94 * @param resourceUri
99 * @return 95 * @return
109 public Node getTargetNodeByUri(String targetUri) { 105 public Node getTargetNodeByUri(String targetUri) {
110 return getNodeFromIndex("uri", targetUri, NodeTypes.RESOURCE); 106 return getNodeFromIndex("uri", targetUri, NodeTypes.RESOURCE);
111 } 107 }
112 108
113 /** 109 /**
114 * Returns the Node with the given key and value. 110 * Returns the Node with the given key and value. Key has to be indexed.
115 * Key has to be indexed.
116 * 111 *
117 * @param key 112 * @param key
118 * @param value 113 * @param value
119 * @param type 114 * @param type
120 * @return 115 * @return
121 */ 116 */
122 public Node getNodeFromIndex(String key, String value, NodeTypes type) { 117 public Node getNodeFromIndex(String key, String value, NodeTypes type) {
123 if (key == null || value == null) return null; 118 if (key == null || value == null) return null;
124 Node person = getNodeIndex(type).get(key, value).getSingle(); 119 Node node = getNodeIndex(type).get(key, value).getSingle();
125 return person; 120 return node;
126 } 121 }
127 122
128 /** 123 /**
129 * Returns list of Actors of given type (Group or Person). 124 * Returns list of Actors of given type (Group or Person). Key has to be
130 * Key has to be indexed. 125 * indexed.
131 * 126 *
132 * @param key 127 * @param key
133 * @param query 128 * @param query
134 * @param type 129 * @param type
135 * @return 130 * @return
147 Actor actor = createActorFromNode(actorNode); 142 Actor actor = createActorFromNode(actorNode);
148 actors.add((T) actor); 143 actors.add((T) actor);
149 } 144 }
150 return actors; 145 return actors;
151 } 146 }
152 147
153 /** 148 /**
154 * Returns list of groups. Key has to be indexed. 149 * Returns list of groups. Key has to be indexed.
150 *
155 * @param key 151 * @param key
156 * @param query 152 * @param query
157 * @return 153 * @return
158 */ 154 */
159 public List<Group> getGroups(String key, String query) { 155 public List<Group> getGroups(String key, String query) {
160 List<Group> groups = getActors(key, query, NodeTypes.GROUP); 156 List<Group> groups = getActors(key, query, NodeTypes.GROUP);
161 return groups; 157 return groups;
162 } 158 }
163 159
164 /** 160 /**
165 * Returns list of Persons. Key has to be indexed. 161 * Returns list of Persons. Key has to be indexed.
162 *
166 * @param key 163 * @param key
167 * @param query 164 * @param query
168 * @return 165 * @return
169 */ 166 */
170 public List<Person> getPersons(String key, String query) { 167 public List<Person> getPersons(String key, String query) {
171 List<Person> persons = getActors(key, query, NodeTypes.PERSON); 168 List<Person> persons = getActors(key, query, NodeTypes.PERSON);
172 return persons; 169 return persons;
173 } 170 }
174 171
175 /** 172 /**
176 * Returns list of uri-like objects of given type (Target or Resource). 173 * Returns list of uri-like objects of given type (Target or Resource). Key
177 * Key has to be indexed. 174 * has to be indexed.
178 * 175 *
179 * @param key 176 * @param key
180 * @param query 177 * @param query
181 * @param type 178 * @param type
182 * @return 179 * @return
195 uris.add((T) uri); 192 uris.add((T) uri);
196 } 193 }
197 return uris; 194 return uris;
198 } 195 }
199 196
200
201 /** 197 /**
202 * Returns list of Targets. Key has to be indexed. 198 * Returns list of Targets. Key has to be indexed.
199 *
203 * @param key 200 * @param key
204 * @param query 201 * @param query
205 * @return 202 * @return
206 */ 203 */
207 public List<Target> getTargets(String key, String query) { 204 public List<Target> getTargets(String key, String query) {
208 List<Target> targets = getUris(key, query, NodeTypes.TARGET); 205 List<Target> targets = getUris(key, query, NodeTypes.TARGET);
209 return targets; 206 return targets;
210 } 207 }
211 208
212 /** 209 /**
213 * Returns list of Resources. Key has to be indexed. 210 * Returns list of Resources. Key has to be indexed.
211 *
214 * @param key 212 * @param key
215 * @param query 213 * @param query
216 * @return 214 * @return
217 */ 215 */
218 public List<Resource> getResources(String key, String query) { 216 public List<Resource> getResources(String key, String query) {
219 List<Resource> targets = getUris(key, query, NodeTypes.RESOURCE); 217 List<Resource> targets = getUris(key, query, NodeTypes.RESOURCE);
220 return targets; 218 return targets;
221 } 219 }
222 220
223 /** 221 /**
224 * Returns List of Annotations. 222 * Returns List of Annotations. Key has to be indexed.
225 * Key has to be indexed.
226 * 223 *
227 * @param key 224 * @param key
228 * @param query 225 * @param query
229 * @return 226 * @return
230 */ 227 */
241 annotations.add(annotation); 238 annotations.add(annotation);
242 } 239 }
243 return annotations; 240 return annotations;
244 } 241 }
245 242
246 243 /**
247 244 * Returns List of Tags. Key has to be indexed.
248 /**
249 * Returns List of Tags.
250 * Key has to be indexed.
251 * 245 *
252 * @param key 246 * @param key
253 * @param query 247 * @param query
254 * @return 248 * @return
255 */ 249 */
593 * @return 587 * @return
594 */ 588 */
595 public Resource createResourceFromNode(Node resourceNode) { 589 public Resource createResourceFromNode(Node resourceNode) {
596 return (Resource) createUriFromNode(resourceNode); 590 return (Resource) createUriFromNode(resourceNode);
597 } 591 }
598 592
599 /** 593 /**
600 * @param targetNode 594 * @param targetNode
601 * @return 595 * @return
602 */ 596 */
603 public Target createTargetFromNode(Node targetNode) { 597 public Target createTargetFromNode(Node targetNode) {
604 return (Target) createUriFromNode(targetNode); 598 return (Target) createUriFromNode(targetNode);
605 } 599 }
606 600
607
608 protected Uri createUriFromNode(Node uriNode) { 601 protected Uri createUriFromNode(Node uriNode) {
609 if (uriNode == null) return null; 602 if (uriNode == null) return null;
610 String uri = (String) uriNode.getProperty("uri", null); 603 String uri = (String) uriNode.getProperty("uri", null);
611 String type = (String) uriNode.getProperty("TYPE", null); 604 String type = (String) uriNode.getProperty("TYPE", null);
612 if (type != null && type.equals("TARGET")) { 605 if (type != null && type.equals("TARGET")) {
1080 * Erzeuge eine urn aus der aktuellen Zeit in millis 1073 * Erzeuge eine urn aus der aktuellen Zeit in millis
1081 * 1074 *
1082 * @return 1075 * @return
1083 */ 1076 */
1084 private String createRessourceURI(String prefix) { 1077 private String createRessourceURI(String prefix) {
1085
1086 Calendar cal = Calendar.getInstance(); 1078 Calendar cal = Calendar.getInstance();
1087
1088 long time = cal.getTimeInMillis(); 1079 long time = cal.getTimeInMillis();
1089 1080 return String.format("%s%s%s", ANNOTATION_URI_PREFIX, prefix, time);
1090 return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
1091
1092 } 1081 }
1093 1082
1094 public List<Annotation> getAnnotationsByTag(String tagUri) { 1083 public List<Annotation> getAnnotationsByTag(String tagUri) {
1095
1096 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 1084 ArrayList<Annotation> ret = new ArrayList<Annotation>();
1097 Node tag = getTagNodeByUri(tagUri); 1085 Node tag = getTagNodeByUri(tagUri);
1098 1086 if (tag != null) {
1099 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); 1087 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG);
1100 1088 for (Relationship rel : rels) {
1101 for (Relationship rel : rels) { 1089 Node node = rel.getStartNode();
1102 Node node = rel.getStartNode(); 1090 ret.add(createAnnotationFromNode(node));
1103 ret.add(createAnnotationFromNode(node)); 1091 }
1104
1105 } 1092 }
1106 return ret; 1093 return ret;
1107 } 1094 }
1108 1095
1109 public List<Annotation> getAnnotationsByResource(String resourceUri) { 1096 public List<Annotation> getAnnotationsByResource(String resourceUri) {
1110 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 1097 ArrayList<Annotation> ret = new ArrayList<Annotation>();
1111 Node tag = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE); 1098 Node res = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE);
1112 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES); 1099 if (res != null) {
1113 for (Relationship rel : rels) { 1100 Iterable<Relationship> rels = res.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES);
1114 Node an = rel.getStartNode(); 1101 for (Relationship rel : rels) {
1115 Node rn = rel.getEndNode(); 1102 Node an = rel.getStartNode();
1116 if (rn.getProperty("TYPE", "").equals("RESOURCE")) { 1103 Node rn = rel.getEndNode();
1117 logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE"); 1104 if (rn.getProperty("TYPE", "").equals("RESOURCE")) {
1118 } 1105 logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE");
1119 ret.add(createAnnotationFromNode(an)); 1106 }
1107 ret.add(createAnnotationFromNode(an));
1108 }
1120 } 1109 }
1121 return ret; 1110 return ret;
1122 } 1111 }
1123 1112
1124 } 1113 }