cancel
Showing results for 
Search instead for 
Did you mean: 

Processing Schema Validation error in BPM?

Former Member
0 Kudos

Hi all,

We have a file to IDOC scenario where if the schema validation fails on the file adapter sender agreement, we want to pull the filename and send it in an error email. So, we are talking about the adapter specific message attribute "Filename".

unfortunately, this does not seem to be available as a container element for alert catagories in ALRTCATDEF. So, we are starting to look into using BPM to send a email via the email adapter. However, since the validation happens prior to BPM being called I am at a bit of a loss. Any ideas on how to get the email with the filename out to the suport group would be appreciated.

Thanks,

Chris

Accepted Solutions (1)

Accepted Solutions (1)

RaghuVamseedhar
Active Contributor
0 Kudos
package com.validate;

import com.sap.aii.mapping.api.*;
import com.sap.aii.mappingtool.tf7.rt.Container;
import java.io.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;

public class ValidateXML extends AbstractTransformation {

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
        OutputStream outputstream = null;
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            outputstream = transformationOutput.getOutputPayload().getOutputStream();
            byte[] b = new byte[inputstream.available()];
            inputstream.read(b);
            String XMLinputFileContent = new String(b);

            // define the type of schema - we use W3C:
            String schemaLang = "http://www.w3.org/2001/XMLSchema";

            // get validation driver:
            SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

            //Place XSD file in PI server same as <a href="http://help.sap.com/saphelp_nwpi711/helpdata/en/44/0bf1b3ec732d2fe10000000a11466f/frameset.htm" TARGET="test_blank">http://help.sap.com/saphelp_nwpi711/helpdata/en/44/0bf1b3ec732d2fe10000000a11466f/frameset.htm</a>
            //OR I think you can place any where on server.
            File XSDfile = new File("C:/xi/runtime_server/validation/schema/SchemaFile.xsd");
            // create schema by reading it from an XSD file:
            Schema schema = factory.newSchema(new StreamSource(XSDfile));
            Validator validator = schema.newValidator();

            // at last perform validation:
            validator.validate(new StreamSource(XMLinputFileContent));

            //If XML it not valid, exception will be thrown, if it is valid sent this PassOutputXML to output

            String PassOutputXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns:MT_OutputXML xmlns:ns=\"http://sap.com/xi\"> <Name>FileName.txt</Name><Validation>Pass</Validation> </ns:MT_OutputXML>";
            outputstream.write(PassOutputXML.getBytes());
        } catch (Exception exception) {
            //Get File name using <a href="http://help.sap.com/saphelp_nwpi711/helpdata/en/43/03612cdecc6e76e10000000a422035/frameset.htm" TARGET="test_blank">http://help.sap.com/saphelp_nwpi711/helpdata/en/43/03612cdecc6e76e10000000a422035/frameset.htm</a>
            Container container = null;
            DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
            DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");
            String inputFileName = conf.get(key);

            String FailOutputXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns:MT_OutputXML xmlns:ns=\"http://sap.com/xi\"> <Name>" + inputFileName + "</Name><Validation>Fail</Validation> </ns:MT_OutputXML>";
            try {
                outputstream.write(FailOutputXML.getBytes());
            } catch (IOException ex) {
                exception.printStackTrace();
                ex.printStackTrace();
            }
        }
    }
}
Former Member
0 Kudos

Hi,

Many thanks for this. I think this will be an acceptable alternative to meet our needs.

Kind Regards,

Chris

Answers (1)

Answers (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Hi Richard Curtis,

I understand your scenario is picking up XML file and validate it with XSD: - if, it is valid then create an IDOC (send it to target system), else, stop processing (send alert mail to support team with subject line containing u2018failed file nameu2019). PI system is PI7.1 or above. Initially I thought it is a simple scenario ;-).

In sender channel you have to select adapter specific message attribute "Filename" (as you said).

1. If you use XML validation feature provided by SAP PI 7.1 [Link|http://help.sap.com/saphelp_nwpi711/helpdata/en/44/0bf1b3ec732d2fe10000000a11466f/frameset.htm] , then are only two possibilities

a. Validation in the sender adapter: - If the structure of the payload differs from the definition of the data type provided for comparison, message processing is stopped. The adapter sends a synchronous response to the sender of the message, informing it about the structure error. This wonu2019t fit to requirement.

b. Validation in the Integration Engine: - If the structure of the message payload does not match the saved definition of the data type, an error description is generated. The message is set to error status and an error report is saved. This seem to fit, after message failed we can trigger alert by ALERTCATDEF, but.

2. Here we cannot use ALRTCATDEF (as you said) to send mail with subject line containing u2018failed file nameu2019, because we cannot access filename. [Link|http://help.sap.com/saphelp_nwpi711/helpdata/en/d0/d4b54020c6792ae10000000a155106/frameset.htm]

3. We can trigger email with subject line containing u2018failed file nameu2019 in ccBPM [Link|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/1382] [original link is broken] [original link is broken] [original link is broken]; (have to do bit Java mapping). But, we cannot even ccBPM (as you said) because if validation fails, message wonu2019t even come to BPM. FYI, Pipeline steps: - XML Validation RQ INB, Receiver determination, Interface determination, Receiver message split, Mapping request, Outbound binding, XML validation RQ OUT, Call adapter.

The problem started when we started using SAP PI XML validation feature. Let go back to old method, we can validate XML with XSD by Java Mapping.

I think this solution will work, I have not tried it out, create a ccBPM, a transformation step (Java Mapping, below) which give xml output like this <Name>FileName.txt</Name><Validation>Fail</Validation>. Based on this output, use a switch statement to decide to trigger mail or transform XML to IDOC (transform step).

Regards,

Raghu_Vamsee