From 568602dfcb7a6af71d41e2b585234a4bed6be4dd Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 5 Oct 2024 13:35:46 +0200 Subject: [PATCH] for legacy compatibility, require all domain-sub-setup-hosting-assets but mainly just the type --- .../DomainSetupHostingAssetFactory.java | 48 +++++++++---------- .../asset/factories/ToStringConverter.java | 2 - 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/DomainSetupHostingAssetFactory.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/DomainSetupHostingAssetFactory.java index 3babaf41..d12e8dcf 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/DomainSetupHostingAssetFactory.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/DomainSetupHostingAssetFactory.java @@ -15,6 +15,7 @@ import net.hostsharing.hsadminng.persistence.EntityManagerWrapper; import jakarta.validation.ValidationException; import java.net.IDN; import java.util.List; +import java.util.Optional; import java.util.function.Function; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_DNS_SETUP; @@ -39,47 +40,40 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory { // TODO.legacy: as long as we need to be compatible, we always do all technical domain-setups - final var subHostingAssetResources = getSubHostingAssetResources(); + final var domainHttpSetupAssetResource = findSubHostingAssetResource(HsHostingAssetTypeResource.DOMAIN_HTTP_SETUP); + final var assignedToUnixUserAssetEntity = domainHttpSetupAssetResource + .map(HsHostingAssetSubInsertResource::getAssignedToAssetUuid) + .map(uuid -> emw.find(HsHostingAssetRealEntity.class, uuid)) + .orElseThrow(() -> new ValidationException("DOMAIN_HTTP_SETUP subAsset with assignedToAssetUuid required in compatibility mode")); subHostingAssets.add( createDomainSubSetupAssetEntity( domainSetupAsset, DOMAIN_HTTP_SETUP, builder -> builder - .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) - .identifier(getDomainName() + "|MBOX") + .assignedToAsset(assignedToUnixUserAssetEntity) + .identifier(getDomainName() + "|HTTP") .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))) ); - domainHttpSetupAsset.setParentAsset(domainSetupAsset); - final var assignedToUnixUserAsset = - emw.find(HsHostingAssetRealEntity.class, domainHttpSetupAssetResource.getAssignedToAssetUuid()); - domainHttpSetupAsset.setAssignedToAsset(assignedToUnixUserAsset); - - - final var domainHttpSetupAsset = createDomainHttpSetupAssetEntity( - subHostingAssetResources, - getDomainName(), - domainSetupAsset); - final var assignedToUnixUserAsset = domainHttpSetupAsset.getAssignedToAsset(); - - // do not add to subHostingAssets, in compatibility mode, DNS setup works via file system + // Do not add to subHostingAssets in compatibility mode, in this case, DNS setup works via file system. + // The entity is created just for validation purposes. createDomainSubSetupAssetEntity( domainSetupAsset, DOMAIN_DNS_SETUP, builder -> builder - .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) + .assignedToAsset(assignedToUnixUserAssetEntity.getParentAsset()) .identifier(getDomainName() + "|DNS") - .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))); + .caption("DNS-Setup für " + IDN.toUnicode(getDomainName()))); subHostingAssets.add( createDomainSubSetupAssetEntity( domainSetupAsset, DOMAIN_MBOX_SETUP, builder -> builder - .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) + .assignedToAsset(assignedToUnixUserAssetEntity.getParentAsset()) .identifier(getDomainName() + "|MBOX") - .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))) + .caption("MBOX-Setup für " + IDN.toUnicode(getDomainName()))) ); subHostingAssets.add( @@ -87,9 +81,9 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory { domainSetupAsset, DOMAIN_SMTP_SETUP, builder -> builder - .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) + .assignedToAsset(assignedToUnixUserAssetEntity.getParentAsset()) .identifier(getDomainName() + "|SMTP") - .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))) + .caption("SMTP-Setup für " + IDN.toUnicode(getDomainName()))) ); return domainSetupAsset; @@ -112,9 +106,7 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory { final Function, HsHostingAssetRealEntity.HsHostingAssetRealEntityBuilder> builderTransformer) { final var resourceType = HsHostingAssetTypeResource.valueOf(subAssetType.name()); - final var subAssetResourceOptional = getSubHostingAssetResources().stream() - .filter(ha -> ha.getType() == resourceType) - .reduce(Reducer::toSingleElement); + final var subAssetResourceOptional = findSubHostingAssetResource(resourceType); subAssetResourceOptional.ifPresentOrElse( subAssetResource -> verifyNotOverspecified(subAssetResource), @@ -128,6 +120,12 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory { .build(); } + private Optional findSubHostingAssetResource(final HsHostingAssetTypeResource resourceType) { + return getSubHostingAssetResources().stream() + .filter(ha -> ha.getType() == resourceType) + .reduce(Reducer::toSingleElement); + } + // TODO.legacy: while we need to stay compatible, only default values can be used, thus only the type can be specified private void verifyNotOverspecified(final HsHostingAssetSubInsertResource givenSubAssetResource) { final var convert = new ToStringConverter().ignoring("assignedToAssetUuid"); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ToStringConverter.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ToStringConverter.java index e436cfec..bf0ec002 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ToStringConverter.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/factories/ToStringConverter.java @@ -1,7 +1,5 @@ package net.hostsharing.hsadminng.hs.hosting.asset.factories; -import net.hostsharing.hsadminng.mapper.Array; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashSet; import java.util.Objects;