Added UI (JSF) and JPA to bayesian network entities.
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / jpa / AnswerJpaController.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 java.util.List;
9 import javax.persistence.EntityManager;
10 import javax.persistence.EntityManagerFactory;
11 import javax.persistence.Query;
12 import javax.persistence.EntityNotFoundException;
13 import javax.persistence.criteria.CriteriaQuery;
14 import javax.persistence.criteria.Root;
15 import javax.transaction.UserTransaction;
16 import org.ufcspa.simdecs.bn.entity.Answer;
17 import org.ufcspa.simdecs.bn.entity.Question;
18 import org.ufcspa.simdecs.bn.jpa.exceptions.NonexistentEntityException;
19 import org.ufcspa.simdecs.bn.jpa.exceptions.RollbackFailureException;
20
21 /**
22  *
23  * @author mchelem
24  */
25 public class AnswerJpaController implements Serializable {
26
27     public AnswerJpaController(UserTransaction utx, EntityManagerFactory emf) {
28         this.utx = utx;
29         this.emf = emf;
30     }
31     private UserTransaction utx = null;
32     private EntityManagerFactory emf = null;
33
34     public EntityManager getEntityManager() {
35         return emf.createEntityManager();
36     }
37
38     public void create(Answer answer) throws RollbackFailureException, Exception {
39         EntityManager em = null;
40         try {
41             utx.begin();
42             em = getEntityManager();
43             Question question = answer.getQuestion();
44             if (question != null) {
45                 question = em.getReference(question.getClass(), question.getId());
46                 answer.setQuestion(question);
47             }
48             em.persist(answer);
49             if (question != null) {
50                 question.getAnswers().add(answer);
51                 question = em.merge(question);
52             }
53             utx.commit();
54         } catch (Exception ex) {
55             try {
56                 utx.rollback();
57             } catch (Exception re) {
58                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
59             }
60             throw ex;
61         } finally {
62             if (em != null) {
63                 em.close();
64             }
65         }
66     }
67
68     public void edit(Answer answer) throws NonexistentEntityException, RollbackFailureException, Exception {
69         EntityManager em = null;
70         try {
71             utx.begin();
72             em = getEntityManager();
73             Answer persistentAnswer = em.find(Answer.class, answer.getId());
74             Question questionOld = persistentAnswer.getQuestion();
75             Question questionNew = answer.getQuestion();
76             if (questionNew != null) {
77                 questionNew = em.getReference(questionNew.getClass(), questionNew.getId());
78                 answer.setQuestion(questionNew);
79             }
80             answer = em.merge(answer);
81             if (questionOld != null && !questionOld.equals(questionNew)) {
82                 questionOld.getAnswers().remove(answer);
83                 questionOld = em.merge(questionOld);
84             }
85             if (questionNew != null && !questionNew.equals(questionOld)) {
86                 questionNew.getAnswers().add(answer);
87                 questionNew = em.merge(questionNew);
88             }
89             utx.commit();
90         } catch (Exception ex) {
91             try {
92                 utx.rollback();
93             } catch (Exception re) {
94                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
95             }
96             String msg = ex.getLocalizedMessage();
97             if (msg == null || msg.length() == 0) {
98                 Long id = answer.getId();
99                 if (findAnswer(id) == null) {
100                     throw new NonexistentEntityException("The answer with id " + id + " no longer exists.");
101                 }
102             }
103             throw ex;
104         } finally {
105             if (em != null) {
106                 em.close();
107             }
108         }
109     }
110
111     public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
112         EntityManager em = null;
113         try {
114             utx.begin();
115             em = getEntityManager();
116             Answer answer;
117             try {
118                 answer = em.getReference(Answer.class, id);
119                 answer.getId();
120             } catch (EntityNotFoundException enfe) {
121                 throw new NonexistentEntityException("The answer with id " + id + " no longer exists.", enfe);
122             }
123             Question question = answer.getQuestion();
124             if (question != null) {
125                 question.getAnswers().remove(answer);
126                 question = em.merge(question);
127             }
128             em.remove(answer);
129             utx.commit();
130         } catch (Exception ex) {
131             try {
132                 utx.rollback();
133             } catch (Exception re) {
134                 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
135             }
136             throw ex;
137         } finally {
138             if (em != null) {
139                 em.close();
140             }
141         }
142     }
143
144     public List<Answer> findAnswerEntities() {
145         return findAnswerEntities(true, -1, -1);
146     }
147
148     public List<Answer> findAnswerEntities(int maxResults, int firstResult) {
149         return findAnswerEntities(false, maxResults, firstResult);
150     }
151
152     private List<Answer> findAnswerEntities(boolean all, int maxResults, int firstResult) {
153         EntityManager em = getEntityManager();
154         try {
155             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
156             cq.select(cq.from(Answer.class));
157             Query q = em.createQuery(cq);
158             if (!all) {
159                 q.setMaxResults(maxResults);
160                 q.setFirstResult(firstResult);
161             }
162             return q.getResultList();
163         } finally {
164             em.close();
165         }
166     }
167
168     public Answer findAnswer(Long id) {
169         EntityManager em = getEntityManager();
170         try {
171             return em.find(Answer.class, id);
172         } finally {
173             em.close();
174         }
175     }
176
177     public int getAnswerCount() {
178         EntityManager em = getEntityManager();
179         try {
180             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
181             Root<Answer> rt = cq.from(Answer.class);
182             cq.select(em.getCriteriaBuilder().count(rt));
183             Query q = em.createQuery(cq);
184             return ((Long) q.getSingleResult()).intValue();
185         } finally {
186             em.close();
187         }
188     }
189     
190 }