Changes in [31:9f653697437e:34:8427930c5f88] in AnnotationManagerN4J
- Location:
- src/main
- Files:
-
- 2 added
- 2 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/Person.java
r15 r32 3 3 */ 4 4 package de.mpiwg.itgroup.annotations; 5 6 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; 5 7 6 8 /** … … 55 57 return null; 56 58 } 59 60 /** 61 * Sets the name from the id using getFullNameFromLdap of the Application. 62 * 63 * @param application 64 * @return 65 */ 66 public String updateName(BaseRestlet application) { 67 if (id != null) { 68 name = application.getFullNameFromLdap(id); 69 } 70 return name; 71 } 57 72 } -
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r31 r34 5 5 6 6 import java.util.ArrayList; 7 import java.util.Arrays; 7 8 import java.util.Calendar; 8 9 import java.util.HashSet; … … 28 29 29 30 /** 31 * Neo4J based Annotation store. 32 * 30 33 * @author casties 31 34 * … … 40 43 ANNOTATION, PERSON, TARGET, GROUP, TAG 41 44 } 45 46 // types of nodes that should not be automatically deleted. 47 public Set<String> permanentNodeTypes = new HashSet<String>(Arrays.asList("PERSON", "GROUP", "TAG")); 42 48 43 49 protected List<Index<Node>> nodeIndexes; … … 85 91 } 86 92 87 88 89 93 public List<Actor> getActors(String key, String query, NodeTypes type) { 90 94 ArrayList<Actor> actors = new ArrayList<Actor>(); … … 124 128 return groups; 125 129 } 126 127 130 128 131 /** 129 132 * Returns List of Annotations. … … 174 177 } 175 178 176 177 /** 179 /** 178 180 * Returns List of Groups the person is member of. 179 181 * … … 240 242 return members; 241 243 } 242 244 243 245 /** 244 246 * Add Person newMember to Group group. … … 257 259 return addedMember; 258 260 } 259 261 260 262 /** 261 263 * Delete Person oldMember from Group group. … … 266 268 public void deleteGroupMember(Group group, Person member) { 267 269 Node gn = getActorNode(group); 270 Node pn = getActorNode(member); 268 271 Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF); 269 272 for (Relationship rel : rels) { 270 273 Node mn = rel.getStartNode(); 271 if (mn.equals(member)) { 272 rel.delete(); 274 if (mn.equals(pn)) { 275 Transaction tx = graphDb.beginTx(); 276 try { 277 rel.delete(); 278 tx.success(); 279 } finally { 280 tx.finish(); 281 } 273 282 // there should be only one 274 283 break; 275 284 } 276 } 277 } 278 285 } 286 } 287 279 288 /** 280 289 * Returns the stored Actor matching the given one. … … 284 293 */ 285 294 public Actor getActor(Actor actor) { 286 Node actorNode = getActorNode(actor); 287 Actor storedActor = createActorFromNode(actorNode); 288 return storedActor; 289 } 290 291 /** 292 * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one. 295 Node actorNode = getActorNode(actor); 296 Actor storedActor = createActorFromNode(actorNode); 297 return storedActor; 298 } 299 300 /** 301 * Stores an Actor (Person or Group). Creates a new actor Node or update an 302 * existing one. 293 303 * 294 304 * @param actor … … 296 306 */ 297 307 public Actor storeActor(Actor actor) { 298 Node actorNode = getOrCreateActorNode(actor); 299 Actor storedActor = createActorFromNode(actorNode); 300 return storedActor; 301 } 302 308 Node actorNode = getOrCreateActorNode(actor); 309 Transaction tx = graphDb.beginTx(); 310 try { 311 // id 312 String id = actor.getId(); 313 if (id != null) { 314 actorNode.setProperty("id", id); 315 } 316 // name 317 String name = actor.getName(); 318 if (name != null) { 319 actorNode.setProperty("name", name); 320 } 321 // uri 322 String uri = actor.getUri(); 323 if (uri != null) { 324 actorNode.setProperty("uri", uri); 325 } 326 tx.success(); 327 } finally { 328 tx.finish(); 329 } 330 Actor storedActor = createActorFromNode(actorNode); 331 return storedActor; 332 } 333 334 /** 335 * Deletes the given Actor. 336 * 337 * @param actor 338 */ 339 public void deleteActor(Actor actor) { 340 String uri = actor.getUriString(); 341 Index<Node> idx; 342 if (actor.isGroup()) { 343 idx = getNodeIndex(NodeTypes.GROUP); 344 } else { 345 idx = getNodeIndex(NodeTypes.PERSON); 346 } 347 Node actorNode = idx.get("uri", uri).getSingle(); 348 if (actorNode != null) { 349 // delete relations 350 Transaction tx = graphDb.beginTx(); 351 try { 352 for (Relationship rel : actorNode.getRelationships()) { 353 rel.delete(); 354 } 355 if (!actorNode.hasRelationship()) { 356 // this shouldn't happen 357 deleteNode(actorNode); 358 } else { 359 logger.error("deleteActor: unable to delete: Node still has relations."); 360 } 361 tx.success(); 362 } finally { 363 tx.finish(); 364 } 365 } 366 } 367 303 368 /** 304 369 * Returns the Annotation with the given id. … … 403 468 */ 404 469 protected Actor createActorFromNode(Node actorNode) { 470 if (actorNode == null) return null; 405 471 String id = (String) actorNode.getProperty("id", null); 406 472 String uri = (String) actorNode.getProperty("uri", null); … … 414 480 return null; 415 481 } 416 482 417 483 public Tag createTagFromNode(Node tagNode) { 418 String name = (String) tagNode.getProperty("name", null);419 String uri = (String) tagNode.getProperty("uri", null);420 String id = (String) tagNode.getProperty("id", null);421 422 return new Tag(id, uri, name); 423 424 } 425 484 if (tagNode == null) return null; 485 String name = (String) tagNode.getProperty("name", null); 486 String uri = (String) tagNode.getProperty("uri", null); 487 String id = (String) tagNode.getProperty("id", null); 488 489 return new Tag(id, uri, name); 490 491 } 426 492 427 493 /** … … 539 605 for (String tag : newTags) { 540 606 // create new tag 541 Node tagNode = getOrCreateTagNode(new Tag(null, null,tag));607 Node tagNode = getOrCreateTagNode(new Tag(null, null, tag)); 542 608 getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); 543 609 } … … 561 627 * @param id 562 628 */ 563 public void delete ById(String id) {629 public void deleteAnnotationById(String id) { 564 630 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); 565 631 if (annotNode != null) { … … 569 635 for (Relationship rel : annotNode.getRelationships()) { 570 636 // delete relation and the related node if it has no other 571 // relations 637 // relations and is not permanent 572 638 Node other = rel.getOtherNode(annotNode); 573 639 rel.delete(); 574 if (! other.hasRelationship()) {640 if (!(other.hasRelationship() || permanentNodeTypes.contains(other.getProperty("TYPE", null)))) { 575 641 deleteNode(other); 576 642 } … … 597 663 * @return 598 664 */ 599 public List<Annotation> search ByUriUser(String targetUri, String userUri, String limit, String offset) {665 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) { 600 666 List<Annotation> annotations = new ArrayList<Annotation>(); 601 667 if (targetUri != null) { … … 720 786 return person; 721 787 } 722 788 723 789 protected Node getOrCreateActorNode(Actor actor) { 724 790 // Person/Group is identified by URI or id … … 773 839 tag.setProperty("name", tagname); 774 840 idx.add(tag, "name", tagname); 775 841 776 842 tag.setProperty("id", inTag.getId()); 777 843 tag.setProperty("uri", inTag.getUri()); 778 844 idx.add(tag, "uri", inTag.getUri()); 779 845 780 846 tx.success(); 781 847 } finally { … … 804 870 if (!oldActorNode.equals(newActorNode)) { 805 871 // new admin is different 806 rel.delete(); 872 Transaction tx = graphDb.beginTx(); 873 try { 874 rel.delete(); 875 tx.success(); 876 } finally { 877 tx.finish(); 878 } 807 879 if (newActorNode != null) { 808 880 rel = getOrCreateRelation(annotNode, type, newActorNode); … … 881 953 } 882 954 883 public List<Annotation> getAnnotationsByTag(String tagUri) { 884 885 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 886 Node tag = getTagNodeByUri(tagUri); 887 888 889 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG); 890 891 for (Relationship rel:rels){ 892 Node node = rel.getStartNode(); 893 ret.add(createAnnotationFromNode(node)); 894 895 } 896 return ret; 897 } 955 public List<Annotation> getAnnotationsByTag(String tagUri) { 956 957 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 958 Node tag = getTagNodeByUri(tagUri); 959 960 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); 961 962 for (Relationship rel : rels) { 963 Node node = rel.getStartNode(); 964 ret.add(createAnnotationFromNode(node)); 965 966 } 967 return ret; 968 } 898 969 899 970 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r31 r34 276 276 277 277 // delete annotation 278 store.delete ById(id);278 store.deleteAnnotationById(id); 279 279 setStatus(Status.SUCCESS_NO_CONTENT); 280 280 return null; -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java
r15 r32 57 57 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); 58 58 AnnotationStore store = getAnnotationStore(); 59 List<Annotation> annots = store.search ByUriUser(uri, user, limit, offset);59 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset); 60 60 for (Annotation annot : annots) { 61 61 // check permission -
src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java
r31 r34 17 17 public class AnnotationsUiRestlet extends BaseRestlet { 18 18 19 public final String version = "AnnotationManagerN4J/Annotation Store 0.1";19 public final String version = "AnnotationManagerN4J/AnnotationsUI 0.2"; 20 20 21 21 public static Logger logger = Logger.getLogger(AnnotationsUiRestlet.class); … … 42 42 router.attach("/groups/{id}/", GroupResource.class); 43 43 router.attach("/groups/{id}/members", GroupMembersResource.class); 44 44 router.attach("/persons", PersonsResource.class); 45 router.attach("/persons/", PersonsResource.class); 46 router.attach("/persons/{id}", PersonResource.class); 47 router.attach("/persons/{id}/", PersonResource.class); 48 45 49 router.attach("/", InfoResource.class); 46 50 // authenticator.setNext(router); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java
r24 r32 5 5 6 6 import org.apache.log4j.Logger; 7 import org.restlet.data.Form; 7 8 import org.restlet.data.MediaType; 8 9 import org.restlet.data.Reference; … … 10 11 import org.restlet.representation.Representation; 11 12 import org.restlet.representation.StringRepresentation; 13 import org.restlet.resource.Delete; 12 14 import org.restlet.resource.Get; 15 import org.restlet.resource.Put; 13 16 import org.restlet.resource.ResourceException; 14 17 import org.restlet.resource.ServerResource; … … 29 32 30 33 protected AnnotationStore store; 31 34 32 35 protected String requestId; 33 36 34 37 protected Group group; 35 38 36 39 @Override 37 40 protected void doInit() throws ResourceException { … … 58 61 @Get("html") 59 62 public Representation doGetHTML(Representation entity) { 60 if (requestId == null || requestId.isEmpty()) { 63 if (group == null) { 64 // invalid id 65 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 66 return null; 67 } 68 String result = null; 69 // get form parameter 70 Form f = this.getQuery(); 71 String form = f.getFirstValue("form"); 72 if (form != null && form.equals("edit")) { 73 // output edit form 74 result = "<html><body>\n"; 75 result += String.format("<h1>Edit group %s</h1>\n", group.getId()); 76 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); 77 // tunnel PUT method through POST 78 result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart()); 79 result += "<table>"; 80 result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n", 81 group.getName()); 82 result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", 83 group.getUriString()); 84 result += "</table>\n"; 85 result += "<p><input type=\"submit\"/></p>"; 86 result += "</table>\n</form>\n</body>\n</html>"; 87 } else { 88 // output group content 89 result = "<html><body>\n<h1>Group</h1>\n"; 90 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); 91 result += "<table>"; 92 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); 93 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); 94 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); 95 result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this 96 .getReference().addSegment("members")); 97 result += "</table>\n"; 98 result += "<p><a href=\"?form=edit\">Edit group</a></p>\n"; 99 // tunnel POST as DELETE 100 result += String.format( 101 "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n", 102 this.getReference().getHierarchicalPart()); 103 result += "</body>\n</html>"; 104 } 105 return new StringRepresentation(result, MediaType.TEXT_HTML); 106 } 107 108 /** 109 * PUT updates the group. 110 * 111 * @param entity 112 * @return 113 */ 114 @Put 115 public Representation doPut(Representation entity) { 116 logger.debug("GroupResource.doPut!"); 117 if (group == null) { 61 118 // invalid id 62 119 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 63 120 return null; 64 121 } 65 String result = null; 66 Reference groupsUrl = this.getReference().getParentRef(); 67 result = "<html><body>\n<h1>Group</h1>\n"; 68 result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl); 69 result += "<table>"; 70 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); 71 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); 72 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); 73 result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference() 74 .addSegment("members")); 75 result += "</table>\n</body>\n</html>"; 76 77 logger.debug("sending:"); 78 logger.debug(result); 79 return new StringRepresentation(result, MediaType.TEXT_HTML); 122 // TODO: do authentication 123 Form form = new Form(entity); 124 String name = form.getFirstValue("name"); 125 String uri = form.getFirstValue("uri"); 126 if (name != null && !name.isEmpty()) { 127 group.setName(name); 128 } 129 if (uri != null && !uri.isEmpty()) { 130 group.setUri(uri); 131 } 132 store.storeActor(group); 133 // return 303: see other 134 setStatus(Status.REDIRECTION_SEE_OTHER); 135 // go GET same URL 136 Reference url = this.getReference(); 137 this.getResponse().setLocationRef(url); 138 return null; 80 139 } 81 140 141 /** 142 * DELETE deletes the group. 143 * 144 * @param entity 145 * @return 146 */ 147 @Delete 148 public Representation doDelete(Representation entity) { 149 logger.debug("GroupResource.doDelete!"); 150 if (group == null) { 151 // invalid id 152 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 153 return null; 154 } 155 // TODO: do authentication 156 store.deleteActor(group); 157 // return 303: see other 158 setStatus(Status.REDIRECTION_SEE_OTHER); 159 // go GET parent URL 160 Reference url = this.getReference().getParentRef(); 161 this.getResponse().setLocationRef(url); 162 return null; 163 } 82 164 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java
r23 r32 15 15 import org.restlet.resource.Get; 16 16 import org.restlet.resource.Post; 17 import org.restlet.resource.ResourceException; 17 18 import org.restlet.resource.ServerResource; 18 19 … … 34 35 private AnnotationStore store; 35 36 37 @Override 38 protected void doInit() throws ResourceException { 39 super.doInit(); 40 // get store instance 41 if (store == null) { 42 store = ((BaseRestlet) getApplication()).getAnnotationStore(); 43 } 44 } 45 36 46 /** 37 47 * GET with HTML content type. Lists all groups. … … 43 53 public Representation doGetHTML(Representation entity) { 44 54 String result = null; 45 store = getAnnotationStore(); 46 // list all groups 47 result = "<html><body>\n<h1>Groups</h1>\n<table>"; 48 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; 49 List<Group> groups = store.getGroups("uri", "*"); 50 for (Group group : groups) { 51 Reference groupUrl = this.getReference(); 52 groupUrl.addSegment(group.getId()); 53 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(), 54 group.getName(), group.getUri()); 55 // get form parameter 56 Form f = this.getQuery(); 57 String form = f.getFirstValue("form"); 58 if (form != null && form.equals("new_group")) { 59 // output new group form 60 result = "<html><body>\n"; 61 result += "<h1>New group</h1>\n"; 62 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference()); 63 result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart()); 64 result += "<table>"; 65 result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n"; 66 result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n"; 67 result += "</table>\n"; 68 result += "<p><input type=\"submit\"/></p>\n"; 69 result += "</form>\n</body>\n</html>"; 70 } else { 71 // list all groups 72 result = "<html><body>\n<h1>Groups</h1>\n"; 73 result += "<table>\n"; 74 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; 75 List<Group> groups = store.getGroups("uri", "*"); 76 for (Group group : groups) { 77 Reference groupUrl = this.getReference().clone(); 78 groupUrl.addSegment(group.getId()); 79 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(), 80 group.getName(), group.getUri()); 81 } 82 result += "</table>\n"; 83 result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n"; 84 result += "</body>\n</html>"; 55 85 } 56 result += "</table>\n</body>\n</html>";57 logger.debug("sending:");58 logger.debug(result);59 86 return new StringRepresentation(result, MediaType.TEXT_HTML); 60 87 } … … 79 106 String gid = makeGroupId(id); 80 107 Group newGroup = new Group(gid, null, name); 81 store = getAnnotationStore();82 108 Actor storedGroup = store.storeActor(newGroup); 83 109 gid = storedGroup.getId(); -
src/main/webapp/index.html
r31 r34 11 11 12 12 <body> 13 <h1>Annotation Server</h1> 13 <h1>Annotation Server</h1> 14 <h2>View</h2> 15 <ul> 16 <li><a href="tags">View tags</a></li> 17 <li><a href="annotationBrowser">All Annotations</a></li> 18 </ul> 19 <h2>Admin</h2> 14 20 <ul> 15 <li><a href="groups">View and edit groups</a></li> 16 <li><a href="tags">Browse by Tags</a></li> 17 <li><a href="annotationBrowser">All Annotations</a></li> 18 21 <li><a href="annotations/groups">View and edit groups</a></li> 22 <li><a href="annotations/persons">View and edit persons</a></li> 19 23 </ul> 20 24 </body>
Note: See TracChangeset
for help on using the changeset viewer.