package at.gv.egiz.moazs; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; import at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType; import at.gv.zustellung.msg.xsd.SenderProfile; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; import static org.springframework.util.Assert.isTrue; import static org.springframework.util.Assert.notNull; @Component public class DeliveryPreprocessor { /** * Validates and augments an incoming {@code request} * * Validates a {@code request} to ensure the availability of all mandatory fields. Where possible, the method * augments the request with values taken from the app's configuration. * @param request * @return validated and augmented request */ public DeliveryRequestType preProcess(DeliveryRequestType request) { validate(request); return new DeliveryRequestTypeBuilder(request) .withConfig(coalesce(request.getConfig(), initDefaultConfig()).get()) .build(); } private void validate(DeliveryRequestType request) { validate(request.getSender()); notNull(request.getReceiver(), "Receiver is missing."); notNull(request.getPayload(), "Payloads are missing."); notNull(request.getMetaData(), "Metadata is missing."); notNull(request.getMetaData().getAppDeliveryID(), "AppDeliveryID is missing."); } private void validate(DeliveryRequestType.Sender sender) { notNull(sender, "Sender is missing."); isTrue(sender.getSenderProfile() != null ^ sender.getCorporateBody() != null , "Either SenderProfile or CorporateBody (but not both) need to be defined."); if(sender.getSenderProfile() != null) validate(sender.getSenderProfile()); else validate(sender.getCorporateBody()); } private void validate(SenderProfile profile) { notNull(profile.getProfileID(), "ProfileID is missing."); } private void validate(CorporateBodyType body) { notNull(body.getIdentification(), "Identification is missing."); notNull(body.getFullName(), "FullName is missing."); isTrue(body.getIdentification().size() > 0, "No Identification provided."); isTrue(body.getIdentification().size() <= 1, "Too many means of Identification were provided."); var id = body.getIdentification().get(0); notNull(id.getType(), "Identification Type is missing"); notNull(id.getValue(), "Identification Value is missing"); } private ConfigType initDefaultConfig() { return configTypeBuilder() .withPerformQueryPersonRequest(false) .build(); } }