--- /dev/null
+/*
+ * 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.BayesianNetwork;
+import org.ufcspa.simdecs.bn.entity.Question;
+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.Node;
+import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
+import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
+
+/**
+ *
+ * @author mchelem
+ */
+public class NodeJpaController implements Serializable {
+
+ public NodeJpaController(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(Node node) throws RollbackFailureException, Exception {
+ if (node.getQuestions() == null) {
+ node.setQuestions(new ArrayList<Question>());
+ }
+ EntityManager em = null;
+ try {
+ utx.begin();
+ em = getEntityManager();
+ BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
+ if (bayesianNetwork != null) {
+ bayesianNetwork = em.getReference(bayesianNetwork.getClass(), bayesianNetwork.getId());
+ node.setBayesianNetwork(bayesianNetwork);
+ }
+ List<Question> attachedQuestions = new ArrayList<Question>();
+ for (Question questionsQuestionToAttach : node.getQuestions()) {
+ questionsQuestionToAttach = em.getReference(questionsQuestionToAttach.getClass(), questionsQuestionToAttach.getId());
+ attachedQuestions.add(questionsQuestionToAttach);
+ }
+ node.setQuestions(attachedQuestions);
+ em.persist(node);
+ if (bayesianNetwork != null) {
+ bayesianNetwork.getNodes().add(node);
+ bayesianNetwork = em.merge(bayesianNetwork);
+ }
+ for (Question questionsQuestion : node.getQuestions()) {
+ Node oldNodeOfQuestionsQuestion = questionsQuestion.getNode();
+ questionsQuestion.setNode(node);
+ questionsQuestion = em.merge(questionsQuestion);
+ if (oldNodeOfQuestionsQuestion != null) {
+ oldNodeOfQuestionsQuestion.getQuestions().remove(questionsQuestion);
+ oldNodeOfQuestionsQuestion = em.merge(oldNodeOfQuestionsQuestion);
+ }
+ }
+ 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(Node node) throws NonexistentEntityException, RollbackFailureException, Exception {
+ EntityManager em = null;
+ try {
+ utx.begin();
+ em = getEntityManager();
+ Node persistentNode = em.find(Node.class, node.getId());
+ BayesianNetwork bayesianNetworkOld = persistentNode.getBayesianNetwork();
+ BayesianNetwork bayesianNetworkNew = node.getBayesianNetwork();
+ List<Question> questionsOld = persistentNode.getQuestions();
+ List<Question> questionsNew = node.getQuestions();
+ if (bayesianNetworkNew != null) {
+ bayesianNetworkNew = em.getReference(bayesianNetworkNew.getClass(), bayesianNetworkNew.getId());
+ node.setBayesianNetwork(bayesianNetworkNew);
+ }
+ List<Question> attachedQuestionsNew = new ArrayList<Question>();
+ for (Question questionsNewQuestionToAttach : questionsNew) {
+ questionsNewQuestionToAttach = em.getReference(questionsNewQuestionToAttach.getClass(), questionsNewQuestionToAttach.getId());
+ attachedQuestionsNew.add(questionsNewQuestionToAttach);
+ }
+ questionsNew = attachedQuestionsNew;
+ node.setQuestions(questionsNew);
+ node = em.merge(node);
+ if (bayesianNetworkOld != null && !bayesianNetworkOld.equals(bayesianNetworkNew)) {
+ bayesianNetworkOld.getNodes().remove(node);
+ bayesianNetworkOld = em.merge(bayesianNetworkOld);
+ }
+ if (bayesianNetworkNew != null && !bayesianNetworkNew.equals(bayesianNetworkOld)) {
+ bayesianNetworkNew.getNodes().add(node);
+ bayesianNetworkNew = em.merge(bayesianNetworkNew);
+ }
+ for (Question questionsOldQuestion : questionsOld) {
+ if (!questionsNew.contains(questionsOldQuestion)) {
+ questionsOldQuestion.setNode(null);
+ questionsOldQuestion = em.merge(questionsOldQuestion);
+ }
+ }
+ for (Question questionsNewQuestion : questionsNew) {
+ if (!questionsOld.contains(questionsNewQuestion)) {
+ Node oldNodeOfQuestionsNewQuestion = questionsNewQuestion.getNode();
+ questionsNewQuestion.setNode(node);
+ questionsNewQuestion = em.merge(questionsNewQuestion);
+ if (oldNodeOfQuestionsNewQuestion != null && !oldNodeOfQuestionsNewQuestion.equals(node)) {
+ oldNodeOfQuestionsNewQuestion.getQuestions().remove(questionsNewQuestion);
+ oldNodeOfQuestionsNewQuestion = em.merge(oldNodeOfQuestionsNewQuestion);
+ }
+ }
+ }
+ 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 = node.getId();
+ if (findNode(id) == null) {
+ throw new NonexistentEntityException("The node 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();
+ Node node;
+ try {
+ node = em.getReference(Node.class, id);
+ node.getId();
+ } catch (EntityNotFoundException enfe) {
+ throw new NonexistentEntityException("The node with id " + id + " no longer exists.", enfe);
+ }
+ BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
+ if (bayesianNetwork != null) {
+ bayesianNetwork.getNodes().remove(node);
+ bayesianNetwork = em.merge(bayesianNetwork);
+ }
+ List<Question> questions = node.getQuestions();
+ for (Question questionsQuestion : questions) {
+ questionsQuestion.setNode(null);
+ questionsQuestion = em.merge(questionsQuestion);
+ }
+ em.remove(node);
+ 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<Node> findNodeEntities() {
+ return findNodeEntities(true, -1, -1);
+ }
+
+ public List<Node> findNodeEntities(int maxResults, int firstResult) {
+ return findNodeEntities(false, maxResults, firstResult);
+ }
+
+ private List<Node> findNodeEntities(boolean all, int maxResults, int firstResult) {
+ EntityManager em = getEntityManager();
+ try {
+ CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+ cq.select(cq.from(Node.class));
+ Query q = em.createQuery(cq);
+ if (!all) {
+ q.setMaxResults(maxResults);
+ q.setFirstResult(firstResult);
+ }
+ return q.getResultList();
+ } finally {
+ em.close();
+ }
+ }
+
+ public Node findNode(Long id) {
+ EntityManager em = getEntityManager();
+ try {
+ return em.find(Node.class, id);
+ } finally {
+ em.close();
+ }
+ }
+
+ public int getNodeCount() {
+ EntityManager em = getEntityManager();
+ try {
+ CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+ Root<Node> rt = cq.from(Node.class);
+ cq.select(em.getCriteriaBuilder().count(rt));
+ Query q = em.createQuery(cq);
+ return ((Long) q.getSingleResult()).intValue();
+ } finally {
+ em.close();
+ }
+ }
+
+}