import com.change_vision.jude.api.inf.model.*;
import com.change_vision.jude.api.inf.project.*;
import groovy.xml.MarkupBuilder
def PROJECT_PATH = args[0]
def OUTPUT_FILE = args[1]
println("Opening project...");
def prjAccessor = ProjectAccessorFactory.getProjectAccessor();
prjAccessor.open(PROJECT_PATH, true, false, true);
def project = prjAccessor.getProject();
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
project.ownedElements.each(){ diagram ->
diagram.ownedElements.each(){ schema ->
if(schema instanceof IERSchema){
schema.entities.each(){ entity ->
xml.createTable(tableName : entity.physicalName.trim(),
remarks:"${entity.logicalName.trim()}\t${entity.definition.replace('\n',' ').trim()}"){
def columns = []
columns.addAll(entity.primaryKeys)
columns.addAll(entity.nonPrimaryKeys)
columns.each{ attr ->
columnAttr = [name: attr.physicalName.trim()]
if(attr.lengthPrecision){
columnAttr.type = "${attr.datatype}(${attr.lengthPrecision})"
}else{
columnAttr.type = "${attr.datatype}"
}
if(attr.defaultValue) {
columnAttr.defaultValue = attr.defaultValue
}
if(attr.definition){
columnAttr.remarks =
"${attr.logicalName.trim()}\t${attr.definition.replace('\n',' ').trim()}"
}else{
columnAttr.remarks = "${attr.logicalName.trim()}"
}
if("TIMESTAMP" == attr.datatype.name){
columnAttr.type = " TIMESTAMP"
}
if(attr.primaryKey){
columnAttr.autoIncrement = true
}
column(columnAttr){
if(attr.primaryKey) constraints(primaryKey:"true", nullable:"false")
if(! attr.primaryKey && attr.notNull) constraints(nullable:"false")
}
}
column(name:"regist_date", type:" TIMESTAMP", remarks:"登録日")
}
}
}
}
}
prjAccessor.close();
if(OUTPUT_FILE){
new File(OUTPUT_FILE).withWriter(){
it << writer.toString()
}
}
println("Finished");