Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / AnswerJpaController.java
diff --git a/src/java/org/ufcspa/simdecs/bn/jpa/AnswerJpaController.java b/src/java/org/ufcspa/simdecs/bn/jpa/AnswerJpaController.java
new file mode 100644 (file)
index 0000000..4dd2bcb
--- /dev/null
@@ -0,0 +1,190 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.ufcspa.simdecs.bn.jpa;
+
+import java.io.Serializable;
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Query;
+import javax.persistence.EntityNotFoundException;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import javax.transaction.UserTransaction;
+import org.ufcspa.simdecs.bn.entity.Answer;
+import org.ufcspa.simdecs.bn.entity.Question;
+import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
+import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
+
+/**
+ *
+ * @author mchelem
+ */
+public class AnswerJpaController implements Serializable {
+
+    public AnswerJpaController(UserTransaction utx, EntityManagerFactory emf) {
+        this.utx = utx;
+        this.emf = emf;
+    }
+    private UserTransaction utx = null;
+    private EntityManagerFactory emf = null;
+
+    public EntityManager getEntityManager() {
+        return emf.createEntityManager();
+    }
+
+    public void create(Answer answer) throws RollbackFailureException, Exception {
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            Question question = answer.getQuestion();
+            if (question != null) {
+                question = em.getReference(question.getClass(), question.getId());
+                answer.setQuestion(question);
+            }
+            em.persist(answer);
+            if (question != null) {
+                question.getAnswers().add(answer);
+                question = em.merge(question);
+            }
+            utx.commit();
+        } catch (Exception ex) {
+            try {
+                utx.rollback();
+            } catch (Exception re) {
+                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
+            }
+            throw ex;
+        } finally {
+            if (em != null) {
+                em.close();
+            }
+        }
+    }
+
+    public void edit(Answer answer) throws NonexistentEntityException, RollbackFailureException, Exception {
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            Answer persistentAnswer = em.find(Answer.class, answer.getId());
+            Question questionOld = persistentAnswer.getQuestion();
+            Question questionNew = answer.getQuestion();
+            if (questionNew != null) {
+                questionNew = em.getReference(questionNew.getClass(), questionNew.getId());
+                answer.setQuestion(questionNew);
+            }
+            answer = em.merge(answer);
+            if (questionOld != null && !questionOld.equals(questionNew)) {
+                questionOld.getAnswers().remove(answer);
+                questionOld = em.merge(questionOld);
+            }
+            if (questionNew != null && !questionNew.equals(questionOld)) {
+                questionNew.getAnswers().add(answer);
+                questionNew = em.merge(questionNew);
+            }
+            utx.commit();
+        } catch (Exception ex) {
+            try {
+                utx.rollback();
+            } catch (Exception re) {
+                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
+            }
+            String msg = ex.getLocalizedMessage();
+            if (msg == null || msg.length() == 0) {
+                Long id = answer.getId();
+                if (findAnswer(id) == null) {
+                    throw new NonexistentEntityException("The answer with id " + id + " no longer exists.");
+                }
+            }
+            throw ex;
+        } finally {
+            if (em != null) {
+                em.close();
+            }
+        }
+    }
+
+    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            Answer answer;
+            try {
+                answer = em.getReference(Answer.class, id);
+                answer.getId();
+            } catch (EntityNotFoundException enfe) {
+                throw new NonexistentEntityException("The answer with id " + id + " no longer exists.", enfe);
+            }
+            Question question = answer.getQuestion();
+            if (question != null) {
+                question.getAnswers().remove(answer);
+                question = em.merge(question);
+            }
+            em.remove(answer);
+            utx.commit();
+        } catch (Exception ex) {
+            try {
+                utx.rollback();
+            } catch (Exception re) {
+                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
+            }
+            throw ex;
+        } finally {
+            if (em != null) {
+                em.close();
+            }
+        }
+    }
+
+    public List<Answer> findAnswerEntities() {
+        return findAnswerEntities(true, -1, -1);
+    }
+
+    public List<Answer> findAnswerEntities(int maxResults, int firstResult) {
+        return findAnswerEntities(false, maxResults, firstResult);
+    }
+
+    private List<Answer> findAnswerEntities(boolean all, int maxResults, int firstResult) {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            cq.select(cq.from(Answer.class));
+            Query q = em.createQuery(cq);
+            if (!all) {
+                q.setMaxResults(maxResults);
+                q.setFirstResult(firstResult);
+            }
+            return q.getResultList();
+        } finally {
+            em.close();
+        }
+    }
+
+    public Answer findAnswer(Long id) {
+        EntityManager em = getEntityManager();
+        try {
+            return em.find(Answer.class, id);
+        } finally {
+            em.close();
+        }
+    }
+
+    public int getAnswerCount() {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            Root<Answer> rt = cq.from(Answer.class);
+            cq.select(em.getCriteriaBuilder().count(rt));
+            Query q = em.createQuery(cq);
+            return ((Long) q.getSingleResult()).intValue();
+        } finally {
+            em.close();
+        }
+    }
+    
+}