/******************************************************************************* * Copyright 2020 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ 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 javax.xml.bind.JAXBElement; import java.util.List; import static at.gv.egiz.moazs.MoaZSException.moaZSException; 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; /** * @author Christof Rabensteiner * */ @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(convertAndRepaceCombinationSequenceWithIdentification(mzsRequest.getReceiver(), identification)) .build(); } private DeliveryRequestTypeBuilder setupBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { return deliveryRequestTypeBuilder() .withVersion(NameSpace.MSG_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)) .withClearingProfilID(sender.getClearingProfilID()) .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 ------------------ public 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 convertAndRepaceCombinationSequenceWithIdentification(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver, IdentificationType identificationType) { return setupReceiverBuilder(receiver) .withIdentification(identificationType) .withPerson(null) .withAustrianAddressesOnly(null) .withAddress(null) .build(); } private Receiver.ReceiverBuilder setupReceiverBuilder(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver receiver) { var builder = receiverBuilder() .withPreAdviceNote(receiver.getPreAdviceNote()) .withAdditionalCriteria(receiver.getAdditionalCriteria()); if (receiver.getIdentification() != null) { builder.withIdentification(convert(receiver.getIdentification())); } else if (receiver.getPerson() != null){ builder.withPerson(convertReceiverPerson(receiver.getPerson())) .withAustrianAddressesOnly(receiver.getAustrianAddressesOnly()); if (receiver.getAddress() != null) { builder.withAddress(convertAddresses(receiver.getAddress())); } } else { throw moaZSException("Cannot create msg:Receiver. Neither Person nor Identification specified."); } return builder; } //------------- ADDRESSES ------------------ private List> convertAddresses( List> addresses) { return addresses.stream() .map(JAXBElement::getValue) .map(this::convert) .collect(toList()); } private JAXBElement convert(at.gv.zustellung.app2mzs.xsd.persondata.AbstractAddressType address) { if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType) return personFactory.createInternetAddress( convert((at.gv.zustellung.app2mzs.xsd.persondata.InternetAddressType) address)); if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType) return personFactory.createPostalAddress( convert((at.gv.zustellung.app2mzs.xsd.persondata.PostalAddressType) address)); if (address instanceof at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) return personFactory.createTelephoneAddress( convert((at.gv.zustellung.app2mzs.xsd.persondata.TelephoneAddressType) address)); throw new IllegalArgumentException(format("No conversion strategy for 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(); } public 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 ------------- public 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 -------------------- public JAXBElement convertReceiverPerson( JAXBElement jaxbPerson) { var mzsPerson = jaxbPerson.getValue(); if (personIsPhysical(mzsPerson)) { var msgPerson = convert((at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType) mzsPerson); return personFactory.createPhysicalPerson(msgPerson); } else { var msgPerson = convert((at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType) mzsPerson); return personFactory.createCorporateBody(msgPerson); } } private boolean personIsPhysical(at.gv.zustellung.app2mzs.xsd.persondata.AbstractPersonType person) { return person instanceof at.gv.zustellung.app2mzs.xsd.persondata.PhysicalPersonType; } public 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(); } }