Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / BayesianNetworkJpaController.java
diff --git a/src/java/org/ufcspa/simdecs/bn/jpa/BayesianNetworkJpaController.java b/src/java/org/ufcspa/simdecs/bn/jpa/BayesianNetworkJpaController.java
new file mode 100644 (file)
index 0000000..68ec8df
--- /dev/null
@@ -0,0 +1,212 @@
+/*
+ * 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 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.BayesianNetwork;
+import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
+import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
+
+/**
+ *
+ * @author mchelem
+ */
+public class BayesianNetworkJpaController implements Serializable {
+
+    public BayesianNetworkJpaController(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(BayesianNetwork bayesianNetwork) throws RollbackFailureException, Exception {
+        if (bayesianNetwork.getNodes() == null) {
+            bayesianNetwork.setNodes(new ArrayList<Node>());
+        }
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            List<Node> attachedNodes = new ArrayList<Node>();
+            for (Node nodesNodeToAttach : bayesianNetwork.getNodes()) {
+                nodesNodeToAttach = em.getReference(nodesNodeToAttach.getClass(), nodesNodeToAttach.getId());
+                attachedNodes.add(nodesNodeToAttach);
+            }
+            bayesianNetwork.setNodes(attachedNodes);
+            em.persist(bayesianNetwork);
+            for (Node nodesNode : bayesianNetwork.getNodes()) {
+                BayesianNetwork oldBayesianNetworkOfNodesNode = nodesNode.getBayesianNetwork();
+                nodesNode.setBayesianNetwork(bayesianNetwork);
+                nodesNode = em.merge(nodesNode);
+                if (oldBayesianNetworkOfNodesNode != null) {
+                    oldBayesianNetworkOfNodesNode.getNodes().remove(nodesNode);
+                    oldBayesianNetworkOfNodesNode = em.merge(oldBayesianNetworkOfNodesNode);
+                }
+            }
+            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(BayesianNetwork bayesianNetwork) throws NonexistentEntityException, RollbackFailureException, Exception {
+        EntityManager em = null;
+        try {
+            utx.begin();
+            em = getEntityManager();
+            BayesianNetwork persistentBayesianNetwork = em.find(BayesianNetwork.class, bayesianNetwork.getId());
+            List<Node> nodesOld = persistentBayesianNetwork.getNodes();
+            List<Node> nodesNew = bayesianNetwork.getNodes();
+            List<Node> attachedNodesNew = new ArrayList<Node>();
+            for (Node nodesNewNodeToAttach : nodesNew) {
+                nodesNewNodeToAttach = em.getReference(nodesNewNodeToAttach.getClass(), nodesNewNodeToAttach.getId());
+                attachedNodesNew.add(nodesNewNodeToAttach);
+            }
+            nodesNew = attachedNodesNew;
+            bayesianNetwork.setNodes(nodesNew);
+            bayesianNetwork = em.merge(bayesianNetwork);
+            for (Node nodesOldNode : nodesOld) {
+                if (!nodesNew.contains(nodesOldNode)) {
+                    nodesOldNode.setBayesianNetwork(null);
+                    nodesOldNode = em.merge(nodesOldNode);
+                }
+            }
+            for (Node nodesNewNode : nodesNew) {
+                if (!nodesOld.contains(nodesNewNode)) {
+                    BayesianNetwork oldBayesianNetworkOfNodesNewNode = nodesNewNode.getBayesianNetwork();
+                    nodesNewNode.setBayesianNetwork(bayesianNetwork);
+                    nodesNewNode = em.merge(nodesNewNode);
+                    if (oldBayesianNetworkOfNodesNewNode != null && !oldBayesianNetworkOfNodesNewNode.equals(bayesianNetwork)) {
+                        oldBayesianNetworkOfNodesNewNode.getNodes().remove(nodesNewNode);
+                        oldBayesianNetworkOfNodesNewNode = em.merge(oldBayesianNetworkOfNodesNewNode);
+                    }
+                }
+            }
+            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 = bayesianNetwork.getId();
+                if (findBayesianNetwork(id) == null) {
+                    throw new NonexistentEntityException("The bayesianNetwork 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();
+            BayesianNetwork bayesianNetwork;
+            try {
+                bayesianNetwork = em.getReference(BayesianNetwork.class, id);
+                bayesianNetwork.getId();
+            } catch (EntityNotFoundException enfe) {
+                throw new NonexistentEntityException("The bayesianNetwork with id " + id + " no longer exists.", enfe);
+            }
+            List<Node> nodes = bayesianNetwork.getNodes();
+            for (Node nodesNode : nodes) {
+                nodesNode.setBayesianNetwork(null);
+                nodesNode = em.merge(nodesNode);
+            }
+            em.remove(bayesianNetwork);
+            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<BayesianNetwork> findBayesianNetworkEntities() {
+        return findBayesianNetworkEntities(true, -1, -1);
+    }
+
+    public List<BayesianNetwork> findBayesianNetworkEntities(int maxResults, int firstResult) {
+        return findBayesianNetworkEntities(false, maxResults, firstResult);
+    }
+
+    private List<BayesianNetwork> findBayesianNetworkEntities(boolean all, int maxResults, int firstResult) {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            cq.select(cq.from(BayesianNetwork.class));
+            Query q = em.createQuery(cq);
+            if (!all) {
+                q.setMaxResults(maxResults);
+                q.setFirstResult(firstResult);
+            }
+            return q.getResultList();
+        } finally {
+            em.close();
+        }
+    }
+
+    public BayesianNetwork findBayesianNetwork(Long id) {
+        EntityManager em = getEntityManager();
+        try {
+            return em.find(BayesianNetwork.class, id);
+        } finally {
+            em.close();
+        }
+    }
+
+    public int getBayesianNetworkCount() {
+        EntityManager em = getEntityManager();
+        try {
+            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
+            Root<BayesianNetwork> rt = cq.from(BayesianNetwork.class);
+            cq.select(em.getCriteriaBuilder().count(rt));
+            Query q = em.createQuery(cq);
+            return ((Long) q.getSingleResult()).intValue();
+        } finally {
+            em.close();
+        }
+    }
+    
+}