package ub.scp2.daoImpl;


import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import ub.scp2.dao.FileSubmissionDao;
import ub.scp2.entities.FileSubmission;
import ub.scp2.entities.Submission;


/**
 * LanguajeBoImpl.java -this class implements the LanguajeBo. This is the Business Object of the Languaje entity 
 * 
 *
 */
public class FileSubmissionDaoImpl implements FileSubmissionDao, java.io.Serializable {
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

        @Override
        public void saveFileSubmission(FileSubmission fileSubmission) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.save(fileSubmission);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
        }

        @Override
        public void deleteFileSubmission(FileSubmission fileSubmission) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.delete(fileSubmission);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
        }

        @Override
        public void updateFileSubmission(FileSubmission fileSubmission) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 session.update(fileSubmission);
			 session.flush();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
        }
        
        @Override
        public List<FileSubmission> getFileSubmissions() {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM submission_file" ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
        }

        @Override
        public List<FileSubmission> getFileBySubmissionId(long id) {
		 Session session = sessionFactory.getCurrentSession();
		 try{
			 return session.createQuery( "FROM submission_file sf WHERE sf.submission_id = "+id ).list();
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return null;
        }
        
	/**
	 * Delete files by one submission
	 */
        @Override
	public void deleteSubmissionsByUser(long id) {
		 Session session = sessionFactory.getCurrentSession();

		 try{
                        Query deleteQuery = session.createSQLQuery("DELETE FROM submission_file WHERE submission_id='"+id +"'");
                        deleteQuery.executeUpdate();
		 }catch(Exception e){
			e.printStackTrace();
		 }
	}
}
