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..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 @@ -178,7 +179,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 @@ -204,6 +210,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 @@ -292,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 @@ -318,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(', ') + ']' } } @@ -341,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) @@ -527,7 +555,14 @@ class GrailsEntityPOJOClass extends EntityPOJOClass { } String renderClassStart() { - "class ${getDeclarationName()}${renderImplements()}{" + def c = new StringBuilder() + if(revengConfig.addRestAnnotation) { + String path = getDeclarationName() + path = Introspector.decapitalize(path) + c.append "@Resource(uri='/$path', formats=['json', 'xml'])" + c.append newline + } + c.append "class ${getDeclarationName()}${renderImplements()}{" } String renderImplements() {