From 13122822fc61e0be30015a573b1c3e2d342c3f79 Mon Sep 17 00:00:00 2001 From: dstieglitz Date: Wed, 19 Feb 2014 12:47:52 -0800 Subject: [PATCH 1/4] renders full mapping block with all columns and column names as reported by database --- .../grails/plugin/reveng/GrailsEntityPOJOClass.groovy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy index b2aa10f..de6cb4d 100644 --- a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy +++ b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy @@ -178,7 +178,12 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { // return tableDef.toString() // } - '' + StringBuilder buf = new StringBuilder() + getAllPropertiesIterator().each { Property property -> + buf.append("\t\t${property.name} column:'${property.name}'\n".toString()); + } + + buf.toString() } @Override From aa4c90c6818013b4cf1982e5f86870d468b5d11c Mon Sep 17 00:00:00 2001 From: Gabe Hamilton Date: Tue, 4 Mar 2014 10:47:45 -0700 Subject: [PATCH 2/4] Added addRestAnnotation configuration option to add the @Resource annotation --- scripts/DbReverseEngineer.groovy | 3 +++ .../plugin/reveng/GrailsEntityPOJOClass.groovy | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/DbReverseEngineer.groovy b/scripts/DbReverseEngineer.groovy index 57bb263..e01b6f3 100644 --- a/scripts/DbReverseEngineer.groovy +++ b/scripts/DbReverseEngineer.groovy @@ -75,6 +75,9 @@ protected Map buildMergedConfig() { else { mergedConfig.overwriteExisting = true } + if (revengConfig.addRestAnnotation) { + mergedConfig.addRestAnnotation = revengConfig.addRestAnnotation + } if (revengConfig.alwaysMapManyToManyTables instanceof Boolean) { mergedConfig.alwaysMapManyToManyTables = revengConfig.alwaysMapManyToManyTables diff --git a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy index b2aa10f..8ef2559 100644 --- a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy +++ b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy @@ -33,6 +33,7 @@ import org.hibernate.type.LongType import org.hibernate.type.TimeType import org.hibernate.type.TimestampType import org.hibernate.type.Type +import org.springframework.util.StringUtils /** * @author Burt Beckwith @@ -204,6 +205,13 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { fixed.append delimiter } + if(revengConfig.addRestAnnotation) { + delimiter = newline + fixed.append delimiter + fixed.append 'import grails.rest.*' + fixed.append delimiter + } + imports = fixed.toString() if (imports) { return imports + newline + newline @@ -527,7 +535,14 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { } String renderClassStart() { - "class ${getDeclarationName()}${renderImplements()}{" + def c = new StringBuilder() + if(revengConfig.addRestAnnotation) { + String path = getDeclarationName() + path = path[0].toLowerCase() + path.substring(1) + c.append "@Resource(uri='/$path', formats=['json', 'xml'])" + c.append newline + } + c.append "class ${getDeclarationName()}${renderImplements()}{" } String renderImplements() { From 510d2a84eca5ad3668c337844c849395c88dc9c1 Mon Sep 17 00:00:00 2001 From: Gabe Hamilton Date: Tue, 4 Mar 2014 10:51:24 -0700 Subject: [PATCH 3/4] removed unnecessary import --- src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy index 8ef2559..08aad9b 100644 --- a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy +++ b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy @@ -33,7 +33,6 @@ import org.hibernate.type.LongType import org.hibernate.type.TimeType import org.hibernate.type.TimestampType import org.hibernate.type.Type -import org.springframework.util.StringUtils /** * @author Burt Beckwith From 26ff47437900e68402a16cae1143fa9c4f36485f Mon Sep 17 00:00:00 2001 From: Gabe Hamilton Date: Wed, 26 Mar 2014 12:31:47 -0700 Subject: [PATCH 4/4] Property name fix for unique key columns that are relationships. --- .../reveng/GrailsEntityPOJOClass.groovy | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy index dacd7e5..b6851a1 100644 --- a/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy +++ b/src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy @@ -33,6 +33,7 @@ import org.hibernate.type.LongType import org.hibernate.type.TimeType import org.hibernate.type.TimestampType import org.hibernate.type.Type +import java.beans.Introspector /** * @author Burt Beckwith @@ -304,6 +305,10 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { def constraints = new StringBuilder() + def belongs = new TreeSet() + def hasMany = new TreeSet() + findBelongsToAndHasMany belongs, hasMany + getAllPropertiesIterator().each { Property property -> if (!getMetaAttribAsBool(property, 'gen-property', true)) { return @@ -330,9 +335,9 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { } clazz.table.uniqueKeyIterator.each { UniqueKey key -> - if (key.columnSpan == 1 || key.name == clazz.table.primaryKey.name) return + if (key.columnSpan == 1 || key.name == clazz.table.primaryKey?.name) return if (key.columns[-1] == column) { - def otherNames = key.columns[0..-2].collect { "\"$it.name\"" } + def otherNames = key.columns[0..-2].collect { "\"" + columnNameAsProperty(it.name, belongs) + "\"" } values.unique = '[' + otherNames.reverse().join(', ') + ']' } } @@ -353,6 +358,17 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { constraints.length() ? "\tstatic constraints = {$newline$constraints\t}" : '' } + protected String columnNameAsProperty(String name, Set belongsTo) { + String relationName = name?.replace('Id', '') + for(r in belongsTo) { + if(r == relationName) { + return Introspector.decapitalize(r) + } + } + + Introspector.decapitalize(name) + } + protected boolean isDateType(Type type) { (type instanceof DateType) || (type instanceof TimestampType) || (type instanceof TimeType) || (type instanceof CalendarType) || (type instanceof CalendarDateType) @@ -542,7 +558,7 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { def c = new StringBuilder() if(revengConfig.addRestAnnotation) { String path = getDeclarationName() - path = path[0].toLowerCase() + path.substring(1) + path = Introspector.decapitalize(path) c.append "@Resource(uri='/$path', formats=['json', 'xml'])" c.append newline }