Created
January 18, 2023 14:08
-
-
Save seolys/b7195b899b9ada8363839811bf5981c9 to your computer and use it in GitHub Desktop.
Generate claim-service Entity.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.database.model.DasTable | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
NOT_NULL_COLUMN_OPTION = ", nullable = false"; | |
TYPE_MAPPING = [ | |
(~/(?i)int8|bigint/) : "Long", | |
(~/(?i)int2|smallint|integer/) : "Integer", | |
(~/(?i)float|double|decimal|real|numeric/): "BigDecimal", | |
(~/(?i)datetime|timestamp/) : "ZonedDateTime", | |
(~/(?i)date/) : "LocalDate", | |
(~/(?i)time/) : "LocalTime", | |
(~/(?i)dom_tf/) : "boolean", | |
(~/(?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 schemaName = DasUtil.getSchema(table) | |
def tableName = table.getName() | |
def className = toClassName(tableName, true) | |
def fields = calcFields(table) | |
new File(dir, className + ".java").withPrintWriter { out -> generate(out, schemaName, tableName, className, fields) } | |
} | |
def generate(out, schemaName, tableName, className, fields) { | |
out.println "import kr.co._29cm.claim.v2.domain.common.BaseEntity;" | |
out.println "import kr.co._29cm.claim.v2.domain.common.converter.TFCodeBooleanConverter;" | |
out.println "import lombok.Getter;" | |
out.println "import lombok.NoArgsConstructor;" | |
out.println "import org.hibernate.annotations.DynamicUpdate;" | |
out.println "" | |
out.println "import javax.persistence.Column;" | |
out.println "import javax.persistence.Convert;" | |
out.println "import javax.persistence.Entity;" | |
out.println "import javax.persistence.GeneratedValue;" | |
out.println "import javax.persistence.GenerationType;" | |
out.println "import javax.persistence.Id;" | |
out.println "import javax.persistence.Table;" | |
out.println "import java.time.ZonedDateTime;" | |
out.println "" | |
out.println "" | |
out.println "@Getter" | |
out.println "@Entity" | |
out.println "@DynamicUpdate" | |
out.println "@NoArgsConstructor" | |
out.println "@Table(schema = \"$schemaName\", name = \"$tableName\")" | |
out.println "public class $className" + " extends BaseEntity {" | |
out.println "" | |
fields.each() { | |
def nullableOption = it.notNull || it.primaryKey ? NOT_NULL_COLUMN_OPTION : "" | |
def comment = notBlank(it.comment) ? "// ${it.comment}" : "" | |
if (it.primaryKey) { | |
out.println " @Id" | |
out.println " @GeneratedValue(strategy = GenerationType.IDENTITY)" | |
} | |
// enum class 대상 | |
if (isCodeColumn(it.name)) { | |
def enumClassName = toClassName(it.name, true); | |
out.println "// @Convert(converter = ${enumClassName}.Converter.class)" | |
out.println "// @Enumerated(value = EnumType.STRING)" | |
out.println " @Column(name = \"${it.colName}\"${nullableOption})" | |
out.println " private ${enumClassName} ${it.name}; ${comment}" | |
out.println "" | |
return | |
} | |
// TFCode | |
if (isTrueFalseColumn(it.spec)) { | |
out.println " @Convert(converter = TFCodeBooleanConverter.class)" | |
} | |
out.println " @Column(name = \"${it.colName}\"${nullableOption})" | |
out.println " private ${it.type} ${it.name}; ${comment}" | |
out.println "" | |
} | |
out.println "}" | |
} | |
def calcFields(table) { | |
def colIndex = 0 | |
DasUtil.getColumns(table).reduce([]) { def fields, col -> | |
def dataType = col.getDataType() | |
def spec = dataType.getSpecification() | |
def suffix = dataType.suffix | |
def typeStr = (String) TYPE_MAPPING.find { p, t -> p.matcher(Case.LOWER.apply(spec)).find() }.value | |
def colName = (String) col.getName() | |
colIndex++ | |
def javaColName = (String) toJavaColName(colName, false) | |
def attrs = col.getTable().getColumnAttrs(col) | |
def columnDefault = col.getDefault() | |
def isAutoInc = DasUtil.isAutoGenerated(col) | |
fields += [[ | |
name : javaColName, | |
colName : colName, | |
suffix : suffix, | |
col : col, | |
spec : spec, | |
attrs : attrs, | |
default : columnDefault, | |
type : typeStr, | |
// constraints : constraints.reduce("") { all, constraint -> | |
// all += "[ name: ${constraint.name}, " + "kind: ${constraint.getKind()}," + "]" | |
// }, | |
notNull : col.isNotNull(), | |
autoInc : isAutoInc, | |
nullable : !col.isNotNull() || columnDefault != null, | |
primaryKey: DasUtil.isPrimary(col), | |
comment : col.getComment(), | |
annos : ""]] | |
fields | |
} | |
} | |
def toClassName(str, capitalize) { | |
def removedPrefixStr = str.replaceAll(/^t_(.*)/, "\$1") | |
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(removedPrefixStr) | |
.collect { Case.LOWER.apply(it).capitalize() } | |
.join("") | |
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_") | |
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1] | |
} | |
def toJavaColName(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 isCodeColumn(columnName) { | |
if (columnName == null) { | |
return false | |
} | |
if (columnName.endsWith("Type") || columnName.endsWith("Status") || columnName.endsWith("Code")) { | |
return true | |
} | |
return false | |
} | |
def isTrueFalseColumn(spec) { | |
if (spec == null) { | |
return false | |
} | |
return java.util.Objects.equals(spec, "dom_tf") | |
} | |
def notBlank(str) { | |
return str != null && str.trim().length() > 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment