2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.ufcspa.simdecs.bn.jpa;
7 import java.io.Serializable;
8 import javax.persistence.Query;
9 import javax.persistence.EntityNotFoundException;
10 import javax.persistence.criteria.CriteriaQuery;
11 import javax.persistence.criteria.Root;
12 import org.ufcspa.simdecs.bn.entity.Node;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.persistence.EntityManager;
16 import javax.persistence.EntityManagerFactory;
17 import javax.transaction.UserTransaction;
18 import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
19 import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
20 import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
26 public class BayesianNetworkJpaController implements Serializable {
28 public BayesianNetworkJpaController(UserTransaction utx, EntityManagerFactory emf) {
32 private UserTransaction utx = null;
33 private EntityManagerFactory emf = null;
35 public EntityManager getEntityManager() {
36 return emf.createEntityManager();
39 public void create(BayesianNetwork bayesianNetwork) throws RollbackFailureException, Exception {
40 if (bayesianNetwork.getNodes() == null) {
41 bayesianNetwork.setNodes(new ArrayList<Node>());
43 EntityManager em = null;
46 em = getEntityManager();
47 List<Node> attachedNodes = new ArrayList<Node>();
48 for (Node nodesNodeToAttach : bayesianNetwork.getNodes()) {
49 nodesNodeToAttach = em.getReference(nodesNodeToAttach.getClass(), nodesNodeToAttach.getId());
50 attachedNodes.add(nodesNodeToAttach);
52 bayesianNetwork.setNodes(attachedNodes);
53 em.persist(bayesianNetwork);
54 for (Node nodesNode : bayesianNetwork.getNodes()) {
55 BayesianNetwork oldBayesianNetworkOfNodesNode = nodesNode.getBayesianNetwork();
56 nodesNode.setBayesianNetwork(bayesianNetwork);
57 nodesNode = em.merge(nodesNode);
58 if (oldBayesianNetworkOfNodesNode != null) {
59 oldBayesianNetworkOfNodesNode.getNodes().remove(nodesNode);
60 oldBayesianNetworkOfNodesNode = em.merge(oldBayesianNetworkOfNodesNode);
64 } catch (Exception ex) {
67 } catch (Exception re) {
68 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
78 public void edit(BayesianNetwork bayesianNetwork) throws NonexistentEntityException, RollbackFailureException, Exception {
79 EntityManager em = null;
82 em = getEntityManager();
83 BayesianNetwork persistentBayesianNetwork = em.find(BayesianNetwork.class, bayesianNetwork.getId());
84 List<Node> nodesOld = persistentBayesianNetwork.getNodes();
85 List<Node> nodesNew = bayesianNetwork.getNodes();
86 List<Node> attachedNodesNew = new ArrayList<Node>();
87 for (Node nodesNewNodeToAttach : nodesNew) {
88 nodesNewNodeToAttach = em.getReference(nodesNewNodeToAttach.getClass(), nodesNewNodeToAttach.getId());
89 attachedNodesNew.add(nodesNewNodeToAttach);
91 nodesNew = attachedNodesNew;
92 bayesianNetwork.setNodes(nodesNew);
93 bayesianNetwork = em.merge(bayesianNetwork);
94 for (Node nodesOldNode : nodesOld) {
95 if (!nodesNew.contains(nodesOldNode)) {
96 nodesOldNode.setBayesianNetwork(null);
97 nodesOldNode = em.merge(nodesOldNode);
100 for (Node nodesNewNode : nodesNew) {
101 if (!nodesOld.contains(nodesNewNode)) {
102 BayesianNetwork oldBayesianNetworkOfNodesNewNode = nodesNewNode.getBayesianNetwork();
103 nodesNewNode.setBayesianNetwork(bayesianNetwork);
104 nodesNewNode = em.merge(nodesNewNode);
105 if (oldBayesianNetworkOfNodesNewNode != null && !oldBayesianNetworkOfNodesNewNode.equals(bayesianNetwork)) {
106 oldBayesianNetworkOfNodesNewNode.getNodes().remove(nodesNewNode);
107 oldBayesianNetworkOfNodesNewNode = em.merge(oldBayesianNetworkOfNodesNewNode);
112 } catch (Exception ex) {
115 } catch (Exception re) {
116 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
118 String msg = ex.getLocalizedMessage();
119 if (msg == null || msg.length() == 0) {
120 Long id = bayesianNetwork.getId();
121 if (findBayesianNetwork(id) == null) {
122 throw new NonexistentEntityException("The bayesianNetwork with id " + id + " no longer exists.");
133 public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
134 EntityManager em = null;
137 em = getEntityManager();
138 BayesianNetwork bayesianNetwork;
140 bayesianNetwork = em.getReference(BayesianNetwork.class, id);
141 bayesianNetwork.getId();
142 } catch (EntityNotFoundException enfe) {
143 throw new NonexistentEntityException("The bayesianNetwork with id " + id + " no longer exists.", enfe);
145 List<Node> nodes = bayesianNetwork.getNodes();
146 for (Node nodesNode : nodes) {
147 nodesNode.setBayesianNetwork(null);
148 nodesNode = em.merge(nodesNode);
150 em.remove(bayesianNetwork);
152 } catch (Exception ex) {
155 } catch (Exception re) {
156 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
166 public List<BayesianNetwork> findBayesianNetworkEntities() {
167 return findBayesianNetworkEntities(true, -1, -1);
170 public List<BayesianNetwork> findBayesianNetworkEntities(int maxResults, int firstResult) {
171 return findBayesianNetworkEntities(false, maxResults, firstResult);
174 private List<BayesianNetwork> findBayesianNetworkEntities(boolean all, int maxResults, int firstResult) {
175 EntityManager em = getEntityManager();
177 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
178 cq.select(cq.from(BayesianNetwork.class));
179 Query q = em.createQuery(cq);
181 q.setMaxResults(maxResults);
182 q.setFirstResult(firstResult);
184 return q.getResultList();
190 public BayesianNetwork findBayesianNetwork(Long id) {
191 EntityManager em = getEntityManager();
193 return em.find(BayesianNetwork.class, id);
199 public int getBayesianNetworkCount() {
200 EntityManager em = getEntityManager();
202 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
203 Root<BayesianNetwork> rt = cq.from(BayesianNetwork.class);
204 cq.select(em.getCriteriaBuilder().count(rt));
205 Query q = em.createQuery(cq);
206 return ((Long) q.getSingleResult()).intValue();