change the transaction in DAO, in case it's in transaction and block all, add exception

This commit is contained in:
Vu Tuan Minh
2025-10-24 23:11:47 +02:00
parent 0553fc13f7
commit d55e48b7e4
4 changed files with 154 additions and 81 deletions

View File

@@ -12,71 +12,97 @@ import fr.istic.taa.jaxrs.metier.Utilisateur;
import jakarta.persistence.EntityTransaction;
public class QuestionDAO extends AbstractJpaDao<Integer, Question> {
public QuestionDAO(){
public QuestionDAO() {
super();
this.setClass(Question.class);
}
public void addReponse(String reponse, int questionId){
public void addReponse(String reponse, int questionId) {
EntityTransaction t = em.getTransaction();
t.begin();
Question q = em.find(Question.class,questionId);
q.getReponse().getReponses().add(reponse);
em.merge(q);
t.commit();
try {
if (!t.isActive()) t.begin();
Question q = em.find(Question.class, questionId);
if (q != null && q.getReponse() != null) {
q.getReponse().getReponses().add(reponse);
em.merge(q);
}
t.commit();
} catch (Exception e) {
if (t.isActive()) t.rollback();
e.printStackTrace();
}
}
public void deleteReponse( int questionId) {
public void deleteReponse(int questionId) {
EntityTransaction t = em.getTransaction();
t.begin();
Question q = em.find(Question.class,questionId);
q.getReponse().setReponses(new ArrayList<>());
em.merge(q);
t.commit();
try {
if (!t.isActive()) t.begin();
Question q = em.find(Question.class, questionId);
if (q != null && q.getReponse() != null) {
q.getReponse().setReponses(new ArrayList<>());
em.merge(q);
}
t.commit();
} catch (Exception e) {
if (t.isActive()) t.rollback();
e.printStackTrace();
}
}
public void setReponse(Reponse rep, Integer id) {
EntityTransaction t = em.getTransaction();
t.begin();
Question q = em.find(Question.class,id);
q.setReponse(rep);
em.merge(q);
t.commit();
try {
if (!t.isActive()) t.begin();
Question q = em.find(Question.class, id);
if (q != null) {
q.setReponse(rep);
em.merge(q);
}
t.commit();
} catch (Exception e) {
if (t.isActive()) t.rollback();
e.printStackTrace();
}
}
public void setChoix(Integer id) {
setReponse(new Choix(),id);
setReponse(new Choix(), id);
}
public void setReponseCourte(Integer id) {
setReponse(new ReponseCourte(),id);
setReponse(new ReponseCourte(), id);
}
public void addChoix(Integer id, String choix) {
EntityTransaction t = em.getTransaction();
t.begin();
Question q = em.find(Question.class,id);
((Choix)q.getReponse()).getChoix().add(choix);
em.merge(q);
t.commit();
try {
if (!t.isActive()) t.begin();
Question q = em.find(Question.class, id);
if (q != null && q.getReponse() instanceof Choix) {
((Choix) q.getReponse()).getChoix().add(choix);
em.merge(q);
}
t.commit();
} catch (Exception e) {
if (t.isActive()) t.rollback();
e.printStackTrace();
}
}
public void changeQuestion(String newQuestion, Integer id) {
EntityTransaction t = em.getTransaction();
t.begin();
Question q = em.find(Question.class,id);
q.setQuestion(newQuestion);
em.merge(q);
t.commit();
try {
if (!t.isActive()) t.begin();
Question q = em.find(Question.class, id);
if (q != null) {
q.setQuestion(newQuestion);
em.merge(q);
}
t.commit();
} catch (Exception e) {
if (t.isActive()) t.rollback();
e.printStackTrace();
}
}
}