package org.jerrymouse.database;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jerrymouse.database.gae.GaeJDODaoSupport;
import org.jerrymouse.database.gae.GaeJPADaoSupport;
import org.jerrymouse.database.hadoop.HadoopJDODaoSupport;
import org.jerrymouse.database.hadoop.HadoopJPADaoSupport;
import org.jerrymouse.database.normal.NormalJDODaoSupport;
import org.jerrymouse.database.normal.NormalJPADaoSupport;

public class DaoAdapter<T> {
	private static Log log = LogFactory.getLog(DaoAdapter.class);
	static String JPA = "jpa";
	static String JDO = "jdo";
	static String GAE = "gae";
	static String NORMAL = "normal";
	static String HADOOP = "hadoop";
	/**
	 * default Normal
	 */
	static String pBottomLayer = NORMAL;

	/**
	 * default Jpa
	 */
	static String pInterface = JPA;

	/**
	 * default gae.pmf
	 */
	public static String pmfName = "gae-pmf";

	public DaoAdapter() {
		Properties properties = new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream(
					"/gaes.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.trace(properties);
		pBottomLayer = (String) properties.get("bottomLayer");
		pInterface = (String) properties.get("interface");
		if (GAE.equalsIgnoreCase(pBottomLayer)) {
			pmfName = (String) properties.get("gae.pmf");
		} else if (NORMAL.equalsIgnoreCase(pBottomLayer)) {
			pmfName = (String) properties.get("normal.pmf");
		} else if (HADOOP.equalsIgnoreCase(pBottomLayer)) {
			pmfName = (String) properties.get("hadoop.pmf");
		}
	}

	public String getpBottomLayer() {
		return pBottomLayer;
	}

	public void setpBottomLayer(String pBottomLayer) {
		this.pBottomLayer = pBottomLayer;
	}

	public String getpInterface() {
		return pInterface;
	}

	public void setpInterface(String pInterface) {
		this.pInterface = pInterface;
	}

	public DaoSupport<T> getDaoSupport() {
		return getDaoSupport(pBottomLayer, pInterface);
	}

	public DaoSupport<T> getDaoSupport(String pBottomLayer, String pInterface) {
		if (NORMAL.equalsIgnoreCase(pBottomLayer)
				&& JPA.equalsIgnoreCase(pInterface)) {
			return new NormalJPADaoSupport();
		} else if (GAE.equalsIgnoreCase(pBottomLayer)
				&& JPA.equalsIgnoreCase(pInterface)) {
			return new GaeJPADaoSupport();
		} else if (NORMAL.equalsIgnoreCase(pBottomLayer)
				&& JDO.equalsIgnoreCase(pInterface)) {
			return new NormalJDODaoSupport();
		} else if (GAE.equalsIgnoreCase(pBottomLayer)
				&& JDO.equalsIgnoreCase(pInterface)) {
			return new GaeJDODaoSupport();
		} else if (HADOOP.equalsIgnoreCase(pBottomLayer)
				&& JDO.equalsIgnoreCase(pInterface)) {
			return new HadoopJDODaoSupport();
		} else if (HADOOP.equalsIgnoreCase(pBottomLayer)
				&& JPA.equalsIgnoreCase(pInterface)) {
			return new HadoopJPADaoSupport();
		}
		return null;
	}

}
