Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / QuestionJpaController.java
diff --git a/src/java/org/ufcspa/simdecs/bn/jpa/QuestionJpaController.java b/src/java/org/ufcspa/simdecs/bn/jpa/QuestionJpaController.java
new file mode 100644 (file)
index 0000000..6d649ca
--- /dev/null
@@ -0,0 +1,241 @@
+/*
+ * 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 javax.persistence.Query;
+import javax.persistence.EntityNotFoundException;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import org.ufcspa.simdecs.bn.entity.Node;
+import org.ufcspa.simdecs.bn.entity.Answer;
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.transaction.UserTransaction;
+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 QuestionJpaController implements Serializable {
+
+    public QuestionJpaController(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(Question question) throws RollbackFailureException, Exception {
+        if (question.getAnswers() == null) {
+            question.setAnswers(new ArrayList<Answer>());
+        }
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            Node node = question.getNode();
+            if (node != null) {
+                node = em.getReference(node.getClass(), node.getId());
+                question.setNode(node);
+            }
+            List<Answer> attachedAnswers = new ArrayList<Answer>();
+            for (Answer answersAnswerToAttach : question.getAnswers()) {
+                answersAnswerToAttach = em.getReference(answersAnswerToAttach.getClass(), answersAnswerToAttach.getId());
+                attachedAnswers.add(answersAnswerToAttach);
+            }
+            question.setAnswers(attachedAnswers);
+            em.persist(question);
+            if (node != null) {
+                node.getQuestions().add(question);
+                node = em.merge(node);
+            }
+            for (Answer answersAnswer : question.getAnswers()) {
+                Question oldQuestionOfAnswersAnswer = answersAnswer.getQuestion();
+                answersAnswer.setQuestion(question);
+                answersAnswer = em.merge(answersAnswer);
+                if (oldQuestionOfAnswersAnswer != null) {
+                    oldQuestionOfAnswersAnswer.getAnswers().remove(answersAnswer);
+                    oldQuestionOfAnswersAnswer = em.merge(oldQuestionOfAnswersAnswer);
+                }
+            }
+            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(Question question) throws NonexistentEntityException, RollbackFailureException, Exception {
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            Question persistentQuestion = em.find(Question.class, question.getId());
+            Node nodeOld = persistentQuestion.getNode();
+            Node nodeNew = question.getNode();
+            List<Answer> answersOld = persistentQuestion.getAnswers();
+            List<Answer> answersNew = question.getAnswers();
+            if (nodeNew != null) {
+                nodeNew = em.getReference(nodeNew.getClass(), nodeNew.getId());
+                question.setNode(nodeNew);
+            }
+            List<Answer> attachedAnswersNew = new ArrayList<Answer>();
+            for (Answer answersNewAnswerToAttach : answersNew) {
+                answersNewAnswerToAttach = em.getReference(answersNewAnswerToAttach.getClass(), answersNewAnswerToAttach.getId());
+                attachedAnswersNew.add(answersNewAnswerToAttach);
+            }
+            answersNew = attachedAnswersNew;
+            question.setAnswers(answersNew);
+            question = em.merge(question);
+            if (nodeOld != null && !nodeOld.equals(nodeNew)) {
+                nodeOld.getQuestions().remove(question);
+                nodeOld = em.merge(nodeOld);
+            }
+            if (nodeNew != null && !nodeNew.equals(nodeOld)) {
+                nodeNew.getQuestions().add(question);
+                nodeNew = em.merge(nodeNew);
+            }
+            for (Answer answersOldAnswer : answersOld) {
+                if (!answersNew.contains(answersOldAnswer)) {
+                    answersOldAnswer.setQuestion(null);
+                    answersOldAnswer = em.merge(answersOldAnswer);
+                }
+            }
+            for (Answer answersNewAnswer : answersNew) {
+                if (!answersOld.contains(answersNewAnswer)) {
+                    Question oldQuestionOfAnswersNewAnswer = answersNewAnswer.getQuestion();
+                    answersNewAnswer.setQuestion(question);
+                    answersNewAnswer = em.merge(answersNewAnswer);
+                    if (oldQuestionOfAnswersNewAnswer != null && !oldQuestionOfAnswersNewAnswer.equals(question)) {
+                        oldQuestionOfAnswersNewAnswer.getAnswers().remove(answersNewAnswer);
+                        oldQuestionOfAnswersNewAnswer = em.merge(oldQuestionOfAnswersNewAnswer);
+                    }
+                }
+            }
+            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 = question.getId();
+                if (findQuestion(id) == null) {
+                    throw new NonexistentEntityException("The question 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();
+            Question question;
+            try {
+                question = em.getReference(Question.class, id);
+                question.getId();
+            } catch (EntityNotFoundException enfe) {
+                throw new NonexistentEntityException("The question with id " + id + " no longer exists.", enfe);
+            }
+            Node node = question.getNode();
+            if (node != null) {
+                node.getQuestions().remove(question);
+                node = em.merge(node);
+            }
+            List<Answer> answers = question.getAnswers();
+            for (Answer answersAnswer : answers) {
+                answersAnswer.setQuestion(null);
+                answersAnswer = em.merge(answersAnswer);
+            }
+            em.remove(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 List<Question> findQuestionEntities() {
+        return findQuestionEntities(true, -1, -1);
+    }
+
+    public List<Question> findQuestionEntities(int maxResults, int firstResult) {
+        return findQuestionEntities(false, maxResults, firstResult);
+    }
+
+    private List<Question> findQuestionEntities(boolean all, int maxResults, int firstResult) {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            cq.select(cq.from(Question.class));
+            Query q = em.createQuery(cq);
+            if (!all) {
+                q.setMaxResults(maxResults);
+                q.setFirstResult(firstResult);
+            }
+            return q.getResultList();
+        } finally {
+            em.close();
+        }
+    }
+
+    public Question findQuestion(Long id) {
+        EntityManager em = getEntityManager();
+        try {
+            return em.find(Question.class, id);
+        } finally {
+            em.close();
+        }
+    }
+
+    public int getQuestionCount() {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            Root<Question> rt = cq.from(Question.class);
+            cq.select(em.getCriteriaBuilder().count(rt));
+            Query q = em.createQuery(cq);
+            return ((Long) q.getSingleResult()).intValue();
+        } finally {
+            em.close();
+        }
+    }
+    
+}