diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0ad3921..0c242c4 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -103,7 +103,7 @@ public String getActor(String actorId) throws JSONException { response.put("actorId", actorId); 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 = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId"; query = String.format(query, actorId); result = this.session.run(query); resultValues = result.list(); @@ -112,4 +112,61 @@ public String getActor(String actorId) throws JSONException { response.put("movies", movies); return response.toString(); } + public String getMovie(String movieId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.name"; + query = String.format(query, movieId); + + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No movie with this ID"); + return "404"; + } + List resultValues = result.list(); + response.put("movieId", movieId); + response.put("name", resultValues.get(0).get("m.name").asString()); + + query = "MATCH (a:actor)-[r:ACTED_IN]->(m:movie { movieId: \"%s\"}) RETURN a.actorId"; + query = String.format(query, movieId); + result = this.session.run(query); + resultValues = result.list(); + JSONArray actors = new JSONArray(); + resultValues.forEach((record)->{actors.put(record.get("a.actorId").asString());}); + response.put("actors", actors); + return response.toString(); + } + public String hasRelationship(String actorId, String movieId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + + query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.name"; + query = String.format(query, movieId); + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No movie with this ID"); + return "404"; + } + + query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.name"; + query = String.format(query, actorId); + result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No actor with this ID"); + return "404"; + } + + response.put("movieId", movieId); + response.put("actorId", actorId); + + query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie { movieId: \"%s\"}) RETURN r"; + query = String.format(query, actorId,movieId); + result = this.session.run(query); + if(!result.hasNext()){ + response.put("hasRelationship", false); + }else{ + response.put("hasRelationship", true); + } + return response.toString(); + } } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index 60bfb05..8eba8c2 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -36,6 +36,12 @@ public void handle(HttpExchange exchange) throws IOException { case "getActor": this.getActor(exchange); break; + case "getMovie": + this.getMovie(exchange); + break; + case "hasRelationship": + this.hasRelationship(exchange); + break; default: break; } @@ -158,7 +164,7 @@ public void addRelationship(HttpExchange r) throws IOException, JSONException { } } - public void getActor(HttpExchange r) throws IOException, JSONException { + public void getActor(HttpExchange r) throws IOException { String body = Utils.convert(r.getRequestBody()); try { JSONObject deserialized = new JSONObject(body); @@ -190,4 +196,69 @@ public void getActor(HttpExchange r) throws IOException, JSONException { r.sendResponseHeaders(500, -1); } } + public void getMovie(HttpExchange r) throws IOException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + String movieId; + + if (deserialized.has("movieId")) { + movieId = deserialized.getString("movieId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + String response = this.dao.getMovie(movieId); + try { + if(response.equals("404")){ + r.sendResponseHeaders(404, -1); + return; + } + } catch (Exception e) { + r.sendResponseHeaders(500, -1); + e.printStackTrace(); + return; + } + r.sendResponseHeaders(200, response.length()); + OutputStream os = r.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } + public void hasRelationship(HttpExchange r) throws IOException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + String movieId, actorId; + + if (deserialized.has("actorId") && deserialized.has("movieId")) { + movieId = deserialized.getString("movieId"); + actorId = deserialized.getString("actorId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + String response = this.dao.hasRelationship(actorId, movieId); + try { + if(response.equals("404")){ + r.sendResponseHeaders(404, -1); + return; + } + } catch (Exception e) { + r.sendResponseHeaders(500, -1); + e.printStackTrace(); + return; + } + r.sendResponseHeaders(200, response.length()); + OutputStream os = r.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } } \ No newline at end of file