package ub.scp2.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

import ub.scp2.dao.UserDao;
import ub.scp2.entities.Task;
import ub.scp2.entities.User;

/**
 * UserDaoImpl.java -this class implements the UserDao. This is the Data Acces Object of the User entity
 * 
 *
 */
public class UserDaoImpl implements UserDao, java.io.Serializable {
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * Retrieve a user list with all users
	 */
	public List<User> getUsers() {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM User u WHERE u.role != 3" ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
	}
	
	/**
	 * Retrieve a user list with all users with role admin
	 */
	public List<User> getAdmins() {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM User u WHERE u.role = 3" ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
	}
	
	/**
	 * Save user in database
	 */
	public void saveUser(User user){
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.save(user);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
	}
	
	/**
	 * Retrieve a User according to the username 
	 */
	public User getUserByUsername(String username){

		 Session session = sessionFactory.getCurrentSession();

		 try{
			 Criteria criteria = session.createCriteria(User.class);
			 criteria.add(Restrictions.eq("username", username));			 
			 return (User) criteria.uniqueResult();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
		
	}


	/**
	 * Delete user in database
	 */
	public void deleteUser(User user) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.delete(user);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
	}

	public boolean usernameExists(String username){
		 Session session = sessionFactory.getCurrentSession();
		 boolean b = false;
		 try{
			 List<User> user = session.createQuery("FROM User WHERE username = ?").setString(0,  username).list();
			 return !user.isEmpty();

		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return false;
	}

	/**
	 * Update user in database
	 */
	public void updateUser(User user) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.update(user);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
	}
	
	/**
	 * Reset the database with default values.
	 */
	public void cleanDataBase(){
		Session session = sessionFactory.getCurrentSession();
		try{
                    /*
			SQLQuery queryUserSubject = session.createSQLQuery("truncate table user_subject");
			SQLQuery querySubject = session.createSQLQuery("truncate table subject");
			SQLQuery queryTask = session.createSQLQuery("truncate table task");
			SQLQuery queryTest = session.createSQLQuery("truncate table test");
			SQLQuery queryTestResult = session.createSQLQuery("truncate table testresult");
			SQLQuery querySubmission = session.createSQLQuery("truncate table submission");
                    */
			SQLQuery queryUserSubject = session.createSQLQuery("delete FROM user_subject");
			SQLQuery querySubject = session.createSQLQuery("delete FROM subject");
			SQLQuery queryTask = session.createSQLQuery("delete FROM task");
			SQLQuery queryTest = session.createSQLQuery("delete FROM test");
			SQLQuery queryTestResult = session.createSQLQuery("delete FROM testresult");
			SQLQuery querySubmission = session.createSQLQuery("delete FROM submission");
			SQLQuery queryUser = session.createSQLQuery("delete FROM user WHERE role<>3");
			queryUserSubject.executeUpdate();
			querySubject.executeUpdate();
			queryTask.executeUpdate();
			queryTest.executeUpdate();
			queryTestResult.executeUpdate();
                        
			queryUser.executeUpdate();
		}catch(Exception e){
			 e.printStackTrace();
		 }
	}
}
