<commit_before>package nl.flamingostyle.quooc.service;

import java.util.List;

import nl.flamingostyle.quooc.domain.Song;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service for processing Songs
 *
 */
@Service
@Transactional
public class SongServiceImpl implements SongService {

    protected static final Logger logger = Logger.getLogger("service");

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * Gets the maximum number of songs from the database
     *
     * @return the max number of songs
     */
    @Override
    public long getMax() {
        logger.debug("Getting max");

        // return (Integer) getCurrentSession().createCriteria("song").setProjection(Projections.rowCount()).uniqueResult();
        
        Query query = getCurrentSession().createQuery("SELECT COUNT(*) FROM  Song song");

        // Retrieve all
        return (Long)query.list().get(0);
    }

    /**
     * Returns a random song from the database.
     *
     * @return A random song from the database
     */
    @Override
    public Song getRandom() {
		// List<Song> songs = getAll();
        // Random randomizer = new Random();
        // Song song = songs.get(randomizer.nextInt(songs.size()));
        // logger.debug("Random song retrieved: " + song.getArtist() + " - " + song.getTitle());

        // Retrieve existing song 'You can call me Al'
        int id = 12070;
        Song song = (Song) getCurrentSession().get(Song.class, id);
        logger.debug("Gotten song " + song.getTitle());

        return song;
    }

    /**
     * Retrieves all songs
     *
     * @return a list of songs
     */
    @SuppressWarnings("unchecked")
    public List<Song> getAll() {
        logger.debug("Retrieving all songs");

        // Create a Hibernate query (HQL)
        Query query = getCurrentSession().createQuery("FROM  Song");

        // Retrieve all
        return query.list();
    }

    /**
     * Retrieves all songs
     *
     * @return a list of songs
     */
    @SuppressWarnings("unchecked")
    public List<Song> getAllPagination(int max, int offset) {
        logger.debug("Retrieving all songs");

        // Create a Hibernate query (HQL)
        Query query = getCurrentSession().createQuery("FROM  Song");
        query.setFirstResult(offset);
        query.setMaxResults(max);

        // Retrieve all
        return query.list();
    }

    /**
     * Retrieves a single song
     */
    public Song get(Integer id) {
        // Retrieve existing song first
        logger.debug("Calling get with the id " + id);
        Song song = (Song) getCurrentSession().get(Song.class, id);
        logger.debug("Gotten song " + song.getTitle());
        return song;
    }

    /**
     * Searches songs with a certain firstname
     *
     * @param name the firstname to search for<commit_msg>Added ordering in list.<commit_after>package nl.flamingostyle.quooc.service;

import java.util.List;

import nl.flamingostyle.quooc.domain.Song;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service for processing Songs
 *
 */
@Service
@Transactional
public class SongServiceImpl implements SongService {

    protected static final Logger logger = Logger.getLogger("service");

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * Gets the maximum number of songs from the database
     *
     * @return the max number of songs
     */
    @Override
    public long getMax() {
        logger.debug("Getting max");

        // return (Integer) getCurrentSession().createCriteria("song").setProjection(Projections.rowCount()).uniqueResult();
        
        Query query = getCurrentSession().createQuery("SELECT COUNT(*) FROM  Song song");

        // Retrieve all
        return (Long)query.list().get(0);
    }

    /**
     * Returns a random song from the database.
     *
     * @return A random song from the database
     */
    @Override
    public Song getRandom() {
		// List<Song> songs = getAll();
        // Random randomizer = new Random();
        // Song song = songs.get(randomizer.nextInt(songs.size()));
        // logger.debug("Random song retrieved: " + song.getArtist() + " - " + song.getTitle());

        // Retrieve existing song 'You can call me Al'
        int id = 12070;
        Song song = (Song) getCurrentSession().get(Song.class, id);
        logger.debug("Gotten song " + song.getTitle());

        return song;
    }

    /**
     * Retrieves all songs
     *
     * @return a list of songs
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Song> getAll() {
        logger.debug("Retrieving all songs");

        // Create a Hibernate query (HQL)
        Query query = getCurrentSession().createQuery("FROM  Song song order by song.firstname");

        // Retrieve all
        return query.list();
    }

    /**
     * Retrieves all songs
     *
     * @return a list of songs
     */
    @SuppressWarnings("unchecked")
    public List<Song> getAllPagination(int max, int offset) {
        logger.debug("Retrieving all songs");

        // Create a Hibernate query (HQL)
        Query query = getCurrentSession().createQuery("FROM  Song song order by song.firstname");
        query.setFirstResult(offset);
        query.setMaxResults(max);

        // Retrieve all
        return query.list();
    }

    /**
     * Retrieves a single song
     */
    public Song get(Integer id) {
        // Retrieve existing song first
        logger.debug("Calling get with the id " + id);
        Song song = (Song) getCurrentSession().get(Song.class, id);
        logger.debug("Gotten song " + song.getTitle());
        return song;
    }

    /**
     * Searches songs with a certain firstname
     *
     * @param name the firstname to search for
{'fraction_non_alphanumeric': 0.11516079632465544, 'fraction_numerical': 0.0021439509954058193, 'mean_word_length': 2.547528517110266, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '142767', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1785, 'n_words': 487}