Advertisement
Guest User

Untitled

a guest
Sep 14th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.50 KB | None | 0 0
  1. import com.intellij.database.model.DasTable
  2. import com.intellij.database.util.Case
  3. import com.intellij.database.util.DasUtil
  4.  
  5. /*
  6.  * Available context bindings:
  7.  *   SELECTION   Iterable<DasObject>
  8.  *   PROJECT     project
  9.  *   FILES       files helper
  10.  */
  11.  
  12. packageName = "pl.monusky.gotpttk.dataAccess.entity"
  13. typeMapping = [
  14.   (~/(?i)int/)                      : "Long",
  15.   (~/(?i)float|double|decimal|real/): "Double",
  16.   (~/(?i)datetime|timestamp/)       : "LocalDateTime",
  17.   (~/(?i)date/)                     : "Date",
  18.   (~/(?i)time/)                     : "Time",
  19.   (~/(?i)/)                         : "String"
  20. ]
  21.  
  22. FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
  23.   SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
  24. }
  25.  
  26. def generate(table, dir) {
  27.   def className = javaName(table.getName(), true)
  28.   def fields = calcFields(table)
  29.   new File(dir, className + ".kt").withPrintWriter { out -> generate(table, out, className, fields) }
  30. }
  31.  
  32. def generate(table, out, className, fields) {
  33.   out.println "package $packageName"
  34.   out.println ""
  35.   if (table.getName().contains("_")) {
  36.     out.println "import pl.monusky.gotpttk.dataAccess.entity.$className" + ".Companion.TABLE_NAME"
  37.   }
  38.   out.println "import javax.persistence.*"
  39.   out.println "import java.time.LocalDateTime"
  40.   out.println ""
  41.   out.println "@Entity"
  42.   if (table.getName().contains("_")) {
  43.     out.println "@Table(name = TABLE_NAME)"
  44.   }
  45.   out.println "class $className : AbstractJpaPersistable<Long>() {"
  46.   out.println ""
  47.   fields.each() {
  48.     if (it.name != "id") {
  49.       if (it.annos != "") out.println "  ${it.annos}"
  50.       out.println "    var ${it.name}: ${it.type}? = null"
  51.       out.println ""
  52.     }
  53.   }
  54.   out.println ""
  55.   out.println ""
  56.   printCompanion(table, out, fields)
  57.   out.println "}"
  58. }
  59.  
  60. def calcFields(table) {
  61.   DasUtil.getColumns(table).reduce([]) { fields, col ->
  62.     def spec = Case.LOWER.apply(col.getDataType().getSpecification())
  63.     def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
  64.     fields += [[
  65.                  name : javaName(col.getName(), false),
  66.                  type : typeStr,
  67.                  annos: ""]]
  68.   }
  69. }
  70.  
  71. def javaName(str, capitalize) {
  72.   def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
  73.     .collect { Case.LOWER.apply(it).capitalize() }
  74.     .join("")
  75.     .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
  76.   capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
  77. }
  78.  
  79. def printCompanion(table, out, fields) {
  80.   out.println "    companion object {"
  81.   out.println "        const val TABLE_NAME = " + getTextAsString(table.getName())
  82.   out.println "        const val COLUMN_ID = " + getTextAsString("ID")
  83.   def companionFields = getCompanionFields(table)
  84.   companionFields.each() {
  85.     if(it.key != "ID") {
  86.       def key = it.key
  87.       def value = it.value
  88.       out.println "        const val COLUMN_$key = $value"
  89.     }
  90.   }
  91.  
  92.   out.println "    }"
  93. }
  94.  
  95. def getCompanionFields(table) {
  96.   return DasUtil.getColumns(table).reduce([]) { fields, col ->
  97.     fields += [[key : getCompanionKey(col.getName()), value : getTextAsString(javaName(col.getName(), true))]]
  98.   }
  99. }
  100.  
  101. def getCompanionKey(str) {
  102.   def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
  103.           .collect { it.toUpperCase() }
  104.           .join("_")
  105.           .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
  106. }
  107.  
  108. def getTextAsString(text) {
  109.   return '"' + text + '"'
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement