package at.gv.egiz.moazs.scheme; import at.gv.zustellung.msg.xsd.*; import at.gv.zustellung.msg.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; import at.gv.zustellung.msg.xsd.persondata.*; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import javax.xml.bind.JAXBElement; import java.util.List; import static at.gv.zustellung.msg.xsd.AttachmentType.attachmentTypeBuilder; import static at.gv.zustellung.msg.xsd.AttachmentsType.attachmentsTypeBuilder; import static at.gv.zustellung.msg.xsd.ConfirmationAddress.confirmationAddressBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; import static at.gv.zustellung.msg.xsd.Receiver.receiverBuilder; import static at.gv.zustellung.msg.xsd.Sender.senderBuilder; import static at.gv.zustellung.msg.xsd.SenderCorporateBodyType.senderCorporateBodyTypeBuilder; import static at.gv.zustellung.msg.xsd.WebserviceURL.webserviceURLBuilder; import static at.gv.zustellung.msg.xsd.persondata.CorporateBodyType.corporateBodyTypeBuilder; import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.Value.valueBuilder; import static at.gv.zustellung.msg.xsd.persondata.IdentificationType.identificationTypeBuilder; import static at.gv.zustellung.msg.xsd.persondata.InternetAddressType.internetAddressTypeBuilder; import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.*; import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.Affix.affixBuilder; import static at.gv.zustellung.msg.xsd.persondata.PersonNameType.FamilyName.familyNameBuilder; import static at.gv.zustellung.msg.xsd.persondata.PhysicalPersonType.physicalPersonTypeBuilder; import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.DeliveryAddress.deliveryAddressBuilder; import static at.gv.zustellung.msg.xsd.persondata.PostalAddressType.postalAddressTypeBuilder; import static at.gv.zustellung.msg.xsd.persondata.TelephoneAddressType.telephoneAddressTypeBuilder; import static java.lang.String.format; import static java.util.stream.Collectors.toList; @Component public class Mzs2MsgConverter { private final at.gv.zustellung.msg.xsd.ObjectFactory msgFactory = new at.gv.zustellung.msg.xsd.ObjectFactory(); private final at.gv.zustellung.msg.xsd.persondata.ObjectFactory personFactory = new at.gv.zustellung.msg.xsd.persondata.ObjectFactory(); public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { return setupBuilder(mzsRequest) .withReceiver(convert(mzsRequest.getReceiver())) .build(); } public DeliveryRequestType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest, IdentificationType identification) { return setupBuilder(mzsRequest) .withReceiver(convert(mzsRequest.getReceiver(), identification)) .build(); } private DeliveryRequestTypeBuilder setupBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { return deliveryRequestTypeBuilder() .withVersion("2.0.0") //todo: remove hardcoded version .withSender(convert(mzsRequest.getSender())) .withMetaData(mzsRequest.getMetaData()) .withAttachments(convertPayloads(mzsRequest.getPayload())); } //------------- SENDER ------------------ private Sender convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { return senderBuilder() .withSenderPerson(extractSenderPerson(sender)) .withLogo(sender.getLogo()) .withAdditionalCriteria(sender.getAdditionalCriteria()) .withConfirmationAddress(convertConfirmationAddress(sender)) .build(); } private JAXBElement extractSenderPerson(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { var builder = senderCorporateBodyTypeBuilder(); if(sender.getSenderProfile() != null) { builder.withSenderProfile(sender.getSenderProfile()); } else { var identification = sender.getCorporateBody().getIdentification().get(0); builder.withCorporateBody(convert(sender.getCorporateBody())) .withIdentification(convert(identification)); } builder.withId(sender.getId()); return msgFactory.createSenderCorporateBody(builder.build()); } private ConfirmationAddress convertConfirmationAddress(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender sender) { var builder = confirmationAddressBuilder(); if(sender.getEMailAddress() != null) { builder.withEmail(convert(sender.getEMailAddress())) .withType(sender.getEMailAddress().getType()); } else { builder.withWebserviceURL(convert(sender.getWebserviceURL())) .withType(sender.getWebserviceURL().getType()); } return builder.build(); } private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.EMailAddress eMailAddress) { return internetAddressTypeBuilder() .withAddress(eMailAddress.getValue()) .build(); } private WebserviceURL convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.WebserviceURL webserviceURL) { return webserviceURLBuilder() .withAddress(webserviceURL.getValue()) .withAlternativeEmail(webserviceURL.getAlternativeEmail()) .build(); } //------------- RECEIVER ------------------ private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { return setupReceiverBuilder(receiver).build(); } private at.gv.zustellung.msg.xsd.Receiver convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver, IdentificationType identificationType) { return setupReceiverBuilder(receiver) .withIdentification(identificationType) .build(); } private Receiver.ReceiverBuilder setupReceiverBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { var builder = receiverBuilder() .withPreAdviceNote(receiver.getPreAdviceNote()) .withAdditionalCriteria(receiver.getAdditionalCriteria()) .withPerson(convert(receiver.getPerson())) .withAddress(convertAddresses(receiver.getAddress())); if (receiver.getIdentification() != null) builder.withIdentification(convert(receiver.getIdentification())); return builder; } //------------- ADDRESSES ------------------ private List> convertAddresses( List> addresses) { return addresses.stream() .map(JAXBElement::getValue) .map(this::convert) .map(personFactory::createAddress) .collect(toList()); } private AbstractAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.AbstractAddressType address) { if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType) address); } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType ) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType) address); } else if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) { return convert((at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) address); } else { throw new IllegalArgumentException(format("No conversion strategy of address of type=%s.", address.getClass().toGenericString())); } } private InternetAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType address) { return internetAddressTypeBuilder() .withAddress(address.getAddress()) .withId(address.getId()) .build(); } private PostalAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType address) { return postalAddressTypeBuilder() .withCountryCode(address.getCountryCode()) .withDeliveryAddress(convert(address.getDeliveryAddress())) .withId(address.getId()) .withMunicipality(address.getMunicipality()) .withMunicipalityNumber(address.getMunicipalityNumber()) .withPostalCode(address.getPostalCode()) .withType(address.getType()) .build(); } private PostalAddressType.DeliveryAddress convert(at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType.DeliveryAddress deliveryAddress) { return deliveryAddressBuilder() .withBuildingNumber(deliveryAddress.getBuildingNumber()) .withDoorNumber(deliveryAddress.getDoorNumber()) .withStreetName(deliveryAddress.getStreetName()) .withUnit(deliveryAddress.getUnit()) .build(); } private TelephoneAddressType convert(at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType address) { return telephoneAddressTypeBuilder() .withId(address.getId()) .withMessengerService(address.getMessengerService()) .withNumber(address.getNumber().getFormattedNumber()) .build(); } //------------- ATTACHMENTS ------------------ private AttachmentsType convertPayloads(List payloadList) { var builder = attachmentsTypeBuilder(); payloadList.stream() .map(this::convert) .forEach(builder::addAttachment); return builder.build(); } private AttachmentType convert(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload payload) { return attachmentTypeBuilder() .withFileName(payload.getFileName()) .withContent(payload.getBinaryDocument()) .withCheckSum(payload.getCheckSum()) .withURL(payload.getDocumentReference()) .withDocumentClass(payload.getDocumentClass()) .withMimeType(payload.getMIMEType()) .withSize(payload.getSize()) .build(); } //------------- IDENTIFICATION TYPE ------------- private IdentificationType convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType identification) { return identificationTypeBuilder() .withId(identification.getId()) .withType(identification.getType()) .withValue(convert(identification.getValue())) .build(); } private IdentificationType.Value convert(at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.Value value) { return valueBuilder() .withId(value.getId()) .withValue(value.getValue()) .build(); } //------------ PERSONS -------------------- private JAXBElement convert( JAXBElement jaxbPerson) { var mzsPerson = jaxbPerson.getValue(); Assert.isTrue(personIsPhysical(mzsPerson) || personIsCorporateBody(mzsPerson), "Person is neither a PhysicalPerson nor a CorporateBody."); var msgPerson = (personIsPhysical(mzsPerson)) ? convert((at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType) mzsPerson) : convert((at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType) mzsPerson); return personFactory.createPerson(msgPerson); } private boolean personIsPhysical(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType; } private boolean personIsCorporateBody(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType; } private CorporateBodyType convert(at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType corporateBody) { return corporateBodyTypeBuilder() .withFullName(corporateBody.getFullName()) .withId(corporateBody.getId()) .withOrganization(corporateBody.getOrganization()) .withTarget(corporateBody.getTarget()) .build(); } private PhysicalPersonType convert(at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType physicalPerson) { return physicalPersonTypeBuilder() .withDateOfBirth(physicalPerson.getDateOfBirth()) .withId(physicalPerson.getId()) .withName(convert(physicalPerson.getName())) .build(); } private PersonNameType convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType name) { return personNameTypeBuilder() .withGivenName(name.getGivenName()) .withFamilyName(convert(name.getFamilyName())) .withAffix(convertAffixes(name.getAffix())) .build(); } private FamilyName convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.FamilyName familyName) { return familyNameBuilder() .withPrefix(familyName.getPrefix()) .withPrimary(familyName.getPrimary()) .withValue(familyName.getValue()) .build(); } private List convertAffixes(List affix) { return affix.stream() .map(this::convert) .collect(toList()); } private Affix convert(at.gv.zustellung.app2mzs.xsd.persondata.PersonNameType.Affix affix) { return affixBuilder() .withPosition(affix.getPosition()) .withType(affix.getType()) .withValue(affix.getValue()) .build(); } }