Permalink
Browse files

added implementation for delete

  • Loading branch information...
1 parent e0ca63e commit 00fdb468497e35f74c0b4bcda241ad3265023608 xamry committed Jan 14, 2013
Showing with 47 additions and 2 deletions.
  1. +47 −2 kundera-neo4j/src/main/java/com/impetus/client/neo4j/Neo4JClient.java
@@ -20,6 +20,7 @@
import java.util.Map;
import javax.persistence.Column;
+import javax.persistence.PersistenceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -74,8 +75,13 @@
@Override
public void populateClientProperties(Client client, Map<String, Object> properties)
{
+ //All client properties currently are those that are specified in neo4j.properties.
+ //No custom property currently defined by Kundera, leaving empty
}
+ /**
+ * Finds an entity from graph database
+ */
@Override
public Object find(Class entityClass, Object key)
{
@@ -143,14 +149,53 @@ public void close()
{
}
+ /**
+ * Deletes an entity from database
+ */
@Override
- public void delete(Object entity, Object pKey)
+ public void delete(Object entity, Object key)
{
+ GraphDatabaseService graphDb = factory.getConnection();
+
+ Transaction tx = graphDb.beginTx();
+
+ try
+ {
+ //Find Node for this particular entity
+ EntityMetadata m = KunderaMetadataManager.getEntityMetadata(entity.getClass());
+ Node node = searchNode(key, m, graphDb);
+ if(node == null)
+ {
+ log.error("Entity to be deleted doesn't exist in graph. Doing nothing");
+ return;
+ }
+
+ //Remove this particular node
+ node.delete();
+
+ //Remove all relationship edges attached to this node (otherwise an exception is thrown)
+ for(Relationship relationship : node.getRelationships())
+ {
+ relationship.delete();
+ }
+
+ tx.success();
+ }
+ catch(Exception e)
+ {
+ log.error("Error while removing entity. Details:" + e.getMessage());
+ }
+ finally
+ {
+ tx.finish();
+ }
+
}
@Override
public void persistJoinTable(JoinTableData joinTableData)
{
+ throw new PersistenceException("Operation not supported for Neo4J");
}
@Override
@@ -215,6 +260,7 @@ protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id
Object targetNodeKey = PropertyAccessorHelper.getId(rh.getRelationValue(), targetNodeMetadata);
Node targetNode = searchNode(targetNodeKey, targetNodeMetadata, graphDb);
+
if(targetNode != null)
{
//Join this node (source node) to target node via relationship
@@ -256,7 +302,6 @@ protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id
}
catch (Exception e)
{
- e.printStackTrace();
log.error("Error while persisting entity " + entity + ". Details:" + e.getMessage());
}
finally

0 comments on commit 00fdb46

Please sign in to comment.