# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
# stuff for JPA
from java.util.logging import Level
from java.util.logging import Logger
from javax.persistence import Persistence

# stuff for ISAM
from dk.vajhoej.isam import Key0
from dk.vajhoej.isam.local import LocalIsamSource

#input class
import AddressISAM
#output class
import AddressJPA
# auto map
import AutoMap

# open JPA
Logger.getLogger("org.hibernate").setLevel(Level.OFF)
emf = Persistence.createEntityManagerFactory("Address")
em = emf.createEntityManager()

# open ISAM
db = LocalIsamSource('address.isq', 'dk.vajhoej.vms.rms.IndexSequential', False)

# process all records
mapper = AutoMap(AddressISAM, AddressJPA, True)
it = db.readGE(AddressISAM, Key0(0))
em.getTransaction().begin()
while it.read():
    oldrec = it.current()
    newrec = AddressJPA()
    mapper.convert(oldrec, newrec)
    em.persist(newrec)
it.close()
em.getTransaction().commit()

# close ISAM
db.close()

# close JPA
em.close()
emf.close()
