From 33fdce6d679c78827d2725c7f59ee89fc1bac77f Mon Sep 17 00:00:00 2001 From: uo287545 Date: Fri, 26 Apr 2024 15:45:05 +0200 Subject: [PATCH] Added new scheduled method to regenerate the questions each 24 hours --- .../uniovi/services/QuestionGeneratorService.java | 12 ++++++++++++ .../java/com/uniovi/services/QuestionService.java | 5 +++++ .../uniovi/services/impl/QuestionServiceImpl.java | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/src/main/java/com/uniovi/services/QuestionGeneratorService.java b/src/main/java/com/uniovi/services/QuestionGeneratorService.java index 253a87db..ec2992b2 100644 --- a/src/main/java/com/uniovi/services/QuestionGeneratorService.java +++ b/src/main/java/com/uniovi/services/QuestionGeneratorService.java @@ -39,9 +39,12 @@ public class QuestionGeneratorService { private Logger log = LoggerFactory.getLogger(InsertSampleDataService.class); + private boolean started; + public QuestionGeneratorService(QuestionService questionService) { this.questionService = questionService; parseQuestionTypes(); + this.started = true; } private void parseQuestionTypes() { @@ -63,6 +66,10 @@ private void parseQuestionTypes() { } } + @Scheduled(fixedRate = 86400000, initialDelay = 86400000) + public void generateAllQuestions(){ + } + @Scheduled(fixedRate = 150000) @Transactional public void generateQuestions() throws IOException { @@ -70,6 +77,11 @@ public void generateQuestions() throws IOException { return; } + if (started){ + started = false; + questionService.deleteAllQuestions(); + } + if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("test")))) { log.info("Test profile active, skipping sample data insertion"); return; diff --git a/src/main/java/com/uniovi/services/QuestionService.java b/src/main/java/com/uniovi/services/QuestionService.java index b482cfd6..34801782 100644 --- a/src/main/java/com/uniovi/services/QuestionService.java +++ b/src/main/java/com/uniovi/services/QuestionService.java @@ -96,4 +96,9 @@ public interface QuestionService { * @param id The id of the question to delete */ void deleteQuestion(Long id); + + /** + * Delete all the questions + */ + void deleteAllQuestions(); } diff --git a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java index f92f7958..5a80136b 100644 --- a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java @@ -165,4 +165,9 @@ public void deleteQuestion(Long id) { questionRepository.delete(question); } } + + @Override + public void deleteAllQuestions() { + questionRepository.deleteAll(); + } }