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;
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;
25 public class AnswerJpaController implements Serializable {
27 public AnswerJpaController(UserTransaction utx, EntityManagerFactory emf) {
31 private UserTransaction utx = null;
32 private EntityManagerFactory emf = null;
34 public EntityManager getEntityManager() {
35 return emf.createEntityManager();
38 public void create(Answer answer) throws RollbackFailureException, Exception {
39 EntityManager em = null;
42 em = getEntityManager();
43 Question question = answer.getQuestion();
44 if (question != null) {
45 question = em.getReference(question.getClass(), question.getId());
46 answer.setQuestion(question);
49 if (question != null) {
50 question.getAnswers().add(answer);
51 question = em.merge(question);
54 } catch (Exception ex) {
57 } catch (Exception re) {
58 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
68 public void edit(Answer answer) throws NonexistentEntityException, RollbackFailureException, Exception {
69 EntityManager em = null;
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);
80 answer = em.merge(answer);
81 if (questionOld != null && !questionOld.equals(questionNew)) {
82 questionOld.getAnswers().remove(answer);
83 questionOld = em.merge(questionOld);
85 if (questionNew != null && !questionNew.equals(questionOld)) {
86 questionNew.getAnswers().add(answer);
87 questionNew = em.merge(questionNew);
90 } catch (Exception ex) {
93 } catch (Exception re) {
94 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
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.");
111 public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
112 EntityManager em = null;
115 em = getEntityManager();
118 answer = em.getReference(Answer.class, id);
120 } catch (EntityNotFoundException enfe) {
121 throw new NonexistentEntityException("The answer with id " + id + " no longer exists.", enfe);
123 Question question = answer.getQuestion();
124 if (question != null) {
125 question.getAnswers().remove(answer);
126 question = em.merge(question);
130 } catch (Exception ex) {
133 } catch (Exception re) {
134 throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
144 public List<Answer> findAnswerEntities() {
145 return findAnswerEntities(true, -1, -1);
148 public List<Answer> findAnswerEntities(int maxResults, int firstResult) {
149 return findAnswerEntities(false, maxResults, firstResult);
152 private List<Answer> findAnswerEntities(boolean all, int maxResults, int firstResult) {
153 EntityManager em = getEntityManager();
155 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
156 cq.select(cq.from(Answer.class));
157 Query q = em.createQuery(cq);
159 q.setMaxResults(maxResults);
160 q.setFirstResult(firstResult);
162 return q.getResultList();
168 public Answer findAnswer(Long id) {
169 EntityManager em = getEntityManager();
171 return em.find(Answer.class, id);
177 public int getAnswerCount() {
178 EntityManager em = getEntityManager();
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();