DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Mule DataMapper Groovy Sample Helper

Mule DataMapper Groovy Sample Helper

Alan Cassar user avatar by
Alan Cassar
·
May. 28, 14 · Integration Zone · Interview
Like (0)
Save
Tweet
6.34K Views

Join the DZone community and get the full member experience.

Join For Free

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.

DataMapper Groovy (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Integrate Event Streaming Into Your Applications
  • Transactions vs. Analytics in Apache Kafka
  • Making Machine Learning More Accessible for Application Developers
  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?

Comments

Integration Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo