neo4j
This commit is contained in:
		@@ -20,64 +20,67 @@ import org.neo4j.driver.v1.StatementResult;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@Controller
 | 
					@Controller
 | 
				
			||||||
public class MainController {
 | 
					public class MainController {
 | 
				
			||||||
	String message = "Welcome to Spring MVC!";
 | 
						
 | 
				
			||||||
 
 | 
					 | 
				
			||||||
	@RequestMapping("/hello")
 | 
						@RequestMapping("/hello")
 | 
				
			||||||
	public ModelAndView showMessage(
 | 
						public ModelAndView showMessage(
 | 
				
			||||||
			@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
 | 
								@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
 | 
				
			||||||
		System.out.println("in controller");
 | 
							System.out.println("in controller");
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
		ModelAndView mv = new ModelAndView("helloworld");
 | 
							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);
 | 
							mv.addObject("name", name);
 | 
				
			||||||
		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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
	@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 = 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})));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ModelAndView mv = new ModelAndView("movies");
 | 
							ModelAndView mv = new ModelAndView("movies");
 | 
				
			||||||
		mv.addObject("userId", userId);
 | 
							mv.addObject("userId", userId);
 | 
				
			||||||
		mv.addObject("movies", movies);
 | 
							mv.addObject("movies", movies);
 | 
				
			||||||
		return mv;
 | 
							return mv;
 | 
				
			||||||
                
 | 
					 | 
				
			||||||
                
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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