package com.fp.bpmlib.task.util;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;

import org.kie.api.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;

/**
 * Necesita esta claese para que funcione el jbpm, internamente utiliza el EntiyManagerFactory.
 * 
 * @author jorge
 */
@ApplicationScoped
public class MaiaApplicationScopeProducer {

    @PersistenceUnit(unitName = "org.jbpm.domain")
    private EntityManagerFactory emf;

    @Produces
    public EntityManagerFactory produceEntityManagerFactory() {
        if (emf == null) {
            emf = Persistence.createEntityManagerFactory("org.jbpm.domain");
        }
        return emf;
    }

    // @Produces
    // @Singleton
    // @PerProcessInstance
    // @PerRequest
    public RuntimeEnvironment produceEnvironment(EntityManagerFactory emf) {

        // RuntimeEnvironment environment =
        // RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf)
        // .userGroupCallback(usergroupCallback).addAsset(ResourceFactory.newClassPathResource("prueba.bpmn2"),
        // ResourceType.BPMN2)
        // .get();
        RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf);
        // builder.userGroupCallback(usergroupCallback);
        // builder.registerableItemsFactory(factory);
        // builder.addAsset(ResourceFactory.newClassPathResource("prueba.bpmn2"), ResourceType.BPMN2);
        RuntimeEnvironment environment = builder.get();
        return environment;
    }

}
