Changeset 16:794077e6288c in AnnotationManagerN4J
- Timestamp:
- Sep 4, 2012, 6:02:59 PM (12 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/Actor.java
r15 r16 32 32 public boolean isEquivalentWith(Person person, AnnotationStore store) { 33 33 if (person == null) return false; 34 if (person.equals(getIdString())) { 35 return true; 36 } 34 if (person.equals(this)) return true; 35 if (person.getIdString().equals(this.getIdString())) return true; 37 36 if (isGroup() && store != null) { 38 37 // check if person in group -
src/main/java/de/mpiwg/itgroup/annotations/Annotation.java
r15 r16 3 3 */ 4 4 package de.mpiwg.itgroup.annotations; 5 6 import java.util.List; 7 import java.util.Set; 5 8 6 9 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; … … 82 85 */ 83 86 protected Actor readPermission; 84 87 88 /** 89 * List of tags on this Annotation. 90 */ 91 protected Set<String> tags; 85 92 86 93 /** … … 318 325 this.readPermission = readPermission; 319 326 } 327 328 /** 329 * @return the tags 330 */ 331 public Set<String> getTags() { 332 return tags; 333 } 334 335 /** 336 * @param tags the tags to set 337 */ 338 public void setTags(Set<String> tags) { 339 this.tags = tags; 340 } 320 341 321 342 -
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r15 r16 6 6 import java.util.ArrayList; 7 7 import java.util.Calendar; 8 import java.util.HashSet; 8 9 import java.util.List; 10 import java.util.Set; 9 11 10 12 import org.apache.log4j.Logger; … … 35 37 36 38 public static enum NodeTypes { 37 ANNOTATION, PERSON, TARGET, GROUP 39 ANNOTATION, PERSON, TARGET, GROUP, TAG 38 40 } 39 41 … … 41 43 42 44 public static enum RelationTypes implements RelationshipType { 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF 45 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF, HAS_TAG 44 46 } 45 47 … … 49 51 super(); 50 52 this.graphDb = graphDb; 51 nodeIndexes = new ArrayList<Index<Node>>( 3);53 nodeIndexes = new ArrayList<Index<Node>>(5); 52 54 // List.set(enum.ordinal(), val) seems not to work. 53 55 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); … … 55 57 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 56 58 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); 59 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags")); 57 60 } 58 61 … … 71 74 } 72 75 73 74 76 /** 75 77 * Returns List of Groups the person is member of. … … 86 88 // make sure we're getting a group 87 89 if (!(group instanceof Group)) { 88 logger.error("target of MEMBER_OF is not GROUP! rel=" +rel);90 logger.error("target of MEMBER_OF is not GROUP! rel=" + rel); 89 91 continue; 90 92 } … … 93 95 return groups; 94 96 } 95 97 96 98 /** 97 99 * Returns if person with uri is in Group group. … … 104 106 Node pn = getPersonNodeByUri(person.getUriString()); 105 107 if (pn == null) return false; 106 // optimi sed version of getGroupsForPersonNode108 // optimized version of getGroupsForPersonNode 107 109 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); 108 110 for (Relationship rel : rels) { … … 114 116 return false; 115 117 } 116 118 117 119 /** 118 120 * Returns the Annotation with the given id. … … 138 140 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 139 141 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 140 // get annotation target from relation 142 /* 143 * get annotation target from relation 144 */ 141 145 Relationship targetRel = getRelation(annotNode, RelationTypes.ANNOTATES, null); 142 146 if (targetRel != null) { … … 151 155 annot.setFragmentType(FragmentTypes.valueOf(ft)); 152 156 } 153 // get creator from relation 157 /* 158 * get creator from relation 159 */ 154 160 Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null); 155 161 if (creatorRel != null) { … … 160 166 logger.error("annotation " + annotNode + " has no creator node!"); 161 167 } 162 // get creation date 168 /* 169 * get creation date 170 */ 163 171 annot.setCreated((String) annotNode.getProperty("created", null)); 164 // get permissions 172 /* 173 * get permissions 174 */ 165 175 Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null); 166 176 if (adminRel != null) { … … 187 197 annot.setReadPermission(read); 188 198 } 199 /* 200 * get tags 201 */ 202 Set<String> tags = new HashSet<String>(); 203 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { 204 String tag = (String) rel.getEndNode().getProperty("name", null); 205 if (tag != null) { 206 tags.add(tag); 207 } 208 } 209 annot.setTags(tags); 189 210 190 211 return annot; … … 286 307 setPermissionRelation(annotNode, RelationTypes.PERMITS_READ, annot.getReadPermission()); 287 308 309 /* 310 * Tags on this annotation. 311 */ 312 Set<String> newTags = annot.getTags(); 313 // we ignore existing tags if tags == null 314 if (newTags != null) { 315 List<Relationship> oldHasTags = new ArrayList<Relationship>(); 316 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { 317 oldHasTags.add(rel); 318 } 319 // adjust to new tags 320 if (newTags.isEmpty()) { 321 // remove old tags 322 if (!oldHasTags.isEmpty()) { 323 for (Relationship rel : oldHasTags) { 324 rel.delete(); 325 // TODO: should we delete orphan nodes too? 326 } 327 } 328 } else { 329 if (!oldHasTags.isEmpty()) { 330 // adjust old tags 331 for (Relationship rel : oldHasTags) { 332 String oldTag = (String) rel.getEndNode().getProperty("name", null); 333 if (newTags.contains(oldTag)) { 334 // tag exists 335 newTags.remove(oldTag); 336 } else { 337 // tag exists no longer 338 rel.delete(); 339 // TODO: should we delete orphan nodes too? 340 } 341 } 342 } 343 if (!newTags.isEmpty()) { 344 // still tags to add 345 for (String tag : newTags) { 346 // create new tag 347 Node tagNode = getOrCreateTagNode(tag); 348 getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); 349 } 350 } 351 352 } 353 } 288 354 tx.success(); 289 355 } finally { … … 371 437 } 372 438 } 439 // TODO: if both uri and user are given we should intersect 373 440 return annotations; 374 441 } 375 442 443 /** 444 * Returns Relationship of type from Node start to Node end. Creates one if 445 * it doesn't exist. 446 * 447 * @param start 448 * @param type 449 * @param end 450 * @return 451 */ 376 452 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { 377 453 if (start.hasRelationship()) { … … 458 534 person.setProperty("TYPE", NodeTypes.GROUP.name()); 459 535 } else { 460 person.setProperty("TYPE", NodeTypes.PERSON.name()); 536 person.setProperty("TYPE", NodeTypes.PERSON.name()); 461 537 } 462 538 person.setProperty("uri", uri); … … 474 550 } 475 551 return person; 552 } 553 554 protected Node getOrCreateTagNode(String tagname) { 555 Index<Node> idx = getNodeIndex(NodeTypes.TAG); 556 IndexHits<Node> tags = idx.get("name", tagname); 557 Node tag = tags.getSingle(); 558 if (tag == null) { 559 // does not exist yet 560 Transaction tx = graphDb.beginTx(); 561 try { 562 tag = graphDb.createNode(); 563 tag.setProperty("TYPE", NodeTypes.TAG.name()); 564 tag.setProperty("name", tagname); 565 idx.add(tag, "name", tagname); 566 tx.success(); 567 } finally { 568 tx.finish(); 569 } 570 } 571 return tag; 476 572 } 477 573 … … 533 629 } 534 630 535 /** returns the (first) Relationship of RelationTypes type from Node start. 536 * 631 /** 632 * returns the (first) Relationship of RelationTypes type from Node start. 633 * 537 634 * @param start 538 635 * @param type … … 549 646 } 550 647 for (Relationship rel : rels) { 648 // just the first one 551 649 return rel; 552 650 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r15 r16 55 55 logger.debug("request authenticated=" + authUser); 56 56 57 Annotation annot = getAnnotationStore().getAnnotationById(id); 57 AnnotationStore store = getAnnotationStore(); 58 Annotation annot = store.getAnnotationById(id); 58 59 if (annot != null) { 59 if (! annot.isActionAllowed("read", authUser, null)) {60 if (! annot.isActionAllowed("read", authUser, store)) { 60 61 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 61 62 return null; … … 160 161 return null; 161 162 } 162 if (! storedAnnot.isActionAllowed("update", authUser, null)) {163 if (! storedAnnot.isActionAllowed("update", authUser, store)) { 163 164 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 164 165 return null; … … 207 208 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 208 209 logger.debug("request authenticated=" + authUser); 209 Annotation annot = getAnnotationStore().getAnnotationById(id); 210 AnnotationStore store = getAnnotationStore(); 211 Annotation annot = store.getAnnotationById(id); 210 212 if (annot != null) { 211 if (! annot.isActionAllowed("delete", authUser, null)) {213 if (! annot.isActionAllowed("delete", authUser, store)) { 212 214 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 213 215 return null; … … 216 218 217 219 // delete annotation 218 getAnnotationStore().deleteById(id);220 store.deleteById(id); 219 221 setStatus(Status.SUCCESS_NO_CONTENT); 220 222 return null; -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r15 r16 10 10 import java.util.ArrayList; 11 11 import java.util.Calendar; 12 import java.util.HashSet; 12 13 import java.util.List; 14 import java.util.Set; 13 15 import java.util.regex.Matcher; 14 16 import java.util.regex.Pattern; … … 190 192 jo.put("uri", annot.getTargetBaseUri()); 191 193 194 /* 195 * user 196 */ 192 197 if (makeUserObject) { 193 198 // create user object … … 214 219 } 215 220 221 /* 222 * ranges 223 */ 216 224 if (annot.getTargetFragment() != null) { 217 225 // we only look at the first xpointer … … 226 234 } 227 235 228 // permissions 236 /* 237 * permissions 238 */ 229 239 JSONObject perms = new JSONObject(); 230 240 jo.put("permissions", perms); … … 267 277 } 268 278 279 /* 280 * tags 281 */ 282 Set<String> tagset = annot.getTags(); 283 if (tagset != null) { 284 JSONArray tags = new JSONArray(); 285 jo.put("tags", tags); 286 for (String tag : tagset) { 287 tags.put(tag); 288 } 289 } 290 291 /* 292 * id 293 */ 269 294 // encode Annotation URL (=id) in base64 270 295 String annotUrl = annot.getUri(); … … 413 438 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException, 414 439 UnsupportedEncodingException { 415 // target uri 440 /* 441 * target uri 442 */ 416 443 if (jo.has("uri")) { 417 444 annot.setTargetBaseUri(jo.getString("uri")); 418 445 } 419 // annotation text 446 /* 447 * annotation text 448 */ 420 449 if (jo.has("text")) { 421 450 annot.setBodyText(jo.getString("text")); 422 451 } 423 // check authentication 452 /* 453 * check authentication 454 */ 424 455 String authUser = checkAuthToken(entity); 425 456 if (authUser == null) { … … 434 465 */ 435 466 } 436 // get or create creator object 467 /* 468 * get or create creator object 469 */ 437 470 Actor creator = annot.getCreator(); 438 471 if (creator == null) { … … 483 516 creator.setUri(userUri); 484 517 } 485 518 /* 519 * creation date 520 */ 486 521 if (annot.getCreated() == null) { 487 522 // set creation date … … 491 526 } 492 527 493 // create xpointer from the first range/area 528 /* 529 * create xpointer from the first range/area 530 */ 494 531 if (jo.has("ranges")) { 495 532 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); … … 505 542 } 506 543 507 // permissions 544 /* 545 * permissions 546 */ 508 547 if (jo.has("permissions")) { 509 548 JSONObject permissions = jo.getJSONObject("permissions"); … … 530 569 } 531 570 571 /* 572 * tags 573 */ 574 if (jo.has("tags")) { 575 HashSet<String> tagset = new HashSet<String>(); 576 JSONArray tags = jo.getJSONArray("tags"); 577 for (int i = 0; i < tags.length(); ++i) { 578 tagset.add(tags.getString(i)); 579 } 580 annot.setTags(tagset); 581 } 582 583 532 584 return annot; 533 585 }
Note: See TracChangeset
for help on using the changeset viewer.