From 1dda1f425c8b583e2eada6df062b6be3b5e6cfe0 Mon Sep 17 00:00:00 2001 From: uo287545 Date: Tue, 9 Apr 2024 00:27:24 +0200 Subject: [PATCH] Removed unused catch clauses and personalized exceptions --- .../components/MultipleQuestionGenerator.java | 3 ++- .../generators/AbstractQuestionGenerator.java | 17 +++-------------- .../generators/QuestionGenerator.java | 3 ++- .../services/InsertSampleDataService.java | 5 +++-- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java b/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java index 6d525d3b..e5001403 100644 --- a/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java @@ -3,6 +3,7 @@ import com.uniovi.components.generators.QuestionGenerator; import com.uniovi.entities.Question; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,7 +14,7 @@ public MultipleQuestionGenerator(QuestionGenerator... generators) { this.generators = generators; } - public List getQuestions() throws InterruptedException { + public List getQuestions() throws InterruptedException, IOException { List questions = new ArrayList<>(); for (QuestionGenerator generator : generators) { questions.addAll(generator.getQuestions()); diff --git a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java index 3d00dc8c..9754c2c5 100644 --- a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java @@ -6,6 +6,8 @@ import com.uniovi.entities.Category; import com.uniovi.entities.Question; import com.uniovi.services.CategoryService; + +import java.io.IOException; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; @@ -46,10 +48,8 @@ public void questionGenerator(String statement, List options, String cor questions.add(question); } - public List getQuestions() throws InterruptedException { + public List getQuestions() throws InterruptedException, IOException { HttpClient client = HttpClient.newHttpClient(); - try { - String endpointUrl = "https://query.wikidata.org/sparql?query=" + URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8) + "&format=json"; @@ -76,12 +76,6 @@ public List getQuestions() throws InterruptedException { questionGenerator(questionStatement, options, correctAnswer, this.getCategory()); } - } catch (InterruptedException e) { - throw e; - } catch (Exception e) { - throw new QuestionGeneratorException("An error occurred while generating questions." + e.getMessage()); - } - return questions; } @@ -90,9 +84,4 @@ public List getQuestions() throws InterruptedException { protected abstract String getQuestionSubject(JsonNode result); - private static class QuestionGeneratorException extends RuntimeException { - public QuestionGeneratorException(String message) { - super(message); - } - } } diff --git a/src/main/java/com/uniovi/components/generators/QuestionGenerator.java b/src/main/java/com/uniovi/components/generators/QuestionGenerator.java index c6db2c9b..fd9356fa 100644 --- a/src/main/java/com/uniovi/components/generators/QuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/QuestionGenerator.java @@ -4,13 +4,14 @@ import com.uniovi.entities.Question; import org.springframework.stereotype.Component; +import java.io.IOException; import java.util.List; @Component public interface QuestionGenerator { String getQuery(); - List getQuestions() throws InterruptedException; + List getQuestions() throws InterruptedException, IOException; Category getCategory(); diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java index 3386e26f..f51a0ed8 100644 --- a/src/main/java/com/uniovi/services/InsertSampleDataService.java +++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java @@ -22,6 +22,7 @@ import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; +import java.io.IOException; import java.time.LocalDateTime; import java.util.*; @@ -49,7 +50,7 @@ public InsertSampleDataService(PlayerService playerService, QuestionService ques @Transactional @EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup - public void insertSampleQuestions() throws InterruptedException { + public void insertSampleQuestions() throws InterruptedException, IOException { if (!playerService.getUserByEmail("test@test.com").isPresent()) { PlayerDto player = new PlayerDto(); player.setEmail("test@test.com"); @@ -74,7 +75,7 @@ public void generateTestQuestions() { } @Transactional - public void generateSampleData() throws InterruptedException { + public void generateSampleData() throws InterruptedException, IOException { questionRepository.deleteAll();