package ub.scp2.daoImpl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import ub.scp2.dao.SubmissionDao;
import ub.scp2.entities.Submission;
import ub.scp2.entities.Task;
import ub.scp2.entities.Test;
import ub.scp2.entities.User;

/**
 * SubmissionDaoImpl.java -this class implements the SubmissionDao. This is the Data Acces Object of the Submission entity
 * 
 *
 */
public class SubmissionDaoImpl implements SubmissionDao, java.io.Serializable {
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Delete submission by one user
	 */
	public void deleteSubmissionsByUser(User user) {
		 Session session = sessionFactory.getCurrentSession();

		 try{
			 Criteria criteria = session.createCriteria(Submission.class);			 
			 criteria.add(Restrictions.eq("user", user));
			 List<Submission> submissionlist = criteria.list();
			 Iterator<Submission> ite = submissionlist.iterator();
			 while (ite.hasNext()){
				 session.delete(ite.next());
			 }
		 }catch(Exception e){
			 e.printStackTrace();
		 }
	}

}
