/*
 * 
 */
package com.fp.bpmlib.task;

import java.math.BigDecimal;
import java.util.List;

import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.fp.bpmlib.ejb.local.JbpmBeanLocal;
import com.fp.common.logger.APPLogger;

// TODO: Auto-generated Javadoc
/**
 * Class TaskCleaner encargada de.
 * 
 * @author gfiallos
 */
public class TaskCleaner {

    /** El valor de em. */
    private EntityManager em;

    /** Constante TID. */
    private static final String TID = "tid";

    /**
     * Crea una nueva instancia de task cleaner.
     * 
     * @throws Exception la exception
     */
    public TaskCleaner() throws Exception {
        JbpmBeanLocal local = (JbpmBeanLocal) new InitialContext().lookup("java:global/maiaear-2.1/bpmlib/jbpmbean");
        this.em = local.getEmf().createEntityManager();
        this.em.joinTransaction();

    }

    /**
     * Delete task.
     * 
     * @param pTask the task
     * @throws Exception la exception
     */
    private void deleteTask(long pTask) throws Exception {
        Query q = this.em.createNativeQuery("delete from jbpm6.PeopleAssignments_PotOwners where task_id= :tid");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.I18NText where Task_Names_Id= :tid or Task_Subjects_Id= :tid  or Task_Descriptions_Id= :tid ");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.task_comment where TaskData_Comments_Id= :tid");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.PeopleAssignments_BAs where task_id= :tid");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.Content where id in (select DOCUMENTCONTENTID from jbpm6.Task where id=:tid)");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.Content where id in (select outputcontentid from jbpm6.Task where id=:tid)");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
        q = this.em.createNativeQuery("delete from jbpm6.Task where id = :tid ");
        q.setParameter(TaskCleaner.TID, pTask);
        q.executeUpdate();
    }

    /**
     * Delete tasks.
     * 
     * @throws Exception la exception
     */
    public void deleteTasks() throws Exception {
        try {
            Query q = this.em
                    .createNativeQuery("select id from jbpm6.Task t where  not exists( select 1 from jbpm6.processinstanceinfo p where p.instanceid = t.processinstanceid)");
            @SuppressWarnings("unchecked")
            List<BigDecimal> l = q.getResultList();
            for (BigDecimal id : l) {
                Long idtarea = Long.valueOf(id.toString());
                this.deleteTask(idtarea);
            }
        } catch (Exception e) {
            APPLogger.getLogger().error(e);
        } finally {
            if (em.isOpen()) {
                //em.close();
                // TODO
            }
        }

    }
}
