Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addRestAnnotation option to add @Resource #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scripts/DbReverseEngineer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 39 additions & 4 deletions src/groovy/grails/plugin/reveng/GrailsEntityPOJOClass.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href='mailto:[email protected]'>Burt Beckwith</a>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(', ') + ']'
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down