Merge commit '60e970e'
This commit is contained in:
commit
8524e3b485
@ -22,6 +22,13 @@ import com.mongodb.client.MongoCursor;
|
|||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import org.bson.Document;
|
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
|
@Controller
|
||||||
public class MainController {
|
public class MainController {
|
||||||
@ -37,15 +44,12 @@ public class MainController {
|
|||||||
mv.addObject("name", name);
|
mv.addObject("name", name);
|
||||||
return mv;
|
return mv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/movies")
|
@RequestMapping("/movies")
|
||||||
public ModelAndView showMovies(
|
public ModelAndView showMovies(
|
||||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||||
System.out.println("show Movies of user " + userId);
|
|
||||||
|
//List<Movie> movies = getMoviesNeo4JByUser(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 = getMoviesMongoDBByUser(userId);
|
List<Movie> movies = getMoviesMongoDBByUser(userId);
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("movies");
|
ModelAndView mv = new ModelAndView("movies");
|
||||||
@ -53,6 +57,42 @@ public class MainController {
|
|||||||
mv.addObject("movies", movies);
|
mv.addObject("movies", movies);
|
||||||
return mv;
|
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){
|
public List<Movie>getMoviesMongoDBByUser(Integer user_id){
|
||||||
ArrayList<Movie> listMovies= new ArrayList<Movie>();
|
ArrayList<Movie> listMovies= new ArrayList<Movie>();
|
||||||
|
@ -28,35 +28,20 @@ public class Neo4jConnector {
|
|||||||
if (INSTANCE == null)
|
if (INSTANCE == null)
|
||||||
{
|
{
|
||||||
INSTANCE = new Neo4jConnector();
|
INSTANCE = new Neo4jConnector();
|
||||||
driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "root1234" ) );
|
|
||||||
session = driver.session();
|
|
||||||
}
|
}
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session getConnection() throws ConnectException {
|
public Session getConnection() throws ConnectException {
|
||||||
if (session == null || driver == null){
|
this.driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "root1234" ) );
|
||||||
Neo4jConnector.getInstance();
|
this.session = driver.session();
|
||||||
}
|
|
||||||
return this.session;
|
return this.session;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean close() throws ConnectException {
|
public void close() {
|
||||||
boolean etat = false;
|
this.session.close();
|
||||||
if (session != null) {
|
this.driver.close();
|
||||||
try {
|
|
||||||
session.close();
|
|
||||||
driver.close();
|
|
||||||
etat = true;
|
|
||||||
} catch (Neo4jException e) {
|
|
||||||
etat = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new ConnectException();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return etat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user