Skip to content

Commit 30c2283

Browse files
committed
fix delete exam
1 parent df5f8f2 commit 30c2283

5 files changed

Lines changed: 84 additions & 22 deletions

File tree

src/main/java/fr/istic/domain/Answer2HybridGradedComment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.quarkus.hibernate.orm.panache.PanacheQuery;
55
import io.quarkus.runtime.annotations.RegisterForReflection;
66
import java.io.Serializable;
7+
import java.util.Set;
8+
79
import jakarta.json.bind.annotation.JsonbTransient;
810
import jakarta.persistence.*;
911
import org.hibernate.annotations.Cache;
@@ -124,6 +126,12 @@ public static long deleteAllAnswerHybridGradedCommentByCommentId(long commentId
124126
public static long deleteAllAnswerHybridGradedCommentByAnswerId(long responseId){
125127
return delete("studentResponse.id", responseId);
126128
}
129+
public static long deleteAllByQIds( Set<Long> qids){
130+
return delete("delete from Answer2HybridGradedComment ar where ar.studentResponse.question.id in ?1", qids);
131+
}
132+
public static long deleteAllAnswerHybridGradedCommentByCommentIds(Set<Long> commentIds){
133+
return delete("delete from Answer2HybridGradedComment ar where ar.hybridcomments.id in ?1", commentIds);
134+
}
127135

128136

129137

src/main/java/fr/istic/domain/Comments.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
33
import jakarta.json.bind.annotation.JsonbTransient;
44
import io.quarkus.hibernate.orm.panache.PanacheQuery;
5+
import io.quarkus.logging.Log;
56
import io.quarkus.runtime.annotations.RegisterForReflection;
67
import org.hibernate.annotations.Cache;
78
import org.hibernate.annotations.CacheConcurrencyStrategy;

src/main/java/fr/istic/domain/HybridGradedComment.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
44
import io.quarkus.hibernate.orm.panache.PanacheQuery;
5+
import io.quarkus.logging.Log;
56
import io.quarkus.runtime.annotations.RegisterForReflection;
67
import java.io.Serializable;
78
import java.util.HashSet;
@@ -47,7 +48,7 @@ public class HybridGradedComment extends PanacheEntityBase implements Serializab
4748
@JsonbTransient
4849
public Question question;
4950

50-
@OneToMany(mappedBy = "hybridcomments")
51+
@OneToMany(mappedBy = "hybridcomments", cascade = CascadeType.REMOVE, orphanRemoval = true)
5152
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
5253
public Set<Answer2HybridGradedComment> valueAnswers = new HashSet<>();
5354

@@ -133,6 +134,10 @@ public static HybridGradedComment persistOrUpdate(HybridGradedComment hybridGrad
133134
public static PanacheQuery<HybridGradedComment> findByQuestionId( long qid) {
134135
return find("select hybridGradedComment from HybridGradedComment hybridGradedComment where hybridGradedComment.question.id =?1", qid);
135136
}
137+
public static PanacheQuery<HybridGradedComment> findByExamId( long qid) {
138+
return find("select hybridGradedComment from HybridGradedComment hybridGradedComment where hybridGradedComment.question.exam.id =?1", qid);
139+
}
140+
136141

137142
public static PanacheQuery<HybridGradedComment> canAccess(long commentId, String login) {
138143
return find("select ex from HybridGradedComment ex join ex.question.exam.course.profs as u where ex.id =?1 and u.login =?2", commentId, login);

src/main/java/fr/istic/service/CourseService.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.stream.Collectors;
2626

2727
@ApplicationScoped
28-
@Transactional
2928
public class CourseService {
3029

3130
private final Logger log = LoggerFactory.getLogger(CourseService.class);
@@ -50,12 +49,37 @@ public CourseDTO persistOrUpdate(CourseDTO courseDTO) {
5049
*
5150
* @param id the id of the entity.
5251
*/
53-
@Transactional
52+
protected void prepareDelete(Long id) {
53+
Course.findByIdOptional(id).ifPresent(course -> {
54+
List<Exam> exams = new ArrayList<>();
55+
exams.addAll(Exam.findExambyCourseId(id).list());
56+
exams.forEach(exam-> this.examService.delete(exam.id));
57+
});
58+
}
59+
60+
61+
62+
/**
63+
* Delete the Course by id.
64+
*
65+
* @param id the id of the entity.
66+
*/
5467
public void delete(Long id) {
5568
log.debug("Request to delete Course : {}", id);
69+
this.prepareDelete(id);
70+
this.deleteinternal(id);
71+
}
72+
73+
74+
/**
75+
* Delete the Course by id.
76+
*
77+
* @param id the id of the entity.
78+
*/
79+
@Transactional
80+
protected void deleteinternal(Long id) {
5681

5782
Course.findByIdOptional(id).ifPresent(course -> {
58-
Exam.findExambyCourseId(id).list().forEach(exam-> this.examService.delete(exam.id));
5983
Course c = Course.findById(id);
6084
c.groups.forEach(g -> {
6185
g.students.forEach(st -> {

src/main/java/fr/istic/service/ExamService.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import fr.istic.domain.FinalResult;
1515
import fr.istic.domain.GradedComment;
1616
import fr.istic.domain.HybridGradedComment;
17+
import fr.istic.domain.Prediction;
1718
import fr.istic.domain.Question;
1819
import fr.istic.domain.QuestionType;
1920
import fr.istic.domain.Scan;
@@ -76,20 +77,29 @@ public ExamDTO persistOrUpdate(ExamDTO examDTO) {
7677
*
7778
* @param id the id of the entity.
7879
*/
79-
@Transactional
8080
public void delete(Long id) {
8181
log.debug("Request to delete Exam : {}", id);
82-
Exam.findByIdOptional(id).ifPresent(exam -> {
83-
StudentResponse.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> {
82+
this.prepareDeleteExam(id);
83+
this.deleteExam(id);
84+
85+
86+
}
87+
88+
@Transactional
89+
public void prepareDeleteExam(long id){
90+
Exam.findByIdOptional(id).ifPresent(exam -> {
91+
ExamSheet.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> sr.cleanBeforDelete());
92+
/*StudentResponse.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> {
8493
sr.clearComments();
8594
Answer2HybridGradedComment.deleteAllAnswerHybridGradedCommentByAnswerId(sr.id);
8695
});
8796
88-
ExamSheet.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> sr.cleanBeforDelete());
8997
StudentResponse.getAll4ExamIdEvenOrphan(id).list().forEach(sr -> sr.delete());
9098
FinalResult.getAll4ExamId(id).list().forEach(f -> f.delete());
99+
var s = HybridGradedComment.deleteByQIds(e.questions.stream().map(q -> q.id).collect(Collectors.toSet()));
100+
log.error("to remove" + s);*/
101+
this.deleteQuestionCommentAndZone(id);
91102
Exam e = Exam.findById(id);
92-
HybridGradedComment.deleteByQIds(e.questions.stream().map(q -> q.id).collect(Collectors.toSet()));
93103

94104
if (e.scanfile != null && this.fichierS3Service.isObjectExist("scan/" + e.scanfile.id + ".pdf")) {
95105
try {
@@ -109,11 +119,18 @@ public void delete(Long id) {
109119
e1.printStackTrace();
110120
}
111121
}
112-
exam.delete();
113122
Comments.deleteCommentByExamId("" + id);
114-
115123
this.cacheService.deleteFile(id);
116124
});
125+
126+
}
127+
128+
129+
@Transactional
130+
protected void deleteExam(long id){
131+
var e = Exam.findById(id);
132+
e.delete();
133+
117134
}
118135

119136
@Transactional
@@ -169,11 +186,18 @@ protected void cleanExamZone(Set<Long> zoneids) {
169186

170187
@Transactional
171188
protected void cleanQuestion(long examId, Set<Long> qids) {
172-
StudentResponse.deleteByQIds(qids);
173-
TextComment.deleteByQIds(qids);
174-
GradedComment.deleteByQIds(qids);
189+
var s = Answer2HybridGradedComment.deleteAllByQIds(qids);
190+
Set<Long> cids = HybridGradedComment.findByExamId(examId).list().stream().map(ex -> ex.id).collect(Collectors.toSet());
191+
s = Answer2HybridGradedComment.deleteAllAnswerHybridGradedCommentByCommentIds(cids);
192+
s= StudentResponse.deleteByQIds(qids);
193+
s= TextComment.deleteByQIds(qids);
194+
s= GradedComment.deleteByQIds(qids);
195+
s= HybridGradedComment.deleteByQIds(qids);
196+
s=Prediction.deleteByQIds(qids);
197+
}
198+
@Transactional
199+
protected void removeQuestion(long examId, Set<Long> qids) {
175200
Question.deleteAllExamId(examId);
176-
177201
}
178202

179203
@Transactional
@@ -187,16 +211,16 @@ protected void cleanFinalResult(long id) {
187211
*
188212
* @param id the id of the entity.
189213
*/
190-
@Transactional
191-
public void deleteQuestionCommentAndZone(Long id) {
192-
log.debug("Request to delete Exam : {}", id);
193-
Set<Long> qids = Question.findQuestionbyExamId(id).list().stream().map(ex -> ex.id).collect(Collectors.toSet());
214+
public void deleteQuestionCommentAndZone(Long examid) {
215+
log.debug("Request to delete Exam : {}", examid);
216+
Set<Long> qids = Question.findQuestionbyExamId(examid).list().stream().map(ex -> ex.id).collect(Collectors.toSet());
194217

195-
this.cleanFinalResult(id);
196-
Set<Long> zonesids = this.cleanExamZone(id);
218+
this.cleanFinalResult(examid);
219+
Set<Long> zonesids = this.cleanExamZone(examid);
197220
this.cleanExamZone(zonesids);
198221
// this.cleanStudentRssponse(id);
199-
this.cleanQuestion(id, qids);
222+
this.cleanQuestion(examid, qids);
223+
this.removeQuestion(examid, qids);
200224

201225
}
202226

0 commit comments

Comments
 (0)