Skip to content

Instantly share code, notes, and snippets.

@shageman
Created April 19, 2011 18:00
Show Gist options
  • Save shageman/929048 to your computer and use it in GitHub Desktop.
Save shageman/929048 to your computer and use it in GitHub Desktop.
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.server.NeoServer;
import org.neo4j.server.database.Database;
@Path( "/clean" )
public class GraphCleaner extends ResourceBase {
public GraphCleaner( @Context Database database, @Context NeoServer server) {
super(database, server);
}
@DELETE
@Produces({"text/html", "application/json"})
@Path( "/all/{secret}" )
public Response deleteAllNodes(
@PathParam("secret") String secret) {
if(secret.equals("something_secret")) {
Transaction tx = database.graph.beginTx();
for(Node node : database.graph.getAllNodes()) {
try {
if(node.getId() != 0) {
deleteAllRelationships(node);
node.delete();
}
} catch(Exception e) {
tx.failure();
tx.finish();
return Response.serverError().build();
}
}
tx.success();
tx.finish();
return Response.ok().build();
} else {
return Response.serverError().build();
}
}
protected boolean deleteAllRelationships(Node node) {
Transaction tx = null;
try {
for(org.neo4j.graphdb.Relationship rel : node.getRelationships()) {
tx = database.graph.beginTx();
rel.delete();
tx.success();
tx.finish();
}
} catch(Exception e) {
if(tx != null) {
tx.failure();
tx.finish();
}
return false;
}
return true;
}
@DELETE
@Produces({"text/html", "application/json"})
@Path( "/relToRoot" )
public Response deleteRootRelations() {
Node root = database.graph.getReferenceNode();
if(deleteAllRelationships(root)) {
return Response.ok().build();
}
return Response.serverError().build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment