-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_data_binders.groovy
206 lines (199 loc) · 8.72 KB
/
generate_data_binders.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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.example.models.binders"
typeMapping = [
(~/(?i)boolean/) : "Boolean",
(~/(?i)int/) : "Integer",
(~/(?i)long/) : "Long",
(~/(?i)float/) : "Float",
(~/(?i)double|decimal|real/): "Double",
(~/(?i)datetime|timestamp/) : "Timestamp",
(~/(?i)date/) : "java.sql.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generateJavaFile(it, dir) }
}
def generateJavaFile(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
new File(dir, className + "Binder.java").withPrintWriter { out -> generateJavaSourceCode(out, className, fields) }
}
def generateJavaSourceCode(out, className, fields) {
out.println "package $packageName;"
out.println ""
out.println "import javafx.beans.property.*;"
out.println "import java.io.Serializable;"
out.println "import java.sql.*;"
out.println "import java.util.*;"
out.println "import java.text.*;"
out.println ""
out.println "public class ${className}Binder implements Serializable {"
out.println ""
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
out.println " private Simple${it.type.replaceAll("java.sql.Date", "String").replaceAll("Timestamp", "String")}Property ${it.name};"
}
out.println ""
out.println ""
out.print " public ${className}Binder ( "
fields.each() {
out.print "${it.type} _${it.name}"
if (it != fields.last())
out.print ", "
}
out.print ") { "
out.println ""
fields.each() {
if (it.type == "java.sql.Date" || it.type == "Timestamp")
out.println " this.${it.name} = new SimpleStringProperty(new SimpleDateFormat(\"yyyy.MM.dd.HH.mm.ss\").format(_${it.name}));"
else
out.println " this.${it.name} = new Simple${it.type.replaceAll("java.sql.", "")}Property(_${it.name});"
}
out.println " }"
out.println ""
out.print " public ${className}Binder(){ }"
out.println ""
fields.each() {
out.println ""
out.println " public ${it.type} get${it.name.capitalize()}() {"
if (it.type == "java.sql.Date")
out.println " return java.sql.Date.valueOf(this.${it.name}.get());"
else if (it.type == "Timestamp")
out.println " return Timestamp.valueOf(this.${it.name}.get());"
else
out.println " return this.${it.name}.get();"
out.println " }"
out.println ""
out.println " public void set${it.name.capitalize()}(${it.type} _${it.name}) {"
if (it.type == "java.sql.Date")
out.println " this.${it.name}.set(_${it.name}.toString());"
else if (it.type == "Timestamp")
out.println " this.${it.name}.set(String.valueOf(_${it.name}));"
else
out.println " this.${it.name}.set(_${it.name});"
out.println " }"
out.println ""
out.println " public void parseAndSet${it.name.capitalize()}(String _${it.name}) {"
if (it.type == "Integer")
out.println " this.set${it.name.capitalize()}(Integer.parseInt(_${it.name}));"
else if (it.type == "Double")
out.println " this.set${it.name.capitalize()}(Double.parseDouble(_${it.name}));"
else if (it.type == "Float")
out.println " this.set${it.name.capitalize()}(Float.parseFloat(_${it.name}));"
else if (it.type == "java.math.BigInteger" || it.type == "BigInteger")
out.println " this.set${it.name.capitalize()}(java.math.BigInteger.valueOf(_${it.name}));"
else if (it.type == "java.math.BigDecimal" || it.type == "BigDecimal")
out.println " this.set${it.name.capitalize()}(java.math.BigDecimal.valueOf(_${it.name}));"
else if (it.type == "java.sql.Date" || it.type == "Date")
out.println " this.set${it.name.capitalize()}(java.sql.Date.valueOf(_${it.name}));"
else if (it.type == "java.sql.Time" || it.type == "Time")
out.println " this.set${it.name.capitalize()}(java.sql.Time.valueOf(_${it.name}));"
else if (it.type == "java.sql.Timestamp" || it.type == "Timestamp")
out.println " this.set${it.name.capitalize()}(java.sql.Timestamp.valueOf(_${it.name}));"
else
out.println " this.${it.name} = new SimpleStringProperty(_${it.name});"
out.println " }"
out.println ""
out.println " public ${className}Binder with${it.name.capitalize()}(${it.type} _${it.name}) {"
out.println " this.set${it.name.capitalize()}(_${it.name});"
out.println " return this;"
out.println " }"
out.println ""
out.println " public ${className}Binder without${it.name.capitalize()}(${it.type} _${it.name}) {"
out.println " this.set${it.name.capitalize()}(null);"
out.println " return this;"
out.println " }"
out.println ""
out.println " public ${className}Binder withParsed${it.name.capitalize()}(String _${it.name}) {"
out.println " this.parseAndSet${it.name.capitalize()}(_${it.name});"
out.println " return this;"
out.println " }"
out.println ""
}
out.println ""
out.println " @Override"
out.println " public boolean equals(Object obj) {"
out.println " if (this == obj) return true;"
out.println " if (!(obj instanceof ${className}Binder)) return false;"
out.println " ${className}Binder other${className.capitalize()} = (${className}Binder) obj;"
if (fields.contains("id")) {
out.println " return this.getId().equals(other${className.capitalize()}.getId());"
} else {
out.println " return "
fields.each() {
out.println " this.get${it.name.capitalize()}().equals(other${className.capitalize()}.get${it.name.capitalize()}()) "
if (it != fields.last()) {
out.print " && "
}
}
out.print ";"
}
out.println " }"
out.println ""
out.println " @Override"
out.println " public int hashCode() {"
if (fields.contains("id")) {
out.println " return Objects.hash(this.getId());"
} else {
out.println " return Objects.hash( "
fields.each() {
out.println " this.get${it.name.capitalize()}()"
if (it != fields.last()) {
out.print ", "
}
}
out.print " );"
}
out.println " }"
out.println ""
out.println " @Override"
out.println " public String toString() {"
out.println ""
out.println " return \"{ \" + "
fields.each() {
out.println " this.get${it.name.capitalize()}() + \", \" + "
}
out.println " \" } \";"
out.println " }"
out.println ""
out.println " public boolean isNew(){"
out.println " return this.getId() == null;"
out.println " }"
out.println ""
out.println " public boolean isEmpty(){"
out.print " return ( "
fields.eachWithIndex() { it, index ->
out.print "this.get${it.name.capitalize()}() == null"
if (it != fields.last())
out.println " && "
}
out.print " );"
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 += [[
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]
}