From a4099f1de08dba63bb0039c77a5340712a47e510 Mon Sep 17 00:00:00 2001 From: Theo Reichert Date: Wed, 29 Nov 2023 16:26:20 +0100 Subject: [PATCH] Fully adapt catalog to salespoint Co-authored-by: Simon Bruder --- src/main/asciidoc/models/design/catalog.puml | 50 +++++++++++------ src/main/asciidoc/models/design/catalog.svg | 4 +- .../catering/catalog/CateringCatalog.java | 33 +++++++++++- .../java/catering/catalog/Consumable.java | 54 ++++++++++++------- src/main/java/catering/catalog/Rentable.java | 47 +++++++++++----- 5 files changed, 135 insertions(+), 53 deletions(-) diff --git a/src/main/asciidoc/models/design/catalog.puml b/src/main/asciidoc/models/design/catalog.puml index 8ecfeab..79cddca 100644 --- a/src/main/asciidoc/models/design/catalog.puml +++ b/src/main/asciidoc/models/design/catalog.puml @@ -6,6 +6,11 @@ package javax.money { class MonetaryAmount } +package java.util { + class Optional + class Set +} + package Salespoint { 'https://st.inf.tu-dresden.de/SalesPoint/api//org/salespointframework/catalog/Catalog.html' interface Catalog { @@ -33,43 +38,54 @@ package Salespoint { ' } } +package catering.order { + enum OrderType +} + package catering.catalog { - interface RentableCatalog { + + interface CateringCatalog { + DEFAULT_SORT : Sort + + findByCategories() + + findRentablesByCategories() + + findConsumablesByCategories() } - interface ConsumableCatalog { - + DEFAULT_SORT : Sort - } - RentableCatalog --|> Catalog - ConsumableCatalog --|> Catalog + CateringCatalog --|> Catalog class Consumable { - promotionPrice : MonetaryAmount - wholesalePrice : MonetaryAmount - + Consumable(name : String, retailPrice : MonetaryAmount, wholesalePrice : MonetaryAmount) : Consumable - + Consumable(name : String, retailPrice : MonetaryAmount, wholesalePrice : MonetaryAmount, promotionPrice : MonetaryAmount) : Consumable + + Consumable(name : String, retailPrice : MonetaryAmount, wholesalePrice : MonetaryAmount, promotionPrice : Optional, Set categories) : Consumable + getPrice() : MonetaryAmount + getRetailPrice() : MonetaryAmount - + setPrice(price : MonetaryAmount) : void - + getPromotionPrice(): MonetaryAmount - + setPromotionPrice(price : MonetaryAmount) - + getWholeSalePrice() : MonetaryAmount - + setWholeSalePrice(price : MonetaryAmount) + + setRetailPrice(price : MonetaryAmount) : void + + getPromotionPrice(): Optional + + setPromotionPrice(price : Optional) + + getWholesalePrice() : MonetaryAmount + + setWholesalePrice(price : MonetaryAmount) } Consumable --|> Product Consumable ..> MonetaryAmount + Consumable ..> java.util.Optional + Consumable ..> java.util.Set + Consumable ..> catering.order.OrderType class Rentable { - + Rentable(name : String, pricePerHour : MonetaryAmount) : Rentable + - wholesalePrice : MonetaryAmount + + Rentable(name : String, pricePerHour : MonetaryAmount, wholesalePrice : MonetaryAmount, Set categories) : Rentable + + getWholesalePrice() : MonetaryAmount + + setWholesalePrice(price : MonetaryAmount) } Rentable --|> Product + Rentable ..> java.util.Set + Rentable ..> catering.order.OrderType - class CatalogDataInitalizer { + class CatalogDataInitializer { - rentableCatalog : RentableCatalog - consumableCatalog : ConsumableCatalog - + initalize() + + initialize() } - CatalogDataInitalizer ..|> DataInitializer + CatalogDataInitializer ..|> DataInitializer } '#TODO: to determine which Products of a Category to offer I need to retrieve information about availability of e.g. Cooks and ServicePersonel from the <>rs or Rentables from the Inventory' diff --git a/src/main/asciidoc/models/design/catalog.svg b/src/main/asciidoc/models/design/catalog.svg index fdb7257..71bcbda 100644 --- a/src/main/asciidoc/models/design/catalog.svg +++ b/src/main/asciidoc/models/design/catalog.svg @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:52fe93917df1aeb09ed27224f70d1f7e3a6460b7ca2daeacb575bbe134bb0922 -size 38640 +oid sha256:189975e2ba132f15225928a9b15be7166ba6cb7e89973f838263b8adc4acbefe +size 39931 diff --git a/src/main/java/catering/catalog/CateringCatalog.java b/src/main/java/catering/catalog/CateringCatalog.java index 0231ed5..7262ace 100644 --- a/src/main/java/catering/catalog/CateringCatalog.java +++ b/src/main/java/catering/catalog/CateringCatalog.java @@ -17,8 +17,37 @@ package catering.catalog; import org.salespointframework.catalog.Catalog; +import org.salespointframework.catalog.Product; import org.springframework.data.domain.Sort; +import org.springframework.data.util.Streamable; +import org.springframework.lang.NonNull; + +import catering.order.OrderType; + +public interface CateringCatalog extends Catalog { + static final Sort DEFAULT_SORT = Sort.sort(Product.class).by(Product::getName).ascending(); + + /** + * Returns all {@link Product}s by type ordered by the given {@link Sort}. + * + * @param category an element of {@link OrderType}, must not be {@literal null}. + * @param sort must not be {@literal null}. + * @return the consumables with the given category, never {@literal null}. + */ + Streamable findByCategories(OrderType category, Sort sort); + + /** + * Returns all {@link Product}s by type ordered by their identifier. + * + * @param category must not be {@literal null}. + * @return the Product with the given category, never {@literal null}. + */ + default Streamable findByCategories(@NonNull OrderType category) { + return findByCategories(category, DEFAULT_SORT); + } + + Streamable findRentablesByCategories(@NonNull String category); + + Streamable findConsumablesByCategories(@NonNull String category); -public interface CateringCatalog extends Catalog { - static final Sort DEFAULT_SORT = Sort.sort(CatalogDummy.class).by(CatalogDummy::getId).descending(); } diff --git a/src/main/java/catering/catalog/Consumable.java b/src/main/java/catering/catalog/Consumable.java index 04f989d..8fc52d8 100644 --- a/src/main/java/catering/catalog/Consumable.java +++ b/src/main/java/catering/catalog/Consumable.java @@ -1,47 +1,61 @@ package catering.catalog; +import java.util.Optional; +import java.util.Set; + import javax.money.MonetaryAmount; -import com.mysema.commons.lang.Assert; -import jakarta.persistence.Entity; + import org.salespointframework.catalog.Product; import org.springframework.lang.NonNull; +import org.springframework.util.Assert; + +import jakarta.persistence.Entity; + +import catering.order.OrderType; @Entity -class Consumable extends Product { +public class Consumable extends Product { private MonetaryAmount wholesalePrice; private MonetaryAmount promotionPrice; - public Consumable(String name, MonetaryAmount price, Iterable categories) { - super(name,price); - Assert.notNull(price, "Price must not be null!"); -// for (String category : categories) { -// this.addCategory(category); -// } - categories.forEach(this::addCategory); + + @SuppressWarnings({ "deprecation" }) + public Consumable() { + } + + public Consumable(String name, MonetaryAmount price, MonetaryAmount wholesalePrice, + Optional promotionPrice, Set categories) { + super(name, price); + Assert.notNull(wholesalePrice, "wholesalePrice must not be null!"); + Assert.notNull(promotionPrice, "promotionPrice must not be null!"); + this.wholesalePrice = wholesalePrice; + this.promotionPrice = promotionPrice.orElse(null); + categories.stream().map(OrderType::toString).forEach(this::addCategory); } @Override public @NonNull MonetaryAmount getPrice() { - MonetaryAmount normal_price = super.getPrice(); - return normal_price.isGreaterThan(promotionPrice) ? promotionPrice : normal_price; + return getPromotionPrice().orElseGet(super::getPrice); } - @NonNull MonetaryAmount getRetailPrice() { + + public @NonNull MonetaryAmount getRetailPrice() { return super.getPrice(); } - @NonNull MonetaryAmount getPromotionPrice() { - return promotionPrice; + + public @NonNull Optional getPromotionPrice() { + return Optional.ofNullable(promotionPrice); } - @NonNull MonetaryAmount getWholesalePrice() { + + public @NonNull MonetaryAmount getWholesalePrice() { return wholesalePrice; } - public void setPromotionPrice(MonetaryAmount price) { - Assert.notNull(price, "promotionPrice must not be null!"); - promotionPrice = price; + public void setPromotionPrice(Optional price) { + promotionPrice = price.orElse(null); } + public void setWholesalePrice(MonetaryAmount price) { Assert.notNull(price, "wholesalePrice must not be null!"); wholesalePrice = price; } - } diff --git a/src/main/java/catering/catalog/Rentable.java b/src/main/java/catering/catalog/Rentable.java index 6a3cf98..8f4d603 100644 --- a/src/main/java/catering/catalog/Rentable.java +++ b/src/main/java/catering/catalog/Rentable.java @@ -1,21 +1,44 @@ package catering.catalog; -import com.mysema.commons.lang.Assert; -import jakarta.persistence.Entity; -import org.salespointframework.catalog.Product; -import org.springframework.lang.NonNull; +import java.util.Set; import javax.money.MonetaryAmount; +import org.salespointframework.catalog.Product; +import org.springframework.lang.NonNull; +import org.springframework.util.Assert; + +import catering.order.OrderType; +import jakarta.persistence.Entity; + @Entity -class Rentable extends Product { - public Rentable(String name, javax.money.MonetaryAmount price, Iterable categories) { - super(name,price); - Assert.notNull(price, "Price must not be null!"); -// for (String category : categories) { -// this.addCategory(category); -// } - categories.forEach(this::addCategory); +public class Rentable extends Product { + + private MonetaryAmount wholesalePrice; + + @SuppressWarnings({ "deprecation" }) + public Rentable() { } + public Rentable(String name, MonetaryAmount pricePerHour, MonetaryAmount wholesalePrice, + Set categories) { + super(name, pricePerHour); + this.wholesalePrice = wholesalePrice; + Assert.notNull(pricePerHour, "pricePerHour must not be null!"); + Assert.notNull(wholesalePrice, "wholesalePrice must not be null!"); + categories.stream().map(OrderType::toString).forEach(this::addCategory); + } + + public @NonNull MonetaryAmount getWholesalePrice() { + return wholesalePrice; + } + + public void setWholesalePrice(MonetaryAmount price) { + Assert.notNull(price, "wholesalePrice must not be null!"); + wholesalePrice = price; + } + + public MonetaryAmount getRetailPrice() { + return super.getPrice(); + } }