Created
May 9, 2019 06:37
-
-
Save fobbyal/4a09187d3769420bf1a2619ee7d3feea to your computer and use it in GitHub Desktop.
Generate TransactionalImmutables.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 | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
packageName = "com.sample;" | |
typeMapping = [ | |
(~/(?i)int/) : "long", | |
(~/(?i)number|float|double|decimal|real/): "java.math.BigDecimal", | |
(~/(?i)datetime|timestamp/) : "org.joda.time.DateTime", | |
(~/(?i)date/) : "org.joda.time.LocalDate", | |
(~/(?i)time/) : "org.joda.time.LocalTime", | |
(~/(?i)/) : "String" | |
] | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) } | |
} | |
def computePackageName(dir) { | |
def path = dir.getAbsolutePath() | |
path.substring(path.indexOf(File.separator + 'java' + File.separator) + 6).replace(File.separator, ".") | |
} | |
def generate(table, dir) { | |
packageName = computePackageName(dir) | |
def className = javaName(table.getName(), true) | |
def fields = calcFields(table) | |
new File(dir, className + "Def.java").withPrintWriter { out -> generate(out, className, fields) } | |
} | |
def generate(out, className, fields) { | |
out.println "package $packageName;" | |
out.println "import com.fasterxml.jackson.databind.annotation.JsonDeserialize;" | |
out.println "import org.immutables.value.Value;" | |
out.println "import com.integ.core.immutables.DefStyle;" | |
out.println "import com.integ.db.mapper.MappableImmutable;" | |
out.println "import com.integ.db.mapper.Mapper;" | |
out.println "import java.util.Optional;" | |
out.println "" | |
out.println "" | |
out.println "@Value.Immutable" | |
out.println "@DefStyle" | |
out.println "@MappableImmutable" | |
out.println "@JsonDeserialize(builder = " + className + ".Builder.class)" | |
def hasModDate = fields.stream() | |
.anyMatch { f -> (f.name == "modifiedDate") } | |
def hasModifier = fields.stream() | |
.anyMatch { f -> (f.name == "modifier") } | |
def isModifiable = hasModDate && hasModifier | |
def extendedInterfaces = isModifiable ? "extends com.integ.transaction.TransactionalPayload" : "" | |
out.println "public interface " + className + "Def $extendedInterfaces {" | |
out.println "" | |
fields.findAll { f -> !isModifiable || (f.name != "modifier" && f.name != "modifiedDate") } | |
.each() { | |
if (it.annos != "") out.println "${it.annos}" | |
out.println " ${it.type} ${it.name}();" | |
out.println "" | |
} | |
if(isModifiable) { | |
out.println " @Override" | |
out.println " @Value.Auxiliary" | |
out.println " Optional<com.integ.immutables.ModificationInfo> modificationInfo();" | |
} | |
out.println "" | |
// fields.each() { | |
// out.println "" | |
// out.println " public ${it.type} get${it.name.capitalize()}() {" | |
// out.println " return ${it.name};" | |
// out.println " }" | |
// out.println "" | |
// out.println " public void set${it.name.capitalize()}(${it.type} ${it.name}) {" | |
// out.println " this.${it.name} = ${it.name};" | |
// out.println " }" | |
// 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 isNull = !col.isNotNull() | |
fields += [[ | |
name : javaName(col.getName(), false), | |
type : isNull ? "Optional<"+typeStr+">" : typeStr, | |
annos: " @Mapper(colName=\"" + col.getName() + "\")" | |
]] | |
} | |
} | |
def javaName(str, capitalize) { | |
def nameExceptions = [ | |
"unitid" : "unitId", | |
"unitname" : "unitName", | |
"shortname" : "shortName", | |
"transdate" : "transDate", | |
"effectivedate": "effectiveDate", | |
"modifieddate" : "modifiedDate", | |
"companyname" : "companyName", | |
"companytype" : "companyType", | |
"statecode" : "stateCode", | |
"unittypecode": "unitTypeCode", | |
"daylightsavings" : "dayLightSavings", | |
"resourcetype_code" : "resourceTypeCode", | |
"rstationcode" : "rStationCode", | |
"runitcode": "rUnitCode", | |
"commercialdate" : "commercialDate", | |
"pjmcosts" : "pjmCosts", | |
"udrtemp" : "udrTemp", | |
"rollup_isoid":"rollUpIsoId", | |
"groupname":"groupName", | |
] | |
if (nameExceptions.containsKey(str.toLowerCase())) | |
return nameExceptions.get(str.toLowerCase()) | |
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] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment