changeset 6:6dfbe2400f64

delete annotation should work now.
author casties
date Wed, 11 Jul 2012 17:08:40 +0200
parents bbf0cc5bee29
children 798a65338565
files .hgignore src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java
diffstat 3 files changed, 365 insertions(+), 283 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Jul 03 21:23:17 2012 +0200
+++ b/.hgignore	Wed Jul 11 17:08:40 2012 +0200
@@ -8,4 +8,6 @@
 syntax: regexp
 ^\.project$
 syntax: regexp
-^\.shell_history$
\ No newline at end of file
+^\.shell_history$
+syntax: regexp
+^src/main/webapp/WEB-INF/consumerkeys\.property$
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Tue Jul 03 21:23:17 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Wed Jul 11 17:08:40 2012 +0200
@@ -26,306 +26,381 @@
  */
 public class AnnotationStore {
 
-    protected static Logger logger = Logger.getLogger(AnnotationStore.class);
-
-    protected GraphDatabaseService graphDb;
+	protected static Logger logger = Logger.getLogger(AnnotationStore.class);
 
-    public static enum RelationTypes implements RelationshipType {
-        ANNOTATES, CREATED
-    }
-
-    public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/";
+	protected GraphDatabaseService graphDb;
+	
+	public static enum NodeTypes {
+		ANNOTATION, PERSON, TARGET
+	}
 
-    public AnnotationStore(GraphDatabaseService graphDb) {
-        super();
-        this.graphDb = graphDb;
-    }
+	protected List<Index<Node>> nodeIndexes;
+	
+	public static enum RelationTypes implements RelationshipType {
+		ANNOTATES, CREATED
+	}
+
+	public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/";
 
-    public Annotation getAnnotationById(String id) {
-        Index<Node> idx = graphDb.index().forNodes("annotations");
-        Node annotNode = idx.get("id", id).getSingle();
-        Annotation annot = createAnnotation(annotNode);
-        return annot;
-    }
+	public AnnotationStore(GraphDatabaseService graphDb) {
+		super();
+		this.graphDb = graphDb;
+		nodeIndexes = new ArrayList<Index<Node>>(3);
+		// List.set(enum.ordinal(), val) seems not to work.
+		nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
+		nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
+		nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
+	}
 
-    /**
-     * Returns an Annotation object from an annotation-Node.
-     * 
-     * @param annotNode
-     * @return
-     */
-    public Annotation createAnnotation(Node annotNode) {
-        Annotation annot = new Annotation();
-        annot.setUri((String) annotNode.getProperty("id", null));
-        annot.setBodyText((String) annotNode.getProperty("bodyText", null));
-        annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
-        // get annotation target from relation
-        Iterable<Relationship> targetRels = annotNode.getRelationships(RelationTypes.ANNOTATES);
-        for (Relationship targetRel : targetRels) {
-            Node target = targetRel.getEndNode();
-            annot.setTargetBaseUri((String) target.getProperty("uri", null));
-            // just the first one
-            break;
-        }
-        annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null));
-        String ft = (String) annotNode.getProperty("fragmentType", null);
-        if (ft != null) {
-            annot.setFragmentType(FragmentTypes.valueOf(ft));
-        }
-        // get creator form relation
-        Iterable<Relationship> creatorRels = annotNode.getRelationships(RelationTypes.CREATED);
-        for (Relationship creatorRel : creatorRels) {
-            Node creator = creatorRel.getStartNode();
-            annot.setCreatorUri((String) creator.getProperty("uri", null));
-            annot.setCreatorName((String) creator.getProperty("name", null));
-            // just the first one
-            break;
-        }
-        annot.setCreated((String) annotNode.getProperty("created", null));
-        return annot;
-    }
+	protected Index<Node> getNodeIndex(NodeTypes type) {
+		return nodeIndexes.get(type.ordinal());
+	}
+	
+	/**
+	 * Returns the Annotation with the given id.
+	 * 
+	 * @param id
+	 * @return
+	 */
+	public Annotation getAnnotationById(String id) {
+		Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
+		Annotation annot = createAnnotation(annotNode);
+		return annot;
+	}
+
+	/**
+	 * Returns an Annotation object from an annotation-Node.
+	 * 
+	 * @param annotNode
+	 * @return
+	 */
+	public Annotation createAnnotation(Node annotNode) {
+		Annotation annot = new Annotation();
+		annot.setUri((String) annotNode.getProperty("id", null));
+		annot.setBodyText((String) annotNode.getProperty("bodyText", null));
+		annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
+		// get annotation target from relation
+		Iterable<Relationship> targetRels = annotNode
+				.getRelationships(RelationTypes.ANNOTATES);
+		for (Relationship targetRel : targetRels) {
+			Node target = targetRel.getEndNode();
+			annot.setTargetBaseUri((String) target.getProperty("uri", null));
+			// just the first one
+			break;
+		}
+		annot.setTargetFragment((String) annotNode.getProperty(
+				"targetFragment", null));
+		String ft = (String) annotNode.getProperty("fragmentType", null);
+		if (ft != null) {
+			annot.setFragmentType(FragmentTypes.valueOf(ft));
+		}
+		// get creator form relation
+		Iterable<Relationship> creatorRels = annotNode
+				.getRelationships(RelationTypes.CREATED);
+		for (Relationship creatorRel : creatorRels) {
+			Node creator = creatorRel.getStartNode();
+			annot.setCreatorUri((String) creator.getProperty("uri", null));
+			annot.setCreatorName((String) creator.getProperty("name", null));
+			// just the first one
+			break;
+		}
+		annot.setCreated((String) annotNode.getProperty("created", null));
+		return annot;
+	}
 
-    /**
-     * Store a new annotation in the store or update an existing one. Returns
-     * the stored annotation.
-     * 
-     * @param annot
-     * @return
-     */
-    public Annotation storeAnnotation(Annotation annot) {
-        Node annotNode = null;
-        Transaction tx = graphDb.beginTx();
-        try {
-            /*
-             * create or get the annotation
-             */
-            String id = annot.getUri();
-            if (id == null) {
-                id = createRessourceURI("annot:");
-            }
-            annotNode = getOrCreateAnnotationNode(id);
+	/**
+	 * Store a new annotation in the store or update an existing one. Returns
+	 * the stored annotation.
+	 * 
+	 * @param annot
+	 * @return
+	 */
+	public Annotation storeAnnotation(Annotation annot) {
+		Node annotNode = null;
+		Transaction tx = graphDb.beginTx();
+		try {
+			/*
+			 * create or get the annotation
+			 */
+			String id = annot.getUri();
+			if (id == null) {
+				id = createRessourceURI("annot:");
+			}
+			annotNode = getOrCreateAnnotationNode(id);
 
-            /*
-             * the annotation body
-             */
-            String bodyText = annot.getBodyText();
-            if (bodyText != null) {
-                annotNode.setProperty("bodyText", bodyText);
-            }
-            String bodyUri = annot.getBodyUri();
-            if (bodyUri != null) {
-                annotNode.setProperty("bodyUri", bodyUri);
-            }
+			/*
+			 * the annotation body
+			 */
+			String bodyText = annot.getBodyText();
+			if (bodyText != null) {
+				annotNode.setProperty("bodyText", bodyText);
+			}
+			String bodyUri = annot.getBodyUri();
+			if (bodyUri != null) {
+				annotNode.setProperty("bodyUri", bodyUri);
+			}
 
-            /*
-             * the annotation target
-             */
-            String targetBaseUri = annot.getTargetBaseUri();
-            if (targetBaseUri != null) {
-                Node target = getOrCreateTargetNode(targetBaseUri);
-                getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
-            }
+			/*
+			 * the annotation target
+			 */
+			String targetBaseUri = annot.getTargetBaseUri();
+			if (targetBaseUri != null) {
+				Node target = getOrCreateTargetNode(targetBaseUri);
+				getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
+			}
 
-            /*
-             * The fragment part of the annotation target.
-             */
-            String targetFragment = annot.getTargetFragment();
-            FragmentTypes fragmentType = annot.getFragmentType();
-            if (targetFragment != null) {
-                annotNode.setProperty("targetFragment", targetFragment);
-                annotNode.setProperty("fragmentType", fragmentType.name());
-            }
+			/*
+			 * The fragment part of the annotation target.
+			 */
+			String targetFragment = annot.getTargetFragment();
+			FragmentTypes fragmentType = annot.getFragmentType();
+			if (targetFragment != null) {
+				annotNode.setProperty("targetFragment", targetFragment);
+				annotNode.setProperty("fragmentType", fragmentType.name());
+			}
 
-            /*
-             * The name of the creator of this annotation.
-             */
-            String creatorName = annot.getCreatorName();
-
-            /*
-             * The URI of the creator of this annotation.
-             */
-            String creatorUri = annot.getCreatorUri();
-            if (creatorUri != null) {
-                Node creator = getOrCreatePersonNode(creatorUri, creatorName);
-                getOrCreateRelation(creator, RelationTypes.CREATED, annotNode);
-            }
+			/*
+			 * The name of the creator of this annotation.
+			 */
+			String creatorName = annot.getCreatorName();
 
-            /*
-             * The creation date of this annotation.
-             */
-            String created = annot.getCreated();
-            if (created != null) {
-                annotNode.setProperty("created", created);
-            }
-
-            tx.success();
-        } finally {
-            tx.finish();
-        }
+			/*
+			 * The URI of the creator of this annotation.
+			 */
+			String creatorUri = annot.getCreatorUri();
+			if (creatorUri != null) {
+				Node creator = getOrCreatePersonNode(creatorUri, creatorName);
+				getOrCreateRelation(creator, RelationTypes.CREATED, annotNode);
+			}
 
-        // re-read and return annotation
-        Annotation storedAnnot = createAnnotation(annotNode);
-        return storedAnnot;
-    }
+			/*
+			 * The creation date of this annotation.
+			 */
+			String created = annot.getCreated();
+			if (created != null) {
+				annotNode.setProperty("created", created);
+			}
 
-    /**
-     * Deletes the annotation with the id.
-     * 
-     * @param id
-     */
-    public void deleteById(String id) {
-        // TODO Auto-generated method stub
+			tx.success();
+		} finally {
+			tx.finish();
+		}
+
+		// re-read and return annotation
+		Annotation storedAnnot = createAnnotation(annotNode);
+		return storedAnnot;
+	}
 
-    }
+	/**
+	 * Deletes the annotation with the given id.
+	 * 
+	 * @param id
+	 */
+	public void deleteById(String id) {
+		Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
+		if (annotNode != null) {
+			// delete related objects
+			Transaction tx = graphDb.beginTx();
+			try {
+				for (Relationship rel : annotNode.getRelationships()) {
+					// delete relation and the related node if it has no other relations
+					Node other = rel.getOtherNode(annotNode);					
+					rel.delete();
+					if (! other.hasRelationship()) {
+						deleteNode(other);
+					}
+				}
+				if (! annotNode.hasRelationship()) {
+					deleteNode(annotNode);
+				} else {
+					logger.error("deleteById: unable to delete: Node still has relations.");
+				}
+				tx.success();
+			} finally {
+				tx.finish();
+			}
+		}
+
+	}
 
-    /**
-     * Returns all annotations with the given uri and/or user.
-     * 
-     * @param uri
-     * @param userUri
-     * @param limit
-     * @param offset
-     * @return
-     */
-    public List<Annotation> searchByUriUser(String targetUri, String userUri, String limit, String offset) {
-        List<Annotation> annotations = new ArrayList<Annotation>();
-        if (targetUri != null) {
-            Index<Node> targetIdx = graphDb.index().forNodes("targets");
-            // there should be only one
-            Node target = targetIdx.get("uri", targetUri).getSingle();
-            if (target != null) {
-                Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
-                for (Relationship relation : relations) {
-                    Node ann = relation.getStartNode();
-                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
-                        Annotation annot = createAnnotation(ann);
-                        annotations.add(annot);
-                    } else {
-                        logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
-                    }
-                }
-            }
-        }
-        if (userUri != null) {
-            Index<Node> persIdx = graphDb.index().forNodes("persons");
-            // there should be only one
-            Node person = persIdx.get("uri", userUri).getSingle();
-            if (person != null) {
-                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
-                for (Relationship relation : relations) {
-                    Node ann = relation.getEndNode();
-                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
-                        Annotation annot = createAnnotation(ann);
-                        annotations.add(annot);
-                    } else {
-                        logger.error("CREATED relation does not end with ANNOTATION: " + ann);
-                    }
-                }
-            }
-        }
-        return annotations;
-    }
-
-    protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
-        if (start.hasRelationship()) {
-            // there are relations
-            Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
-            for (Relationship rel : rels) {
-                if (rel.getEndNode().equals(end)) {
-                    // relation exists
-                    return rel;
-                }
-            }
-        }
-        // create new one
-        Relationship rel;
-        Transaction tx = graphDb.beginTx();
-        try {
-            rel = start.createRelationshipTo(end, type);
-            tx.success();
-        } finally {
-            tx.finish();
-        }
-        return rel;
-    }
+	/**
+	 * Returns all annotations with the given uri and/or user.
+	 * 
+	 * @param uri
+	 * @param userUri
+	 * @param limit
+	 * @param offset
+	 * @return
+	 */
+	public List<Annotation> searchByUriUser(String targetUri, String userUri,
+			String limit, String offset) {
+		List<Annotation> annotations = new ArrayList<Annotation>();
+		if (targetUri != null) {
+			// there should be only one
+			Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle();
+			if (target != null) {
+				Iterable<Relationship> relations = target
+						.getRelationships(RelationTypes.ANNOTATES);
+				for (Relationship relation : relations) {
+					Node ann = relation.getStartNode();
+					if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
+						Annotation annot = createAnnotation(ann);
+						annotations.add(annot);
+					} else {
+						logger.error("ANNOTATES relation does not start with ANNOTATION: "
+								+ ann);
+					}
+				}
+			}
+		}
+		if (userUri != null) {
+			// there should be only one
+			Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
+			if (person != null) {
+				Iterable<Relationship> relations = person
+						.getRelationships(RelationTypes.CREATED);
+				for (Relationship relation : relations) {
+					Node ann = relation.getEndNode();
+					if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
+						Annotation annot = createAnnotation(ann);
+						annotations.add(annot);
+					} else {
+						logger.error("CREATED relation does not end with ANNOTATION: "
+								+ ann);
+					}
+				}
+			}
+		}
+		return annotations;
+	}
 
-    protected Node getOrCreateAnnotationNode(String id) {
-        Index<Node> idx = graphDb.index().forNodes("annotations");
-        IndexHits<Node> annotations = idx.get("id", id);
-        Node annotation = annotations.getSingle();
-        if (annotation == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
-                annotation = graphDb.createNode();
-                annotation.setProperty("TYPE", "ANNOTATION");
-                annotation.setProperty("id", id);
-                idx.add(annotation, "id", id);
-                tx.success();
-            } finally {
-                tx.finish();
-            }
-        }
-        return annotation;
-    }
+	protected Relationship getOrCreateRelation(Node start,
+			RelationshipType type, Node end) {
+		if (start.hasRelationship()) {
+			// there are relations
+			Iterable<Relationship> rels = start.getRelationships(type,
+					Direction.OUTGOING);
+			for (Relationship rel : rels) {
+				if (rel.getEndNode().equals(end)) {
+					// relation exists
+					return rel;
+				}
+			}
+		}
+		// create new one
+		Relationship rel;
+		Transaction tx = graphDb.beginTx();
+		try {
+			rel = start.createRelationshipTo(end, type);
+			tx.success();
+		} finally {
+			tx.finish();
+		}
+		return rel;
+	}
+
+	protected Node getOrCreateAnnotationNode(String id) {
+		Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
+		IndexHits<Node> annotations = idx.get("id", id);
+		Node annotation = annotations.getSingle();
+		if (annotation == null) {
+			// does not exist yet
+			Transaction tx = graphDb.beginTx();
+			try {
+				annotation = graphDb.createNode();
+				annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name());
+				annotation.setProperty("id", id);
+				idx.add(annotation, "id", id);
+				tx.success();
+			} finally {
+				tx.finish();
+			}
+		}
+		return annotation;
+	}
 
-    protected Node getOrCreateTargetNode(String uri) {
-        Index<Node> idx = graphDb.index().forNodes("targets");
-        IndexHits<Node> targets = idx.get("uri", uri);
-        Node target = targets.getSingle();
-        if (target == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
-                target = graphDb.createNode();
-                target.setProperty("TYPE", "TARGET");
-                target.setProperty("uri", uri);
-                idx.add(target, "uri", uri);
-                tx.success();
-            } finally {
-                tx.finish();
-            }
-        }
-        return target;
-    }
+	protected Node getOrCreateTargetNode(String uri) {
+		Index<Node> idx = getNodeIndex(NodeTypes.TARGET);
+		IndexHits<Node> targets = idx.get("uri", uri);
+		Node target = targets.getSingle();
+		if (target == null) {
+			// does not exist yet
+			Transaction tx = graphDb.beginTx();
+			try {
+				target = graphDb.createNode();
+				target.setProperty("TYPE", NodeTypes.TARGET.name());
+				target.setProperty("uri", uri);
+				idx.add(target, "uri", uri);
+				tx.success();
+			} finally {
+				tx.finish();
+			}
+		}
+		return target;
+	}
 
-    protected Node getOrCreatePersonNode(String uri, String name) {
-        Index<Node> idx = graphDb.index().forNodes("persons");
-        // Person is identified by URI
-        IndexHits<Node> persons = idx.get("uri", uri);
-        Node person = persons.getSingle();
-        if (person == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
-                person = graphDb.createNode();
-                person.setProperty("TYPE", "PERSON");
-                person.setProperty("uri", uri);
-                idx.add(person, "uri", uri);
-                if (name != null) {
-                    person.setProperty("name", name);
-                }
-                tx.success();
-            } finally {
-                tx.finish();
-            }
-        }
-        return person;
-    }
+	protected Node getOrCreatePersonNode(String uri, String name) {
+		// Person is identified by URI
+		Index<Node> idx = getNodeIndex(NodeTypes.PERSON);
+		IndexHits<Node> persons = idx.get("uri", uri);
+		Node person = persons.getSingle();
+		if (person == null) {
+			// does not exist yet
+			Transaction tx = graphDb.beginTx();
+			try {
+				person = graphDb.createNode();
+				person.setProperty("TYPE", NodeTypes.PERSON.name());
+				person.setProperty("uri", uri);
+				idx.add(person, "uri", uri);
+				if (name != null) {
+					person.setProperty("name", name);
+				}
+				tx.success();
+			} finally {
+				tx.finish();
+			}
+		}
+		return person;
+	}
 
-    /**
-     * Erzeuge eine urn aus der aktuellen Zeit in millis
-     * 
-     * @return
-     */
-    private String createRessourceURI(String prefix) {
+	/**
+	 * Unindexes and deletes given Node if it has no relations.
+	 * @param node
+	 */
+	protected void deleteNode(Node node) {
+		Transaction tx = graphDb.beginTx();
+		try {
+			if (node.hasRelationship()) {
+				logger.error("deleteNode: unable to delete: Node still has relations.");
+			} else {
+				String ts = (String) node.getProperty("TYPE", null);
+				try {
+					NodeTypes type = NodeTypes.valueOf(ts);
+					getNodeIndex(type).remove(node);
+				} catch (Exception e) {
+					logger.error("deleteNode: unable to get TYPE of node: "+node);
+				}
+				node.delete();
+			}
+			tx.success();
+		} finally {
+			tx.finish();
+		}
+	}
+	
+	/**
+	 * Erzeuge eine urn aus der aktuellen Zeit in millis
+	 * 
+	 * @return
+	 */
+	private String createRessourceURI(String prefix) {
 
-        Calendar cal = Calendar.getInstance();
+		Calendar cal = Calendar.getInstance();
 
-        long time = cal.getTimeInMillis();
+		long time = cal.getTimeInMillis();
 
-        return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
+		return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
 
-    }
+	}
 
 }
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Tue Jul 03 21:23:17 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Wed Jul 11 17:08:40 2012 +0200
@@ -54,6 +54,9 @@
     private AnnotationStore store;
     public static final String ANNSTORE_KEY = "annotationmanager.store";
 
+    private String ldapServerUrl;
+    public static final String LDAP_SERVER_KEY = "annotationmanager.ldapserver.url";
+
     /**
      * constructor
      * 
@@ -80,6 +83,7 @@
                      * read serverconfig options
                      */
                     graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath);
+                    ldapServerUrl =  serverConfig.getProperty(LDAP_SERVER_KEY, null);
                 } catch (IOException e) {
                     logger.warn("Error loading server config: ", e);
                 }
@@ -181,13 +185,14 @@
     public String getFullNameFromLdap(String creator) {
         String retString = creator; // falls nichts gefunden wird einfach den
                                     // creator zurueckgeben
+        if (ldapServerUrl == null) {
+        	return retString;
+        }
         Hashtable<String, String> env = new Hashtable<String, String>();
         String sp = "com.sun.jndi.ldap.LdapCtxFactory";
         env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
 
-        // TODO: should go into config file
-        String ldapUrl = "ldap://ldap.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; 
-        env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
+        env.put(javax.naming.Context.PROVIDER_URL, ldapServerUrl);
 
         DirContext dctx;
         try {