Created
May 16, 2018 10:22
-
-
Save ansidev/2f43d48f46072c4eee4cc6cd10c39b3a to your computer and use it in GitHub Desktop.
Generate JPA Entity Script
This file contains hidden or 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.model.ObjectKind | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
import javax.swing.* | |
/* | |
* Written by ansidev. | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
typeMapping = [ | |
(~/(?i)int/) : "Integer", | |
(~/(?i)float|double|decimal|real/): "Double", | |
(~/(?i)datetime|timestamp/) : "Integer", | |
(~/(?i)date/) : "java.sql.Date", | |
(~/(?i)time/) : "java.sql.Time", | |
(~/(?i)/) : "String" | |
] | |
def input = { | |
JFrame jframe = new JFrame() | |
String answer = JOptionPane.showInputDialog(jframe, it) | |
jframe.dispose() | |
answer | |
} | |
packageName = input("Enter package name") | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> | |
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) } | |
} | |
def generate(table, dir) { | |
def tableName = table.getName() | |
def className = javaName(tableName, true) | |
def fields = calcFields(table) | |
new File(dir, className + ".java").withPrintWriter { out -> generate(out, tableName, className, fields) } | |
} | |
def generate(out, tableName, className, fields) { | |
out.println "package $packageName;" | |
out.println "" | |
out.println "import javax.persistence.*;" | |
out.println "" | |
out.println "@Entity" | |
out.println "@Table(name = \"$tableName\")" | |
out.println "public class $className {" | |
out.println "" | |
fields.each() { | |
if (it.annos != "") out.println " ${it.annos}" | |
if (it.name == "id") { | |
out.println " @Id" | |
out.println " @GeneratedValue" | |
} | |
out.println " @Column(name = \"${it.colName}\")" | |
out.println " private ${it.type} ${it.name};" | |
out.println "" | |
} | |
fields.each() { | |
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 | |
fields += [[ | |
colName: col.getName(), | |
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] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@entity
public class TopupData implements KeyedObject {
@id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String subscriberNo;
private String accountType;
private TelecomOperator telecomOperator;
private TelecomOperator subscriberNoTelecomOperator;
private String organizationCode;
private String requestOrganizationCode;
private Integer amount;
private String rechargeType;
private OperatorService serviceType;
private boolean transpose;
private String requestDate;
private String operatorPackage;
private String businessUniqueCode;
private String requestTraceCode;
private String customerNationalCode;
private String externalTransactionId;
private String previousAttemptStatus;
private String resultCode;
private String statusCode;
private String txType;
private String referenceNo;
private String operatorTransactionId;
private String pointOfService;
private Short specialPackageCode;
private String providerBalance;
private String specialPackageList;
private String specialPackage;
private String clientAppName;