edi 2 idoc , tpm testing from customiflow, groovy code might be useful. praveen class

edi 2 idoc , scenario, posting edi from custom scenario. 

-------

/* Refer the link below to learn more about the use cases of script.

https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/148851bf8192412cba1f9d2c17f4bd25.html


If you want to know more about the SCRIPT APIs, refer the link below

https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/index.html */

import com.sap.gateway.ip.core.customdev.util.Message

import java.util.HashMap


// Do not use 'def' for global vars

// https://stackoverflow.com/questions/6305910/how-do-i-create-and-access-the-global-variables-in-groovy

//ediMessageType = '???'      // message type

//ediMessage = '???'          // message

//ediMessageVersion = '???'   // message version

//regexChars = '.*+^$'        // These need to be escaped if used as delims

//debug = 'none'


def Message processData(Message message) {

    // Global

    ediMessageType = '???'      // message type

    ediMessage = '???'          // message

    ediMessageVersion = '???'   // message version

    regexChars = '.*+^$'        // These need to be escaped if used as delims

    debug = 'none'


    // Body

    //def reader = message.getBody(java.io.Reader)

    def reader = message.getBody()  // modified for debugging locally

    def bodyLinesX12 = []

    def bodyLinesEDIFACT = []

    def bodyLines = []

    def lineCount = 0

    def lineElements

    def elementDelim = ''

    def segmentDelim = ''

    def currentLine = ''

    def oneLiner = false


    // TODO: convert .equals to == for strings

    reader.eachLine { String line ->

        if (lineCount == 0) {

            // What type of file is this?

            if (line.startsWith('ISA')) {

                ediMessageType = 'X12'

                // element delim is position 3 on ISA line

                elementDelim = line.substring(3, 4)

                if (regexChars.indexOf(elementDelim) > -1) {

                    elementDelim = '\\' + elementDelim

                }

                // TODO: set up to read without line breaks

                // segment delim is last position on ISA line

                segmentDelim = line.substring(line.length() - 1)

            } else {

                if (line.startsWith("UNA") || line.startsWith("UNB")) {

                    ediMessageType = 'EDIFACT'

                }

            }

        }


        // X12 proccessing

        if (ediMessageType.equals('X12') && lineCount == 1) {

            debug = 'elementDelim: ' + elementDelim + ', segmentDelim: ' + segmentDelim

            currentLine = line.split(segmentDelim)

            lineElements = currentLine[0].split(elementDelim)

            ediMessageVersion = lineElements[lineElements.size() - 1]

            //lineElements[lineElements.size() - 1] = '02001';

            //message.setBody(lineElements)

            

        }


        if (ediMessageType.equals('X12') && lineCount == 2) {

            lineElements = line.split(elementDelim)

            ediMessage = lineElements[1]

        }


        // EDIFACT processing, ref: https://en.wikipedia.org/wiki/EDIFACT

        // EDIFACT does not always have lines, so find the line delimiter and split

        // EDIFACT from OpenText it's always formatted

        // Number of segments (lines) at start of file for EDIFACT can be either 2 or 3 and start with:

        // UNA (optional) - 6 chars following UNA that indicate special chars in use

        // UNB (required) - indicates which syntax rules are in effect

        // UNH (required) - message header, last element in this segment contains

        // 4 parts: message type [M 1/6], message version [M 1/3], message release number [M 1/3], controlling agency [M 1/3]

        // e.g. - UNH+460960+ORDERS:D:02B:UN'

        // messsage type, messsage version and message release number need to be set in properties for iFlow

        if (ediMessageType.equals('EDIFACT') && lineCount == 0) {

            // Check if one long line

            def headerIndex = line.indexOf('UNH')

            if (headerIndex > 0) {

                // Not occuring at beginning of line, so most likely everything is here

                // Since UNH is required and is the start of a segment, backup 1 to get

                // the segment delimiter

                segmentDelim = line.substring(headerIndex - 1, headerIndex)

                bodyLinesEDIFACT = line.split(segmentDelim)

                oneLiner = true


                for (int i = 0; i < bodyLinesEDIFACT.size(); i++) {

                    if ((i == 1 || i == 2) && bodyLinesEDIFACT[i].startsWith('UNH')) {

                        elementDelim = getElementDelimiterEDIFACT(bodyLinesEDIFACT[i])

                        lineElements = bodyLinesEDIFACT[i].split(elementDelim)

                        assignEDIFACTProps(lineElements)

                    }

                }

            }

        }


        if (!oneLiner && ediMessageType.equals('EDIFACT') && (lineCount == 1 || lineCount == 2) && line.startsWith('UNH')) {

            // line 2 or 3 usually has message and verion, look for line starting with UNH

            // character after UNH is the line element delimiter

            elementDelim = getElementDelimiterEDIFACT(line)

            lineElements = line.split(elementDelim)

            assignEDIFACTProps(lineElements)

        }


        bodyLines.add(line)

        lineCount++

    }


    // Properties

    // Set new properties for EDI Type (X12, EDIFACT), Transaction (850, etc), Version ()

    def properties = message.getProperties()

    message.setProperty('ediMessageType', ediMessageType)

    message.setProperty('ediMessage', ediMessage)

    message.setProperty('ediMessageVersion', ediMessageVersion)

    message.setProperty('ediDebug', debug)

    return message

}


def String getElementDelimiterEDIFACT(String headerLine) {

    def elementDelim

    if (headerLine.startsWith('UNH')) {

        elementDelim = headerLine.substring(3, 4)

    }

    if (regexChars.indexOf(elementDelim) > -1) {

        elementDelim = "\\" + elementDelim

    }

    return elementDelim

}


def assignEDIFACTProps(String[] headerLineTransactionPart) {

    def parts = headerLineTransactionPart[2].split(':')

    ediMessage = parts[0]

    ediMessageVersion = parts[1] + ':' + parts[2]

}

Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

SAP CPI camel conditions and xpath conditions

oauth call to cpi integraiton suite from sap apim