/*
 * Acceso a Datos
 */
package com.fp.bpmlib.db;

import java.text.SimpleDateFormat;

import com.fp.general.exception.GeneralException;
import com.fp.persistence.commondb.HqlStatement;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pbpm.gene.TbpmProcessInstance;
import com.fp.persistence.pgeneral.gene.TgeneParameters;
import com.fp.persistence.pgeneral.gene.TgeneParametersKey;

// TODO: Auto-generated Javadoc
/**
 * Clase utilitaria para el manejo de Caché del proyecto BPMLIB.
 * 
 * @author GF
 * @version 2.1
 */
public final class DataHelper {

    /** Almacena una instancia de DataHelper. */
    private static DataHelper cache;

    /** Constante HQL_PROCESS. */
    private static final String HQL_PROCESS = "from TbpmProcessInstance tb  " + " where tb.processid=:pid ";

    /**
     * Entrega una instancia de DataHelper.
     * 
     * @return DataHelper
     */
    public static DataHelper getInstance() {
        synchronized (DataHelper.class) {
            if (DataHelper.cache != null) {
                return DataHelper.cache;
            }

            if (DataHelper.cache == null) {
                DataHelper.cache = new DataHelper();
            }
        }
        return DataHelper.cache;
    }



    /**
     * Obtiene el valor de date time format.
     * 
     * @param pCompany the company
     * @return Valor de date time format
     * @throws Exception la exception
     */
    public SimpleDateFormat getDateTimeFormat(Integer pCompany) throws Exception {
        TgeneParametersKey k = new TgeneParametersKey("FORMATDATE", pCompany);
        TgeneParameters f = TgeneParameters.find(PersistenceHelper.getEntityManager(), k);
        return new SimpleDateFormat(f.getTextvalue() + " HH:mm");
    }


    /**
     * Obtiene el valor de tbpm process instance.
     * 
     * @param pId the id
     * @return Valor de tbpm process instance
     * @throws Exception la exception
     */
    public TbpmProcessInstance getTbpmProcessInstance(Long pId) throws Exception {
        TbpmProcessInstance process = null;
        HqlStatement hql = new HqlStatement(DataHelper.HQL_PROCESS);
        hql.setLong("pid", pId);
        process = hql.getObject(TbpmProcessInstance.class);
        if (process == null) {
            throw new GeneralException("BPM-0004", "LA INSTANCIA {0} NO ESTA DISPONIBLE O YA FUE FINALIZADA", pId);
        }
        return process;
    }


}
