change the transaction in DAO, in case it's in transaction and block all, add exception
This commit is contained in:
@@ -17,38 +17,53 @@ public class QuestionDAO extends AbstractJpaDao<Integer, Question> {
|
|||||||
this.setClass(Question.class);
|
this.setClass(Question.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addReponse(String reponse, int questionId) {
|
public void addReponse(String reponse, int questionId) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
t.begin();
|
try {
|
||||||
|
if (!t.isActive()) t.begin();
|
||||||
Question q = em.find(Question.class, questionId);
|
Question q = em.find(Question.class, questionId);
|
||||||
|
if (q != null && q.getReponse() != null) {
|
||||||
q.getReponse().getReponses().add(reponse);
|
q.getReponse().getReponses().add(reponse);
|
||||||
em.merge(q);
|
em.merge(q);
|
||||||
t.commit();
|
|
||||||
}
|
}
|
||||||
|
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();
|
EntityTransaction t = em.getTransaction();
|
||||||
t.begin();
|
try {
|
||||||
|
if (!t.isActive()) t.begin();
|
||||||
Question q = em.find(Question.class, questionId);
|
Question q = em.find(Question.class, questionId);
|
||||||
|
if (q != null && q.getReponse() != null) {
|
||||||
q.getReponse().setReponses(new ArrayList<>());
|
q.getReponse().setReponses(new ArrayList<>());
|
||||||
em.merge(q);
|
em.merge(q);
|
||||||
t.commit();
|
|
||||||
}
|
}
|
||||||
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setReponse(Reponse rep, Integer id) {
|
public void setReponse(Reponse rep, Integer id) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
t.begin();
|
try {
|
||||||
|
if (!t.isActive()) t.begin();
|
||||||
Question q = em.find(Question.class, id);
|
Question q = em.find(Question.class, id);
|
||||||
|
if (q != null) {
|
||||||
q.setReponse(rep);
|
q.setReponse(rep);
|
||||||
em.merge(q);
|
em.merge(q);
|
||||||
t.commit();
|
|
||||||
}
|
}
|
||||||
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setChoix(Integer id) {
|
public void setChoix(Integer id) {
|
||||||
setReponse(new Choix(), id);
|
setReponse(new Choix(), id);
|
||||||
@@ -58,25 +73,36 @@ public class QuestionDAO extends AbstractJpaDao<Integer, Question> {
|
|||||||
setReponse(new ReponseCourte(), id);
|
setReponse(new ReponseCourte(), id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addChoix(Integer id, String choix) {
|
public void addChoix(Integer id, String choix) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
t.begin();
|
try {
|
||||||
|
if (!t.isActive()) t.begin();
|
||||||
Question q = em.find(Question.class, id);
|
Question q = em.find(Question.class, id);
|
||||||
|
if (q != null && q.getReponse() instanceof Choix) {
|
||||||
((Choix) q.getReponse()).getChoix().add(choix);
|
((Choix) q.getReponse()).getChoix().add(choix);
|
||||||
em.merge(q);
|
em.merge(q);
|
||||||
t.commit();
|
|
||||||
}
|
}
|
||||||
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void changeQuestion(String newQuestion, Integer id) {
|
public void changeQuestion(String newQuestion, Integer id) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
t.begin();
|
try {
|
||||||
|
if (!t.isActive()) t.begin();
|
||||||
Question q = em.find(Question.class, id);
|
Question q = em.find(Question.class, id);
|
||||||
|
if (q != null) {
|
||||||
q.setQuestion(newQuestion);
|
q.setQuestion(newQuestion);
|
||||||
em.merge(q);
|
em.merge(q);
|
||||||
|
}
|
||||||
t.commit();
|
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);
|
this.setClass(Quizz.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteAllQustion(int quizz_id){
|
public void deleteAllQustion(int quizzId) {
|
||||||
EntityTransaction et= em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
et.begin();
|
try {
|
||||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
if (!t.isActive()) t.begin();
|
||||||
|
Quizz quizz = em.find(Quizz.class, quizzId);
|
||||||
|
if (quizz != null) {
|
||||||
quizz.getQuestions().clear();
|
quizz.getQuestions().clear();
|
||||||
em.merge(quizz);
|
em.merge(quizz); // ✅ merge instead of persist
|
||||||
et.commit();
|
}
|
||||||
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addQuestion(int quizz_id, int question_id){
|
public void addQuestion(int quizzId, int questionId) {
|
||||||
EntityTransaction et= em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
et.begin();
|
try {
|
||||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
if (!t.isActive()) t.begin();
|
||||||
Question question =em.find(Question.class, question_id);
|
Quizz quizz = em.find(Quizz.class, quizzId);
|
||||||
|
Question question = em.find(Question.class, questionId);
|
||||||
|
|
||||||
|
if (quizz != null && question != null) {
|
||||||
quizz.getQuestions().add(question);
|
quizz.getQuestions().add(question);
|
||||||
em.merge(quizz);
|
em.merge(quizz);
|
||||||
et.commit();
|
}
|
||||||
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,19 @@ public class SessionDAO extends AbstractJpaDao<Integer, Session> {
|
|||||||
|
|
||||||
public List<Session> findByTheme(String theme) {
|
public List<Session> findByTheme(String theme) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
Query query=em.createQuery("select s from Session s where s.theme=:theme");
|
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);
|
query.setParameter("theme", theme);
|
||||||
List<Session> sessions = query.getResultList();
|
List<Session> sessions = query.getResultList();
|
||||||
if(sessions.size()>0){
|
t.commit();
|
||||||
return sessions;
|
return sessions;
|
||||||
}else{
|
} catch (Exception e) {
|
||||||
return null;
|
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) {
|
public Utilisateur findByEmail(String email) {
|
||||||
List<Utilisateur> results =
|
List<Utilisateur> results = em.createQuery(
|
||||||
em.createQuery("SELECT u FROM Utilisateur u WHERE u.email = :email", Utilisateur.class)
|
"SELECT u FROM Utilisateur u WHERE u.email = :email", Utilisateur.class)
|
||||||
.setParameter("email", email).getResultList();
|
.setParameter("email", email)
|
||||||
|
.getResultList();
|
||||||
if (results.isEmpty()) {
|
return results.isEmpty() ? null : results.get(0);
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return results.get(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToSession(int sessionId, int userId) {
|
public void addToSession(int userId, int sessionId) {
|
||||||
EntityTransaction t = em.getTransaction();
|
EntityTransaction t = em.getTransaction();
|
||||||
|
try {
|
||||||
|
if (!t.isActive()) {
|
||||||
t.begin();
|
t.begin();
|
||||||
|
}
|
||||||
|
|
||||||
Session s = em.find(Session.class, sessionId);
|
Session s = em.find(Session.class, sessionId);
|
||||||
Utilisateur u = em.find(Utilisateur.class, userId);
|
Utilisateur u = em.find(Utilisateur.class, userId);
|
||||||
|
|
||||||
//FAUT AJOUTER OWNING SIDE ( Ici u --> s)
|
if (s == null || u == null) {
|
||||||
|
throw new IllegalArgumentException("User or session not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!u.getSession().contains(s)) {
|
||||||
u.getSession().add(s);
|
u.getSession().add(s);
|
||||||
//Jsp il faut birectionnelle ou pas?
|
|
||||||
//s.getUtilisateurs().add(u);
|
|
||||||
em.merge(u);
|
em.merge(u);
|
||||||
|
}
|
||||||
|
|
||||||
t.commit();
|
t.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (t.isActive()) t.rollback();
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void create(Utilisateur entity) {
|
||||||
|
EntityTransaction t = em.getTransaction();
|
||||||
|
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