From 900d3bdc75b4ca7d1a522e385a21a14ca95a860b Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 25 Sep 2024 12:56:35 +0200 Subject: [PATCH] use Mapper.fetchEntity to resolve sub-entities --- .../hsadminng/hs/booking/item/HsBookingItemController.java | 2 -- .../office/sepamandate/HsOfficeSepaMandateController.java | 4 ++++ src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java index 88f3a4e9..f28d418d 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java @@ -17,8 +17,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import java.time.LocalDate; import java.util.List; import java.util.UUID; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java index 9511bdd6..1ccfb457 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java @@ -1,6 +1,8 @@ package net.hostsharing.hsadminng.hs.office.sepamandate; import net.hostsharing.hsadminng.context.Context; +import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity; +import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeSepaMandatesApi; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateInsertResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandatePatchResource; @@ -135,6 +137,8 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi { }; final BiConsumer SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { + entity.setDebitor(mapper.fetchEntity(HsOfficeDebitorEntity.class, resource.getDebitorUuid())); + entity.setBankAccount(mapper.fetchEntity(HsOfficeBankAccountEntity.class, resource.getBankAccountUuid())); entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo())); }; } diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java index b16db84b..91dc725d 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java @@ -26,6 +26,7 @@ public class Mapper extends ModelMapper { EntityManager em; public Mapper() { + // make sure that resource.whateverUuid does not get mapped to entity.uuid, if resource.uuid does not exist getConfiguration().setMatchingStrategy(STRICT); } @@ -67,7 +68,7 @@ public class Mapper extends ModelMapper { if (subEntityUuid == null) { continue; } - ReflectionUtils.setField(f, target, findEntityById(f.getType(), subEntityUuid)); + ReflectionUtils.setField(f, target, fetchEntity(f.getType(), subEntityUuid)); } return target; } @@ -83,9 +84,9 @@ public class Mapper extends ModelMapper { .toArray(Field[]::new); } - private Object findEntityById(final Class entityClass, final Object subEntityUuid) { + public E fetchEntity(final Class entityClass, final Object subEntityUuid) { // using getReference would be more efficent, but results in very technical error messages - final var entity = em.find(entityClass, subEntityUuid); + final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference if (entity != null) { return entity; }