Changeset 22:b1fb0d117877 in AnnotationManagerN4J
- Timestamp:
- Sep 20, 2012, 3:42:26 PM (12 years ago)
- Branch:
- default
- Location:
- src/main
- Files:
-
- 3 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r19 r22 140 140 } 141 141 142 /** 143 * Returns the members of the group. 144 * 145 * @param group 146 * @return 147 */ 148 public List<Person> getMembersOfGroup(Group group) { 149 ArrayList<Person> members = new ArrayList<Person>(); 150 Node gn = getActorNode(group); 151 Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF); 152 for (Relationship rel : rels) { 153 Node memberNode = rel.getStartNode(); 154 Actor member = createActorFromNode(memberNode); 155 // make sure we're getting a group 156 if (!(member instanceof Person)) { 157 logger.error("source of MEMBER_OF is not PERSON! rel=" + rel); 158 continue; 159 } 160 members.add((Person) member); 161 } 162 return members; 163 } 164 165 /** 166 * Returns the stored Actor matching the given one. 167 * 168 * @param actor 169 * @return 170 */ 171 public Actor getActor(Actor actor) { 172 Node actorNode = getActorNode(actor); 173 Actor storedActor = createActorFromNode(actorNode); 174 return storedActor; 175 } 176 177 /** 178 * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one. 179 * 180 * @param actor 181 * @return 182 */ 183 public Actor storeActor(Actor actor) { 184 Node actorNode = getOrCreateActorNode(actor); 185 Actor storedActor = createActorFromNode(actorNode); 186 return storedActor; 187 } 188 142 189 /** 143 190 * Returns the Annotation with the given id. … … 536 583 } 537 584 585 protected Node getActorNode(Actor actor) { 586 // Person/Group is identified by URI or id 587 String uri = actor.getUriString(); 588 Index<Node> idx; 589 if (actor.isGroup()) { 590 idx = getNodeIndex(NodeTypes.GROUP); 591 } else { 592 idx = getNodeIndex(NodeTypes.PERSON); 593 } 594 IndexHits<Node> persons = idx.get("uri", uri); 595 Node person = persons.getSingle(); 596 return person; 597 } 598 538 599 protected Node getOrCreateActorNode(Actor actor) { 539 600 // Person/Group is identified by URI or id -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java
r21 r22 6 6 import java.util.List; 7 7 8 import javax.servlet.ServletContext;9 10 8 import org.apache.log4j.Logger; 11 9 import org.restlet.data.Form; 12 10 import org.restlet.data.MediaType; 11 import org.restlet.data.Reference; 12 import org.restlet.data.Status; 13 13 import org.restlet.representation.Representation; 14 14 import org.restlet.representation.StringRepresentation; 15 15 import org.restlet.resource.Get; 16 import org.restlet.resource.Post; 16 17 import org.restlet.resource.ServerResource; 17 18 19 import de.mpiwg.itgroup.annotations.Actor; 18 20 import de.mpiwg.itgroup.annotations.Group; 21 import de.mpiwg.itgroup.annotations.Person; 19 22 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 20 23 21 24 /** 22 25 * @author casties 23 * 26 * 24 27 */ 25 28 public class AnnotationStoreGroups extends ServerResource { … … 29 32 private AnnotationStore store; 30 33 34 /** 35 * GET with HTML content type. Lists all groups. 36 * 37 * @param entity 38 * @return 39 */ 31 40 @Get("html") 32 public Representation doGetHTML(Representation entity){ 33 Form form = getRequest().getResourceRef().getQueryAsForm(); 41 public Representation doGetHTML(Representation entity) { 34 42 // id from URI /annotations/groups/{id} 35 43 String id = (String) getRequest().getAttributes().get("id"); 36 44 logger.debug("group-id=" + id); 37 String result="<html><body>\n<h1>Groups</h1>\n<table>"; 38 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; 45 String result = null; 39 46 store = getAnnotationStore(); 40 47 if (id == null) { 41 48 // list all groups 49 result = "<html><body>\n<h1>Groups</h1>\n<table>"; 50 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; 42 51 List<Group> groups = store.getGroups("uri", "*"); 43 52 for (Group group : groups) { 44 String groupLink = group.getId(); 45 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri()); 53 Reference groupUrl = this.getReference(); 54 groupUrl.addSegment(group.getId()); 55 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, 56 group.getId(), group.getName(), group.getUri()); 46 57 } 58 result += "</table>\n</body>\n</html>"; 47 59 } else { 48 60 // just one group 49 List<Group> groups = store.getGroups("uri", "*"); 50 for (Group group : groups) { 51 String groupLink = group.getId(); 52 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri()); 61 Reference groupsUrl = this.getReference().getParentRef(); 62 result = "<html><body>\n<h1>Group</h1>\n"; 63 result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl); 64 Group group = new Group(id); 65 group = (Group) store.getActor(group); 66 result += "<table>"; 67 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); 68 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); 69 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); 70 result += "<tr><td><b>members</b></td><td>"; 71 List<Person> members = store.getMembersOfGroup(group); 72 for (Person p : members) { 73 result += String.format("%s (%s)\n", p.getName(), p.getIdString()); 53 74 } 75 result += "</td></tr>\n"; 76 result += "</table>\n</body>\n</html>"; 54 77 } 55 result += "</table>\n</body>\n</html>"; 56 78 57 79 logger.debug("sending:"); 58 80 logger.debug(result); 59 return new StringRepresentation(result,MediaType.TEXT_HTML); 81 return new StringRepresentation(result, MediaType.TEXT_HTML); 82 } 83 84 /** 85 * POST with HTML content-type. Creates a new Group. 86 * 87 * @return 88 */ 89 @Post 90 public Representation doPostHTML(Representation entity) { 91 logger.debug("AnnotationStoreGroups doPostHTML!"); 92 // TODO: do authentication 93 Form form = new Form(entity); 94 String id = form.getFirstValue("id"); 95 String name = form.getFirstValue("name"); 96 if (id == null || id.isEmpty()) { 97 // invalid id 98 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 99 return null; 100 } 101 String gid = makeGroupId(id); 102 Group newGroup = new Group(gid, null, name); 103 store = getAnnotationStore(); 104 Actor storedGroup = store.storeActor(newGroup); 105 gid = storedGroup.getId(); 106 // return 303: see other 107 setStatus(Status.REDIRECTION_SEE_OTHER); 108 // go GET URL for this group 109 Reference groupUrl = this.getReference(); 110 groupUrl.addSegment(gid); 111 this.getResponse().setLocationRef(groupUrl); 112 return null; 113 } 114 115 /** 116 * Returns a group id based on the given id. 117 * 118 * @param id 119 * @return 120 */ 121 protected String makeGroupId(String id) { 122 // TODO: should we use different ids? 123 id = id.replaceAll("\\W", "_"); 124 id = id.toLowerCase(); 125 return id; 60 126 } 61 127 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreRestlet.java
r18 r22 36 36 37 37 router.attach("/groups", AnnotationStoreGroups.class); 38 router.attach("/groups/", AnnotationStoreGroups.class); 38 39 router.attach("/groups/{id}", AnnotationStoreGroups.class); 39 40 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r20 r22 49 49 logger.debug("annotation-id=" + id); 50 50 51 // TODO: what to return without id - list of all annotations? 51 if (id == null) { 52 // TODO: what to return without id - list all annotations? 53 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 54 return null; 55 } 52 56 53 57 // do authentication -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r19 r22 66 66 67 67 public String encodeJsonId(String id) { 68 if (id == null) return null; 68 69 try { 69 70 return Base64.encodeBase64URLSafeString(id.getBytes("UTF-8")); … … 74 75 75 76 public String decodeJsonId(String id) { 77 if (id == null) return null; 76 78 try { 77 79 return new String(Base64.decodeBase64(id), "UTF-8"); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java
r19 r22 135 135 logger.error("Unable to get resource " + dbFn); 136 136 } 137 } else { 138 // get existing AnnotationStore 139 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY); 137 140 } 138 141 /*
Note: See TracChangeset
for help on using the changeset viewer.