Skip to content

Commit

Permalink
getRequests: implemented bacon path and number
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyZ1435 committed Oct 27, 2022
1 parent a021086 commit 7fd324e
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 43 deletions.
119 changes: 86 additions & 33 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -26,90 +24,145 @@ 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()){
System.out.println("Actor not found: " + actorId);
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()){
System.out.println("Movie not found: " + 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()){
System.out.println("No actor with this ID");
return "404";
}
List<Record> 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<record.get(0).size(); i++){
if(record.get(0).get(i).asNode().hasLabel("actor")){
baconNumber++;
}
}
response.put("baconPath", baconNumber);
return response.toString();
}

public String computeBaconPath(String actorId) throws JSONException {
JSONObject response = new JSONObject();
String query;
JSONArray path = new JSONArray();

if(actorId.equals(kevinBid)){
path.put(kevinBid);
response.put("baconPath", kevinBid);
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);
System.out.println(result);
if(!result.hasNext()){
System.out.println("No path");
return "404";
}
Record record = result.next();
for(int i=0; i<record.get(0).size(); i++){
path.put(record.get(0).get(i).get("id").asString());
}
response.put("baconPath", path);
return response.toString();
}
}
86 changes: 76 additions & 10 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void handle(HttpExchange exchange) throws IOException {
case "getActor":
this.getActor(exchange);
break;
case "computeBaconPath":
this.computeBaconPath(exchange);
break;
case "computeBaconNumber":
this.computeBaconNumber(exchange);
break;
default:
break;
}
Expand Down Expand Up @@ -80,16 +86,13 @@ public void addActor(HttpExchange r) throws IOException, JSONException {
}

try {
if(this.dao.addActor(name, actorId) == false){
r.sendResponseHeaders(400, -1);
return;
}
r.sendResponseHeaders(this.dao.addActor(name, actorId), -1);
return;
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
r.sendResponseHeaders(200, -1);
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
Expand All @@ -112,16 +115,13 @@ public void addMovie(HttpExchange r) throws IOException, JSONException {
}

try {
if(this.dao.addMovie(name, movieId) == false){
r.sendResponseHeaders(400, -1);
return;
}
r.sendResponseHeaders(this.dao.addMovie(name, movieId), -1);
return;
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
r.sendResponseHeaders(200, -1);
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
Expand Down Expand Up @@ -190,4 +190,70 @@ public void getActor(HttpExchange r) throws IOException, JSONException {
r.sendResponseHeaders(500, -1);
}
}

public void computeBaconNumber(HttpExchange r) throws IOException, JSONException{
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String actorId;

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
String response = this.dao.computeBaconNumber(actorId);
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 computeBaconPath(HttpExchange r) throws IOException, JSONException{
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String actorId;

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
String response = this.dao.computeBaconPath(actorId);
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);
}
}
}

0 comments on commit 7fd324e

Please sign in to comment.