refactoring des Controlleurs db pour switch plus facilement entre mongodb et neo4j

This commit is contained in:
Quentin Rouland 2016-12-09 10:50:01 +01:00
parent cad44a4abb
commit 72b75d938c
4 changed files with 65 additions and 22 deletions

View File

@ -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);
}

View File

@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMethod;
import com.camillepradel.movierecommender.model.Genre;
import com.camillepradel.movierecommender.model.Movie;
import com.camillepradel.movierecommender.model.Rating;
@ -18,6 +17,10 @@ import com.camillepradel.movierecommender.model.Rating;
public class MainController {
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")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
@ -33,8 +36,7 @@ public class MainController {
public ModelAndView showMovies(
@RequestParam(value = "user_id", required = false) Integer userId) {
List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
//List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
List<Movie> movies = db.getMoviesByUser(userId);
ModelAndView mv = new ModelAndView("movies");
mv.addObject("userId", userId);
@ -49,13 +51,11 @@ public class MainController {
System.out.println("GET /movieratings for user " + userId);
// write query to retrieve all movies from DB
//List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
List<Movie> allMovies = db.getMoviesByUser(null);
// write query to retrieve all ratings from the specified user
//List<Rating> ratings = MongoDBController.getRatingMongoDBByUser(userId);
// write query to retrieve all ratings from the specified user
List<Rating> ratings=db.getRatinByUser(userId);
List<Rating> ratings=Neo4JController.GetMoviesRatingNeo4JByUser(userId);
ModelAndView mv = new ModelAndView("movieratings");
mv.addObject("userId", userId);
mv.addObject("allMovies", allMovies);
@ -69,7 +69,7 @@ public class MainController {
System.out.println("POST /movieratings for user " + rating.getUserId()
+ ", movie " + rating.getMovie().getTitle()
+ ", score " + rating.getScore());
Neo4JController.updateMovieRatingNeo4J(rating);
db.updateMovieRating(rating);
return "redirect:/movieratings?user_id=" + rating.getUserId();
}
@ -82,11 +82,10 @@ public class MainController {
List<Rating> recommendations = new LinkedList<Rating>();
if (processingMode.equals(1)){
recommendations = Neo4JController.ProcessRecommendationV1(userId);
recommendations = db.ProcessRecommendationV1(userId);
}
else if (processingMode.equals(2)){
recommendations = Neo4JController.ProcessRecommendationV2(userId);
recommendations = db.ProcessRecommendationV2(userId);
}
ModelAndView mv = new ModelAndView("recommendations");

View File

@ -26,8 +26,8 @@ import org.bson.Document;
*
* @author sidya
*/
public class MongoDBController {
public static List<Movie>getMoviesMongoDBByUser(Integer user_id){
public class MongoDBController implements DBControllerInterface {
public List<Movie>getMoviesByUser(Integer user_id){
ArrayList<Movie> listMovies= new ArrayList<Movie>();
MongoCursor<Document> cursor;
@ -76,7 +76,7 @@ public class MongoDBController {
return listMovies;
}
public static List<Rating>getRatingMongoDBByUser(Integer user_id){
public List<Rating>getRatinByUser(Integer user_id){
ArrayList<Rating> listRating= new ArrayList<Rating>();
MongoCursor<Document> cursor;
@ -120,7 +120,7 @@ public class MongoDBController {
return listRating;
}
public static void updateMovieRating(Rating rating) {
public void updateMovieRating(Rating rating) {
ArrayList<Rating> listRating= new ArrayList<Rating>();
MongoCursor<Document> cursor;
@ -155,4 +155,16 @@ public class MongoDBController {
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.
}
}

View File

@ -20,9 +20,9 @@ import org.neo4j.driver.v1.StatementResult;
*
* @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>();
int id = 0;
StatementResult result = null;
@ -54,7 +54,7 @@ public class Neo4JController {
return movies;
}
public static List<Rating> GetMoviesRatingNeo4JByUser(Integer userId) {
public List<Rating> getRatinByUser(Integer userId) {
List<Rating> moviesRating = new LinkedList<Rating>();
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();
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>();
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>();
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.
}
}