Skip to Content

Hello Integrators,

with inspiration from the blog post “Key-Value Mapping in CPI for huge data sets has never been easier” from my colleague Martin Pankraz, I changed his code a bit to put it in an integration Flow so I do not need to start Eclipse all the time and to use it in with the whole team.

I use the script in an HTTP-Input integration flow (see my blogpost: Working with HTTP-Input in SAP Cloud Platform Integration Flows and How to Test These Flows with Postman). The payload in Postman is the csv-text and I have four headers for the agency and identifier. The output is than the generated key-value mapping xml.

This is the modified groovy-Script to create the xml-mapping.

//original from Martin Pankraz and modified by Dominic Beckbauer
//

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.MarkupBuilder

def Message processData(Message message) {
       def map = message.getHeaders();
      
      
        def agency1 = map.get("agency1").toString();
        def agency2 = map.get("agency2").toString();
        def identifier1 = map.get("identifier1").toString();
        def identifier2 = map.get("identifier2").toString();       
        
        
         def encoding = message.getBody(java.lang.String);

		def stringPerCode = [:];
		/***********************
			Cheat sheet for pre-processing of CSV:
			([\s]{2,})(?=[aA-zZ])
			([_]{2,})
			
		*/
		encoding.splitEachLine(";") {fields ->
			def listOfItems = [];
			
			fields[1] = fields[1].toString().trim();
			if(fields[1].equals("")){
				fields[1] = "Unbekannt";
			}
			if(fields[1].toString().length() > 60){
				fields[1] = fields[1].substring(0,60);
			}
			fields[1] = fields[1].toString().replaceAll(/[\s]{2,}/,' ');
			
			if(stringPerCode.containsKey(fields[1])){
				def alreadyListed = stringPerCode.get(fields[1]).contains(fields[0]);
				if(!alreadyListed){
					stringPerCode.get(fields[1]) << fields[0];
				} 
			}else{
				listOfItems << fields[0];
				stringPerCode[fields[1]] = listOfItems;
			}
		}
			
		def stringWriter = new StringWriter();
		def mappingBuilder = new MarkupBuilder(stringWriter);
		
		mappingBuilder.mkp.xmlDeclaration(version: "1.0", encoding: encoding)
		
		mappingBuilder.root{
			vm(version:'2.0'){
				stringPerCode.each{code, strings ->
					strings.eachWithIndex { item, index ->
						group(id:UUID.randomUUID().toString()) {
							if(index == 0){
								entry(isDefault:'true') {
									agency(agency1)
									schema(identifier1)
									value(item)
								}
								entry() {
									agency(agency2)
									schema(identifier2)
									value(code)
								}
							}else{
								entry{
									agency(agency1)
									schema(identifier1)
									value(item)
								}
								entry{
									agency(agency2)
									schema(identifier2)
									value(code)
								}
							}
						}
					}
				}
			}
		};
		
		
		message.setBody(stringWriter.toString());
    
    return message;
}

You can find the original Version from Martin here

My Integration flow looks like this:

And not to forget the Postman POST-Request-Header:

and body:

This way you have a nice way to create a value mapping xml without starting eclipse.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply