<commit_before>package at.fh.swenga.dao;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import at.fh.swenga.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import at.fh.swenga.service.GetProperties;

import info.movito.themoviedbapi.TmdbApi;
import info.movito.themoviedbapi.TmdbMovies;
import info.movito.themoviedbapi.TmdbPeople;
import info.movito.themoviedbapi.TmdbMovies.MovieMethod;
import info.movito.themoviedbapi.TmdbSearch;
import info.movito.themoviedbapi.model.MovieDb;
import info.movito.themoviedbapi.model.core.MovieResultsPage;
import info.movito.themoviedbapi.model.people.PersonCast;
import info.movito.themoviedbapi.model.people.PersonPeople;


@Repository
@Transactional
public class MovieDao
{
	@PersistenceContext
	protected EntityManager entityManager;

	@Autowired
	private GenreDao genreDao;

	@Autowired
	private ActorDao actorDao;

	@Autowired
	private UserMovieDao userMovieDao;


	GetProperties gp = new GetProperties();
	Properties properties = gp.getPropValues();

	private String apiKey = properties.getProperty("apiKey");
	
	private TmdbMovies tmdbMovies = new TmdbApi(apiKey).getMovies();
	private TmdbSearch tmdbSearch = new TmdbApi(apiKey).getSearch();
	private TmdbPeople tmdbPeople = new TmdbApi(apiKey).getPeople();

	public List<MovieModel> getMovies()
	{
		TypedQuery<MovieModel> typedQuery = entityManager.createQuery("select m from MovieModel m", MovieModel.class);
		List<MovieModel> typedResultList = typedQuery.getResultList();
		return typedResultList;
	}


	public List<MovieModel> getUserMovies(User user)
	{
		// Inner JOIN UserMovie um ON m.id = um WHERE um.owner_userName = :userName
		TypedQuery<MovieModel> typedQuery = entityManager.createQuery("SELECT m FROM MovieModel m left join fetch  m.userMovies um where um.owner = :user", MovieModel.class);
		typedQuery.setParameter("user", user);

		List<MovieModel> typedResultList = typedQuery.getResultList();
		return typedResultList;
	}



	//TODO replace mapMovie with other lightweight method
	//because mapping movie genres and actors for multiple movies takes too long
	public List<MovieModel> searchMovies(String searchString)
	{
        MovieResultsPage result = tmdbSearch.searchMovie(searchString, null, "en-US", false, null);
        List<MovieDb> resultList = result.getResults();      
        List<MovieModel> movieModelList = new ArrayList<MovieModel>();
        
        for (MovieDb mDB : resultList)
        {
        	movieModelList.add(mapMovie(tmdbMovies, mDB.getId(), false));		//Replace!!!
        }
        
        return movieModelList;
	}

	public MovieModel getMovieById(int id) throws DataAccessException
	{
		return entityManager.find(MovieModel.class, id);
	}

	//TODO FR: Add Genre und Actor in DAOs auslagern
	public MovieModel mapMovie(TmdbMovies movies, int id, boolean fullContent)
	{
		//TmdbMovies movies = new TmdbApi(apiKey).getMovies();
    	MovieDb movie = movies.getMovie(id, "en", MovieMethod.credits);
    	MovieModel movieModel = new MovieModel();
    	
    	movieModel.setId(movie.getId());
    	movieModel.setTmdb_id(movie.getId());
    	movieModel.setTitle(movie.getTitle());
    	movieModel.setOverview(movie.getOverview());
    	movieModel.setAdult(movie.isAdult());
    	movieModel.setVote_average(movie.getPopularity());
    	movieModel.setVote_count(movie.getVoteCount());
    	
    	DateFormat format = new SimpleDateFormat("YYYY-MM-dd", Locale.ENGLISH);
    	try 
    	{ 
    		if (movie.getReleaseDate() != "") 
    			movieModel.setRelease_date(format.parse(movie.getReleaseDate()));<commit_msg>Added benchmarking to searchMovies, limited Search to first 6 Movies<commit_after>package at.fh.swenga.dao;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import at.fh.swenga.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import at.fh.swenga.service.GetProperties;

import info.movito.themoviedbapi.TmdbApi;
import info.movito.themoviedbapi.TmdbMovies;
import info.movito.themoviedbapi.TmdbPeople;
import info.movito.themoviedbapi.TmdbMovies.MovieMethod;
import info.movito.themoviedbapi.TmdbSearch;
import info.movito.themoviedbapi.model.MovieDb;
import info.movito.themoviedbapi.model.core.MovieResultsPage;
import info.movito.themoviedbapi.model.people.PersonCast;
import info.movito.themoviedbapi.model.people.PersonPeople;


@Repository
@Transactional
public class MovieDao
{
	@PersistenceContext
	protected EntityManager entityManager;

	@Autowired
	private GenreDao genreDao;

	@Autowired
	private ActorDao actorDao;

	@Autowired
	private UserMovieDao userMovieDao;


	GetProperties gp = new GetProperties();
	Properties properties = gp.getPropValues();

	private String apiKey = properties.getProperty("apiKey");
	
	private TmdbMovies tmdbMovies = new TmdbApi(apiKey).getMovies();
	private TmdbSearch tmdbSearch = new TmdbApi(apiKey).getSearch();
	private TmdbPeople tmdbPeople = new TmdbApi(apiKey).getPeople();

	public List<MovieModel> getMovies()
	{
		TypedQuery<MovieModel> typedQuery = entityManager.createQuery("select m from MovieModel m", MovieModel.class);
		List<MovieModel> typedResultList = typedQuery.getResultList();
		return typedResultList;
	}


	public List<MovieModel> getUserMovies(User user)
	{
		// Inner JOIN UserMovie um ON m.id = um WHERE um.owner_userName = :userName
		TypedQuery<MovieModel> typedQuery = entityManager.createQuery("SELECT m FROM MovieModel m left join fetch  m.userMovies um where um.owner = :user", MovieModel.class);
		typedQuery.setParameter("user", user);

		List<MovieModel> typedResultList = typedQuery.getResultList();
		return typedResultList;
	}



	//TODO replace mapMovie with other lightweight method
	//because mapping movie genres and actors for multiple movies takes too long
	public List<MovieModel> searchMovies(String searchString)
	{
		System.out.println("SEARCH STARTED");
		
		long startTime = System.currentTimeMillis();
		
        MovieResultsPage result = tmdbSearch.searchMovie(searchString, null, "en-US", false, null);
        List<MovieDb> resultList = result.getResults();      
        List<MovieModel> movieModelList = new ArrayList<MovieModel>();
        
    	int maxMovies = 6;
    	int count = 0;
        
        for (MovieDb mDB : resultList)
        {	
        	if (count < maxMovies){
        		movieModelList.add(mapMovie(tmdbMovies, mDB.getId(), false));		//Replace!!!
        		count ++;
        		System.out.println(count);
        	}
        	else{
        		break;
        	}
        }
		
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);
        
        return movieModelList;
	}

	public MovieModel getMovieById(int id) throws DataAccessException
	{
		return entityManager.find(MovieModel.class, id);
	}

	//TODO FR: Add Genre und Actor in DAOs auslagern
	public MovieModel mapMovie(TmdbMovies movies, int id, boolean fullContent)
	{		
		//TmdbMovies movies = new TmdbApi(apiKey).getMovies();
    	MovieDb movie = movies.getMovie(id, "en", MovieMethod.credits);
    	MovieModel movieModel = new MovieModel();
    	
    	movieModel.setId(movie.getId());
    	movieModel.setTmdb_id(movie.getId());
    	movieModel.setTitle(movie.getTitle());
    	movieModel.setOverview(movie.getOverview());
    	movieModel.setAdult(movie.isAdult());
    	movieModel.setVote_average(movie.getPopularity());
    	movieModel.setVote_count(movie.getVoteCount());
    	
    	DateFormat format = new SimpleDateFormat("YYYY-MM-dd", Locale.ENGLISH);
    	try 
    	{ 
    		if (movie.getReleaseDate() != "") 
    			movieModel.setRelease_date(format.parse(movie.getReleaseDate()));
{'fraction_non_alphanumeric': 0.10029974636845745, 'fraction_numerical': 0.00034586119437399123, 'mean_word_length': 4.614886731391586, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1346554', 'n_tokens_mistral': 2896, 'n_tokens_neox': 2726, 'n_words': 547}