Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.intellij.database.model.DasTable
- import com.intellij.database.util.Case
- import com.intellij.database.util.DasUtil
- /*
- * Available context bindings:
- * SELECTION Iterable<DasObject>
- * PROJECT project
- * FILES files helper
- */
- packageName = "pl.monusky.gotpttk.dataAccess.entity"
- typeMapping = [
- (~/(?i)int/) : "Long",
- (~/(?i)float|double|decimal|real/): "Double",
- (~/(?i)datetime|timestamp/) : "LocalDateTime",
- (~/(?i)date/) : "Date",
- (~/(?i)time/) : "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)
- new File(dir, className + ".kt").withPrintWriter { out -> generate(table, out, className, fields) }
- }
- def generate(table, out, className, fields) {
- out.println "package $packageName"
- out.println ""
- if (table.getName().contains("_")) {
- out.println "import pl.monusky.gotpttk.dataAccess.entity.$className" + ".Companion.TABLE_NAME"
- }
- out.println "import javax.persistence.*"
- out.println "import java.time.LocalDateTime"
- out.println ""
- out.println "@Entity"
- if (table.getName().contains("_")) {
- out.println "@Table(name = TABLE_NAME)"
- }
- out.println "class $className : AbstractJpaPersistable<Long>() {"
- out.println ""
- fields.each() {
- if (it.name != "id") {
- if (it.annos != "") out.println " ${it.annos}"
- out.println " var ${it.name}: ${it.type}? = null"
- out.println ""
- }
- }
- out.println ""
- out.println ""
- printCompanion(table, out, fields)
- 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
- fields += [[
- name : javaName(col.getName(), false),
- type : typeStr,
- annos: ""]]
- }
- }
- 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 printCompanion(table, out, fields) {
- out.println " companion object {"
- out.println " const val TABLE_NAME = " + getTextAsString(table.getName())
- out.println " const val COLUMN_ID = " + getTextAsString("ID")
- def companionFields = getCompanionFields(table)
- companionFields.each() {
- if(it.key != "ID") {
- def key = it.key
- def value = it.value
- out.println " const val COLUMN_$key = $value"
- }
- }
- out.println " }"
- }
- def getCompanionFields(table) {
- return DasUtil.getColumns(table).reduce([]) { fields, col ->
- fields += [[key : getCompanionKey(col.getName()), value : getTextAsString(javaName(col.getName(), true))]]
- }
- }
- def getCompanionKey(str) {
- def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
- .collect { it.toUpperCase() }
- .join("_")
- .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
- }
- def getTextAsString(text) {
- return '"' + text + '"'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement