change the transaction in DAO, in case it's in transaction and block all, add exception
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,23 +10,39 @@ public class QuizzDAO extends AbstractJpaDao<Integer, Quizz> {
|
||||
this.setClass(Quizz.class);
|
||||
}
|
||||
|
||||
public void deleteAllQustion(int quizz_id){
|
||||
EntityTransaction et= em.getTransaction();
|
||||
et.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
||||
quizz.getQuestions().clear();
|
||||
em.merge(quizz);
|
||||
et.commit();
|
||||
public void deleteAllQustion(int quizzId) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
try {
|
||||
if (!t.isActive()) t.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizzId);
|
||||
if (quizz != null) {
|
||||
quizz.getQuestions().clear();
|
||||
em.merge(quizz); // ✅ merge instead of persist
|
||||
}
|
||||
t.commit();
|
||||
} catch (Exception e) {
|
||||
if (t.isActive()) t.rollback();
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void addQuestion(int quizz_id, int question_id){
|
||||
EntityTransaction et= em.getTransaction();
|
||||
et.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
||||
Question question =em.find(Question.class, question_id);
|
||||
public void addQuestion(int quizzId, int questionId) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
try {
|
||||
if (!t.isActive()) t.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizzId);
|
||||
Question question = em.find(Question.class, questionId);
|
||||
|
||||
quizz.getQuestions().add(question);
|
||||
em.merge(quizz);
|
||||
et.commit();
|
||||
if (quizz != null && question != null) {
|
||||
quizz.getQuestions().add(question);
|
||||
em.merge(quizz);
|
||||
}
|
||||
t.commit();
|
||||
} catch (Exception e) {
|
||||
if (t.isActive()) t.rollback();
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,21 @@ public class SessionDAO extends AbstractJpaDao<Integer, Session> {
|
||||
this.setClass(Session.class);
|
||||
}
|
||||
|
||||
public List<Session> findByTheme(String theme){
|
||||
EntityTransaction t=em.getTransaction();
|
||||
Query query=em.createQuery("select s from Session s where s.theme=:theme");
|
||||
query.setParameter("theme",theme);
|
||||
List<Session> sessions=query.getResultList();
|
||||
if(sessions.size()>0){
|
||||
public List<Session> findByTheme(String theme) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
try {
|
||||
if (!t.isActive()) {
|
||||
t.begin();
|
||||
}
|
||||
Query query = em.createQuery("SELECT s FROM Session s WHERE s.theme = :theme", Session.class);
|
||||
query.setParameter("theme", theme);
|
||||
List<Session> sessions = query.getResultList();
|
||||
t.commit();
|
||||
return sessions;
|
||||
}else{
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
if (t.isActive()) t.rollback();
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,28 +13,53 @@ public class UtilisateurDAO extends AbstractJpaDao<Integer, Utilisateur> {
|
||||
}
|
||||
|
||||
public Utilisateur findByEmail(String email) {
|
||||
List<Utilisateur> results =
|
||||
em.createQuery("SELECT u FROM Utilisateur u WHERE u.email = :email", Utilisateur.class)
|
||||
.setParameter("email", email).getResultList();
|
||||
List<Utilisateur> results = em.createQuery(
|
||||
"SELECT u FROM Utilisateur u WHERE u.email = :email", Utilisateur.class)
|
||||
.setParameter("email", email)
|
||||
.getResultList();
|
||||
return results.isEmpty() ? null : results.get(0);
|
||||
}
|
||||
|
||||
if (results.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return results.get(0);
|
||||
public void addToSession(int userId, int sessionId) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
try {
|
||||
if (!t.isActive()) {
|
||||
t.begin();
|
||||
}
|
||||
|
||||
Session s = em.find(Session.class, sessionId);
|
||||
Utilisateur u = em.find(Utilisateur.class, userId);
|
||||
|
||||
if (s == null || u == null) {
|
||||
throw new IllegalArgumentException("User or session not found");
|
||||
}
|
||||
|
||||
if (!u.getSession().contains(s)) {
|
||||
u.getSession().add(s);
|
||||
em.merge(u);
|
||||
}
|
||||
|
||||
t.commit();
|
||||
} catch (Exception e) {
|
||||
if (t.isActive()) t.rollback();
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void addToSession(int sessionId, int userId) {
|
||||
@Override
|
||||
public void create(Utilisateur entity) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Session s = em.find(Session.class, sessionId);
|
||||
Utilisateur u = em.find(Utilisateur.class, userId);
|
||||
|
||||
//FAUT AJOUTER OWNING SIDE ( Ici u --> s)
|
||||
u.getSession().add(s);
|
||||
//Jsp il faut birectionnelle ou pas?
|
||||
//s.getUtilisateurs().add(u);
|
||||
em.merge(u);
|
||||
t.commit();
|
||||
try {
|
||||
if (!t.isActive()) {
|
||||
t.begin();
|
||||
}
|
||||
em.merge(entity);
|
||||
t.commit();
|
||||
} catch (Exception e) {
|
||||
if (t.isActive()) t.rollback();
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user