Created
April 19, 2011 18:00
-
-
Save shageman/929048 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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