Mercurial > hg > IndexMetaContextualizer
diff src/main/java/de/mpiwg/indexmeta/services/PersistentService.java @ 7:bc57f2660b0f
implementation of web service
author | Jorge Urzua <jurzua@mpiwg-berlin.mpg.de> |
---|---|
date | Fri, 12 Apr 2013 17:48:42 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/indexmeta/services/PersistentService.java Fri Apr 12 17:48:42 2013 +0200 @@ -0,0 +1,71 @@ +package de.mpiwg.indexmeta.services; + +import java.util.List; + +import javax.persistence.Query; + +import de.mpiwg.indexmeta.bo.Contextualization; +import de.mpiwg.indexmeta.utils.QuadMap; + +public class PersistentService { + + private QuadMap<Contextualization> ctxMap = null; + private javax.persistence.EntityManagerFactory emf; + private javax.persistence.EntityManager em; + private String PERSISTENCE_UNIT_NAME = "uno"; + + + public QuadMap<Contextualization> getCtxMap() { + if(ctxMap == null){ + this.loadCtxMap(); + } + return ctxMap; + } + + public void saveCtx(Contextualization ctx){ + System.out.println("Saving= " + ctx.toString()); + + initEntityManager(); + em.getTransaction().begin(); + + em.persist(ctx); + ctxMap.put(ctx.getKey(), ctx); + + em.getTransaction().commit(); + closeEntityManager(); + + System.out.println("Saved= " + ctx.toString()); + } + + + private void loadCtxMap(){ + ctxMap = new QuadMap<Contextualization>(); + + initEntityManager(); + em.getTransaction().begin(); + + Query q = em.createQuery("from Contextualization"); + List<Contextualization> list = q.getResultList(); + + for(Contextualization ctx : list){ + ctxMap.put(ctx.getKey(), ctx); + } + + em.getTransaction().commit(); + closeEntityManager(); + } + + + private void initEntityManager() { + emf = javax.persistence.Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); + em = emf.createEntityManager(); + } + + private void closeEntityManager() { + em.close(); + emf.close(); + } + + + +}