Over a million developers have joined DZone.

Mule DataMapper Groovy Sample Helper

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

As Mule consultants, one issue we seem to repeatedly face is the excessive amount of time spent building the input/output structure of the DataMapper. The DataMapper in Mule 3.4 helps you solve this issue if you have XML, by importing the input using an XSD. The same with JSON, you can provide a JSON sample. But when it comes to other things, like Maps and List of Maps, it becomes very time consuming.

This issue occurs most often when dealing with databases. A JDBC select statement in Mule returns either a Map, or a List of Maps. Each time you want to use the DataMapper, you have to build the input structure manually. In Mule 3.5.0, Mule uses DataSense with the new JDBC connector to help simplify this process, however if you are stuck with an older version of Mule, you will most likely face this issue at some point.

The DataMapper allows you to supply a Groovy sample, so how can we use this to make our job easier? Well you can use a Logger to display the payload after the JDBC call, but the logger displays the output in a different way to how you would write it in Groovy. The following is a List of Maps printed using the Logger. Note that one of the elements of the first Map is another Map:

org.mule.api.processor.LoggerMessageProcessor: [{id3=value3, id2=value2, id1=1}, {id3={id3=value3, id2=value2, id1=value1}, id2=value2, id1=value1}, {id3=value3, id2=value2, id1=value1}]

If we supply this input to the DataMapper as a Groovy sample, it won’t get us anywhere. To solve the problem, we have written a simple Groovy script that displays the elements as you would write them in Groovy. The same List of Maps shown above will be printed as follows:

Printed object: [['id3' : 'value3', 'id2' : 'value2', 'id1' : 1], ['id3' : ['id3' : 'value3', 'id2' : 'value2', 'id1' : 'value1'], 'id2' : 'value2', 'id1' : 'value1'], ['id3' : 'value3', 'id2' : 'value2', 'id1' : 'value1']]

As you can see, the String elements are enclosed in single quotes. Both Maps and Lists are enclosed in square brackets, and Map key and value pairs are separated using colon. This is exactly what Groovy is expecting. If we copy the printed result and supply it to the DataMapper, it happily creates the input/output structure:

DataMapper Groovy Sample

DataMapper Groovy Sample

DataMapper Input Structure

DataMapper Input Structure

Here is the Groovy script:

def s = printObject(message.payload)

println('Printed object: ' + s)
return payload

String printObject(o)
	if (o instanceof List) return printList(o)
	if (o instanceof Map) return printMap(o)
	if ((o instanceof Integer) || (o instanceof Long) || (o instanceof Float) || (o instanceof Double)) return printNumber(o)
	return printString(o)

String printList(l)
	def sl = new StringBuilder()
	sl << '['
	l.each{ i -> sl << printObject(i) +', ' }
	sl.delete(sl.length()-2, sl.length())
	sl << ']'
	return sl

String printMap(m)
	def sm = new StringBuilder()
	sm << '['
	m.each{ k, v-> sm << printObject(k) +' : '+ printObject(v) +', ' }
	sm.delete(sm.length()-2, sm.length())
	sm << ']'
	return sm

String printNumber(n)
	return n

String printString(s)
	return "'" + s + "'"

To make this work in Mule, drop a Groovy component message processor, and paste the script inside the script text section. It prints the object without affecting the original payload.

Once done, you can copy the printed object from the logs and supply it to the DataMapper.

I hope you have enjoyed this blog post and that you find this information useful.

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.


Published at DZone with permission of Alan Cassar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}