-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjava.py
248 lines (187 loc) · 8.12 KB
/
java.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
import jinja2
from jinja2 import FileSystemLoader
e = jinja2.Environment(loader=FileSystemLoader('tpls/java'))
def camel_case(s):
return ''.join([word.capitalize() for word in s.split('_')])
def camel_back(s):
c = camel_case(s)
return c[0].lower() + c[1:]
def getJavaFieldName(colName):
try:
return camel_back(colName[colName.index('_'):])
except ValueError:
return camel_back(colName)
def getJavaMethodName(colName):
try:
return camel_case(colName[colName.index('_'):])
except ValueError:
return camel_case(colName)
def getJavaType(field):
return field.get_java_type()
def getTypeName(table):
return "t_" + table.name
def create_java_getter(fieldName, fieldType, methodName):
return """ public """ + fieldType + """ get""" + methodName + """() {
return """ + fieldName + """;
}"""
def create_java_setter(fieldName, fieldType, methodName):
return """ public void set""" + methodName + """(""" + fieldType + """ """ + fieldName + """) {
this.""" + fieldName + """ = """ + fieldName + """;
}"""
def create_class_name(table):
return camel_case(table.name)
def create_field_name(table):
return camel_back(table.name)
type_package = 'domain'
iface_package = 'persistence'
impl_package = iface_package + '.impl'
service_sfx = 'SProcService'
def create_java_enum(table, package):
out = "package " + package + ";\n\n"
out += "public enum " + table.getClassName() + " {\n"
values = []
for v in table.values:
values.append(" " + v)
out += ",\n".join(values)
out += "\n}\n"
return out
def create_java_type(table, package):
cols = []
funcs = []
fieldAnn = " @DatabaseField\n"
imports = {'de.zalando.typemapper.annotations.DatabaseField': 1,
'de.zalando.typemapper.annotations.DatabaseType': 2}
for f in table.fields:
fieldName = getJavaFieldName(f.name)
methodName = getJavaMethodName(f.name)
typeName = getJavaType(f)
col = fieldAnn
if not f.isNullable:
if typeName == 'String':
col += " @NotBlank\n"
imports['org.hibernate.validator.constraints.NotBlank'] = 1
else:
col += " @NotNull\n"
imports['javax.validation.constraints.NotNull'] = 1
if f.maxLength is not None and f.maxLength > 0:
col += " @Max(" + str(f.maxLength) + ")\n"
imports['javax.validation.constraints.Max'] = 1
col += " private " + getJavaType(f) + " " + fieldName + ";\n"
cols.append(col)
funcs.append(create_java_getter(fieldName, getJavaType(f), methodName))
funcs.append(create_java_setter(fieldName, getJavaType(f), methodName))
for a in table.associations:
if a.tableFrom == table:
# single item
fieldName = create_field_name(a.tableTo)
className = create_class_name(a.tableTo)
cols.append(fieldAnn + " private " + className + " " + fieldName + ";\n")
funcs.append(create_java_getter(fieldName, className, className))
funcs.append(create_java_setter(fieldName, className, className))
elif a.tableTo == table:
# collection
cols.append(fieldAnn + " private List<" + create_class_name(a.tableFrom) + "> " + create_field_name(
a.tableFrom) + "s;\n")
source = "package " + package + ";\n"
for k, v in imports.iteritems():
source += "\nimport " + k + ";"
source += "\n\n" + "@DatabaseType(name = " + getTypeName(table) +", inheritance = true)" + "\nclass " + table.getClassName() + " {\n"
source += "\n".join(cols)
source += "\n"
source += "\n\n".join(funcs)
source += "\n}\n"
return source
def get_signatures_for_table(table):
signatures = [(table.getClassName(), "create", table.getClassName()),
(table.getClassName(), "delete", table.getClassName()),
(table.getClassName(), "update", table.getClassName())]
return signatures
def create_sproc_service_interface(table, package):
t = e.get_template('sproc_interface.java')
import_list = "\nimport " + package + type_package + "." + table.getClassName() + ";\n"
package = "package " + package + iface_package + ";\n\n"
sproc_list = []
l = get_signatures_for_table(table)
for (field_name, method_name, p) in l:
lower_field_name = field_name[0].lower() + field_name[1:]
sproc_list.append(
" public " + field_name + " " + method_name + "(" + field_name + " " + lower_field_name + ");")
for ui in table.indexes:
keys = []
names = []
for i in ui:
f = table.fields[int(i) - 1]
keys.append(getJavaType(f) + " " + getJavaFieldName(f.name))
names.append(getJavaMethodName(f.name))
sproc_list.append(
" public " + table.getClassName() + " getBy" + "".join(names) + "(" + ", ".join(keys) + ");")
return t.render(interfaceName=table.getClassName() + service_sfx,
sprocList="\n".join(sproc_list),
importList=import_list,
package=package,
prefix=table.name)
def create_sproc_service_implementation(table, package):
t = e.get_template('sproc_implementation.java')
import_list = "\nimport " + package + type_package + "." + table.getClassName() + ";\n"
import_list += "import " + package + iface_package + "." + table.getClassName() + service_sfx + ";\n"
package = "package " + package + impl_package + ";\n\n"
sproc_list = []
l = get_signatures_for_table(table)
for (field_name, method_name, p) in l:
lower_field_name = field_name[0].lower() + field_name[1:]
sproc_list.append(
" @Override\n public " + field_name + " " + method_name + "(" + field_name + " " + lower_field_name + """) {
return sproc.""" + method_name + """(""" + lower_field_name + """);
}""")
for ui in table.indexes:
keys = []
names = []
types = []
for i in ui:
f = table.fields[int(i) - 1]
types.append(getJavaType(f) + " " + getJavaFieldName(f.name))
keys.append(getJavaFieldName(f.name))
names.append(getJavaMethodName(f.name))
sproc_list.append(
" @Override\n public " + table.getClassName() + " getBy" + "".join(names) + "(" + ", ".join(types) + """) {
return sproc.getBy""" + "".join(names) + "(" + ", ".join(keys) + """);
}""")
return t.render(interfaceName=table.getClassName() + service_sfx,
functionImplementations="\n\n".join(sproc_list),
datasourceProvider='SingleDataSourceProvider',
importList=import_list,
package=package)
def get_package_path(package):
path = ['src', 'main', 'java']
for word in package.split('.'):
path.append(word)
return os.sep.join(path)
def save_file(path, class_name, data):
if not os.path.isdir(path):
os.makedirs(path, 0755)
fd = open(path + os.sep + class_name + ".java", "w")
fd.write(data)
fd.close()
def generate_code(table, package, path):
if package != '':
package += '.'
pp = get_package_path(package + type_package)
for k, v in table.complexTypes.iteritems():
class_name = v.getClassName()
if v.isEnum():
src = create_java_enum(v, package + type_package)
else:
src = create_java_type(v, package + type_package)
save_file(path + os.sep + pp, class_name, src)
class_name = table.getClassName()
src = create_java_type(table, package + type_package)
save_file(path + os.sep + pp, class_name, src)
pp = get_package_path(package + iface_package)
class_name += service_sfx
src = create_sproc_service_interface(table, package)
save_file(path + os.sep + pp, class_name, src)
pp = get_package_path(package + impl_package)
class_name += 'Impl'
src = create_sproc_service_implementation(table, package)
save_file(path + os.sep + pp, class_name, src)