refactoring des Controlleurs db pour switch plus facilement entre mongodb et neo4j
This commit is contained in:
parent
cad44a4abb
commit
72b75d938c
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package com.camillepradel.movierecommender.controller;
|
||||||
|
|
||||||
|
import com.camillepradel.movierecommender.model.Movie;
|
||||||
|
import com.camillepradel.movierecommender.model.Rating;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author pomme
|
||||||
|
*/
|
||||||
|
public interface DBControllerInterface {
|
||||||
|
public abstract List<Movie>getMoviesByUser(Integer user_id);
|
||||||
|
|
||||||
|
public abstract List<Rating>getRatinByUser(Integer user_id);
|
||||||
|
|
||||||
|
public abstract void updateMovieRating(Rating rating);
|
||||||
|
|
||||||
|
public abstract List<Rating> ProcessRecommendationV1(Integer userId);
|
||||||
|
|
||||||
|
public abstract List<Rating> ProcessRecommendationV2(Integer userId);
|
||||||
|
|
||||||
|
public abstract List<Rating> ProcessRecommendationV3(Integer userId);
|
||||||
|
}
|
@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
import com.camillepradel.movierecommender.model.Genre;
|
|
||||||
import com.camillepradel.movierecommender.model.Movie;
|
import com.camillepradel.movierecommender.model.Movie;
|
||||||
import com.camillepradel.movierecommender.model.Rating;
|
import com.camillepradel.movierecommender.model.Rating;
|
||||||
|
|
||||||
@ -18,6 +17,10 @@ import com.camillepradel.movierecommender.model.Rating;
|
|||||||
public class MainController {
|
public class MainController {
|
||||||
String message = "Welcome to Spring MVC!";
|
String message = "Welcome to Spring MVC!";
|
||||||
|
|
||||||
|
//Commenter et Decommenter en fonction de BD que l'on veut utiliser
|
||||||
|
DBControllerInterface db = new MongoDBController();
|
||||||
|
//DBControllerInterface db = new Neo4jConnector();
|
||||||
|
|
||||||
@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) {
|
||||||
@ -33,8 +36,7 @@ public class MainController {
|
|||||||
public ModelAndView showMovies(
|
public ModelAndView showMovies(
|
||||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||||
|
|
||||||
List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
List<Movie> movies = db.getMoviesByUser(userId);
|
||||||
//List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
|
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("movies");
|
ModelAndView mv = new ModelAndView("movies");
|
||||||
mv.addObject("userId", userId);
|
mv.addObject("userId", userId);
|
||||||
@ -49,13 +51,11 @@ public class MainController {
|
|||||||
System.out.println("GET /movieratings for user " + userId);
|
System.out.println("GET /movieratings for user " + userId);
|
||||||
|
|
||||||
// write query to retrieve all movies from DB
|
// write query to retrieve all movies from DB
|
||||||
//List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
|
List<Movie> allMovies = db.getMoviesByUser(null);
|
||||||
List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
|
|
||||||
|
|
||||||
// write query to retrieve all ratings from the specified user
|
// write query to retrieve all ratings from the specified user
|
||||||
//List<Rating> ratings = MongoDBController.getRatingMongoDBByUser(userId);
|
List<Rating> ratings=db.getRatinByUser(userId);
|
||||||
|
|
||||||
List<Rating> ratings=Neo4JController.GetMoviesRatingNeo4JByUser(userId);
|
|
||||||
ModelAndView mv = new ModelAndView("movieratings");
|
ModelAndView mv = new ModelAndView("movieratings");
|
||||||
mv.addObject("userId", userId);
|
mv.addObject("userId", userId);
|
||||||
mv.addObject("allMovies", allMovies);
|
mv.addObject("allMovies", allMovies);
|
||||||
@ -69,7 +69,7 @@ public class MainController {
|
|||||||
System.out.println("POST /movieratings for user " + rating.getUserId()
|
System.out.println("POST /movieratings for user " + rating.getUserId()
|
||||||
+ ", movie " + rating.getMovie().getTitle()
|
+ ", movie " + rating.getMovie().getTitle()
|
||||||
+ ", score " + rating.getScore());
|
+ ", score " + rating.getScore());
|
||||||
Neo4JController.updateMovieRatingNeo4J(rating);
|
db.updateMovieRating(rating);
|
||||||
return "redirect:/movieratings?user_id=" + rating.getUserId();
|
return "redirect:/movieratings?user_id=" + rating.getUserId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,11 +82,10 @@ public class MainController {
|
|||||||
List<Rating> recommendations = new LinkedList<Rating>();
|
List<Rating> recommendations = new LinkedList<Rating>();
|
||||||
|
|
||||||
if (processingMode.equals(1)){
|
if (processingMode.equals(1)){
|
||||||
recommendations = Neo4JController.ProcessRecommendationV1(userId);
|
recommendations = db.ProcessRecommendationV1(userId);
|
||||||
}
|
}
|
||||||
else if (processingMode.equals(2)){
|
else if (processingMode.equals(2)){
|
||||||
recommendations = Neo4JController.ProcessRecommendationV2(userId);
|
recommendations = db.ProcessRecommendationV2(userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("recommendations");
|
ModelAndView mv = new ModelAndView("recommendations");
|
||||||
|
@ -26,8 +26,8 @@ import org.bson.Document;
|
|||||||
*
|
*
|
||||||
* @author sidya
|
* @author sidya
|
||||||
*/
|
*/
|
||||||
public class MongoDBController {
|
public class MongoDBController implements DBControllerInterface {
|
||||||
public static List<Movie>getMoviesMongoDBByUser(Integer user_id){
|
public List<Movie>getMoviesByUser(Integer user_id){
|
||||||
ArrayList<Movie> listMovies= new ArrayList<Movie>();
|
ArrayList<Movie> listMovies= new ArrayList<Movie>();
|
||||||
MongoCursor<Document> cursor;
|
MongoCursor<Document> cursor;
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ public class MongoDBController {
|
|||||||
return listMovies;
|
return listMovies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Rating>getRatingMongoDBByUser(Integer user_id){
|
public List<Rating>getRatinByUser(Integer user_id){
|
||||||
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
||||||
MongoCursor<Document> cursor;
|
MongoCursor<Document> cursor;
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class MongoDBController {
|
|||||||
return listRating;
|
return listRating;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateMovieRating(Rating rating) {
|
public void updateMovieRating(Rating rating) {
|
||||||
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
||||||
MongoCursor<Document> cursor;
|
MongoCursor<Document> cursor;
|
||||||
|
|
||||||
@ -155,4 +155,16 @@ public class MongoDBController {
|
|||||||
users.updateOne(searchQuery, newDocument);
|
users.updateOne(searchQuery, newDocument);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Rating> ProcessRecommendationV1(Integer userId) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Rating> ProcessRecommendationV2(Integer userId) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Rating> ProcessRecommendationV3(Integer userId) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,9 @@ import org.neo4j.driver.v1.StatementResult;
|
|||||||
*
|
*
|
||||||
* @author renando
|
* @author renando
|
||||||
*/
|
*/
|
||||||
public class Neo4JController {
|
public class Neo4JController implements DBControllerInterface {
|
||||||
|
|
||||||
public static List<Movie> getMoviesNeo4JByUser(Integer userId) {
|
public List<Movie> getMoviesByUser(Integer userId) {
|
||||||
List<Movie> movies = new LinkedList<Movie>();
|
List<Movie> movies = new LinkedList<Movie>();
|
||||||
int id = 0;
|
int id = 0;
|
||||||
StatementResult result = null;
|
StatementResult result = null;
|
||||||
@ -54,7 +54,7 @@ public class Neo4JController {
|
|||||||
return movies;
|
return movies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Rating> GetMoviesRatingNeo4JByUser(Integer userId) {
|
public List<Rating> getRatinByUser(Integer userId) {
|
||||||
List<Rating> moviesRating = new LinkedList<Rating>();
|
List<Rating> moviesRating = new LinkedList<Rating>();
|
||||||
|
|
||||||
StatementResult result = null;
|
StatementResult result = null;
|
||||||
@ -92,7 +92,7 @@ public class Neo4JController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateMovieRatingNeo4J(Rating note) {
|
public void updateMovieRating(Rating note) {
|
||||||
|
|
||||||
Integer user_id = note.getUserId();
|
Integer user_id = note.getUserId();
|
||||||
try {
|
try {
|
||||||
@ -122,7 +122,7 @@ public class Neo4JController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Rating> ProcessRecommendationV1(Integer userId) {
|
public List<Rating> ProcessRecommendationV1(Integer userId) {
|
||||||
|
|
||||||
List<Rating> ratings = new LinkedList<Rating>();
|
List<Rating> ratings = new LinkedList<Rating>();
|
||||||
StatementResult result = null;
|
StatementResult result = null;
|
||||||
@ -157,7 +157,7 @@ public class Neo4JController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Rating> ProcessRecommendationV2(Integer userId) {
|
public List<Rating> ProcessRecommendationV2(Integer userId) {
|
||||||
|
|
||||||
List<Rating> ratings = new LinkedList<Rating>();
|
List<Rating> ratings = new LinkedList<Rating>();
|
||||||
StatementResult result = null;
|
StatementResult result = null;
|
||||||
@ -194,4 +194,8 @@ public class Neo4JController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Rating> ProcessRecommendationV3(Integer userId) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user