package com.web.javsterisk.dao;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.web.javsterisk.entity.ExtensionsWizzard;
import com.web.javsterisk.util.HibernateUtil;

/**
 * 
 * @author freddycucho
 *
 */
public class ExtensionsWizzardDAO {
	
	private static final Logger log = LogManager.getLogger(ExtensionsWizzardDAO.class);
		
	/**
	 * Register record in DB
	 * @param object
	 * @throws Exceptions
	 */
	public void register(ExtensionsWizzard object) throws Exception {
		log.info("Register : {}", object.toString());
		Transaction tx = null;
		try {
			HibernateUtil.openSessionAndBindToThread();			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.persist(object);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			log.error(e.getMessage(), e);	
			throw e;
		} finally {
			HibernateUtil.closeSessionAndUnbindFromThread();	
		}	
	}
	
	public void merger(ExtensionsWizzard object) throws Exception {
		log.info("Merger : {}", object.toString());
		Transaction tx = null;
		try {
			HibernateUtil.openSessionAndBindToThread();			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.merge(object);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			log.error(e.getMessage(), e);	
			throw e;
		} finally {
			HibernateUtil.closeSessionAndUnbindFromThread();	
		}	
	}

	/**
	 * Modifier record in DB
	 * @param object
	 * @throws Exceptions
	 */
	public void modifier(ExtensionsWizzard object) throws Exception {			
		log.info("Modifier : {}", object.toString());
		Transaction tx = null;
		try {
			HibernateUtil.openSessionAndBindToThread();			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.update(object);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			log.error(e.getMessage(), e);				
		} finally {
			HibernateUtil.closeSessionAndUnbindFromThread();	
		}
	}

	/**
	 * Deleter record in DB
	 * @param object
	 * @throws Exceptions
	 */
	public void deleter(ExtensionsWizzard object) throws Exception {		
		log.info("Deleter : {}", object.toString());
		Transaction tx = null;
		try {
			HibernateUtil.openSessionAndBindToThread();			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.delete(object);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			log.error(e.getMessage(), e);				
		} finally {
			HibernateUtil.closeSessionAndUnbindFromThread();	
		}
	}
	
}
