From a0a6194aeb66606eeb7a4cbd1db5ab39f16de060 Mon Sep 17 00:00:00 2001 From: Chip3211 Date: Fri, 13 Sep 2024 22:11:38 +0300 Subject: [PATCH 1/2] - Removed static units --- .../ac_scripts/S01_InitDatabaseScript.java | 33 - .../ingredient/model/Ingredient.java | 2 +- .../recipe/service/RecipeService.java | 35 +- .../recipe/service/ScraperService.java | 20 +- .../ba_entities/unit/model/Unit.java | 9 +- .../unit/repository/UnitRepository.java | 4 - src/main/resources/initialization/units.json | 572 ------------------ 7 files changed, 40 insertions(+), 635 deletions(-) delete mode 100644 src/main/resources/initialization/units.json diff --git a/src/main/java/de/flavormate/ac_scripts/S01_InitDatabaseScript.java b/src/main/java/de/flavormate/ac_scripts/S01_InitDatabaseScript.java index 6caf58b..9902bd9 100644 --- a/src/main/java/de/flavormate/ac_scripts/S01_InitDatabaseScript.java +++ b/src/main/java/de/flavormate/ac_scripts/S01_InitDatabaseScript.java @@ -13,8 +13,6 @@ import de.flavormate.ba_entities.categoryGroup.repository.CategoryGroupRepository; import de.flavormate.ba_entities.role.model.Role; import de.flavormate.ba_entities.role.repository.RoleRepository; -import de.flavormate.ba_entities.unit.model.Unit; -import de.flavormate.ba_entities.unit.repository.UnitRepository; import de.flavormate.utils.JSONUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -43,9 +41,6 @@ public class S01_InitDatabaseScript extends AScript { @Value("classpath*:initialization/l10n/categoryGroups/*.json") private Resource[] categoryGroupsLocalizationsFiles; - @Value("classpath:initialization/units.json") - private Resource unitsFile; - @Autowired private RoleRepository roleRepository; @@ -61,9 +56,6 @@ public class S01_InitDatabaseScript extends AScript { @Autowired private CategoryGroupLocalizationRepository categoryGroupLocalizationRepository; - @Autowired - private UnitRepository unitRepository; - public S01_InitDatabaseScript() { super("Initialize Database"); } @@ -72,7 +64,6 @@ public void run() { log("Starting database initialization"); initializeRoles(); - initializeUnits(); initializeCategoryGroups(); initializeCategoryGroupLocalizations(); @@ -230,28 +221,4 @@ private Boolean initializeCategoryGroupLocalizations() { return false; } } - - private Boolean initializeUnits() { - try { - if (unitRepository.count() > 0) { - log("Skipping unit initialization"); - return true; - } - - log("Reading units from JSON file"); - var unitsArr = JSONUtils.mapper.readValue(unitsFile.getInputStream(), Unit[].class); - var units = Arrays.asList(unitsArr); - - log("Found {} units", units.size()); - - log("Saving units into the database"); - unitRepository.saveAll(units); - - log("Saved {} units", units.size()); - return true; - } catch (Exception e) { - warning("Units could not be initialized"); - return false; - } - } } diff --git a/src/main/java/de/flavormate/ba_entities/ingredient/model/Ingredient.java b/src/main/java/de/flavormate/ba_entities/ingredient/model/Ingredient.java index 4c7d249..09723cb 100644 --- a/src/main/java/de/flavormate/ba_entities/ingredient/model/Ingredient.java +++ b/src/main/java/de/flavormate/ba_entities/ingredient/model/Ingredient.java @@ -24,7 +24,7 @@ public class Ingredient extends BaseEntity { @Column(nullable = false) private Double amount; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "unit_id", referencedColumnName = "id") private Unit unit; diff --git a/src/main/java/de/flavormate/ba_entities/recipe/service/RecipeService.java b/src/main/java/de/flavormate/ba_entities/recipe/service/RecipeService.java index 1c3ffc8..8afe3e7 100644 --- a/src/main/java/de/flavormate/ba_entities/recipe/service/RecipeService.java +++ b/src/main/java/de/flavormate/ba_entities/recipe/service/RecipeService.java @@ -32,6 +32,8 @@ import de.flavormate.ba_entities.story.repository.StoryRepository; import de.flavormate.ba_entities.tag.model.Tag; import de.flavormate.ba_entities.tag.repository.TagRepository; +import de.flavormate.ba_entities.unit.model.Unit; +import de.flavormate.ba_entities.unit.repository.UnitRepository; import de.flavormate.utils.JSONUtils; import jakarta.transaction.Transactional; import lombok.extern.slf4j.Slf4j; @@ -65,11 +67,12 @@ public class RecipeService extends BaseService implements ICRUDService { - var ingredients = iG.ingredients().stream().map(i -> (Ingredient) Ingredient.builder() - .amount(i.amount()).label(i.label()).unit(i.unit()).build()).toList(); + var ingredients = iG.ingredients().stream().map(i -> { + Unit unit = null; + if (i.unit() != null) { + unit = unitRepository.findByLabel(i.unit().getLabel()).orElse(null); + if (unit == null) { + i.unit().setId(null); + unit = unitRepository.save(i.unit()); + } + } + return (Ingredient) Ingredient.builder() + .amount(i.amount()).label(i.label()).unit(unit).build(); + }).toList(); return (IngredientGroup) IngredientGroup.builder().ingredients(ingredients) .label(iG.label()).build(); }).toList(); @@ -198,8 +212,19 @@ public Recipe update(Long id, JsonNode json) throws CustomException { var ingredientGroups = data.ingredientGroups().stream().map(iG -> { var ingredients = iG - .ingredients().stream().map(i -> (Ingredient) Ingredient.builder() - .amount(i.amount()).label(i.label()).unit(i.unit()).build()) + .ingredients().stream().map(i -> { + Unit unit = null; + if (i.unit() != null) { + unit = unitRepository.findByLabel(i.unit().getLabel()).orElse(null); + if (unit == null) { + i.unit().setId(null); + unit = unitRepository.save(i.unit()); + } + } + + return (Ingredient) Ingredient.builder() + .amount(i.amount()).label(i.label()).unit(unit).build(); + }) .toList(); return (IngredientGroup) IngredientGroup.builder().ingredients(ingredients) .label(iG.label()).build(); diff --git a/src/main/java/de/flavormate/ba_entities/recipe/service/ScraperService.java b/src/main/java/de/flavormate/ba_entities/recipe/service/ScraperService.java index d0bf8a5..1874701 100644 --- a/src/main/java/de/flavormate/ba_entities/recipe/service/ScraperService.java +++ b/src/main/java/de/flavormate/ba_entities/recipe/service/ScraperService.java @@ -138,34 +138,28 @@ private IngredientGroupDraft getIngredientGroup(JsonNode json) { .collect(Collectors.toCollection(ArrayList::new)); var ttt = ingredients.stream().map(i -> { - var list = new ArrayList(Arrays.asList(i.split(" ")));// List.of(i.split(" - // "));// - // .removeAll(); + var list = new ArrayList(Arrays.asList(i.split(" "))); list.removeAll(Arrays.asList("", null)); String[] parts = list.toArray(new String[0]); double amount = NumberUtils.tryParseDouble(parts[0], -1); String label; - Optional unit; + Unit unit; int startIndex = 0; if (amount < 0) { - var unitList = unitRepository.findByLabelOrShortLabel(parts[0], parts[0]); - unit = unitList.isEmpty() ? Optional.empty() : Optional.of(unitList.get(0)); - if (unit.isEmpty()) { - unit = unitRepository.findByLabel(""); + unit = unitRepository.findByLabel(parts[0]).orElse(null); + if (unit == null) { startIndex = 0; } else { startIndex = 1; } } else { - var unitList = unitRepository.findByLabelOrShortLabel(parts[1], parts[1]); - unit = unitList.isEmpty() ? Optional.empty() : Optional.of(unitList.get(0)); + unit = unitRepository.findByLabel(parts[1]).orElse(null); - if (unit.isEmpty()) { - unit = unitRepository.findByLabel(""); + if (unit == null) { startIndex = 1; } else { startIndex = 2; @@ -174,7 +168,7 @@ private IngredientGroupDraft getIngredientGroup(JsonNode json) { label = String.join(" ", List.of(parts).subList(startIndex, parts.length)); - return new IngredientDraft(amount, unit.get(), label); + return new IngredientDraft(amount, unit, label); }).toList(); return new IngredientGroupDraft("", ttt); diff --git a/src/main/java/de/flavormate/ba_entities/unit/model/Unit.java b/src/main/java/de/flavormate/ba_entities/unit/model/Unit.java index ef29873..adcc0b1 100644 --- a/src/main/java/de/flavormate/ba_entities/unit/model/Unit.java +++ b/src/main/java/de/flavormate/ba_entities/unit/model/Unit.java @@ -1,6 +1,6 @@ package de.flavormate.ba_entities.unit.model; -import de.flavormate.aa_interfaces.models.ManualBaseEntity; +import de.flavormate.aa_interfaces.models.BaseEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Table; @@ -49,7 +49,7 @@ @SuperBuilder @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true) @ToString(onlyExplicitlyIncluded = true, callSuper = true) -public class Unit extends ManualBaseEntity { +public class Unit extends BaseEntity { /** * The label of the unit. This field is marked as non-nullable in the database. @@ -57,9 +57,4 @@ public class Unit extends ManualBaseEntity { @NotNull @Column(nullable = false) private String label; - - /** - * The short label of the unit. - */ - private String shortLabel; } diff --git a/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java b/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java index 123c20e..e544011 100644 --- a/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java +++ b/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java @@ -3,13 +3,9 @@ import de.flavormate.ba_entities.unit.model.Unit; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; import java.util.Optional; public interface UnitRepository extends JpaRepository { Optional findByLabel(String label); - - List findByLabelOrShortLabel(String label, String shortLabel); - } diff --git a/src/main/resources/initialization/units.json b/src/main/resources/initialization/units.json deleted file mode 100644 index 9b9462c..0000000 --- a/src/main/resources/initialization/units.json +++ /dev/null @@ -1,572 +0,0 @@ -[ - { - "id": 1, - "label": "Becher", - "shortLabel": "" - }, - { - "id": 2, - "label": "Beet(e)", - "shortLabel": "" - }, - { - "id": 3, - "label": "Beutel", - "shortLabel": "" - }, - { - "id": 4, - "label": "Blatt", - "shortLabel": "" - }, - { - "id": 5, - "label": "Blätter", - "shortLabel": "" - }, - { - "id": 6, - "label": "Bund", - "shortLabel": "" - }, - { - "id": 7, - "label": "Bündel", - "shortLabel": "" - }, - { - "id": 8, - "label": "cl", - "shortLabel": "" - }, - { - "id": 9, - "label": "cm", - "shortLabel": "" - }, - { - "id": 10, - "label": "dicke", - "shortLabel": "" - }, - { - "id": 11, - "label": "dl", - "shortLabel": "" - }, - { - "id": 12, - "label": "Dose(n)", - "shortLabel": "" - }, - { - "id": 13, - "label": "dünne", - "shortLabel": "" - }, - { - "id": 14, - "label": "Ecke(n)", - "shortLabel": "" - }, - { - "id": 15, - "label": "Eimer", - "shortLabel": "" - }, - { - "id": 16, - "label": "einige", - "shortLabel": "" - }, - { - "id": 17, - "label": "einige Stiele", - "shortLabel": "" - }, - { - "id": 18, - "label": "Esslöffel", - "shortLabel": "EL" - }, - { - "id": 19, - "label": "Esslöffel, gehäuft", - "shortLabel": "EL, gehäuft" - }, - { - "id": 20, - "label": "Esslöffel, gestrichen", - "shortLabel": "EL, gestr." - }, - { - "id": 21, - "label": "etwas", - "shortLabel": "" - }, - { - "id": 22, - "label": "eventuell", - "shortLabel": "evtl." - }, - { - "id": 23, - "label": "extra", - "shortLabel": "" - }, - { - "id": 24, - "label": "Fässchen", - "shortLabel": "" - }, - { - "id": 25, - "label": "Fläschchen", - "shortLabel": "" - }, - { - "id": 26, - "label": "Flasche(n)", - "shortLabel": "" - }, - { - "id": 27, - "label": "Gramm", - "shortLabel": "gr" - }, - { - "id": 28, - "label": "Glas", - "shortLabel": "" - }, - { - "id": 29, - "label": "Gläser", - "shortLabel": "" - }, - { - "id": 30, - "label": "große Dose(n)", - "shortLabel": "gr. Dose(n)" - }, - { - "id": 31, - "label": "große Flasche(n)", - "shortLabel": "gr. Flasche(n)" - }, - { - "id": 32, - "label": "großes Glas", - "shortLabel": "gr. Glas" - }, - { - "id": 33, - "label": "große Gläser", - "shortLabel": "gr. Gläser" - }, - { - "id": 34, - "label": "großer Kopf", - "shortLabel": "gr. Kopf" - }, - { - "id": 35, - "label": "große Scheibe(n)", - "shortLabel": "gr. Scheibe(n)" - }, - { - "id": 36, - "label": "große(s) Stück(e)", - "shortLabel": "gr. Stück(e)" - }, - { - "id": 37, - "label": "große", - "shortLabel": "gr." - }, - { - "id": 38, - "label": "großen", - "shortLabel": "gr." - }, - { - "id": 39, - "label": "großer", - "shortLabel": "gr." - }, - { - "id": 40, - "label": "großes", - "shortLabel": "gr." - }, - { - "id": 41, - "label": "halbe", - "shortLabel": "1/2" - }, - { - "id": 42, - "label": "Halm(e)", - "shortLabel": "" - }, - { - "id": 43, - "label": "Handvoll", - "shortLabel": "" - }, - { - "id": 44, - "label": "Kästchen", - "shortLabel": "" - }, - { - "id": 45, - "label": "Kilogramm", - "shortLabel": "kg" - }, - { - "id": 46, - "label": "kleiner Bund", - "shortLabel": "kl. Bund" - }, - { - "id": 47, - "label": "kleine Dose(n)", - "shortLabel": "kl. Dose(n)" - }, - { - "id": 48, - "label": "kleine Flasche(n)", - "shortLabel": "kl. Flasche(n)" - }, - { - "id": 49, - "label": "kleines Glas", - "shortLabel": "kl. Glas" - }, - { - "id": 50, - "label": "kleine Gläser", - "shortLabel": "kl. Gläser" - }, - { - "id": 51, - "label": "kleiner Kopf", - "shortLabel": "kl. Kopf" - }, - { - "id": 52, - "label": "kleine Scheibe(n)", - "shortLabel": "kl. Scheibe(n)" - }, - { - "id": 53, - "label": "kleine Stange(n)", - "shortLabel": "kl. Stange(n)" - }, - { - "id": 54, - "label": "kleine(s) Stück(e)", - "shortLabel": "kl. Stück(e)" - }, - { - "id": 55, - "label": "kleine", - "shortLabel": "kl." - }, - { - "id": 56, - "label": "kleiner", - "shortLabel": "kl." - }, - { - "id": 57, - "label": "kleines", - "shortLabel": "kl." - }, - { - "id": 58, - "label": "Knolle(n)", - "shortLabel": "" - }, - { - "id": 59, - "label": "Kopf", - "shortLabel": "" - }, - { - "id": 60, - "label": "Köpfe", - "shortLabel": "" - }, - { - "id": 61, - "label": "Körner", - "shortLabel": "" - }, - { - "id": 62, - "label": "Kugel(n)", - "shortLabel": "" - }, - { - "id": 63, - "label": "Liter", - "shortLabel": "L" - }, - { - "id": 64, - "label": "mittel große", - "shortLabel": "mittel gr." - }, - { - "id": 65, - "label": "mittel großer", - "shortLabel": "mittel gr." - }, - { - "id": 66, - "label": "mittel großes", - "shortLabel": "mittel gr." - }, - { - "id": 67, - "label": "mehr", - "shortLabel": "" - }, - { - "id": 68, - "label": "Milligramm", - "shortLabel": "mg" - }, - { - "id": 69, - "label": "Milliliter", - "shortLabel": "ml" - }, - { - "id": 70, - "label": "Messerspitze", - "shortLabel": "Msp." - }, - { - "id": 71, - "label": "nach Belieben", - "shortLabel": "n. B." - }, - { - "id": 72, - "label": "Paar", - "shortLabel": "" - }, - { - "id": 73, - "label": "Paket", - "shortLabel": "" - }, - { - "id": 74, - "label": "Päckchen", - "shortLabel": "Pck." - }, - { - "id": 75, - "label": "Paket", - "shortLabel": "Pkt." - }, - { - "id": 76, - "label": "Platte(n)", - "shortLabel": "" - }, - { - "id": 77, - "label": "Portion(en)", - "shortLabel": "Port." - }, - { - "id": 78, - "label": "Prise(n)", - "shortLabel": "" - }, - { - "id": 79, - "label": "Prozent", - "shortLabel": "%" - }, - { - "id": 80, - "label": "Riegel", - "shortLabel": "" - }, - { - "id": 81, - "label": "Ring(e)", - "shortLabel": "" - }, - { - "id": 82, - "label": "Rippe(n)", - "shortLabel": "" - }, - { - "id": 83, - "label": "Rispe(n)", - "shortLabel": "" - }, - { - "id": 84, - "label": "Rolle(n)", - "shortLabel": "" - }, - { - "id": 85, - "label": "Schälchen", - "shortLabel": "" - }, - { - "id": 86, - "label": "Scheibe(n)", - "shortLabel": "" - }, - { - "id": 87, - "label": "Schuss", - "shortLabel": "" - }, - { - "id": 88, - "label": "Spritzer", - "shortLabel": "" - }, - { - "id": 89, - "label": "Stange(n)", - "shortLabel": "" - }, - { - "id": 90, - "label": "Stängel", - "shortLabel": "" - }, - { - "id": 91, - "label": "Staude(n)", - "shortLabel": "" - }, - { - "id": 92, - "label": "Stiel(e)", - "shortLabel": "" - }, - { - "id": 93, - "label": "Streifen", - "shortLabel": "" - }, - { - "id": 94, - "label": "Stück(e)", - "shortLabel": "" - }, - { - "id": 95, - "label": "Tablette(n)", - "shortLabel": "" - }, - { - "id": 96, - "label": "Tafel(n)", - "shortLabel": "" - }, - { - "id": 97, - "label": "Tasse(n)", - "shortLabel": "" - }, - { - "id": 98, - "label": "Teil(e)", - "shortLabel": "" - }, - { - "id": 99, - "label": "Teelöffel", - "shortLabel": "TL" - }, - { - "id": 100, - "label": "Teelöffel gehäuft", - "shortLabel": "TL gehäuft" - }, - { - "id": 101, - "label": "Teelöffel gestrichen", - "shortLabel": "TL gestr." - }, - { - "id": 102, - "label": "Topf", - "shortLabel": "" - }, - { - "id": 103, - "label": "Tropfen", - "shortLabel": "" - }, - { - "id": 104, - "label": "Tube(n)", - "shortLabel": "" - }, - { - "id": 105, - "label": "Tüte(n)", - "shortLabel": "" - }, - { - "id": 106, - "label": "viel", - "shortLabel": "" - }, - { - "id": 107, - "label": "wenig", - "shortLabel": "" - }, - { - "id": 108, - "label": "Würfel", - "shortLabel": "" - }, - { - "id": 109, - "label": "Wurzel(n)", - "shortLabel": "" - }, - { - "id": 110, - "label": "Zehe(n)", - "shortLabel": "" - }, - { - "id": 111, - "label": "Zweig(e)", - "shortLabel": "" - }, - { - "id": 112, - "label": "ganze", - "shortLabel": "" - }, - { - "id": 113, - "label": "", - "shortLabel": "" - }, - { - "id": 114, - "label": "Abrieb", - "shortLabel": "" - } -] From aa639a3329fc9d7d2ce41c0dfc84afc538938ec5 Mon Sep 17 00:00:00 2001 From: Chip3211 Date: Fri, 13 Sep 2024 23:06:43 +0300 Subject: [PATCH 2/2] - Added script to clean unused units --- .../ac_scripts/S99_CleanScript.java | 29 +++++++++++++++++++ .../unit/repository/UnitRepository.java | 5 ++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/de/flavormate/ac_scripts/S99_CleanScript.java b/src/main/java/de/flavormate/ac_scripts/S99_CleanScript.java index b0d28f1..e80a947 100644 --- a/src/main/java/de/flavormate/ac_scripts/S99_CleanScript.java +++ b/src/main/java/de/flavormate/ac_scripts/S99_CleanScript.java @@ -5,6 +5,7 @@ import de.flavormate.ba_entities.tag.repository.TagRepository; import de.flavormate.ba_entities.token.model.Token; import de.flavormate.ba_entities.token.repository.TokenRepository; +import de.flavormate.ba_entities.unit.repository.UnitRepository; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -26,6 +27,9 @@ public class S99_CleanScript extends AScript { @Autowired private TagRepository tagRepository; + @Autowired + private UnitRepository unitRepository; + @Autowired private TokenRepository tokenRepository; @@ -38,11 +42,13 @@ public void run() { cleanHighlights(); cleanTags(); + cleanUnits(); cleanTokens(); log("Finished database cleaning"); } + private Boolean cleanHighlights() { try { if (highlightRepository.count() == 0) { @@ -95,6 +101,29 @@ private Boolean cleanTags() { } } + private boolean cleanUnits() { + try { + var units = unitRepository.findEmpty(); + + if (units.isEmpty()) { + log("Skipping unit cleaning"); + return true; + } + + log("Found {} invalid units", units.size()); + + log("Deleting invalid units"); + + unitRepository.deleteAll(units); + + log("Deleted {} units", units.size()); + return true; + } catch (Exception e) { + warning("Units could not be cleaned"); + return false; + } + } + private Boolean cleanTokens() { try { if (tokenRepository.count() == 0) { diff --git a/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java b/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java index e544011..9360720 100644 --- a/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java +++ b/src/main/java/de/flavormate/ba_entities/unit/repository/UnitRepository.java @@ -2,10 +2,15 @@ import de.flavormate.ba_entities.unit.model.Unit; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import java.util.List; import java.util.Optional; public interface UnitRepository extends JpaRepository { Optional findByLabel(String label); + + @Query(nativeQuery = true, value = "SELECT u.* FROM units u LEFT JOIN ingredients i ON u.id = i.unit_id WHERE i.unit_id IS NULL") + List findEmpty(); }