neo4j
This commit is contained in:
parent
a0978cc0f1
commit
60e970e386
@ -20,7 +20,6 @@ import org.neo4j.driver.v1.StatementResult;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
String message = "Welcome to Spring MVC!";
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public ModelAndView showMessage(
|
||||
@ -28,56 +27,60 @@ public class MainController {
|
||||
System.out.println("in controller");
|
||||
|
||||
ModelAndView mv = new ModelAndView("helloworld");
|
||||
|
||||
Session session = null;
|
||||
try {
|
||||
session =Neo4jConnector.getInstance().getConnection();
|
||||
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
StatementResult result = session.run( "MATCH (u:User) RETURN COUNT( DISTINCT u.occupation) AS CPT" );
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
System.out.println( record.toString() );
|
||||
mv.addObject("message", record.get( "CPT" ));
|
||||
}
|
||||
|
||||
try {
|
||||
Neo4jConnector.getInstance().close();
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
||||
|
||||
mv.addObject("name", name);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@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 = new LinkedList<Movie>();
|
||||
Genre genre0 = new Genre(0, "genre0");
|
||||
Genre genre1 = new Genre(1, "genre1");
|
||||
Genre genre2 = new Genre(2, "genre2");
|
||||
movies.add(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})));
|
||||
movies.add(new Movie(1, "Titre 1", Arrays.asList(new Genre[] {genre0, genre2})));
|
||||
movies.add(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})));
|
||||
movies.add(new Movie(3, "Titre 3", Arrays.asList(new Genre[] {genre0, genre1, genre2})));
|
||||
List<Movie> movies = getMoviesNeo4JByUser(userId);
|
||||
|
||||
ModelAndView mv = new ModelAndView("movies");
|
||||
mv.addObject("userId", userId);
|
||||
mv.addObject("movies", movies);
|
||||
return mv;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user