neo4J rating
This commit is contained in:
parent
fc31eb97d4
commit
538b17474a
@ -13,23 +13,12 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.camillepradel.movierecommender.model.Genre;
|
||||
import com.camillepradel.movierecommender.model.Movie;
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import java.util.ArrayList;
|
||||
import org.bson.Document;
|
||||
import java.net.ConnectException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.neo4j.driver.v1.Record;
|
||||
import org.neo4j.driver.v1.Session;
|
||||
import org.neo4j.driver.v1.StatementResult;
|
||||
import com.camillepradel.movierecommender.model.Rating;
|
||||
|
||||
@Controller
|
||||
@ -40,7 +29,6 @@ public class MainController {
|
||||
public ModelAndView showMessage(
|
||||
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
|
||||
System.out.println("in controller");
|
||||
|
||||
ModelAndView mv = new ModelAndView("helloworld");
|
||||
mv.addObject("message", message);
|
||||
mv.addObject("name", name);
|
||||
@ -51,49 +39,18 @@ public class MainController {
|
||||
public ModelAndView showMovies(
|
||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||
|
||||
//List<Movie> movies = getMoviesNeo4JByUser(userId);
|
||||
List<Movie> movies = getMoviesMongoDBByUser(userId);
|
||||
List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
||||
//List<Movie> movies = getMoviesMongoDBByUser(userId);
|
||||
|
||||
ModelAndView mv = new ModelAndView("movies");
|
||||
mv.addObject("userId", userId);
|
||||
mv.addObject("movies", movies);
|
||||
return mv;
|
||||
}
|
||||
public List<Movie>getMoviesNeo4JByUser (Integer userId){
|
||||
List<Movie> movies = new LinkedList<Movie>();
|
||||
String oldMovie=null;
|
||||
int id=0;
|
||||
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >=0 ){
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (me:User { id: "+userId+" })-[:RATED]->(movie:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN movie.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
else{
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (m:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN m.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally{
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for(String g : listGenre){
|
||||
if(g != null)
|
||||
listTypeGenre.add(new Genre(id, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
|
||||
}
|
||||
movies.add(new Movie(id, record.get("movies").asString(), (listTypeGenre.isEmpty())?null:listTypeGenre ) );
|
||||
id+=1;
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Movie>getMoviesMongoDBByUser(Integer user_id){
|
||||
@ -162,9 +119,10 @@ public class MainController {
|
||||
|
||||
// TODO: write query to retrieve all ratings from the specified user
|
||||
List<Rating> ratings = new LinkedList<Rating>();
|
||||
ratings.add(new Rating(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})), userId, 3));
|
||||
ratings.add(new Rating(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})), userId, 4));
|
||||
//ratings.add(new Rating(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})), userId, 3));
|
||||
//ratings.add(new Rating(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})), userId, 4));
|
||||
|
||||
ratings=Neo4JController.GetMoviesRatingNeo4JByUser(userId);
|
||||
ModelAndView mv = new ModelAndView("movieratings");
|
||||
mv.addObject("userId", userId);
|
||||
mv.addObject("allMovies", allMovies);
|
||||
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.camillepradel.movierecommender.controller;
|
||||
|
||||
import com.camillepradel.movierecommender.model.Genre;
|
||||
import com.camillepradel.movierecommender.model.Movie;
|
||||
import com.camillepradel.movierecommender.model.Rating;
|
||||
import java.net.ConnectException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.neo4j.driver.v1.Record;
|
||||
import org.neo4j.driver.v1.StatementResult;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author renando
|
||||
*/
|
||||
public class Neo4JController {
|
||||
|
||||
public static List<Movie>getMoviesNeo4JByUser (Integer userId){
|
||||
List<Movie> movies = new LinkedList<Movie>();
|
||||
int id=0;
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >=0 ){
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (me:User { id: "+userId+" })-[:RATED]->(movie:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN movie.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
else{
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (m:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN m.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally{
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for(String g : listGenre){
|
||||
if(g != null)
|
||||
listTypeGenre.add(new Genre(id, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
|
||||
}
|
||||
movies.add(new Movie(id, record.get("movies").asString(), (listTypeGenre.isEmpty())?null:listTypeGenre ) );
|
||||
id+=1;
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
|
||||
public static List<Rating>GetMoviesRatingNeo4JByUser(Integer userId){
|
||||
List<Rating> moviesRating = new LinkedList<Rating>();
|
||||
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >=0 ){
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (u:User{id:"+userId+"})-[r:RATED]->(m:Movie)-[:CATEGORIZED_AS]->(g:Genre) RETURN m.id AS id, m.title AS title, collect(g.name) AS genre, r.note AS note ORDER BY id" );
|
||||
}
|
||||
else{
|
||||
return null;
|
||||
}
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally{
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
Movie movie = null;
|
||||
Rating rate = null;
|
||||
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
System.out.println(record.get("p").toString());
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for(String g : listGenre){
|
||||
if(g != null)
|
||||
listTypeGenre.add(new Genre(0, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
|
||||
}
|
||||
movie = new Movie(record.get("id").asInt(), record.get("title").asString(),listTypeGenre );
|
||||
rate = new Rating(movie, userId, record.get("note").asInt());
|
||||
moviesRating.add(rate);
|
||||
}
|
||||
return moviesRating;
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user