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


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

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
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() {
		
		List<BpelDAOConnection> conns = null;
		
		Query q = em.createQuery("SELECT x FROM BPELDAOConnectionImpl x order by x._id asc");
		
		try {
			conns = (List<BpelDAOConnection>)q.getResultList();
			
		} catch (NoResultException e) {
			return new BPELDAOConnectionImpl(new Long(1),em);
		}
		
		if ( conns.size() < 1 ) {
			return new BPELDAOConnectionImpl(new Long(1),em);
		}
		
		BPELDAOConnectionImpl conn = (BPELDAOConnectionImpl)conns.get(conns.size()-1);
		conn.setEntityManger(em);
		
		return conn;
	}
	
	public BpelDAOConnection getConnection(Long connID) {
		BPELDAOConnectionImpl conn = null;
		
		Query q = em.createQuery("SELECT x FROM BPELDAOConnectionImpl x WHERE x._id = ?1");
		q.setParameter(1, connID);
		
		try {
			conn = (BPELDAOConnectionImpl)q.getSingleResult();
			conn.setEntityManger(em);
		} catch (NoResultException e){}
		
		if ( conn == null ) {
			conn = new BPELDAOConnectionImpl(connID,em);
		}
		
		return conn;
	}
	
	public Long getConnectionId(BpelDAOConnection conn) {
		BPELDAOConnectionImpl oConn = (BPELDAOConnectionImpl)conn;
		return oConn.getID();
	}

	public void init(Properties properties) {
	}

}

@@ -488,16 +488,54 @@
 er em;%0D%0A
+%09private BPELDAOConnectionImpl conn;%0D%0A
 %09%0D%0A%09publ
@@ -660,16 +660,42 @@
  %7B%0D%0A%09%09%0D%0A
+%09%09if ( conn == null ) %7B%0D%0A%09
 %09%09List%3CB
@@ -721,34 +721,36 @@
 onns = null;%0D%0A%09%09
+%09
 %0D%0A
+%09
 %09%09Query q = em.c
@@ -820,26 +820,28 @@
 d asc%22);%0D%0A%09%09
+%09
 %0D%0A
+%09
 %09%09try %7B%0D%0A%09%09%09
@@ -836,24 +836,25 @@
 %09%09try %7B%0D%0A%09%09%09
+%09
 conns = (Lis
@@ -901,54 +901,47 @@
 %0A%09%09%09
-%0D%0A%09%09%7D catch (NoResultException e) %7B%0D%0A%09%09%09return
+%09if ( conns.size() %3C 1 ) %7B%0D%0A%09%09%09%09%09conn =
  new
@@ -987,139 +987,179 @@
 %0D%0A%09%09
-%7D%0D%0A%09%09%0D%0A%09%09if ( conns.size() %3C 1 ) %7B%0D%0A%09%09%09return new BPELDAOConnectionImpl(new Long(1),
+%09%09%7D else %7B%0D%0A%09%09%09%09%09conn = (BPELDAOConnectionImpl)conns.get(conns.size()-1);%0D%0A%09%09%09%09%09conn.setEntityManger(
 em);%0D%0A
+%09%09
 %09%09%7D%0D%0A%09%09
+%09
+%09
 %0D%0A%09%09
-BPELDAOConnectionImpl 
+%09%7D catch (NoResultException e) %7B%0D%0A%09%09%09%09
 conn = 
-(
+new 
 BPEL
@@ -1179,64 +1179,36 @@
 Impl
-)conns.get(conns.size()-1);%0D%0A%09%09conn.setEntityManger(em);
+(new Long(1),em);%0D%0A%09%09%09%7D%0D%0A%09%09%7D
 %0D%0A%09%09
@@ -1284,24 +1284,106 @@
  connID) %7B%0D%0A
+%09%09if ( conn != null && conn.getID().equals(connID) ) %7B%0D%0A%09%09%09return conn;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09BPELDAOCon
@@ -1394,17 +1394,20 @@
 ionImpl 
-c
+tmpC
 onn = nu
@@ -1544,25 +1544,28 @@
 %09%09try %7B%0D%0A%09%09%09
-c
+tmpC
 onn = (BPELD
@@ -1606,17 +1606,20 @@
 ();%0D%0A%09%09%09
-c
+tmpC
 onn.setE
@@ -1674,33 +1674,36 @@
 )%7B%7D%0D%0A%09%09%0D%0A%09%09if ( 
-c
+tmpC
 onn == null ) %7B%0D
@@ -1752,16 +1752,36 @@
 D,em);%0D%0A
+%09%09%09tmpConn = conn;%0D%0A
 %09%09%7D%0D%0A%09%09%0D
@@ -1782,33 +1782,36 @@
 %7D%0D%0A%09%09%0D%0A%09%09return 
-c
+tmpC
 onn;%0D%0A%09%7D%0D%0A%09%0D%0A%09pu

add connection variable