109 lines
3.2 KiB
Java
109 lines
3.2 KiB
Java
package fr.istic.taa.jaxrs.DAO;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import fr.istic.taa.jaxrs.metier.Choix;
|
|
import fr.istic.taa.jaxrs.metier.Question;
|
|
import fr.istic.taa.jaxrs.metier.Reponse;
|
|
import fr.istic.taa.jaxrs.metier.ReponseCourte;
|
|
import fr.istic.taa.jaxrs.metier.Session;
|
|
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
|
import jakarta.persistence.EntityTransaction;
|
|
|
|
public class QuestionDAO extends AbstractJpaDao<Integer, Question> {
|
|
public QuestionDAO() {
|
|
super();
|
|
this.setClass(Question.class);
|
|
}
|
|
|
|
public void addReponse(String reponse, int questionId) {
|
|
EntityTransaction t = em.getTransaction();
|
|
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) {
|
|
EntityTransaction t = em.getTransaction();
|
|
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();
|
|
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);
|
|
}
|
|
|
|
public void setReponseCourte(Integer id) {
|
|
setReponse(new ReponseCourte(), id);
|
|
}
|
|
|
|
public void addChoix(Integer id, String choix) {
|
|
EntityTransaction t = em.getTransaction();
|
|
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();
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|