{"id":954,"date":"2011-08-15T13:28:55","date_gmt":"2011-08-15T11:28:55","guid":{"rendered":"http:\/\/www.pleus.net\/blog\/?p=954"},"modified":"2011-08-15T13:28:55","modified_gmt":"2011-08-15T11:28:55","slug":"beautiful-transformations-with-groovy","status":"publish","type":"post","link":"https:\/\/www.pleus.net\/blog\/?p=954","title":{"rendered":"Beautiful Transformations with Groovy"},"content":{"rendered":"<p>Data transformations are the daily business in ETL and ESB scenarios. If you have a service- or business process boundary it is very likely that data has to be transformed between different representation.<br \/>\nTypical integration scenarios have to deal with a huge amount of different formats (flat file, xml, csv, json, even binary). To make things even worse they tend to change from time to time. Therefore it is important to have tools that are flexible  and easy to understand. I found Groovy to be very handy for the transformation job. Let&#8217;s have a look at a typical example. Assume we have a flat file in the following format:<\/p>\n<pre class=\"brush:text\">\r\nId Product     Amount\r\n 1 Cheese      15\r\n 2 Sausage     2\r\n 3 Bread       3\r\n 4 Butter      4\r\n<\/pre>\n<p>What we want is an xml representation like this:<\/p>\n<pre class=\"brush:xml\">\r\n<order>\r\n  <item>\r\n    <id id='1' \/>\r\n    <name>Cheese<\/name>\r\n    <amount>15<\/amount>\r\n  <\/item>\r\n  <item>\r\n    <id id='2' \/>\r\n    <name>Sausage<\/name>\r\n    <amount>2<\/amount>\r\n  <\/item>\r\n  <item>\r\n    <id id='3' \/>\r\n    <name>Bread<\/name>\r\n    <amount>3<\/amount>\r\n  <\/item>\r\n  <item>\r\n    <id id='4' \/>\r\n    <name>Butter<\/name>\r\n    <amount>4<\/amount>\r\n  <\/item>\r\n<\/order>\r\n<\/pre>\n<p>The groovy script below does the job. The great thing is not only that is works but also that its easy to develop and maintain. A developer can see the source and target structure at the same time.<\/p>\n<pre class=\"brush:groovy;highlight:[5,8,9,10,11]\">\r\ndef reader = new StringReader(flatfile);\r\ndef writer = new StringWriter();\r\ndef builder = new groovy.xml.MarkupBuilder(writer)\r\n\r\nbuilder.order(){\r\n    reader.eachLine() {\r\n        def line = it\r\n        item(){  \r\n            id(id:line.getAt(0..1).trim())\r\n            name(line.getAt(3..14).trim())\r\n            amount(line.substring(15).trim())\r\n        }\r\n    }\r\n}\r\n\r\nprintln writer.toString();\r\n<\/pre>\n<p>Thanks to closures an the MarkupBuilder class the transformation can be written in a fluent and intuitive way. Typical other transformations are as simple as that, making Groovy a first choice for data transformations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data transformations are the daily business in ETL and ESB scenarios. If you have a service- or business process boundary it is very likely that data has to be transformed between different representation. Typical integration scenarios have to deal with a huge amount of different formats (flat file, xml, csv, json, even binary). To make &hellip; <a href=\"https:\/\/www.pleus.net\/blog\/?p=954\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Beautiful Transformations with Groovy<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,3],"tags":[11],"class_list":["post-954","post","type-post","status-publish","format-standard","hentry","category-bpm","category-soa","tag-groovy"],"_links":{"self":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=954"}],"version-history":[{"count":37,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":1101,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/954\/revisions\/1101"}],"wp:attachment":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}