Adicionado classificaĆ§Ć£o do log conforme exemplo
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / BayesianNetworkJpaController.java
CommitLineData
c547eea0
MS
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.ufcspa.simdecs.bn.jpa;
6
7import java.io.Serializable;
8import javax.persistence.Query;
9import javax.persistence.EntityNotFoundException;
10import javax.persistence.criteria.CriteriaQuery;
11import javax.persistence.criteria.Root;
12import org.ufcspa.simdecs.bn.entity.Node;
13import java.util.ArrayList;
14import java.util.List;
15import javax.persistence.EntityManager;
16import javax.persistence.EntityManagerFactory;
17import javax.transaction.UserTransaction;
18import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
19import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
20import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
21
22/**
23 *
24 * @author mchelem
25 */
26public class BayesianNetworkJpaController implements Serializable {
27
28 public BayesianNetworkJpaController(UserTransaction utx, EntityManagerFactory emf) {
29 this.utx = utx;
30 this.emf = emf;
31 }
32 private UserTransaction utx = null;
33 private EntityManagerFactory emf = null;
34
35 public EntityManager getEntityManager() {
36 return emf.createEntityManager();
37 }
38
39 public void create(BayesianNetwork bayesianNetwork) throws RollbackFailureException, Exception {
40 if (bayesianNetwork.getNodes() == null) {
41 bayesianNetwork.setNodes(new ArrayList<Node>());
42 }
43 EntityManager em = null;
44 try {
45 utx.begin();
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);
51 }
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);
61 }
62 }
63 utx.commit();
64 } catch (Exception ex) {
65 try {
66 utx.rollback();
67 } catch (Exception re) {
68 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
69 }
70 throw ex;
71 } finally {
72 if (em != null) {
73 em.close();
74 }
75 }
76 }
77
78 public void edit(BayesianNetwork bayesianNetwork) throws NonexistentEntityException, RollbackFailureException, Exception {
79 EntityManager em = null;
80 try {
81 utx.begin();
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);
90 }
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);
98 }
99 }
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);
108 }
109 }
110 }
111 utx.commit();
112 } catch (Exception ex) {
113 try {
114 utx.rollback();
115 } catch (Exception re) {
116 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
117 }
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.");
123 }
124 }
125 throw ex;
126 } finally {
127 if (em != null) {
128 em.close();
129 }
130 }
131 }
132
133 public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
134 EntityManager em = null;
135 try {
136 utx.begin();
137 em = getEntityManager();
138 BayesianNetwork bayesianNetwork;
139 try {
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);
144 }
145 List<Node> nodes = bayesianNetwork.getNodes();
146 for (Node nodesNode : nodes) {
147 nodesNode.setBayesianNetwork(null);
148 nodesNode = em.merge(nodesNode);
149 }
150 em.remove(bayesianNetwork);
151 utx.commit();
152 } catch (Exception ex) {
153 try {
154 utx.rollback();
155 } catch (Exception re) {
156 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
157 }
158 throw ex;
159 } finally {
160 if (em != null) {
161 em.close();
162 }
163 }
164 }
165
166 public List<BayesianNetwork> findBayesianNetworkEntities() {
167 return findBayesianNetworkEntities(true, -1, -1);
168 }
169
170 public List<BayesianNetwork> findBayesianNetworkEntities(int maxResults, int firstResult) {
171 return findBayesianNetworkEntities(false, maxResults, firstResult);
172 }
173
174 private List<BayesianNetwork> findBayesianNetworkEntities(boolean all, int maxResults, int firstResult) {
175 EntityManager em = getEntityManager();
176 try {
177 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
178 cq.select(cq.from(BayesianNetwork.class));
179 Query q = em.createQuery(cq);
180 if (!all) {
181 q.setMaxResults(maxResults);
182 q.setFirstResult(firstResult);
183 }
184 return q.getResultList();
185 } finally {
186 em.close();
187 }
188 }
189
190 public BayesianNetwork findBayesianNetwork(Long id) {
191 EntityManager em = getEntityManager();
192 try {
193 return em.find(BayesianNetwork.class, id);
194 } finally {
195 em.close();
196 }
197 }
198
199 public int getBayesianNetworkCount() {
200 EntityManager em = getEntityManager();
201 try {
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();
207 } finally {
208 em.close();
209 }
210 }
211
212}