| 1 |
package org.chama.builder.model |
|---|
| 2 |
|
|---|
| 3 |
import org.chama.builder.java.JavaProperty |
|---|
| 4 |
import org.chama.db.SqlMapper |
|---|
| 5 |
import org.chama.Inflection |
|---|
| 6 |
|
|---|
| 7 |
import groovy.text.Template |
|---|
| 8 |
import groovy.text.SimpleTemplateEngine |
|---|
| 9 |
import groovy.util.GroovyScriptEngine |
|---|
| 10 |
import org.codehaus.groovy.runtime.InvokerHelper |
|---|
| 11 |
|
|---|
| 12 |
class JavaModelDAOBuilder implements ModelBuilder { |
|---|
| 13 |
def dbMetaData |
|---|
| 14 |
def packages |
|---|
| 15 |
def parentDir |
|---|
| 16 |
def templateDir |
|---|
| 17 |
def buildType = BUILD_ALL |
|---|
| 18 |
def modelToBuild |
|---|
| 19 |
def ignoreMethods = ["invokeMethod", |
|---|
| 20 |
"getMetaClass", |
|---|
| 21 |
"setMetaClass", |
|---|
| 22 |
"setProperty", |
|---|
| 23 |
"getProperty", |
|---|
| 24 |
"hashCode", |
|---|
| 25 |
"getClass", |
|---|
| 26 |
"wait", |
|---|
| 27 |
"equals", |
|---|
| 28 |
"notify", |
|---|
| 29 |
"notifyAll", |
|---|
| 30 |
"toString"] |
|---|
| 31 |
def build() { |
|---|
| 32 |
def files =[:] |
|---|
| 33 |
//first check the build type |
|---|
| 34 |
switch(buildType) { |
|---|
| 35 |
case BUILD_ALL: |
|---|
| 36 |
//get all the tables from the database |
|---|
| 37 |
dbMetaData.tables.each { |
|---|
| 38 |
def modelName = Inflection.singularize(SqlMapper.columnToCamelCaseAll(it.key)) |
|---|
| 39 |
buildDAO(modelName) |
|---|
| 40 |
buildModel(modelName) |
|---|
| 41 |
|
|---|
| 42 |
} |
|---|
| 43 |
break |
|---|
| 44 |
case BUILD_MODEL: |
|---|
| 45 |
buildDAO(modelToBuild) |
|---|
| 46 |
buildModel(modelToBuild) |
|---|
| 47 |
break |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
def buildModel(modelName) { |
|---|
| 53 |
def engine = new SimpleTemplateEngine() |
|---|
| 54 |
|
|---|
| 55 |
def modelTable = dbMetaData.getTable(Inflection.pluralize(modelName)); |
|---|
| 56 |
//first we need to find this model in one of the packages |
|---|
| 57 |
//convert packages to file system |
|---|
| 58 |
packages = packages.collect { new File(parentDir + it.replace('.', '/')) } |
|---|
| 59 |
|
|---|
| 60 |
//next see if we can find any models on the file system |
|---|
| 61 |
def files = [] |
|---|
| 62 |
packages.each { dir -> |
|---|
| 63 |
dir.eachFileMatch(~/${modelName}\.groovy/) { |
|---|
| 64 |
files.add(it) |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
//now that we have the groovy models we need the JavaModelTemplate |
|---|
| 69 |
def modelTemplateFile = new File(templateDir + "/model/JavaModel.tmpl") |
|---|
| 70 |
|
|---|
| 71 |
//need to build up all of the properties for adding to the template |
|---|
| 72 |
def properties = modelTable.columns.collect { |
|---|
| 73 |
def javaProperty = new JavaProperty(name:SqlMapper.columnToCamelCase(it.name), |
|---|
| 74 |
type:SqlMapper.getPropertyType(it.typeName), |
|---|
| 75 |
allCamelCaseName:SqlMapper.columnToCamelCaseAll(it.name)) |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
def imports = [] |
|---|
| 79 |
|
|---|
| 80 |
def binding = [packageName:"org.chama.test.models", |
|---|
| 81 |
imports:imports, |
|---|
| 82 |
className:modelName, |
|---|
| 83 |
properties:properties] |
|---|
| 84 |
def modelTemplate = engine.createTemplate(modelTemplateFile).make(binding) |
|---|
| 85 |
return modelTemplate.toString() |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
def buildDAO(modelName) { |
|---|
| 89 |
def engine = new SimpleTemplateEngine() |
|---|
| 90 |
|
|---|
| 91 |
def modelTable = dbMetaData.getTable(Inflection.pluralize(modelName)); |
|---|
| 92 |
//first we need to find this model in one of the packages |
|---|
| 93 |
//convert packages to file system |
|---|
| 94 |
def dirs = packages.collect { new File(parentDir + it.replace('.', '/')) } |
|---|
| 95 |
|
|---|
| 96 |
//next see if we can find any models on the file system |
|---|
| 97 |
def files = [] |
|---|
| 98 |
dirs.each { dir -> |
|---|
| 99 |
dir.eachFileMatch(~/${modelName}\.groovy/) { |
|---|
| 100 |
files.add(it) |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
if(files.size() == 1) { |
|---|
| 104 |
def model = files.get(0) |
|---|
| 105 |
//easiest thing to do is to grab the first line and get the package |
|---|
| 106 |
def packageLine |
|---|
| 107 |
model.withReader { reader -> |
|---|
| 108 |
packageLine = reader.readLine() |
|---|
| 109 |
} |
|---|
| 110 |
def packageName = packageLine.substring(8, packageLine.indexOf(";")) |
|---|
| 111 |
def scriptEngine = new GroovyScriptEngine(".", parentDir) |
|---|
| 112 |
def clazz = scriptEngine.loadScriptByName("${packageName}.${modelName}") |
|---|
| 113 |
def obj = clazz.newInstance() |
|---|
| 114 |
def methods = clazz.methods |
|---|
| 115 |
methods = methods.findAll { |
|---|
| 116 |
if(!ignoreMethods.contains(it.name)) { |
|---|
| 117 |
return it |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
def classInterceptor = new ModelInterceptor(obj.class) |
|---|
| 121 |
//def registry = InvokerHelper.instance.metaRegistry |
|---|
| 122 |
//registry.setMetaClass(obj.class, classInterceptor) |
|---|
| 123 |
obj.setMetaClass(classInterceptor) |
|---|
| 124 |
|
|---|
| 125 |
def newMethods = [] |
|---|
| 126 |
methods.each {method -> |
|---|
| 127 |
//passing in the actual method as arguments so we can build it |
|---|
| 128 |
//in the interceptor |
|---|
| 129 |
InvokerHelper.invokeMethod(obj, method.name, method) |
|---|
| 130 |
//grab the method representation we just worked on |
|---|
| 131 |
newMethods.add(classInterceptor.method) |
|---|
| 132 |
} |
|---|
| 133 |
//set the templateDir on each method so it can build itself |
|---|
| 134 |
newMethods.each {newMethod -> |
|---|
| 135 |
newMethod.templateDir = templateDir |
|---|
| 136 |
} |
|---|
| 137 |
//now that we have the groovy models we need the JavaModelTemplate |
|---|
| 138 |
def modelTemplateFile = new File(templateDir + "/model/JavaDAO.tmpl") |
|---|
| 139 |
|
|---|
| 140 |
//need to build up all of the properties for adding to the template |
|---|
| 141 |
def properties = modelTable.columns.collect { |
|---|
| 142 |
def javaProperty = new JavaProperty(name:SqlMapper.columnToCamelCase(it.name), |
|---|
| 143 |
type:SqlMapper.getPropertyType(it.typeName), |
|---|
| 144 |
allCamelCaseName:SqlMapper.columnToCamelCaseAll(it.name)) |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
def imports = ["org.chama.test.models.Band", |
|---|
| 148 |
"com.sourcebeat.tap101.events.dao.BandDAO"] |
|---|
| 149 |
|
|---|
| 150 |
def binding = [packageName:packageName, |
|---|
| 151 |
imports:imports, |
|---|
| 152 |
className:modelName, |
|---|
| 153 |
properties:properties, |
|---|
| 154 |
methods:newMethods] |
|---|
| 155 |
def modelTemplate = engine.createTemplate(modelTemplateFile).make(binding) |
|---|
| 156 |
return modelTemplate.toString() |
|---|
| 157 |
|
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
} |
|---|