diff options
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java')
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java index e7ee357..240a677 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java @@ -1,36 +1,41 @@ package at.gv.egiz.moazs.preprocess; +import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.app2mzs.xsd.ObjectFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import java.util.Map; import static at.gv.egiz.moazs.MoaZSException.moaZSException; +import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; -import static java.lang.String.format; @Component public class DeliveryRequestAugmenter { + private static final Logger log = LoggerFactory.getLogger(DeliveryRequestAugmenter.class); + private static final ObjectFactory FACTORY = new ObjectFactory(); + private final ConfigUtil util; private final Map<String, ConfigType> configs; private final MzsDeliveryRequestValidator validator; + private final Marshaller mzsMarshaller; - private static final String INCOMPLETE_TNVZ_ERROR_MESSAGE = "mzs:DeliveryRequest is incomplete because mandatory " + - "fields for sending a tnvz:QueryPersonRequest are missing."; - private static final String INCOMPLETE_CONFIG_ERROR_MESSAGE = "Could not find a profile for " + - "the delivery request configuration, and the configuration attached to mzs:DeliveryRequest is incomplete."; - private static final String INCOMPLETE_MERGED_CONFIG_ERROR_MESSAGE = "I merged parameters from " + - "mzs:DeliveryRequest/Config with parameters from config profile with ProfileId='%s', but the " + - "configuration is incomplete."; + private static final String CONFIG_MISSING_ERROR_MSG = "Delivery request configuration is missing."; @Autowired - public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigUtil util, MzsDeliveryRequestValidator validator) { + public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigUtil util, + MzsDeliveryRequestValidator validator, Marshaller mzsMarshaller) { this.configs = deliveryRequestConfigs; this.util = util; this.validator = validator; + this.mzsMarshaller = mzsMarshaller; } /** @@ -46,41 +51,29 @@ public class DeliveryRequestAugmenter { var fallbackProfileId = determineProfileIdFrom(requestConfig); var fallbackConfig = configs.get(fallbackProfileId); - if (fallbackConfig == null) { - - if (!validator.isConfigProfileComplete(request.getConfig())) { - throw moaZSException(INCOMPLETE_CONFIG_ERROR_MESSAGE); - } else if (!validator.isTnvzComplete(request)) { - throw moaZSException(INCOMPLETE_TNVZ_ERROR_MESSAGE); - } else { - return request; - } + trace("request config", requestConfig); + trace("fallback config", fallbackConfig); - } else { + var augmentedConfig = (requestConfig != null && fallbackConfig != null) + ? util.merge(requestConfig, fallbackConfig) + : coalesce(requestConfig, fallbackConfig) + .orElseThrow(()-> moaZSException(CONFIG_MISSING_ERROR_MSG)); - var mergedConfig = (requestConfig == null) - ? fallbackConfig - : util.merge(requestConfig, fallbackConfig); + trace("augmented config", augmentedConfig); - if (!validator.isConfigProfileComplete(mergedConfig)) { - var message = format(INCOMPLETE_MERGED_CONFIG_ERROR_MESSAGE, fallbackProfileId); - throw moaZSException(message); - } + validator.isConfigProfileComplete(augmentedConfig); - var mergedRequest = deliveryRequestTypeBuilder(request) - .withConfig(mergedConfig) - .build(); + var augmentedRequest = deliveryRequestTypeBuilder(request) + .withConfig(augmentedConfig) + .build(); - if (!validator.isTnvzComplete(mergedRequest)) { - throw moaZSException(INCOMPLETE_TNVZ_ERROR_MESSAGE); - } + validator.isTnvzComplete(augmentedRequest); - return mergedRequest; + return augmentedRequest; - } } - private String determineProfileIdFrom(ConfigType requestConfig) { + private String determineProfileIdFrom(@Nullable ConfigType requestConfig) { return (requestConfig == null || requestConfig.getProfileID() == null || isProfileMissing(requestConfig.getProfileID())) @@ -92,4 +85,11 @@ public class DeliveryRequestAugmenter { return !configs.containsKey(id); } + private void trace(String description, ConfigType config) { + if (log.isTraceEnabled()) { + log.trace("{} : {}", description, mzsMarshaller.marshallXml(FACTORY.createConfig(config))); + } + } + + } |