Mercurial > hg > AnnotationManagerN4J
changeset 28:f4ed2ed33e5b
Restinterface zur Anzeige von Tags hinzugefuegt-
author | dwinter |
---|---|
date | Tue, 25 Sep 2012 09:32:56 +0200 |
parents | e208a7b1a37a |
children | 3be0ebb6d5ad |
files | src/main/java/de/mpiwg/itgroup/annotations/NS.java src/main/java/de/mpiwg/itgroup/annotations/Tag.java src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java |
diffstat | 7 files changed, 375 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/NS.java Sun Sep 23 16:28:05 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/NS.java Tue Sep 25 09:32:56 2012 +0200 @@ -2,6 +2,7 @@ public class NS { public static final String MPIWG_PERSONS_URL = "http://entities.mpiwg-berlin.mpg.de/persons/"; + public static final String MPIWG_TAGS_URL = "http://entities.mpiwg-berlin.mpg.de/tags/"; public static final String MPIWG_GROUPS_URL = "http://entities.mpiwg-berlin.mpg.de/groups/"; public static final String OAC_NS = "http://www.openannotation.org/ns/"; public static final String CNT_NS = "http://www.w3.org/2011/content#";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/Tag.java Tue Sep 25 09:32:56 2012 +0200 @@ -0,0 +1,44 @@ +package de.mpiwg.itgroup.annotations; + +/** + * @author dwinter + * + */ +public class Tag { + + public String uri; + public String name; + public String id; + + + public Tag(String id, String uri, String name) { + this.uri=uri; + this.id=id; + this.name=name; + } + public String getUri() { + if (uri==null){ + return NS.MPIWG_TAGS_URL+getId(); // erzeuge uri aus if falls keine ID + } + return uri; + } + public void setUri(String uri) { + this.uri = uri; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getId() { + if (id==null){ + return getName(); //take name if id not defined + } + return id; + } + public void setId(String id) { + this.id = id; + } + +}
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Sun Sep 23 16:28:05 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Tue Sep 25 09:32:56 2012 +0200 @@ -24,6 +24,7 @@ import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes; import de.mpiwg.itgroup.annotations.Group; import de.mpiwg.itgroup.annotations.Person; +import de.mpiwg.itgroup.annotations.Tag; /** * @author casties @@ -73,6 +74,16 @@ return person; } + /** + * @param tagUri + * @return + */ + public Node getTagNodeByUri(String tagUri) { + if (tagUri == null) return null; + Node person = getNodeIndex(NodeTypes.TAG).get("uri", tagUri).getSingle(); + return person; + } + public List<Actor> getActors(String key, String query, NodeTypes type) { ArrayList<Actor> actors = new ArrayList<Actor>(); Index<Node> idx = getNodeIndex(type); @@ -110,8 +121,33 @@ } return groups; } + + + /** + * Returns List of Tags. + * Key has to be indexed. + * + * @param key + * @param query + * @return + */ + public List<Tag> getTags(String key, String query) { + ArrayList<Tag> tags = new ArrayList<Tag>(); + Index<Node> idx = getNodeIndex(NodeTypes.TAG); + if (key == null) { + key = "uri"; + query = "*"; + } + IndexHits<Node> groupNodes = idx.query(key, query); + for (Node groupNode : groupNodes) { + Tag tag = createTagFromNode(groupNode); + tags.add(tag); + } + return tags; + } - /** + + /** * Returns List of Groups the person is member of. * * @param person @@ -314,6 +350,16 @@ } return null; } + + public Tag createTagFromNode(Node tagNode) { + String name = (String) tagNode.getProperty("name", null); + String uri = (String) tagNode.getProperty("uri", null); + String id = (String) tagNode.getProperty("id", null); + + return new Tag(id, uri, name); + + } + /** * Store a new annotation in the store or update an existing one. Returns @@ -429,7 +475,7 @@ // still tags to add for (String tag : newTags) { // create new tag - Node tagNode = getOrCreateTagNode(tag); + Node tagNode = getOrCreateTagNode(new Tag(null,null,tag)); getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); } } @@ -650,8 +696,9 @@ return person; } - protected Node getOrCreateTagNode(String tagname) { + protected Node getOrCreateTagNode(Tag inTag) { Index<Node> idx = getNodeIndex(NodeTypes.TAG); + String tagname = inTag.getName(); IndexHits<Node> tags = idx.get("name", tagname); Node tag = tags.getSingle(); if (tag == null) { @@ -662,6 +709,11 @@ tag.setProperty("TYPE", NodeTypes.TAG.name()); tag.setProperty("name", tagname); idx.add(tag, "name", tagname); + + tag.setProperty("id", inTag.getId()); + tag.setProperty("uri", inTag.getUri()); + idx.add(tag, "uri", inTag.getUri()); + tx.success(); } finally { tx.finish(); @@ -765,4 +817,20 @@ } + public List<Annotation> getAnnotationsByTag(String tagUri) { + + ArrayList<Annotation> ret = new ArrayList<Annotation>(); + Node tag = getTagNodeByUri(tagUri); + + + Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG); + + for (Relationship rel:rels){ + Node node = rel.getStartNode(); + ret.add(createAnnotationFromNode(node)); + + } + return ret; + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java Tue Sep 25 09:32:56 2012 +0200 @@ -0,0 +1,105 @@ +package de.mpiwg.itgroup.annotations.restlet; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.neo4j.graphdb.Node; +import org.restlet.data.Form; +import org.restlet.data.Parameter; +import org.restlet.data.Status; +import org.restlet.ext.json.JsonRepresentation; +import org.restlet.representation.Representation; +import org.restlet.resource.Get; +import org.restlet.resource.ServerResource; + +import de.mpiwg.itgroup.annotations.Annotation; +import de.mpiwg.itgroup.annotations.NS; +import de.mpiwg.itgroup.annotations.Tag; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; +import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; + +/** + * API for accessing tags in the Annotation store. + * + * @author dwinter + * + */ +public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { + protected String getAllowedMethodsForHeader() { + return "OPTIONS,GET"; + } + + @Get("json") + public Representation doGetJSON(Representation entity) { + logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); + setCorsHeaders(); + + String jsonId = (String) getRequest().getAttributes().get("id"); + + Form form = getRequest().getResourceRef().getQueryAsForm(); + String sortBy=null; + for (Parameter parameter : form) { + if (parameter.getName().equals("sortBy")){ + sortBy = parameter.getValue(); + } + } + + //String id = decodeJsonId(jsonId); + String id = jsonId; + logger.debug("annotation-id=" + id); + + AnnotationStore store = getAnnotationStore(); + String tagUri=NS.MPIWG_TAGS_URL+id; + List<Annotation> annotations = store.getAnnotationsByTag(tagUri); + + //JSONArray results = new JSONArray(); + ArrayList<JSONObject> results = new ArrayList<JSONObject>(); + + for (Annotation annot : annotations) { + JSONObject jo = createAnnotatorJson(annot,false); + results.add(jo); + } + + if (sortBy!=null){ + sortAnnotations(results,sortBy); + } + + JSONArray resultsJa = new JSONArray(); + for (JSONObject result:results){ + resultsJa.put(result); + } + + // assemble result object + JSONObject result = new JSONObject(); + try { + result.put("rows", resultsJa); + result.put("total", resultsJa.length()); + } catch (JSONException e) { + setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); + return null; + } + logger.debug("sending:"); + logger.debug(result); + return new JsonRepresentation(result); +} + + + /** + * Sortiere array nach einem Parameter in den Annotationen + * @param results + * @return + */ + private void sortAnnotations(List<JSONObject> results,String attribute) { + JSONObjectComparator comp = new JSONObjectComparator(attribute); + + + + java.util.Collections.sort( results, comp); + + + + } +} \ No newline at end of file
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java Sun Sep 23 16:28:05 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java Tue Sep 25 09:32:56 2012 +0200 @@ -41,7 +41,10 @@ router.attach("/annotations/{id}", AnnotatorAnnotations.class); router.attach("/search", AnnotatorSearch.class); router.attach("/groups", AnnotatorGroups.class); - + router.attach("/tags", AnnotatorTags.class); + router.attach("/tags/{id}", AnnotatorTags.class); + router.attach("/tags/{id}", AnnotatorTags.class); + router.attach("/tags/{id}/annotations", AnnotatorAnnotationsByTags.class); router.attach("/", AnnotatorInfo.class); // authenticator.setNext(router); // return authenticator;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java Tue Sep 25 09:32:56 2012 +0200 @@ -0,0 +1,112 @@ +/** + * ReST API for accessing groups in the Annotation store. + */ +package de.mpiwg.itgroup.annotations.restlet; + +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.neo4j.graphdb.Node; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.ext.json.JsonRepresentation; +import org.restlet.representation.Representation; +import org.restlet.resource.Get; + +import de.mpiwg.itgroup.annotations.Actor; +import de.mpiwg.itgroup.annotations.Group; +import de.mpiwg.itgroup.annotations.NS; +import de.mpiwg.itgroup.annotations.Tag; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes; + + +/** + * API for accessing tags in the Annotation store. + * + * @author dwinter + * + */ +public class AnnotatorTags extends AnnotatorResourceImpl { + protected String getAllowedMethodsForHeader() { + return "OPTIONS,GET"; + } + + /** + * GET with JSON content-type. + * Parameters: + * user: short user name + * uri: user uri + * + * @param entity + * @return + */ + @Get("json") + public Representation doGetJSON(Representation entity) { + logger.debug("AnnotatorGroups doGetJSON!"); + setCorsHeaders(); + + String jsonId = (String) getRequest().getAttributes().get("id"); + //String id = decodeJsonId(jsonId); + String id = jsonId; + logger.debug("annotation-id=" + id); + + if (id==null){ + return getAllTags(); + } else { + + return getTag(id); + } + } + + protected Representation getTag(String id){ + AnnotationStore store = getAnnotationStore(); + String tagUri=NS.MPIWG_TAGS_URL+id; + Node tagNode = store.getTagNodeByUri(tagUri); + Tag tag = store.createTagFromNode(tagNode); + JSONObject jo = new JSONObject(); + try { + jo.put("id", tag.getId()); + jo.put("name", tag.getName()); + jo.put("uri", tag.getUri()); + } catch (JSONException e) { + } + + return new JsonRepresentation(jo); + } + protected Representation getAllTags() { + JSONArray results = new JSONArray(); + AnnotationStore store = getAnnotationStore(); + + + List<Tag> tags = store.getTags(null, null); + for (Tag tag : tags) { + JSONObject jo = new JSONObject(); + try { + jo.put("id", tag.getId()); + jo.put("name", tag.getName()); + jo.put("uri", tag.getUri()); + } catch (JSONException e) { + } + results.put(jo); + } + + // assemble result object + JSONObject result = new JSONObject(); + try { + result.put("rows", results); + result.put("total", results.length()); + } catch (JSONException e) { + setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); + return null; + } + logger.debug("sending:"); + logger.debug(result); + return new JsonRepresentation(result); + } + + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java Tue Sep 25 09:32:56 2012 +0200 @@ -0,0 +1,38 @@ +package de.mpiwg.itgroup.annotations.restlet.utils; + +import java.util.Comparator; + +import de.mpiwg.itgroup.annotations.Annotation; + +import org.json.JSONException; +import org.json.JSONObject; + +public class JSONObjectComparator implements Comparator<JSONObject>{ + + private String attributeToSort; + public JSONObjectComparator(String attribute){ + this.attributeToSort=attribute; + } + + public int compare( JSONObject a, JSONObject b ) { + // je quadratischer, desto grösser + String sortA; + try { + sortA = a.getString(attributeToSort); + } catch (JSONException e) { + sortA =""; + } + String sortB; + try { + sortB = b.getString(attributeToSort); + } catch (JSONException e) { + sortB=""; + } + + + return sortA.compareToIgnoreCase(sortB); + } + + + +} \ No newline at end of file