diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0ad3921..50b7078 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -3,7 +3,7 @@ import org.json.*; import org.neo4j.driver.*; import org.neo4j.driver.Record; - +import org.neo4j.driver.types.Path; import java.util.List; @@ -15,9 +15,7 @@ public class Neo4jDAO { private final Session session; private final Driver driver; - private final String uriDb = "bolt://localhost:7687"; - private final String username = "neo4j"; - private final String password = "123456"; + private final String kevinBid = "nm0000102"; @Inject public Neo4jDAO(Driver driver) { @@ -26,39 +24,39 @@ public Neo4jDAO(Driver driver) { } - public boolean addActor(String name, String actorId) { + public int addActor(String Name, String id) { String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId"; - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; + query = String.format(query, id); Result result = this.session.run(query); if(result.hasNext()){ - System.out.println("Actor Found: " + actorId); - return false; + System.out.println("Actor Found: " + id); + return 400; } - query = "CREATE (a:actor {name: \"%s\", actorId: \"%s\"})"; - query = String.format(query, name, actorId); + query = "CREATE (a:actor {Name: \"%s\", id: \"%s\"})"; + query = String.format(query, Name, id); this.session.run(query); - return true; + return 200; } - public boolean addMovie(String name, String movieId) { + public int addMovie(String Name, String id) { String query; - query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; - query = String.format(query, movieId); + query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id"; + query = String.format(query, id); Result result = this.session.run(query); if(result.hasNext()){ - System.out.println("Movie Found: " + movieId); - return false; + System.out.println("Movie Found: " + id); + return 400; } - query = "CREATE (m:movie {name: \"%s\", movieId: \"%s\"})"; - query = String.format(query, name, movieId); + query = "CREATE (m:movie {Name: \"%s\", id: \"%s\"})"; + query = String.format(query, Name, id); this.session.run(query); - return true; + return 200; } public int addRelationship(String actorId, String movieId) { String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId"; + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; query = String.format(query, actorId); Result result = this.session.run(query); if(!result.hasNext()){ @@ -66,7 +64,7 @@ public int addRelationship(String actorId, String movieId) { return 404; } - query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; + query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id"; query = String.format(query, movieId); result = this.session.run(query); if(!result.hasNext()){ @@ -74,25 +72,25 @@ public int addRelationship(String actorId, String movieId) { return 404; } - query = "MATCH (a:actor {actorId: \"%s\"})-[:ACTED_IN]->(m:movie {movieId: \"%s\"}) RETURN a.actorId"; + query = "MATCH (a:actor {id: \"%s\"})-[:ACTED_IN]->(m:movie {id: \"%s\"}) RETURN a.id"; query = String.format(query, actorId, movieId); result = this.session.run(query); if(result.hasNext()){ - System.out.println("Relationship Found: " + movieId); + System.out.println("Relationship Found: " + actorId +"->"+ movieId); return 400; } - query = "MATCH (a:actor {actorId: \"%s\"}), (m:movie {movieId: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)"; + query = "MATCH (a:actor {id: \"%s\"}), (m:movie {id: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)"; query = String.format(query, actorId, movieId); this.session.run(query); return 200; } - public String getActor(String actorId) throws JSONException { + public String getActor(String id) throws JSONException { JSONObject response = new JSONObject(); String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.name"; - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; + query = String.format(query, id); Result result = this.session.run(query); if(!result.hasNext()){ @@ -100,16 +98,71 @@ public String getActor(String actorId) throws JSONException { return "404"; } List resultValues = result.list(); - response.put("actorId", actorId); - response.put("name", resultValues.get(0).get("a.name").asString()); + response.put("id", id); + response.put("Name", resultValues.get(0).get("a.Name").asString()); - query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId";//maybe change Movie to lowercase - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.id";//maybe change Movie to lowercase + query = String.format(query, id); result = this.session.run(query); resultValues = result.list(); JSONArray movies = new JSONArray(); - resultValues.forEach((record)->{movies.put(record.get("m.movieId").asString());}); + resultValues.forEach((record)->{movies.put(record.get("m.id").asString());}); + //movies.put(record.get("m.id").asString()); response.put("movies", movies); return response.toString(); } + + public String computeBaconNumber(String actorId) throws JSONException{ + JSONObject response = new JSONObject(); + String query; + int baconNumber = 0; + + if(actorId.equals(kevinBid)){ + response.put("baconNumber", 0); + return response.toString(); + } + query = "MATCH (a:actor {id: \"%s\"}), (KevinB:actor {id: \"%s\"} ), p = shortestPath((a)-[:ACTED_IN*]-(KevinB)) RETURN nodes(p)"; + query = String.format(query, actorId, kevinBid); + + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No path"); + return "404"; + } + Record record = result.next(); + for(int i=0; i