cancel
Showing results for 
Search instead for 
Did you mean: 

CPI: Groovy[HashMap] Data Retrieval Issue

babruvahana
Contributor
0 Kudos
335

Hi Experts,

I need to replace all the payScaleArea codes with payScaleArea names for further processing. Considering performance, I don't want to use a looping process call with a splitter as there are more than 5L+ user IDs from CE Query. So I am fetching all the payScaleArea from payScaleArea OData API initiating the HashMap and retrieving it for the target structure.

Step:1 Source XMl will be the body

<queryCompoundEmployeeResponse>
	<CompoundEmployee>
		<id>11111</id>
		<person>
			<person_id_external>ABCDEF</person_id_external>
			<employment_information>
				<action>NO CHANGE</action>
				<last_modified_on>2024-01-13T07:41:27.000Z</last_modified_on>
				<user_id>ABCDEF-1</user_id>
				<job_information>
					<payScaleArea>ESP/EV</payScaleArea>
				</job_information>
				<job_information>
					<payScaleArea>ESP/EM</payScaleArea>
				</job_information>
				<job_information>
					<payScaleArea>ESP/EL</payScaleArea>
				</job_information>
			</employment_information>
		</person>
		<execution_timestamp>2025-01-30T06:26:34.000Z</execution_timestamp>
		<version_id>1.0</version_id>
	</CompoundEmployee>
</queryCompoundEmployeeResponse>

Step:2 Initiated the HashMap and stored the values in the property

hashmapOutput={ESP/EM\=aaaaaa, ESP/EL\=bbbbb, ESP/EV\=ccccc, ESP/EN\=ddddd}

 Step:3 Target Body

<queryCompoundEmployeeResponse>
	<CompoundEmployee>
		<id>11111</id>
		<person>
			<person_id_external>ABCDEF</person_id_external>
			<employment_information>
				<action>NO CHANGE</action>
				<last_modified_on>2024-01-13T07:41:27.000Z</last_modified_on>
				<user_id>ABCDEF-1</user_id>
				<job_information>
					<payScaleArea>ESP/EV:ccccc</payScaleArea>
				</job_information>
				<job_information>
					<payScaleArea>ESP/EM:aaaaa</payScaleArea>
				</job_information>
				<job_information>
					<payScaleArea>ESP/EL:bbbbb</payScaleArea>
				</job_information>
			</employment_information>
		</person>
		<execution_timestamp>2025-01-30T06:26:34.000Z</execution_timestamp>
		<version_id>1.0</version_id>
	</CompoundEmployee>
</queryCompoundEmployeeResponse>

Step:4 Retrieve the HashMap and replace all the payScaleArea values to create the target body above

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

def Message processData(Message message) {
	
	def body = message.getBody(java.lang.String);
	def map = message.getProperties();
	def hmap1 = map.get("hashmapOutput");   
   	
   	def Root = new XmlParser().parseText(body);
    Root.CompoundEmployee.each{CE ->
	    CE.person.employment_information.each{EMP->
		    EMP.job_information.each{JOB->
		     if (hmap1.containsKey(JOB.payScaleArea)){
		         JOB.payScaleArea[0].value = hmap1.get(JOB.payScaleArea[0].text().toString())
		     }
		    }
		    }
		}
       
	message.setBody(XmlUtil.serialize(Root));
   	return message;
}

The values itself is not getting replaced. 

Any leads will be invaluable.

Regards,

Pavan

View Entire Topic
rasjoshi
Active Contributor

Check this --

GroovyIDE 

import groovy.json.JsonSlurper
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.xml.*

def Message processData(Message message) {
def body = message.getBody(String)
def map = message.getProperties()
println "map: " + map

def hashmapOutput = map.get("hashmapOutput")
println "hash: " + hashmapOutput

if (!hashmapOutput || hashmapOutput.trim().isEmpty()) {
throw new IllegalArgumentException("hashmapOutput is null or empty")
}
 
def hmap1 = new JsonSlurper().parseText(hashmapOutput) as Map

def parser = new XmlParser(false, false)
def Root = parser.parseText(body)

Root.CompoundEmployee.each { CE ->
CE.person.employment_information.each { EMP ->
EMP.job_information.each { JOB ->
def payScaleAreaCode = JOB.payScaleArea.text()
if (payScaleAreaCode && hmap1.containsKey(payScaleAreaCode)) {
JOB.payScaleArea[0].value = "${payScaleAreaCode}:${hmap1.get(payScaleAreaCode)}"
}
}
}
}

def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(Root)
message.setBody(writer.toString())

return message
}