
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.text.SimpleDateFormat
packageName = "com.sample;"
typeMapping = [
(~/(?i)int/) : "long",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)date/) : "Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
packageName = getPackageName(dir)
new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields, table) }
}
def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import lombok.Data;"
out.println "import javax.persistence.*;"
out.println "import java.io.Serializable;"
out.println "import io.swagger.annotations.ApiModel;"
out.println "import io.swagger.annotations.ApiModelProperty;"
Set types = new HashSet()
fields.each() {
types.add(it.type)
}
if (types.contains("Date")) {
out.println "import java.util.Date;"
out.println "import com.fasterxml.jackson.annotation.JsonFormat;"
out.println "import org.springframework.format.annotation.DateTimeFormat;"
}
if (types.contains("InputStream")) {
out.println "import java.io.InputStream;"
}
out.println ""
out.println ""
out.println "/**\n" +
" * ${table.comment}\n"+
" * \n"+
" * @Author Gavino\n" +
" * @Create " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " \n" +
" * @Description \n" +
" */"
out.println "@Data"
out.println "@ApiModel(value=\"${table.comment}\")"
out.println "@Table ( name =\"" + table.getName() + "\" , schema = \"\")"
out.println "public class $className implements Serializable{"
out.println ""
out.println " "+genSerialID()
out.println ""
fields.each() {
out.println ""
if (isNotEmpty(it.commoent)) {
out.println " /**"
out.println " * ${it.commoent.toString()}"
out.println " */"
}
if ((it.annos+"").indexOf("[@Id]") >= 0){
out.println " @Id"
out.println " @GeneratedValue(strategy = GenerationType.IDENTITY,generator = \"select sequence.nextval from dual\")"
}
if (it.type == "Date"){
out.println " @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")"
out.println " @DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")"
}
out.println " @ApiModelProperty(value = \"${it.commoent.toString()}\")"
out.println " @Column(name = \"${it.colum}\")"
out.println " private ${it.type} ${it.colum};"
}
out.println ""
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
def comm = [
name : javaName(col.getName(), false),
colum: col.getName(),
type : typeStr,
commoent: col.getComment(),
annos : "@Column(name = \"" + col.getName() + "\" )"]
if ("n_a".equals(Case.LOWER.apply(col.getName())))
comm.annos += "[@Id]"
fields += [comm]
}
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
static String genSerialID() {
return "\tprivate static final long serialVersionUID = " + Math.abs(new Random().nextLong()) + "L;"
}