refactoring for implicit creation of dependend hosting-assets #108

Merged
hsh-michaelhoennig merged 22 commits from refactoring-for-implicit-creation-of-dependend-hosting-assets into master 2024-09-26 10:51:30 +02:00
3 changed files with 22 additions and 7 deletions
Showing only changes of commit 0701d8a629 - Show all commits

View File

@ -84,7 +84,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class); final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class);
if ( body.getDebitorRel() != null ) { if ( body.getDebitorRel() != null ) {
body.getDebitorRel().setType(DEBITOR.name()); body.getDebitorRel().setType(DEBITOR.name());
final var debitorRel = mapper.map(body.getDebitorRel(), HsOfficeRelationRealEntity.class); final var debitorRel = mapper.map("debitorRel.", body.getDebitorRel(), HsOfficeRelationRealEntity.class);
validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor()); validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor());
validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder()); validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder());
validateEntityExists("debitorRel.contactUuid", debitorRel.getContact()); validateEntityExists("debitorRel.contactUuid", debitorRel.getContact());

View File

@ -47,6 +47,10 @@ abstract class Mapper extends ModelMapper {
@Override @Override
public <D> D map(final Object source, final Class<D> destinationType) { public <D> D map(final Object source, final Class<D> destinationType) {
return map("", source, destinationType);
}
public <D> D map(final String namePrefix, final Object source, final Class<D> destinationType) {
final var target = super.map(source, destinationType); final var target = super.map(source, destinationType);
for (Field f : getDeclaredFieldsIncludingSuperClasses(destinationType)) { for (Field f : getDeclaredFieldsIncludingSuperClasses(destinationType)) {
if (f.getAnnotation(ManyToOne.class) == null) { if (f.getAnnotation(ManyToOne.class) == null) {
@ -66,7 +70,7 @@ abstract class Mapper extends ModelMapper {
if (subEntityUuid == null) { if (subEntityUuid == null) {
continue; continue;
} }
ReflectionUtils.setField(f, target, fetchEntity(f.getType(), subEntityUuid)); ReflectionUtils.setField(f, target, fetchEntity(namePrefix + f.getName() + ".uuid", f.getType(), subEntityUuid));
} }
return target; return target;
} }
@ -82,13 +86,15 @@ abstract class Mapper extends ModelMapper {
.toArray(Field[]::new); .toArray(Field[]::new);
} }
public <E> E fetchEntity(final Class<E> entityClass, final Object subEntityUuid) { public <E> E fetchEntity(final String propertyName, 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 efficient, but results in very technical error messages
final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference
if (entity != null) { if (entity != null) {
return entity; return entity;
} }
throw new ValidationException("Unable to find " + DisplayName.of(entityClass) + " by uuid: " + subEntityUuid); throw new ValidationException(
"Unable to find " + DisplayName.of(entityClass) +
" by " + propertyName + ": " + subEntityUuid);
} }
public <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) { public <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
@ -99,4 +105,13 @@ abstract class Mapper extends ModelMapper {
postMapper.accept(source, target); postMapper.accept(source, target);
return target; return target;
} }
public <S, T> T map(final String namePrefix, final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
if (source == null) {
return null;
}
final var target = map(source, targetClass);
postMapper.accept(source, target);
return target;
}
} }

View File

@ -405,7 +405,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu
.post("http://localhost/api/hs/office/debitors") .post("http://localhost/api/hs/office/debitors")
.then().log().all().assertThat() .then().log().all().assertThat()
.statusCode(400) .statusCode(400)
.body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contactUuid: 00000000-0000-0000-0000-000000000000")); .body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contact.uuid: 00000000-0000-0000-0000-000000000000"));
// @formatter:on // @formatter:on
} }
@ -433,7 +433,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu
.post("http://localhost/api/hs/office/debitors") .post("http://localhost/api/hs/office/debitors")
.then().log().all().assertThat() .then().log().all().assertThat()
.statusCode(400) .statusCode(400)
.body("message", is("ERROR: [400] Unable to find RealRelation by uuid: 00000000-0000-0000-0000-000000000000")); .body("message", is("ERROR: [400] Unable to find RealRelation by debitorRel.uuid: 00000000-0000-0000-0000-000000000000"));
// @formatter:on // @formatter:on
} }
} }