From 2f861569c6f9dc750503b365bb071b9fb05f991e Mon Sep 17 00:00:00 2001 From: Olivier Barais Date: Tue, 9 Sep 2025 12:09:45 +0200 Subject: [PATCH 1/2] fix cache clean --- src/main/java/fr/istic/service/ExamService.java | 13 +++++++++++++ src/main/java/fr/istic/web/rest/ExamResource.java | 12 ++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/istic/service/ExamService.java b/src/main/java/fr/istic/service/ExamService.java index 8978cc32..498019f7 100644 --- a/src/main/java/fr/istic/service/ExamService.java +++ b/src/main/java/fr/istic/service/ExamService.java @@ -9,6 +9,7 @@ import io.quarkus.panache.common.Page; import fr.istic.domain.Answer2HybridGradedComment; import fr.istic.domain.Comments; +import fr.istic.domain.Course; import fr.istic.domain.Exam; import fr.istic.domain.ExamSheet; import fr.istic.domain.FinalResult; @@ -25,6 +26,7 @@ import fr.istic.domain.enumeration.GradeType; import fr.istic.service.customdto.answernotebooks.AnswersNoteBook; import fr.istic.service.customdto.answernotebooks.QuestionNoteBook; +import fr.istic.service.dto.CourseDTO; import fr.istic.service.dto.ExamDTO; import fr.istic.service.mapper.ExamMapper; import org.slf4j.Logger; @@ -251,6 +253,17 @@ public Paged findAll(Page page) { .map(exam -> examMapper.toDto((Exam) exam)); } + /** + * Get all the exams. + * @param page the pagination information. + * @return the list of entities. + */ + public Paged findAll4User(Page page, User u) { + log.debug("Request to get all Exams " + u.login ); + return new Paged<>(Exam.findExambyLogin(u.login).page(page)) + .map(exam -> examMapper.toDto((Exam) exam)); + } + @Transactional public Paged findExambyCourseId(Page page, long courseId) { log.debug("Request to get all Exams"); diff --git a/src/main/java/fr/istic/web/rest/ExamResource.java b/src/main/java/fr/istic/web/rest/ExamResource.java index e16207a0..a43eb0f4 100644 --- a/src/main/java/fr/istic/web/rest/ExamResource.java +++ b/src/main/java/fr/istic/web/rest/ExamResource.java @@ -6,9 +6,6 @@ import fr.istic.domain.Course; import fr.istic.domain.Exam; import fr.istic.domain.ExamSheet; -import fr.istic.domain.Scan; -import fr.istic.domain.Student; -import fr.istic.domain.StudentResponse; import fr.istic.domain.User; import fr.istic.security.AuthoritiesConstants; import fr.istic.service.ExamService; @@ -208,6 +205,7 @@ public Response getAllExams(@BeanParam PageRequestVM pageRequest, @BeanParam Sor result = examService.findExambyScanId(page, Long.parseLong("" + scanId.get(0))); } else { + if (ctx.getUserPrincipal().getName()!= null){ @@ -223,7 +221,13 @@ public Response getAllExams(@BeanParam PageRequestVM pageRequest, @BeanParam Sor && user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_ADMIN")))) { result = examService.findAll(page); - } else { + } + else if (user.get().authorities.size() >= 1 + && user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_USER")))) { + result = examService.findAll4User(page,user.get()); + + } + else { return Response.status(403, "Current user cannot access to this ressource").build(); } From 5ed81cedc9480a32339edb3de7c4d92ead336650 Mon Sep 17 00:00:00 2001 From: Olivier Barais Date: Wed, 10 Sep 2025 17:06:39 +0200 Subject: [PATCH 2/2] close https://github.com/correctexam/corrigeExamFront/issues/617 --- .../fr/istic/service/CacheUploadService.java | 174 ++++++++++++++++++ .../java/fr/istic/web/rest/ExtendedAPI.java | 17 ++ 2 files changed, 191 insertions(+) diff --git a/src/main/java/fr/istic/service/CacheUploadService.java b/src/main/java/fr/istic/service/CacheUploadService.java index a9d6837d..926087a9 100644 --- a/src/main/java/fr/istic/service/CacheUploadService.java +++ b/src/main/java/fr/istic/service/CacheUploadService.java @@ -25,6 +25,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -400,7 +401,180 @@ public long getCachePageInTemplateSqlite(long id) throws IOException { return 0; } + public long getCacheTimeStampSqlite(long id) throws IOException { + InputStream inputStream = null; + String dbpath = ""; + if (pathsqlite.containsKey(id) && Paths.get(pathsqlite.get(id)).toFile().exists()) { + dbpath = pathsqlite.get(id); + } else { + if (this.uses3) { + String fileName = "cache/" + id + ".sqlite3"; + try { + if (this.fichierS3Service.isObjectExist(fileName)) { + inputStream = this.getObject(fileName); + dbpath = this.writeStreamToTempFile(inputStream, id + ".sqlite3").getAbsolutePath(); + pathsqlite.put(id, dbpath); + } + } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) { + e.printStackTrace(); + return 0; + } + } else { + String fileName = id + ".sqlite3"; + File customDir = new File(UPLOAD_DIR); + fileName = customDir.getAbsolutePath() + + File.separator + fileName; + if (Paths.get(fileName).toFile().exists()) { + dbpath = Paths.get(fileName).toFile().getAbsolutePath(); + pathsqlite.put(id, dbpath); + + } + } + } + if ("".equals(dbpath) || dbpath == null) { + return 0; + } + + Connection conn = null; + try { + // db parameters + String url = "jdbc:sqlite:" + dbpath; + + // create a connection to the database + conn = DriverManager.getConnection(url); + String query = "select DT from exam"; + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery(query); + boolean hasAline = rs.next(); + if (hasAline) { + Date time = rs.getTime(1); + return time.getTime(); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + + } catch (SQLException e) { + + System.out.println(e.getMessage()); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch (SQLException ex) { + log.error(ex.getMessage()); + } + } + return 0; + } + + + +public long getCacheTimeStamp(long id) throws IOException{ + InputStream inputStream = null; + long timestamp = 0; + if (this.uses3) { + String fileName = "cache/" + id + "indexdb.json"; + + try { + if (this.fichierS3Service.isObjectExist(fileName)) { + inputStream = this.getObject(fileName); + } else { + fileName = "cache/" + id + "_exam_template_indexdb.json"; + + if (this.fichierS3Service.isObjectExist(fileName)) { + + inputStream = this.getObject(fileName); + + } + } + } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) { + e.printStackTrace(); + return 0; + } + if (inputStream == null) { + return this.getCacheTimeStampSqlite(id); + + } + + } else { + String fileName = id + "indexdb.json"; + File customDir = new File(UPLOAD_DIR); + fileName = customDir.getAbsolutePath() + + File.separator + fileName; + if (Paths.get(fileName).toFile().exists()) { + + inputStream = Files.newInputStream(Paths.get(fileName)); + + } else { + fileName = "cache/" + id + "_exam_template_indexdb.json"; + fileName = customDir.getAbsolutePath() + + File.separator + fileName; + if (Paths.get(fileName).toFile().exists()) { + inputStream = Files.newInputStream(Paths.get(fileName)); + + } + + } + if (inputStream == null) { + return this.getCacheTimeStampSqlite(id); + + } + + } + + JsonReader reader = new JsonReader(new InputStreamReader(inputStream)); + reader.beginObject(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.beginObject(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.beginArray(); + + // exams + + reader.beginObject(); + reader.nextName(); + reader.nextString(); + reader.nextName(); + reader.skipValue(); + reader.nextName(); + reader.beginArray(); + while (reader.hasNext()){ + reader.beginObject(); + reader.nextName(); + reader.nextLong(); + if (reader.hasNext()){ + reader.nextName(); + timestamp= reader.nextLong(); + } + reader.endObject(); + } + reader.endArray(); + + + reader.endObject(); + + + // reader.endObject(); + + reader.close(); + inputStream.close(); + return timestamp; + +} public long getCachePageInTemplate(long id) throws IOException { diff --git a/src/main/java/fr/istic/web/rest/ExtendedAPI.java b/src/main/java/fr/istic/web/rest/ExtendedAPI.java index 51b7cbdd..a9d029c6 100644 --- a/src/main/java/fr/istic/web/rest/ExtendedAPI.java +++ b/src/main/java/fr/istic/web/rest/ExtendedAPI.java @@ -1347,6 +1347,23 @@ public Response getCachePageNoAlign(@PathParam("examId") long examId, @PathParam } } + @GET + @Path("/getCacheTimeStamp/{examId}") + @Produces(MediaType.TEXT_PLAIN) + public Response getCacheTimeStamp(@PathParam("examId") long examId) { + try { + return Response + .status(Response.Status.OK) + .entity(cacheUploadService.getCacheTimeStamp(examId)) + .type(MediaType.TEXT_PLAIN) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + return Response.serverError().build(); + + } + } /* * @GET