/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.entity;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.hibernate.cfg.Environment;
import org.hibernate.ejb.HibernatePersistence;
import org.hibernate.ejb.packaging.PersistenceMetadata;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.1.2.5 $
 */
public class EntityManagerFactoryLoader
{
   public static EntityManagerFactory loadFactory(PersistenceMetadata xml, List<InputStream> hbmXmlFiles, List<String> packages, List<Class> classes, Properties props)
   {
      if (xml != null && xml.getJtaDatasource() != null)
      {
         props.put(Environment.DATASOURCE, xml.getJtaDatasource());
      }
      return loadFactory(hbmXmlFiles, packages, classes, props);
   }

   public static EntityManagerFactory loadFactory(List<InputStream> hbmXmlFiles, List<String> packages, List<Class> classes, Properties props)
   {
      Properties copy = new Properties();
      copy.putAll(props);

      // Third-party vendors are just gonna have to conform to these APIs
      if (classes != null) copy.put(HibernatePersistence.LOADED_CLASSES, classes);
      if (packages != null) copy.put(HibernatePersistence.PACKAGE_NAMES, packages);
      if (hbmXmlFiles != null) copy.put(HibernatePersistence.HBXML_FILES, hbmXmlFiles);
      HibernatePersistence provider = new HibernatePersistence();
      return provider.createEntityManagerFactory(copy);
   }
}
