Adicionado classificaĆ§Ć£o do log conforme exemplo
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / NodeJpaController.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.ufcspa.simdecs.bn.jpa;
6
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.BayesianNetwork;
13 import org.ufcspa.simdecs.bn.entity.Question;
14 import java.util.ArrayList;
15 import java.util.List;
16 import javax.persistence.EntityManager;
17 import javax.persistence.EntityManagerFactory;
18 import javax.transaction.UserTransaction;
19 import org.ufcspa.simdecs.bn.entity.Node;
20 import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
21 import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
22
23 /**
24  *
25  * @author mchelem
26  */
27 public class NodeJpaController implements Serializable {
28
29     public NodeJpaController(UserTransaction utx, EntityManagerFactory emf) {
30         this.utx = utx;
31         this.emf = emf;
32     }
33     private UserTransaction utx = null;
34     private EntityManagerFactory emf = null;
35
36     public EntityManager getEntityManager() {
37         return emf.createEntityManager();
38     }
39
40     public void create(Node node) throws RollbackFailureException, Exception {
41         if (node.getQuestions() == null) {
42             node.setQuestions(new ArrayList<Question>());
43         }
44         EntityManager em = null;
45         try {
46             utx.begin();
47             em = getEntityManager();
48             BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
49             if (bayesianNetwork != null) {
50                 bayesianNetwork = em.getReference(bayesianNetwork.getClass(), bayesianNetwork.getId());
51                 node.setBayesianNetwork(bayesianNetwork);
52             }
53             List<Question> attachedQuestions = new ArrayList<Question>();
54             for (Question questionsQuestionToAttach : node.getQuestions()) {
55                 questionsQuestionToAttach = em.getReference(questionsQuestionToAttach.getClass(), questionsQuestionToAttach.getId());
56                 attachedQuestions.add(questionsQuestionToAttach);
57             }
58             node.setQuestions(attachedQuestions);
59             em.persist(node);
60             if (bayesianNetwork != null) {
61                 bayesianNetwork.getNodes().add(node);
62                 bayesianNetwork = em.merge(bayesianNetwork);
63             }
64             for (Question questionsQuestion : node.getQuestions()) {
65                 Node oldNodeOfQuestionsQuestion = questionsQuestion.getNode();
66                 questionsQuestion.setNode(node);
67                 questionsQuestion = em.merge(questionsQuestion);
68                 if (oldNodeOfQuestionsQuestion != null) {
69                     oldNodeOfQuestionsQuestion.getQuestions().remove(questionsQuestion);
70                     oldNodeOfQuestionsQuestion = em.merge(oldNodeOfQuestionsQuestion);
71                 }
72             }
73             utx.commit();
74         } catch (Exception ex) {
75             try {
76                 utx.rollback();
77             } catch (Exception re) {
78                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
79             }
80             throw ex;
81         } finally {
82             if (em != null) {
83                 em.close();
84             }
85         }
86     }
87
88     public void edit(Node node) throws NonexistentEntityException, RollbackFailureException, Exception {
89         EntityManager em = null;
90         try {
91             utx.begin();
92             em = getEntityManager();
93             Node persistentNode = em.find(Node.class, node.getId());
94             BayesianNetwork bayesianNetworkOld = persistentNode.getBayesianNetwork();
95             BayesianNetwork bayesianNetworkNew = node.getBayesianNetwork();
96             List<Question> questionsOld = persistentNode.getQuestions();
97             List<Question> questionsNew = node.getQuestions();
98             if (bayesianNetworkNew != null) {
99                 bayesianNetworkNew = em.getReference(bayesianNetworkNew.getClass(), bayesianNetworkNew.getId());
100                 node.setBayesianNetwork(bayesianNetworkNew);
101             }
102             List<Question> attachedQuestionsNew = new ArrayList<Question>();
103             for (Question questionsNewQuestionToAttach : questionsNew) {
104                 questionsNewQuestionToAttach = em.getReference(questionsNewQuestionToAttach.getClass(), questionsNewQuestionToAttach.getId());
105                 attachedQuestionsNew.add(questionsNewQuestionToAttach);
106             }
107             questionsNew = attachedQuestionsNew;
108             node.setQuestions(questionsNew);
109             node = em.merge(node);
110             if (bayesianNetworkOld != null && !bayesianNetworkOld.equals(bayesianNetworkNew)) {
111                 bayesianNetworkOld.getNodes().remove(node);
112                 bayesianNetworkOld = em.merge(bayesianNetworkOld);
113             }
114             if (bayesianNetworkNew != null && !bayesianNetworkNew.equals(bayesianNetworkOld)) {
115                 bayesianNetworkNew.getNodes().add(node);
116                 bayesianNetworkNew = em.merge(bayesianNetworkNew);
117             }
118             for (Question questionsOldQuestion : questionsOld) {
119                 if (!questionsNew.contains(questionsOldQuestion)) {
120                     questionsOldQuestion.setNode(null);
121                     questionsOldQuestion = em.merge(questionsOldQuestion);
122                 }
123             }
124             for (Question questionsNewQuestion : questionsNew) {
125                 if (!questionsOld.contains(questionsNewQuestion)) {
126                     Node oldNodeOfQuestionsNewQuestion = questionsNewQuestion.getNode();
127                     questionsNewQuestion.setNode(node);
128                     questionsNewQuestion = em.merge(questionsNewQuestion);
129                     if (oldNodeOfQuestionsNewQuestion != null && !oldNodeOfQuestionsNewQuestion.equals(node)) {
130                         oldNodeOfQuestionsNewQuestion.getQuestions().remove(questionsNewQuestion);
131                         oldNodeOfQuestionsNewQuestion = em.merge(oldNodeOfQuestionsNewQuestion);
132                     }
133                 }
134             }
135             utx.commit();
136         } catch (Exception ex) {
137             try {
138                 utx.rollback();
139             } catch (Exception re) {
140                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
141             }
142             String msg = ex.getLocalizedMessage();
143             if (msg == null || msg.length() == 0) {
144                 Long id = node.getId();
145                 if (findNode(id) == null) {
146                     throw new NonexistentEntityException("The node with id " + id + " no longer exists.");
147                 }
148             }
149             throw ex;
150         } finally {
151             if (em != null) {
152                 em.close();
153             }
154         }
155     }
156
157     public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
158         EntityManager em = null;
159         try {
160             utx.begin();
161             em = getEntityManager();
162             Node node;
163             try {
164                 node = em.getReference(Node.class, id);
165                 node.getId();
166             } catch (EntityNotFoundException enfe) {
167                 throw new NonexistentEntityException("The node with id " + id + " no longer exists.", enfe);
168             }
169             BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
170             if (bayesianNetwork != null) {
171                 bayesianNetwork.getNodes().remove(node);
172                 bayesianNetwork = em.merge(bayesianNetwork);
173             }
174             List<Question> questions = node.getQuestions();
175             for (Question questionsQuestion : questions) {
176                 questionsQuestion.setNode(null);
177                 questionsQuestion = em.merge(questionsQuestion);
178             }
179             em.remove(node);
180             utx.commit();
181         } catch (Exception ex) {
182             try {
183                 utx.rollback();
184             } catch (Exception re) {
185                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
186             }
187             throw ex;
188         } finally {
189             if (em != null) {
190                 em.close();
191             }
192         }
193     }
194
195     public List<Node> findNodeEntities() {
196         return findNodeEntities(true, -1, -1);
197     }
198
199     public List<Node> findNodeEntities(int maxResults, int firstResult) {
200         return findNodeEntities(false, maxResults, firstResult);
201     }
202
203     private List<Node> findNodeEntities(boolean all, int maxResults, int firstResult) {
204         EntityManager em = getEntityManager();
205         try {
206             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
207             cq.select(cq.from(Node.class));
208             Query q = em.createQuery(cq);
209             if (!all) {
210                 q.setMaxResults(maxResults);
211                 q.setFirstResult(firstResult);
212             }
213             return q.getResultList();
214         } finally {
215             em.close();
216         }
217     }
218
219     public Node findNode(Long id) {
220         EntityManager em = getEntityManager();
221         try {
222             return em.find(Node.class, id);
223         } finally {
224             em.close();
225         }
226     }
227
228     public int getNodeCount() {
229         EntityManager em = getEntityManager();
230         try {
231             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
232             Root<Node> rt = cq.from(Node.class);
233             cq.select(em.getCriteriaBuilder().count(rt));
234             Query q = em.createQuery(cq);
235             return ((Long) q.getSingleResult()).intValue();
236         } finally {
237             em.close();
238         }
239     }
240     
241 }