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.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;
27 public class QuestionJpaController implements Serializable {
29 public QuestionJpaController(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(Question question) throws RollbackFailureException, Exception {
41 if (question.getAnswers() == null) {
42 question.setAnswers(new ArrayList<Answer>());
44 EntityManager em = null;
47 em = getEntityManager();
48 Node node = question.getNode();
50 node = em.getReference(node.getClass(), node.getId());
51 question.setNode(node);
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);
58 question.setAnswers(attachedAnswers);
61 node.getQuestions().add(question);
62 node = em.merge(node);
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);
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(Question question) throws NonexistentEntityException, RollbackFailureException, Exception {
89 EntityManager em = null;
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);
102 List<Answer> attachedAnswersNew = new ArrayList<Answer>();
103 for (Answer answersNewAnswerToAttach : answersNew) {
104 answersNewAnswerToAttach = em.getReference(answersNewAnswerToAttach.getClass(), answersNewAnswerToAttach.getId());
105 attachedAnswersNew.add(answersNewAnswerToAttach);
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);
114 if (nodeNew != null && !nodeNew.equals(nodeOld)) {
115 nodeNew.getQuestions().add(question);
116 nodeNew = em.merge(nodeNew);
118 for (Answer answersOldAnswer : answersOld) {
119 if (!answersNew.contains(answersOldAnswer)) {
120 answersOldAnswer.setQuestion(null);
121 answersOldAnswer = em.merge(answersOldAnswer);
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);
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 = question.getId();
145 if (findQuestion(id) == null) {
146 throw new NonexistentEntityException("The question with id " + id + " no longer exists.");
157 public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
158 EntityManager em = null;
161 em = getEntityManager();
164 question = em.getReference(Question.class, id);
166 } catch (EntityNotFoundException enfe) {
167 throw new NonexistentEntityException("The question with id " + id + " no longer exists.", enfe);
169 Node node = question.getNode();
171 node.getQuestions().remove(question);
172 node = em.merge(node);
174 List<Answer> answers = question.getAnswers();
175 for (Answer answersAnswer : answers) {
176 answersAnswer.setQuestion(null);
177 answersAnswer = em.merge(answersAnswer);
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<Question> findQuestionEntities() {
196 return findQuestionEntities(true, -1, -1);
199 public List<Question> findQuestionEntities(int maxResults, int firstResult) {
200 return findQuestionEntities(false, maxResults, firstResult);
203 private List<Question> findQuestionEntities(boolean all, int maxResults, int firstResult) {
204 EntityManager em = getEntityManager();
206 CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
207 cq.select(cq.from(Question.class));
208 Query q = em.createQuery(cq);
210 q.setMaxResults(maxResults);
211 q.setFirstResult(firstResult);
213 return q.getResultList();
219 public Question findQuestion(Long id) {
220 EntityManager em = getEntityManager();
222 return em.find(Question.class, id);
228 public int getQuestionCount() {
229 EntityManager em = getEntityManager();
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();