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.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;
27 public class NodeJpaController implements Serializable {
29 public NodeJpaController(UserTransaction utx, EntityManagerFactory emf) {
33 private UserTransaction utx = null;
34 private EntityManagerFactory emf = null;
36 public EntityManager getEntityManager() {
37 return emf.createEntityManager();
40 public void create(Node node) throws RollbackFailureException, Exception {
41 if (node.getQuestions() == null) {
42 node.setQuestions(new ArrayList<Question>());
44 EntityManager em = null;
47 em = getEntityManager();
48 BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
49 if (bayesianNetwork != null) {
50 bayesianNetwork = em.getReference(bayesianNetwork.getClass(), bayesianNetwork.getId());
51 node.setBayesianNetwork(bayesianNetwork);
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);
58 node.setQuestions(attachedQuestions);
60 if (bayesianNetwork != null) {
61 bayesianNetwork.getNodes().add(node);
62 bayesianNetwork = em.merge(bayesianNetwork);
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);
74 } catch (Exception ex) {
77 } catch (Exception re) {
78 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
88 public void edit(Node node) throws NonexistentEntityException, RollbackFailureException, Exception {
89 EntityManager em = null;
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);
102 List<Question> attachedQuestionsNew = new ArrayList<Question>();
103 for (Question questionsNewQuestionToAttach : questionsNew) {
104 questionsNewQuestionToAttach = em.getReference(questionsNewQuestionToAttach.getClass(), questionsNewQuestionToAttach.getId());
105 attachedQuestionsNew.add(questionsNewQuestionToAttach);
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);
114 if (bayesianNetworkNew != null && !bayesianNetworkNew.equals(bayesianNetworkOld)) {
115 bayesianNetworkNew.getNodes().add(node);
116 bayesianNetworkNew = em.merge(bayesianNetworkNew);
118 for (Question questionsOldQuestion : questionsOld) {
119 if (!questionsNew.contains(questionsOldQuestion)) {
120 questionsOldQuestion.setNode(null);
121 questionsOldQuestion = em.merge(questionsOldQuestion);
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);
136 } catch (Exception ex) {
139 } catch (Exception re) {
140 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
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.");
157 public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
158 EntityManager em = null;
161 em = getEntityManager();
164 node = em.getReference(Node.class, id);
166 } catch (EntityNotFoundException enfe) {
167 throw new NonexistentEntityException("The node with id " + id + " no longer exists.", enfe);
169 BayesianNetwork bayesianNetwork = node.getBayesianNetwork();
170 if (bayesianNetwork != null) {
171 bayesianNetwork.getNodes().remove(node);
172 bayesianNetwork = em.merge(bayesianNetwork);
174 List<Question> questions = node.getQuestions();
175 for (Question questionsQuestion : questions) {
176 questionsQuestion.setNode(null);
177 questionsQuestion = em.merge(questionsQuestion);
181 } catch (Exception ex) {
184 } catch (Exception re) {
185 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
195 public List<Node> findNodeEntities() {
196 return findNodeEntities(true, -1, -1);
199 public List<Node> findNodeEntities(int maxResults, int firstResult) {
200 return findNodeEntities(false, maxResults, firstResult);
203 private List<Node> findNodeEntities(boolean all, int maxResults, int firstResult) {
204 EntityManager em = getEntityManager();
206 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
207 cq.select(cq.from(Node.class));
208 Query q = em.createQuery(cq);
210 q.setMaxResults(maxResults);
211 q.setFirstResult(firstResult);
213 return q.getResultList();
219 public Node findNode(Long id) {
220 EntityManager em = getEntityManager();
222 return em.find(Node.class, id);
228 public int getNodeCount() {
229 EntityManager em = getEntityManager();
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();