Merge commit '60e970e'

This commit is contained in:
Quentin Rouland 2016-11-30 12:27:56 +01:00
commit 8524e3b485
2 changed files with 52 additions and 27 deletions

View File

@ -22,6 +22,13 @@ 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;
@Controller
public class MainController {
@ -37,15 +44,12 @@ public class MainController {
mv.addObject("name", name);
return mv;
}
@RequestMapping("/movies")
public ModelAndView showMovies(
@RequestParam(value = "user_id", required = false) Integer userId) {
System.out.println("show Movies of user " + userId);
// TODO: write query to retrieve all movies from DB or all movies rated by user with id userId,
// depending on whether or not a value was given for userId
//List<Movie> movies = getMoviesNeo4JByUser(userId);
List<Movie> movies = getMoviesMongoDBByUser(userId);
ModelAndView mv = new ModelAndView("movies");
@ -53,6 +57,42 @@ public class MainController {
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){
ArrayList<Movie> listMovies= new ArrayList<Movie>();

View File

@ -28,35 +28,20 @@ public class Neo4jConnector {
if (INSTANCE == null)
{
INSTANCE = new Neo4jConnector();
driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "root1234" ) );
session = driver.session();
}
return INSTANCE;
}
public Session getConnection() throws ConnectException {
if (session == null || driver == null){
Neo4jConnector.getInstance();
}
this.driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "root1234" ) );
this.session = driver.session();
return this.session;
}
public boolean close() throws ConnectException {
boolean etat = false;
if (session != null) {
try {
session.close();
driver.close();
etat = true;
} catch (Neo4jException e) {
etat = false;
}
} else {
throw new ConnectException();
}
return etat;
public void close() {
this.session.close();
this.driver.close();
}
}