package org.jerrymouse.database.hadoop.jpa;

import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.ipc.HBaseClient;
import org.apache.zookeeper.ZooKeeper;

public class HadoopEntityManagerFactory implements EntityManagerFactory {

	private HBaseAdmin hBaseAdmin;
	private HBaseConfiguration hBaseConfiguration;

	public HadoopEntityManagerFactory(Map map) {
		boolean autoCreateTable = new Boolean((String) map.get("cc.autoCreateTable"));
		String host = (String) map.get("cc.host");
		String entityPackage = (String) map.get("cc.entityPackage");
		hBaseConfiguration = new HBaseConfiguration();
		try {
			hBaseAdmin = new HBaseAdmin(hBaseConfiguration);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		}
		if (autoCreateTable) {
			HadoopTable.autoCreateTable=true;
			HadoopTable hadoopTable=new HadoopTable(hBaseAdmin, hBaseConfiguration);
			hadoopTable.checkTables(entityPackage);
		}
	}

	public void close() {

	}

	public EntityManager createEntityManager() {
		return new HadoopEntityManager(hBaseAdmin, hBaseConfiguration);
	}

	public EntityManager createEntityManager(Map map) {
		return new HadoopEntityManager(hBaseAdmin, hBaseConfiguration);
	}

	public boolean isOpen() {
		return true;
	}

}
