package com.camillepradel.movierecommender.controller;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.camillepradel.movierecommender.model.Genre;
import com.camillepradel.movierecommender.model.Movie;

@Controller
public class MainController {
	String message = "Welcome to Spring MVC!";
 
	@RequestMapping("/hello")
	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);
		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 = 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");
		mv.addObject("userId", userId);
		mv.addObject("movies", movies);
		return mv;
	}
}

@@ -464,16 +464,230 @@
 l.Movie;
+%0Aimport java.net.ConnectException;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0Aimport org.neo4j.driver.v1.Record;%0Aimport org.neo4j.driver.v1.Session;%0Aimport org.neo4j.driver.v1.StatementResult;
 %0A%0A@Contr
@@ -1016,43 +1016,960 @@
 %22);%0A
-%09%09mv.addObject(%22message%22, message);
+%0A                Session session = null;%0A                try %7B%0A                session =Neo4jConnector.getInstance().getConnection();%0A                %0A                %7D catch (ConnectException ex) %7B%0A                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);%0A                %7D%0A                StatementResult result = session.run( %22MATCH (u:User) RETURN COUNT( DISTINCT u.occupation) AS CPT%22 );%0A                while ( result.hasNext() )%0A                %7B%0A                Record record = result.next();%0A                System.out.println( record.toString() );%0A                 mv.addObject(%22message%22, record.get( %22CPT%22 ));%0A                %7D%0A                %0A                try %7B%0A                Neo4jConnector.getInstance().close();%0A            %7D catch (ConnectException ex) %7B%0A                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);%0A            %7D%0A                %0A               
 %0A%09%09m
@@ -2003,24 +2003,32 @@
 %09%09return mv;
+%0A%0A      
 %0A%09%7D%0A%0A%09@Reque
@@ -2993,16 +2993,16 @@
 ovies);%0A
-
 %09%09return
@@ -3006,13 +3006,47 @@
 urn mv;%0A
+                %0A                %0A
 %09%7D%0A%7D%0A

test neo4js