wordstudent

Word learning program
git clone git://git.wimdupont.com/wordstudent.git
Log | Files | Refs | LICENSE

commit a78a20ea4643c973e8a05b277c0cbf2048670834
parent 259b833021896ffc9af25a2738625b0ea790232b
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 20 May 2023 11:18:22 +0200

sb upgrade and lombok removal

Diffstat:
Mpom.xml | 7+------
Msrc/main/java/com/wimdupont/WordStudentAdvancedApplication.java | 2--
Msrc/main/java/com/wimdupont/client/DictionaryAPI.java | 14++++++++++++--
Msrc/main/java/com/wimdupont/client/model/dto/DefinitionDto.java | 14++++++++------
Msrc/main/java/com/wimdupont/client/model/dto/DictionaryDto.java | 18++++++++++--------
Msrc/main/java/com/wimdupont/client/model/dto/MeaningDto.java | 12++++++------
Msrc/main/java/com/wimdupont/client/model/dto/PhoneticDto.java | 12++++++------
Asrc/main/java/com/wimdupont/config/AppConfig.java | 14++++++++++++++
Msrc/main/java/com/wimdupont/converter/AntonymConverter.java | 5+++--
Msrc/main/java/com/wimdupont/converter/DefinitionConverter.java | 20+++++++++++++-------
Msrc/main/java/com/wimdupont/converter/DictionaryConverter.java | 14+++++++++++---
Msrc/main/java/com/wimdupont/converter/MeaningConverter.java | 14+++++++++-----
Msrc/main/java/com/wimdupont/converter/PhoneticConverter.java | 6+++---
Msrc/main/java/com/wimdupont/converter/SynonymConverter.java | 6+++---
Msrc/main/java/com/wimdupont/converter/WordConverter.java | 10++++++++--
Msrc/main/java/com/wimdupont/repository/dao/Antonym.java | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Msrc/main/java/com/wimdupont/repository/dao/Definition.java | 129++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/main/java/com/wimdupont/repository/dao/Dictionary.java | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Msrc/main/java/com/wimdupont/repository/dao/Meaning.java | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/main/java/com/wimdupont/repository/dao/Phonetic.java | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/main/java/com/wimdupont/repository/dao/Synonym.java | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Msrc/main/java/com/wimdupont/repository/dao/Word.java | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Msrc/main/java/com/wimdupont/service/WordFetcher.java | 1+
Msrc/main/java/com/wimdupont/service/WordService.java | 20+++++++++++++++-----
Msrc/main/java/com/wimdupont/service/WordTester.java | 16++++++++++++----
25 files changed, 696 insertions(+), 204 deletions(-)

diff --git a/pom.xml b/pom.xml @@ -6,7 +6,7 @@ <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> - <version>2.5.6</version> + <version>3.1.0</version> </parent> <groupId>com.wimdupont</groupId> <artifactId>WordStudentAdvanced</artifactId> @@ -19,11 +19,6 @@ <dependencies> <dependency> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - <scope>provided</scope> - </dependency> - <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> diff --git a/src/main/java/com/wimdupont/WordStudentAdvancedApplication.java b/src/main/java/com/wimdupont/WordStudentAdvancedApplication.java @@ -5,9 +5,7 @@ import com.wimdupont.service.WordTester; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.ComponentScan; -@ComponentScan @SpringBootApplication public class WordStudentAdvancedApplication { diff --git a/src/main/java/com/wimdupont/client/DictionaryAPI.java b/src/main/java/com/wimdupont/client/DictionaryAPI.java @@ -2,6 +2,9 @@ package com.wimdupont.client; import com.fasterxml.jackson.databind.ObjectMapper; import com.wimdupont.client.model.dto.DictionaryDto; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -11,10 +14,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +@Component public class DictionaryAPI { private static final String AFFIRMATION_URL = "https://api.dictionaryapi.dev/api/v2/entries/en/"; - private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryAPI.class); + private final ObjectMapper objectMapper; + + public DictionaryAPI(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } public List<DictionaryDto> getAffirmation(String word) { try { @@ -26,8 +35,9 @@ public class DictionaryAPI { while ((aux = in.readLine()) != null) { sbuilder.append(aux); } - return Arrays.asList(OBJECT_MAPPER.readValue(sbuilder.toString(), DictionaryDto[].class)); + return Arrays.asList(objectMapper.readValue(sbuilder.toString(), DictionaryDto[].class)); } catch (Exception e) { + LOGGER.error(e.getMessage(), e); return new ArrayList<>(); } } diff --git a/src/main/java/com/wimdupont/client/model/dto/DefinitionDto.java b/src/main/java/com/wimdupont/client/model/dto/DefinitionDto.java @@ -1,12 +1,14 @@ package com.wimdupont.client.model.dto; -import lombok.Builder; -import lombok.extern.jackson.Jacksonized; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; -@Builder -@Jacksonized -public record DefinitionDto(String definition, String example, - List<String> synonyms, List<String> antonyms) { +@JsonIgnoreProperties(ignoreUnknown = true) +public record DefinitionDto( + String definition, + String example, + List<String> synonyms, + List<String> antonyms +) { } diff --git a/src/main/java/com/wimdupont/client/model/dto/DictionaryDto.java b/src/main/java/com/wimdupont/client/model/dto/DictionaryDto.java @@ -1,14 +1,16 @@ package com.wimdupont.client.model.dto; -import lombok.Builder; -import lombok.extern.jackson.Jacksonized; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; -@Builder -@Jacksonized -public record DictionaryDto(String word, String phonetic, - List<PhoneticDto> phonetics, - String origin, - List<MeaningDto> meanings) { +@JsonIgnoreProperties(ignoreUnknown = true) +public record DictionaryDto( + String word, + String phonetic, + List<PhoneticDto> phonetics, + String origin, + List<MeaningDto> meanings +) { } diff --git a/src/main/java/com/wimdupont/client/model/dto/MeaningDto.java b/src/main/java/com/wimdupont/client/model/dto/MeaningDto.java @@ -1,13 +1,13 @@ package com.wimdupont.client.model.dto; -import lombok.Builder; -import lombok.extern.jackson.Jacksonized; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; -@Builder -@Jacksonized -public record MeaningDto(String partOfSpeech, - List<DefinitionDto> definitions) { +@JsonIgnoreProperties(ignoreUnknown = true) +public record MeaningDto( + String partOfSpeech, + List<DefinitionDto> definitions +) { } diff --git a/src/main/java/com/wimdupont/client/model/dto/PhoneticDto.java b/src/main/java/com/wimdupont/client/model/dto/PhoneticDto.java @@ -1,11 +1,11 @@ package com.wimdupont.client.model.dto; -import lombok.Builder; -import lombok.extern.jackson.Jacksonized; - -@Builder -@Jacksonized -public record PhoneticDto( String text, String audio) { +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +@JsonIgnoreProperties(ignoreUnknown = true) +public record PhoneticDto( + String text, + String audio +) { } diff --git a/src/main/java/com/wimdupont/config/AppConfig.java b/src/main/java/com/wimdupont/config/AppConfig.java @@ -0,0 +1,14 @@ +package com.wimdupont.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AppConfig { + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } +} diff --git a/src/main/java/com/wimdupont/converter/AntonymConverter.java b/src/main/java/com/wimdupont/converter/AntonymConverter.java @@ -5,11 +5,12 @@ import com.wimdupont.repository.dao.Antonym; import org.springframework.stereotype.Component; @Component -record AntonymConverter() { +public class AntonymConverter { public Antonym convert(String relatedWord) { - return Antonym.builder() + return Antonym.Builder.newBuilder() .word(relatedWord) .build(); } + } diff --git a/src/main/java/com/wimdupont/converter/DefinitionConverter.java b/src/main/java/com/wimdupont/converter/DefinitionConverter.java @@ -4,22 +4,28 @@ import com.wimdupont.client.model.dto.DefinitionDto; import com.wimdupont.repository.dao.Definition; import org.springframework.stereotype.Component; -import java.util.stream.Collectors; - @Component -record DefinitionConverter(SynonymConverter synonymConverter, - AntonymConverter antonymConverter) { +public class DefinitionConverter { + + private final SynonymConverter synonymConverter; + private final AntonymConverter antonymConverter; + + public DefinitionConverter(SynonymConverter synonymConverter, + AntonymConverter antonymConverter) { + this.synonymConverter = synonymConverter; + this.antonymConverter = antonymConverter; + } public Definition convert(DefinitionDto definitionDto) { - return Definition.builder() + return Definition.Builder.newBuilder() .definition(definitionDto.definition()) .example(definitionDto.example()) .synonyms(definitionDto.synonyms().stream() .map(synonymConverter::convert) - .collect(Collectors.toList())) + .toList()) .antonyms(definitionDto.antonyms().stream() .map(antonymConverter::convert) - .collect(Collectors.toList())) + .toList()) .build(); } } diff --git a/src/main/java/com/wimdupont/converter/DictionaryConverter.java b/src/main/java/com/wimdupont/converter/DictionaryConverter.java @@ -7,11 +7,19 @@ import org.springframework.stereotype.Component; import java.util.stream.Collectors; @Component -record DictionaryConverter(MeaningConverter meaningConverter, - PhoneticConverter phoneticConverter) { +public class DictionaryConverter { + + private final PhoneticConverter phoneticConverter; + private final MeaningConverter meaningConverter; + + public DictionaryConverter(PhoneticConverter phoneticConverter, + MeaningConverter meaningConverter) { + this.phoneticConverter = phoneticConverter; + this.meaningConverter = meaningConverter; + } public Dictionary convert(DictionaryDto dictionaryDto) { - return Dictionary.builder() + return Dictionary.Builder.newBuilder() .phonetic(dictionaryDto.phonetic()) .word(dictionaryDto.word()) .phonetics(dictionaryDto.phonetics().stream() diff --git a/src/main/java/com/wimdupont/converter/MeaningConverter.java b/src/main/java/com/wimdupont/converter/MeaningConverter.java @@ -4,17 +4,21 @@ import com.wimdupont.client.model.dto.MeaningDto; import com.wimdupont.repository.dao.Meaning; import org.springframework.stereotype.Component; -import java.util.stream.Collectors; - @Component -record MeaningConverter(DefinitionConverter definitionConverter) { +public class MeaningConverter { + + private final DefinitionConverter definitionConverter; + + public MeaningConverter(DefinitionConverter definitionConverter) { + this.definitionConverter = definitionConverter; + } public Meaning convert(MeaningDto meaningDto) { - return Meaning.builder() + return Meaning.Builder.newBuilder() .partOfSpeech(meaningDto.partOfSpeech()) .definitions(meaningDto.definitions().stream() .map(definitionConverter::convert) - .collect(Collectors.toList())) + .toList()) .build(); } } diff --git a/src/main/java/com/wimdupont/converter/PhoneticConverter.java b/src/main/java/com/wimdupont/converter/PhoneticConverter.java @@ -5,10 +5,10 @@ import com.wimdupont.repository.dao.Phonetic; import org.springframework.stereotype.Component; @Component -record PhoneticConverter() { +public class PhoneticConverter { - public Phonetic convert(PhoneticDto phoneticDto){ - return Phonetic.builder() + public Phonetic convert(PhoneticDto phoneticDto) { + return Phonetic.Builder.newBuilder() .audio(phoneticDto.audio()) .text(phoneticDto.text()) .build(); diff --git a/src/main/java/com/wimdupont/converter/SynonymConverter.java b/src/main/java/com/wimdupont/converter/SynonymConverter.java @@ -4,10 +4,10 @@ import com.wimdupont.repository.dao.Synonym; import org.springframework.stereotype.Component; @Component -record SynonymConverter() { +public class SynonymConverter { - public Synonym convert(String relatedWord){ - return Synonym.builder() + public Synonym convert(String relatedWord) { + return Synonym.Builder.newBuilder() .word(relatedWord) .build(); } diff --git a/src/main/java/com/wimdupont/converter/WordConverter.java b/src/main/java/com/wimdupont/converter/WordConverter.java @@ -8,10 +8,16 @@ import java.util.List; import java.util.stream.Collectors; @Component -public record WordConverter(DictionaryConverter dictionaryConverter) { +public class WordConverter { + + private final DictionaryConverter dictionaryConverter; + + public WordConverter(DictionaryConverter dictionaryConverter) { + this.dictionaryConverter = dictionaryConverter; + } public Word convert(String word, List<DictionaryDto> dictionaryDtoList) { - return Word.builder() + return Word.Builder.newBuilder() .word(word) .dictionaries(dictionaryDtoList.stream() .map(dictionaryConverter::convert) diff --git a/src/main/java/com/wimdupont/repository/dao/Antonym.java b/src/main/java/com/wimdupont/repository/dao/Antonym.java @@ -1,32 +1,74 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -@Data +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.annotations.UuidGenerator; + + @Entity -@Builder -@NoArgsConstructor -@AllArgsConstructor +@JsonPOJOBuilder(withPrefix = "") public class Antonym { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String word; + protected Antonym() { + + } + + private Antonym(Builder builder) { + setId(builder.id); + setWord(builder.word); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getWord() { + return word; + } + + public void setWord(String word) { + this.word = word; + } + + @Override public String toString() { return getWord(); } + + public static final class Builder { + private String id; + private String word; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder word(String val) { + word = val; + return this; + } + + public Antonym build() { + return new Antonym(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Definition.java b/src/main/java/com/wimdupont/repository/dao/Definition.java @@ -1,33 +1,26 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import org.hibernate.annotations.UuidGenerator; + import java.util.List; -@Data @Entity -@Builder -@NoArgsConstructor -@AllArgsConstructor +@JsonPOJOBuilder(withPrefix = "") public class Definition { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; + @Column(length = 500) private String definition; + @Column(length = 500) private String example; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @@ -38,6 +31,59 @@ public class Definition { @JoinColumn(name = "definition_id") private List<Antonym> antonyms; + protected Definition() { + + } + + private Definition(Builder builder) { + setId(builder.id); + setDefinition(builder.definition); + setExample(builder.example); + setSynonyms(builder.synonyms); + setAntonyms(builder.antonyms); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDefinition() { + return definition; + } + + public void setDefinition(String definition) { + this.definition = definition; + } + + public String getExample() { + return example; + } + + public void setExample(String example) { + this.example = example; + } + + public List<Synonym> getSynonyms() { + return synonyms; + } + + public void setSynonyms(List<Synonym> synonyms) { + this.synonyms = synonyms; + } + + public List<Antonym> getAntonyms() { + return antonyms; + } + + public void setAntonyms(List<Antonym> antonyms) { + this.antonyms = antonyms; + } + + @Override public String toString() { return "\t" + getDefinition() + System.lineSeparator() + @@ -46,4 +92,47 @@ public class Definition { (getExample() == null ? "" : "Example: " + getExample())); } + public static final class Builder { + private String id; + private String definition; + private String example; + private List<Synonym> synonyms; + private List<Antonym> antonyms; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder definition(String val) { + definition = val; + return this; + } + + public Builder example(String val) { + example = val; + return this; + } + + public Builder synonyms(List<Synonym> val) { + synonyms = val; + return this; + } + + public Builder antonyms(List<Antonym> val) { + antonyms = val; + return this; + } + + public Definition build() { + return new Definition(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Dictionary.java b/src/main/java/com/wimdupont/repository/dao/Dictionary.java @@ -1,31 +1,19 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import org.hibernate.annotations.UuidGenerator; + import java.util.List; @Entity -@Builder(toBuilder = true) -@Data -@NoArgsConstructor -@AllArgsConstructor public class Dictionary { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String word; private String phonetic; @@ -37,9 +25,119 @@ public class Dictionary { @JoinColumn(name = "dictionary_id") private List<Meaning> meanings; + protected Dictionary() { + + } + + private Dictionary(Builder builder) { + setId(builder.id); + setWord(builder.word); + setPhonetic(builder.phonetic); + setPhonetics(builder.phonetics); + setOrigin(builder.origin); + setMeanings(builder.meanings); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getWord() { + return word; + } + + public void setWord(String word) { + this.word = word; + } + + public String getPhonetic() { + return phonetic; + } + + public void setPhonetic(String phonetic) { + this.phonetic = phonetic; + } + + public List<Phonetic> getPhonetics() { + return phonetics; + } + + public void setPhonetics(List<Phonetic> phonetics) { + this.phonetics = phonetics; + } + + public String getOrigin() { + return origin; + } + + public void setOrigin(String origin) { + this.origin = origin; + } + + public List<Meaning> getMeanings() { + return meanings; + } + + public void setMeanings(List<Meaning> meanings) { + this.meanings = meanings; + } + @Override public String toString() { return getMeanings().toString(); } + public static final class Builder { + private String id; + private String word; + private String phonetic; + private List<Phonetic> phonetics; + private String origin; + private List<Meaning> meanings; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder word(String val) { + word = val; + return this; + } + + public Builder phonetic(String val) { + phonetic = val; + return this; + } + + public Builder phonetics(List<Phonetic> val) { + phonetics = val; + return this; + } + + public Builder origin(String val) { + origin = val; + return this; + } + + public Builder meanings(List<Meaning> val) { + meanings = val; + return this; + } + + public Dictionary build() { + return new Dictionary(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Meaning.java b/src/main/java/com/wimdupont/repository/dao/Meaning.java @@ -1,30 +1,18 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import org.hibernate.annotations.UuidGenerator; + import java.util.List; -@Data @Entity -@Builder -@NoArgsConstructor -@AllArgsConstructor public class Meaning { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String partOfSpeech; @@ -32,9 +20,75 @@ public class Meaning { @JoinColumn(name = "meaning_id") private List<Definition> definitions; + protected Meaning() { + + } + + private Meaning(Builder builder) { + setId(builder.id); + setPartOfSpeech(builder.partOfSpeech); + setDefinitions(builder.definitions); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getPartOfSpeech() { + return partOfSpeech; + } + + public void setPartOfSpeech(String partOfSpeech) { + this.partOfSpeech = partOfSpeech; + } + + public List<Definition> getDefinitions() { + return definitions; + } + + public void setDefinitions(List<Definition> definitions) { + this.definitions = definitions; + } + @Override public String toString() { return getPartOfSpeech() + System.lineSeparator() + (getDefinitions().isEmpty() ? "" : "Definitions: " + System.lineSeparator() + getDefinitions()); } + + public static final class Builder { + private String id; + private String partOfSpeech; + private List<Definition> definitions; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder partOfSpeech(String val) { + partOfSpeech = val; + return this; + } + + public Builder definitions(List<Definition> val) { + definitions = val; + return this; + } + + public Meaning build() { + return new Meaning(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Phonetic.java b/src/main/java/com/wimdupont/repository/dao/Phonetic.java @@ -1,28 +1,81 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.annotations.UuidGenerator; @Entity -@Builder(toBuilder = true) -@Data -@NoArgsConstructor -@AllArgsConstructor public class Phonetic { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String text; private String audio; + + protected Phonetic() { + + } + + private Phonetic(Builder builder) { + setId(builder.id); + setText(builder.text); + setAudio(builder.audio); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getAudio() { + return audio; + } + + public void setAudio(String audio) { + this.audio = audio; + } + + public static final class Builder { + private String id; + private String text; + private String audio; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder text(String val) { + text = val; + return this; + } + + public Builder audio(String val) { + audio = val; + return this; + } + + public Phonetic build() { + return new Phonetic(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Synonym.java b/src/main/java/com/wimdupont/repository/dao/Synonym.java @@ -1,32 +1,70 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -@Data +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.annotations.UuidGenerator; + @Entity -@Builder -@NoArgsConstructor -@AllArgsConstructor public class Synonym { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String word; + protected Synonym() { + + } + + private Synonym(Builder builder) { + setId(builder.id); + setWord(builder.word); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getWord() { + return word; + } + + public void setWord(String word) { + this.word = word; + } + @Override public String toString() { return getWord(); } + + public static final class Builder { + private String id; + private String word; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder word(String val) { + word = val; + return this; + } + + public Synonym build() { + return new Synonym(this); + } + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Word.java b/src/main/java/com/wimdupont/repository/dao/Word.java @@ -1,40 +1,93 @@ package com.wimdupont.repository.dao; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.ToString.Exclude; -import org.hibernate.annotations.GenericGenerator; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import org.hibernate.annotations.UuidGenerator; + import java.util.List; @Entity -@Builder(toBuilder = true) -@Data -@NoArgsConstructor -@AllArgsConstructor public class Word { @Id - @GeneratedValue(generator = "uuid") - @GenericGenerator(name = "uuid", strategy = "uuid") - @Exclude + @UuidGenerator private String id; private String word; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "word_id") private List<Dictionary> dictionaries; + protected Word() { + } + + private Word(Builder builder) { + setId(builder.id); + setWord(builder.word); + setDictionaries(builder.dictionaries); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getWord() { + return word; + } + + public void setWord(String word) { + this.word = word; + } + + public List<Dictionary> getDictionaries() { + return dictionaries; + } + + public void setDictionaries(List<Dictionary> dictionaries) { + this.dictionaries = dictionaries; + } + @Override - public String toString(){ + public String toString() { return dictionaries.toString().replaceAll("\\[", "").replaceAll("]", ""); } + + public static final class Builder { + private String id; + private String word; + private List<Dictionary> dictionaries; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder id(String val) { + id = val; + return this; + } + + public Builder word(String val) { + word = val; + return this; + } + + public Builder dictionaries(List<Dictionary> val) { + dictionaries = val; + return this; + } + + public Word build() { + return new Word(this); + } + } } diff --git a/src/main/java/com/wimdupont/service/WordFetcher.java b/src/main/java/com/wimdupont/service/WordFetcher.java @@ -15,6 +15,7 @@ import java.util.Properties; public class WordFetcher { private static final String CSV_PROPERTY = "csv.dir"; + public List<String> fetch() { final Properties properties = new Properties(); try (InputStream is = getClass().getResourceAsStream("/application.properties")) { diff --git a/src/main/java/com/wimdupont/service/WordService.java b/src/main/java/com/wimdupont/service/WordService.java @@ -5,26 +5,36 @@ import com.wimdupont.client.model.dto.DictionaryDto; import com.wimdupont.converter.WordConverter; import com.wimdupont.repository.WordRepository; import com.wimdupont.repository.dao.Word; -import lombok.RequiredArgsConstructor; +import jakarta.persistence.EntityManager; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import javax.persistence.EntityManager; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @Service @Transactional -@RequiredArgsConstructor public class WordService { - private static final DictionaryAPI DICTIONARY_API = new DictionaryAPI(); + private final DictionaryAPI dictionaryAPI; private final WordRepository wordRepository; private final WordConverter wordConverter; private final EntityManager entityManager; private final WordFetcher wordFetcher; + public WordService(DictionaryAPI dictionaryAPI, + WordRepository wordRepository, + WordConverter wordConverter, + EntityManager entityManager, + WordFetcher wordFetcher) { + this.dictionaryAPI = dictionaryAPI; + this.wordRepository = wordRepository; + this.wordConverter = wordConverter; + this.entityManager = entityManager; + this.wordFetcher = wordFetcher; + } + public void saveNew() { List<String> words = wordFetcher.fetch(); List<Word> savedWordList = wordRepository.findAll(); @@ -34,7 +44,7 @@ public class WordService { words.stream() .filter(csvWord -> !savedWords.contains(csvWord)) .forEach(newWord -> { - List<DictionaryDto> response = DICTIONARY_API.getAffirmation(newWord); + List<DictionaryDto> response = dictionaryAPI.getAffirmation(newWord); if (response.isEmpty()) { System.out.printf("No results found for '%s', no data saved%n", newWord); count.get(); diff --git a/src/main/java/com/wimdupont/service/WordTester.java b/src/main/java/com/wimdupont/service/WordTester.java @@ -5,7 +5,6 @@ import com.wimdupont.client.model.dto.DictionaryDto; import com.wimdupont.converter.WordConverter; import com.wimdupont.repository.WordRepository; import com.wimdupont.repository.dao.Word; -import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -15,16 +14,25 @@ import java.util.Optional; import java.util.Scanner; @Component -@RequiredArgsConstructor @Transactional public class WordTester { - private static final DictionaryAPI DICTIONARY_API = new DictionaryAPI(); + private final DictionaryAPI dictionaryAPI; private final WordRepository wordRepository; private final WordConverter wordConverter; private final WordFetcher wordFetcher; + public WordTester(DictionaryAPI dictionaryAPI, + WordRepository wordRepository, + WordConverter wordConverter, + WordFetcher wordFetcher) { + this.dictionaryAPI = dictionaryAPI; + this.wordRepository = wordRepository; + this.wordConverter = wordConverter; + this.wordFetcher = wordFetcher; + } + public void process() { Scanner scanner = new Scanner(System.in); List<String> words = wordFetcher.fetch(); @@ -37,7 +45,7 @@ public class WordTester { Optional<Word> result = wordRepository.findByWord(word); Word saved; if (result.isEmpty()) { - List<DictionaryDto> response = DICTIONARY_API.getAffirmation(word); + List<DictionaryDto> response = dictionaryAPI.getAffirmation(word); saved = wordRepository.save(wordConverter.convert(word, response)); } else { saved = result.get();