refactoring for implicit creation of dependend hosting-assets #108
@ -17,8 +17,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
|
||||||
import jakarta.persistence.PersistenceContext;
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package net.hostsharing.hsadminng.hs.office.sepamandate;
|
package net.hostsharing.hsadminng.hs.office.sepamandate;
|
||||||
|
|
||||||
import net.hostsharing.hsadminng.context.Context;
|
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.api.HsOfficeSepaMandatesApi;
|
||||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateInsertResource;
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateInsertResource;
|
||||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandatePatchResource;
|
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandatePatchResource;
|
||||||
@ -135,6 +137,8 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
|
|||||||
};
|
};
|
||||||
|
|
||||||
final BiConsumer<HsOfficeSepaMandateInsertResource, HsOfficeSepaMandateEntity> SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
|
final BiConsumer<HsOfficeSepaMandateInsertResource, HsOfficeSepaMandateEntity> 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()));
|
entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ public class Mapper extends ModelMapper {
|
|||||||
EntityManager em;
|
EntityManager em;
|
||||||
|
|
||||||
public Mapper() {
|
public Mapper() {
|
||||||
|
// make sure that resource.whateverUuid does not get mapped to entity.uuid, if resource.uuid does not exist
|
||||||
getConfiguration().setMatchingStrategy(STRICT);
|
getConfiguration().setMatchingStrategy(STRICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +68,7 @@ public class Mapper extends ModelMapper {
|
|||||||
if (subEntityUuid == null) {
|
if (subEntityUuid == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ReflectionUtils.setField(f, target, findEntityById(f.getType(), subEntityUuid));
|
ReflectionUtils.setField(f, target, fetchEntity(f.getType(), subEntityUuid));
|
||||||
}
|
}
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
@ -83,9 +84,9 @@ public class Mapper extends ModelMapper {
|
|||||||
.toArray(Field[]::new);
|
.toArray(Field[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object findEntityById(final Class<?> entityClass, final Object subEntityUuid) {
|
public <E> E fetchEntity(final Class<E> entityClass, final Object subEntityUuid) {
|
||||||
// using getReference would be more efficent, but results in very technical error messages
|
// 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) {
|
if (entity != null) {
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user