Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / QuestionJpaController.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.Node;
13 import org.ufcspa.simdecs.bn.entity.Answer;
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.Question;
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 QuestionJpaController implements Serializable {
28
29     public QuestionJpaController(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(Question question) throws RollbackFailureException, Exception {
41         if (question.getAnswers() == null) {
42             question.setAnswers(new ArrayList<Answer>());
43         }
44         EntityManager em = null;
45         try {
46             utx.begin();
47             em = getEntityManager();
48             Node node = question.getNode();
49             if (node != null) {
50                 node = em.getReference(node.getClass(), node.getId());
51                 question.setNode(node);
52             }
53             List<Answer> attachedAnswers = new ArrayList<Answer>();
54             for (Answer answersAnswerToAttach : question.getAnswers()) {
55                 answersAnswerToAttach = em.getReference(answersAnswerToAttach.getClass(), answersAnswerToAttach.getId());
56                 attachedAnswers.add(answersAnswerToAttach);
57             }
58             question.setAnswers(attachedAnswers);
59             em.persist(question);
60             if (node != null) {
61                 node.getQuestions().add(question);
62                 node = em.merge(node);
63             }
64             for (Answer answersAnswer : question.getAnswers()) {
65                 Question oldQuestionOfAnswersAnswer = answersAnswer.getQuestion();
66                 answersAnswer.setQuestion(question);
67                 answersAnswer = em.merge(answersAnswer);
68                 if (oldQuestionOfAnswersAnswer != null) {
69                     oldQuestionOfAnswersAnswer.getAnswers().remove(answersAnswer);
70                     oldQuestionOfAnswersAnswer = em.merge(oldQuestionOfAnswersAnswer);
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(Question question) throws NonexistentEntityException, RollbackFailureException, Exception {
89         EntityManager em = null;
90         try {
91             utx.begin();
92             em = getEntityManager();
93             Question persistentQuestion = em.find(Question.class, question.getId());
94             Node nodeOld = persistentQuestion.getNode();
95             Node nodeNew = question.getNode();
96             List<Answer> answersOld = persistentQuestion.getAnswers();
97             List<Answer> answersNew = question.getAnswers();
98             if (nodeNew != null) {
99                 nodeNew = em.getReference(nodeNew.getClass(), nodeNew.getId());
100                 question.setNode(nodeNew);
101             }
102             List<Answer> attachedAnswersNew = new ArrayList<Answer>();
103             for (Answer answersNewAnswerToAttach : answersNew) {
104                 answersNewAnswerToAttach = em.getReference(answersNewAnswerToAttach.getClass(), answersNewAnswerToAttach.getId());
105                 attachedAnswersNew.add(answersNewAnswerToAttach);
106             }
107             answersNew = attachedAnswersNew;
108             question.setAnswers(answersNew);
109             question = em.merge(question);
110             if (nodeOld != null && !nodeOld.equals(nodeNew)) {
111                 nodeOld.getQuestions().remove(question);
112                 nodeOld = em.merge(nodeOld);
113             }
114             if (nodeNew != null && !nodeNew.equals(nodeOld)) {
115                 nodeNew.getQuestions().add(question);
116                 nodeNew = em.merge(nodeNew);
117             }
118             for (Answer answersOldAnswer : answersOld) {
119                 if (!answersNew.contains(answersOldAnswer)) {
120                     answersOldAnswer.setQuestion(null);
121                     answersOldAnswer = em.merge(answersOldAnswer);
122                 }
123             }
124             for (Answer answersNewAnswer : answersNew) {
125                 if (!answersOld.contains(answersNewAnswer)) {
126                     Question oldQuestionOfAnswersNewAnswer = answersNewAnswer.getQuestion();
127                     answersNewAnswer.setQuestion(question);
128                     answersNewAnswer = em.merge(answersNewAnswer);
129                     if (oldQuestionOfAnswersNewAnswer != null && !oldQuestionOfAnswersNewAnswer.equals(question)) {
130                         oldQuestionOfAnswersNewAnswer.getAnswers().remove(answersNewAnswer);
131                         oldQuestionOfAnswersNewAnswer = em.merge(oldQuestionOfAnswersNewAnswer);
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 = question.getId();
145                 if (findQuestion(id) == null) {
146                     throw new NonexistentEntityException("The question 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             Question question;
163             try {
164                 question = em.getReference(Question.class, id);
165                 question.getId();
166             } catch (EntityNotFoundException enfe) {
167                 throw new NonexistentEntityException("The question with id " + id + " no longer exists.", enfe);
168             }
169             Node node = question.getNode();
170             if (node != null) {
171                 node.getQuestions().remove(question);
172                 node = em.merge(node);
173             }
174             List<Answer> answers = question.getAnswers();
175             for (Answer answersAnswer : answers) {
176                 answersAnswer.setQuestion(null);
177                 answersAnswer = em.merge(answersAnswer);
178             }
179             em.remove(question);
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<Question> findQuestionEntities() {
196         return findQuestionEntities(true, -1, -1);
197     }
198
199     public List<Question> findQuestionEntities(int maxResults, int firstResult) {
200         return findQuestionEntities(false, maxResults, firstResult);
201     }
202
203     private List<Question> findQuestionEntities(boolean all, int maxResults, int firstResult) {
204         EntityManager em = getEntityManager();
205         try {
206             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
207             cq.select(cq.from(Question.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 Question findQuestion(Long id) {
220         EntityManager em = getEntityManager();
221         try {
222             return em.find(Question.class, id);
223         } finally {
224             em.close();
225         }
226     }
227
228     public int getQuestionCount() {
229         EntityManager em = getEntityManager();
230         try {
231             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
232             Root<Question> rt = cq.from(Question.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 }