/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.openjpa.das;

import java.io.InputStream;
import java.sql.Connection;

import javax.persistence.Persistence;

import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.sdo.SDOEntityManager;
import org.apache.openjpa.sdo.SDOEntityManagerFactory;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.DASFactory;
import org.apache.tuscany.das.rdb.config.Config;

/**
 * Implements Tuscany DASFactory through delegation to OpenJPA 
 * EntityManagerFactory.
 * 
 * @author Pinaki Poddar
 * @since 0.3.0
 *
 */
public class FluidDASFactory implements DASFactory {
	private static Localizer _loc = Localizer.forPackage(FluidDASFactory.class);
	
	private SDOEntityManagerFactory emf;
	
	/**
	 * Construct a factory.
	 * <br>
	 * All persistence related configuration is specified in 
	 * <code>META-INF/persistence.xml</code> which must be available as a 
	 * resource in the invocation classpath.
	 * <br>The <code>persistence.xml</code> must be configured for Fluid.
	 * The persistence unit configuration is configured for Fluid by specifying
	 * following properties:<br>
	 *		&lt;property name="openjpa.EntityManagerFactory" value="sdo"/&gt;
	 *		&lt;property name="openjpa.MetaDataFactory" value="sdo(Resources=META-INF/a.xsd;META-INF/b.xsd)"/&gt;
	 * <br>
	 * where <code>a.xsd</code>, <code>b.xsd</code> are the XML Schema 
	 * Definition for SDO Types.
	 * 
	 * @param unitName name of a JPA Persistence Unit. 
	 * The unit name must match one of the unit names specified therein.
	 * 
	 */
	public FluidDASFactory(String unitName) {
		try {
			emf = (SDOEntityManagerFactory)Persistence
			.createEntityManagerFactory(unitName);
		} catch (ClassCastException ex) {
			throw new RuntimeException(_loc.get("not-fluid").toString());
		}
	}
	
	private DAS createDAS() {
		return new FluidDAS((SDOEntityManager)emf.createEntityManager());
	}
	
	/**
	 * Gets a new DAS.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public DAS createDAS(InputStream arg0) {
		return createDAS();
	}

	/**
	 * Gets a new DAS.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public DAS createDAS(Config arg0) {
		return createDAS();
	}

	/**
	 * Gets a new DAS.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public DAS createDAS(Connection arg0) {
		return createDAS();
	}

	/**
	 * Gets a new DAS.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public DAS createDAS(InputStream arg0, Connection arg1) {
		return createDAS();
	}

	/**
	 * Gets a new DAS.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public DAS createDAS(Config arg0, Connection arg1) {
		return createDAS();
	}
	
	/**
	 * Gets the underlying implementation.
	 * 
	 * <B>NOTE:</B> The input is ignored. You can pass a null. 
	 */
	public SDOEntityManagerFactory getDelegate() {
		return emf;
	}
}
