package task.timer.model;
import java.util.List;
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import org.hibernate.SessionFactory;

public class ManageEntity implements IEntityManager {
   private SessionFactory factory;
   private EntityType entityType;
   
   public ManageEntity(EntityType entityType){
	   this.entityType = entityType;
	   
	   try {
			factory=new Configuration().configure().buildSessionFactory();}
		catch(Throwable ex) {
			System.err.println("Failed to create sessionFactory object." + ex);
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}
   }
   
   public ManageEntity(SessionFactory factory){
	   this.factory = factory;
   }
   
   public ManageEntity(SessionFactory factory, EntityType entityType){
	   this.factory = factory;
	   this.entityType = entityType;
   }
   

@Override
public Integer add(AbstractEntity entity) {
	Session session = factory.openSession();
    Transaction tx = null;
    Integer entityID = null;
    try{
       tx = session.beginTransaction();      
       entityID = (Integer) session.save(entity); 
       tx.commit();
    }catch (HibernateException e) {
       if (tx!=null) tx.rollback();
       e.printStackTrace(); 
    }finally {
  	 session.close();
    }
    return entityID;
}

/*@Override
public void update(int id, AbstractEntity newEntity) throws ClassNotFoundException {
	Session session = factory.openSession();
    Transaction tx = null;
    try{
       tx = session.beginTransaction();
       AbstractEntity existingEntity = 
                  (AbstractEntity)session.get(Class.forName(entityType.toString()), id); 
       existingEntity = newEntity;
       
       session.update(existingEntity); 
       tx.commit();
    }catch (HibernateException e) {
       if (tx!=null) tx.rollback();
       e.printStackTrace(); 
    }finally {
   	 session.close(); 
    }	
}*/

@Override
public void update(AbstractEntity newEntity) throws ClassNotFoundException {
	Session session = factory.openSession();
    Transaction tx = null;
    try{
       tx = session.beginTransaction();          
       session.update(newEntity); 
       tx.commit();
    }catch (HibernateException e) {
       if (tx!=null) tx.rollback();
       e.printStackTrace(); 
    }finally {
   	 session.close(); 
    }	
}

@Override
public void delete(int id) throws ClassNotFoundException {
	Session session = factory.openSession();
    Transaction tx = null;
    try{
       tx = session.beginTransaction();
       AbstractEntity existingEntity = 
               (AbstractEntity)session.get(Class.forName(entityType.getClassType()), id); 
       session.delete(existingEntity); 
       tx.commit();
    }catch (HibernateException e) {
       if (tx!=null) tx.rollback();
       e.printStackTrace(); 
    }finally {
   	session.close(); 
    }	
}

@Override
public List<AbstractEntity> list(){
    Session session = factory.openSession();
    Transaction tx = null;
    List<AbstractEntity> entities = null;
    try{
       tx = session.beginTransaction();
       entities = session.createQuery("from " + entityType).list(); 
       tx.commit();
       return entities;
    }catch (HibernateException e) {
       if (tx!=null) tx.rollback();
       e.printStackTrace(); 
    }finally {
  	  session.close(); 
    }
    return entities;
 }


}
@@ -42,17 +42,70 @@
 l.List;%0A
- 
+import java.util.Set;%0A%0Aimport org.hibernate.Hibernate;
 %0Aimport 
@@ -3243,24 +3243,31 @@
 e).list(); %0A
+      %0A
        tx.co
@@ -3468,10 +3468,602 @@
 es;%0A %7D%0A%0A
+public Set%3CUser%3E listUsersOfProject(int idProject)%7B%0A    Session session = factory.openSession();%0A    Transaction tx = null;%0A    Set%3CUser%3E entities = null;%0A    try%7B%0A       tx = session.beginTransaction();%0A       Project project = (Project) session.get(Project.class, idProject);%0A      %0A       entities = project.getUsers(); %0A       Hibernate.initialize(entities);%0A       %0A       tx.commit();%0A       return entities;%0A    %7Dcatch (HibernateException e) %7B%0A       if (tx!=null) tx.rollback();%0A       e.printStackTrace(); %0A    %7D%0A    finally%7B%0A    %09session.close();%0A    %7D%0A    %0A    return entities;%0A %7D%0A
 %0A%7D

add "listUsersOfProject" method