package org.apache.ode.dao.jpa.ojpa;


import java.util.List;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.ode.bpel.dao.BpelDAOConnection;
import org.apache.ode.bpel.dao.BpelDAOConnectionFactory;
import org.apache.ode.dao.jpa.BPELDAOConnectionImpl;

public class BPELDAOConnectionFactoryImpl implements BpelDAOConnectionFactory {

	private EntityManager em;
	
	public BPELDAOConnectionFactoryImpl(EntityManager em) {
		this.em = em;
	}
	
	public BpelDAOConnection getConnection() {
		
		Query q = em.createQuery("SELECT x FROM BPELDAOConnectionImpl x order by x._id asc");
		
		List<BpelDAOConnection> conns = (List<BpelDAOConnection>)q.getResultList();
		
		if ( conns.size() < 1 ) {
			return new BPELDAOConnectionImpl(new Long(1));
		}
		
		return conns.get(conns.size()-1);
	}
	
	public BpelDAOConnection getConnection(Long connID) {
		
		Query q = em.createQuery("SELECT x FROM BPELDAOConnectionImpl x WHERE x._id = ?1");
		q.setParameter(1, connID);
		
		BpelDAOConnection conn = (BpelDAOConnection)q.getSingleResult();
		
		if ( conn == null ) {
			conn = new BPELDAOConnectionImpl(connID);
		}
		
		return conn;
	}
	
	public Long getConnectionId(BpelDAOConnection conn) {
		BPELDAOConnectionImpl oConn = (BPELDAOConnectionImpl)conn;
		return oConn.getID();
	}

	public void init(Properties properties) {
	}

}

@@ -132,16 +132,61 @@
 nager;%0D%0A
+import javax.persistence.NoResultException;%0D%0A
 import j
@@ -210,16 +210,16 @@
 Query;%0D%0A
-
 %0D%0Aimport
@@ -614,32 +614,77 @@
 ection() %7B%0D%0A%09%09%0D%0A
+%09%09List%3CBpelDAOConnection%3E conns = null;%0D%0A%09%09%0D%0A
 %09%09Query q = em.c
@@ -766,32 +766,18 @@
 %0D%0A%09%09
-List%3CBpelDAOConnection%3E 
+try %7B%0D%0A%09%09%09
 conn
@@ -821,24 +821,115 @@
 ultList();%0D%0A
+%09%09%7D catch (NoResultException e) %7B%0D%0A%09%09%09return new BPELDAOConnectionImpl(new Long(1));%0D%0A%09%09%7D%0D%0A
 %09%09%0D%0A%09%09if ( c
@@ -1105,24 +1105,58 @@
  connID) %7B%0D%0A
+%09%09BpelDAOConnection conn = null;%0D%0A
 %09%09%0D%0A%09%09Query 
@@ -1270,34 +1270,26 @@
 %0D%0A%09%09%0D%0A%09%09
-BpelDAOConnection 
+try %7B%0D%0A%09%09%09
 conn = (
@@ -1324,24 +1324,59 @@
 eResult();%0D%0A
+%09%09%7D catch (NoResultException e)%7B%7D%0D%0A
 %09%09%0D%0A%09%09if ( c

Add new getConnection request