tag:blogger.com,1999:blog-7017083405558956091.post850268555516945982..comments2023-07-05T05:50:39.922-07:00Comments on Kickin' down the cobblestones: Another way to use Groovy in the EnterpriseDave Kleinhttp://www.blogger.com/profile/05996044798024907436noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-7017083405558956091.post-74255431969499501702008-08-18T20:21:00.000-07:002008-08-18T20:21:00.000-07:00This may go without saying, but if you want to cam...This may go without saying, but if you want to camel case words that are space delimited, you can just do this:<BR/><BR/><BR/> def camelCaseConverter(orig) {<BR/> orig.toLowerCase().replaceAll(/[_ ](\w)?/) {wholeMatch, firstLetter -><BR/> firstLetter?.toUpperCase() ?: ""<BR/> }<BR/> }Joshua Jacobshttps://www.blogger.com/profile/09166525479894214388noreply@blogger.comtag:blogger.com,1999:blog-7017083405558956091.post-36328580008371841452008-06-09T09:53:00.000-07:002008-06-09T09:53:00.000-07:00Groovy is awesome for this kind of work. I have be...Groovy is awesome for this kind of work. I have been doing a less groovy technique using custom DAO that map result set columns onto POGOs. I did all my mapping in the custom queries that would alias column names onto the name used by the POGO. For example:<BR/><BR/>SELECT FIRST_NAME as firstName, LAST_NAME as lastName, ... and so on...<BR/><BR/>I'll have to give this technique a try instead.<BR/><BR/>What's great about this is that even if you are forced (for various reasons) not to use GORM for certain data you can still define constraints on these POGOs and show them to Controllers that can then do the normal Grails validation magic before sending the POGO back to the DAO.<BR/><BR/>Or you can use this as a data import or ETL.<BR/><BR/>Either way, neat stuff!Anonymoushttps://www.blogger.com/profile/07041739178513685832noreply@blogger.comtag:blogger.com,1999:blog-7017083405558956091.post-78896439163957252022008-05-16T20:03:00.000-07:002008-05-16T20:03:00.000-07:00You can also use regular expressions to do the sam...You can also use regular expressions to do the same thing in a pretty groovy way. String has a replaceAll that you can pass a closure to. In that closure, you have access to the groups that were matched in your regular expression:<BR/><BR/><BR/>def camelCaseConverter(orig) {<BR/> orig.toLowerCase().replaceAll(/_(\w)?/) { wholeMatch, firstLetter -><BR/> firstLetter?.toUpperCase() ?: ""<BR/> }<BR/>}<BR/><BR/>String.metaClass.toCamelCase = {-><BR/> camelCaseConverter(delegate)<BR/>}<BR/><BR/>assert "fooBarBaz" == "FOO_bar_bAZ_".toCamelCase()<BR/>assert "camelCaseWord" == "camel_case_word".toCamelCase()Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7017083405558956091.post-52688428571045094252008-05-16T13:10:00.000-07:002008-05-16T13:10:00.000-07:00Thanks Andres! I updated the post with your groov...Thanks Andres! I updated the post with your groovier version.Dave Kleinhttps://www.blogger.com/profile/05996044798024907436noreply@blogger.comtag:blogger.com,1999:blog-7017083405558956091.post-41509987346623074872008-05-16T12:37:00.000-07:002008-05-16T12:37:00.000-07:00Here are 2 versions, the first one is Java-ish (so...Here are 2 versions, the first one is Java-ish (sorry if formatting messes up the code)<BR/><BR/>def camelCaseConverter = { str -><BR/> def result = ""<BR/> str.split( "_" ). eachWithIndex { word, index -><BR/> word = word.toLowerCase()<BR/> switch( index ){<BR/> case 0: <BR/> result += word<BR/> break<BR/> default:<BR/> result += word[0].toUpperCase() + word[1..-1]<BR/> break<BR/> }<BR/> }<BR/> result<BR/>}<BR/><BR/>The second one is groovier<BR/><BR/>def camelCaseConverter = { str -><BR/> def result = str.split("_").collect { word -><BR/> word[0].toUpperCase() + word[1..-1].toLowerCase()<BR/> }.join("")<BR/> // make sure first char is lowerCase<BR/> result[0].toLowerCase() + result[1..-1]<BR/>}<BR/><BR/>Of course you can test them with<BR/><BR/>def columnName = "EMPLOYEE_ID"<BR/>assert "employeeId" == camelCaseConverter( columnName )<BR/><BR/>String.metaClass.toCamelCase = { -><BR/> camelCaseConverter(delegate)<BR/>}<BR/>assert "employeeId" == columnName.toCamelCase()<BR/><BR/>=)Andres Almirayhttps://www.blogger.com/profile/10710950259740699258noreply@blogger.com