-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
executable file
·212 lines (164 loc) · 5.56 KB
/
generator.py
File metadata and controls
executable file
·212 lines (164 loc) · 5.56 KB
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
#!/usr/bin/python
# coding: utf-8
'''
Created on 08/03/2018
@author: Newton Muchael
'''
import re, os
import sys
import json
config = {};
serviceMethods = {
'create': """ /**
* Método para inserir {entityName}
*
* @param {entityNameLowerCamelCase}
* @return
*/
@Override
public {entityName} insert{entityName}( {entityName} {entityNameLowerCamelCase} )
{{
return this.{entityNameLowerCamelCase}Repository.save( {entityNameLowerCamelCase} );
}}\n
""",
'read': """ /**
* Método para listar {entityName}
*
* @param {entityNameLowerCamelCase}
* @return
*/
@Override
public {entityName} list{entityName}( String filter )
{{
return this.{entityNameLowerCamelCase}Repository.list{entityName}ByFilters( filter );
}}\n
""",
'update':""" /**
* Método para atualizar {entityName}
*
* @param {entityNameLowerCamelCase}
* @return
*/
@Override
public {entityName} update{entityName}( {entityName} {entityNameLowerCamelCase} )
{{
return this.{entityNameLowerCamelCase}Repository.save( {entityNameLowerCamelCase} );
}}\n
""",
'delete':""" /**
* Método para remover {entityName}
*
* @param {entityNameLowerCamelCase}
* @return
*/
@Override
public void remove{entityName}( Long id )
{{
return this.{entityNameLowerCamelCase}Repository.delete( id );
}}\n
"""
}
def readConf( fileName ):
with open(fileName) as json_data:
global config
config = json.load(json_data)
def generateAttributes( attributes ):
result = ""
for i in attributes:
result += ('\t/**\n'
'\t * Representa o ' + i['name'] + '\n'
'\t */\n')
if i['notNull']:
if i['type'] == 'String':
result += '\t@NotEmpty\n'
else:
result += '\t@NotNull\n'
result += '\tprivate ' + i['type'] + ' ' + i['name'] + ';\n\n'
return result
def generateEntity( entity ):
directory = 'generated/' + entity['package'].replace('.', '/') + '/entity/'
if not os.path.exists(directory):
os.makedirs(directory)
with open( 'template/entity.java', 'r' ) as file:
resultFile = open(directory + entity['entityName'] + '.java', 'w')
for line in file:
match = re.search(r"\{\w+\}", line);
if match:
attribute = match.group(0)[1:-1]
if attribute == 'attributes':
resultFile.write( generateAttributes( entity['attributes'] ) )
else:
resultFile.write( line.format( **entity ) )
else:
resultFile.write(line)
resultFile.close()
def generateRepositoryFields( entity ):
result = ''
for atribute in entity['attributes']:
if atribute['filter']:
result += "\t\t\t\t\"(FILTER(" + entity['entityNameLowerCamelCase'] + '.' + atribute['name'] + ", :filter) = TRUE) \" + \n"
if len(result) > 0:
return result[0: -4] + '\n'
return result
def generateRepository( entity ):
directory = 'generated/' + entity['package'].replace('.', '/') + '/repository/'
if not os.path.exists(directory):
os.makedirs(directory)
with open( 'template/repository.java', 'r' ) as file:
resultFile = open( directory + 'I' + entity['entityName'] + 'Repository.java', 'w')
entityNameLowerCamelCase = entity['entityName'][0].lower() + entity['entityName'][1:]
entity['entityNameLowerCamelCase'] = entityNameLowerCamelCase
for line in file:
match = re.search(r"\{\w+\}", line);
if match:
attribute = match.group(0)[1:-1]
if attribute == 'attributes':
resultFile.write( generateRepositoryFields( entity ) )
else:
resultFile.write( line.format( **entity ) )
else:
resultFile.write(line)
resultFile.close()
def generateServiceMethods( entity ):
result = ''
for method in entity['service']:
if method == 'C':
result += serviceMethods['create']
if method == 'R':
result += serviceMethods['read']
if method == 'U':
result += serviceMethods['update']
if method == 'D':
result += serviceMethods['delete']
return result.format( **entity )
def generateService( entity ):
if 'service' not in entity:
return
directory = 'generated/' + entity['package'].replace('.', '/') + '/service/'
if not os.path.exists(directory):
os.makedirs(directory)
with open( 'template/service.java', 'r' ) as file:
resultFile = open(directory + entity['entityName'] + 'Service.java', 'w')
entityNameLowerCamelCase = entity['entityName'][0].lower() + entity['entityName'][1:]
entity['entityNameLowerCamelCase'] = entityNameLowerCamelCase
for line in file:
match = re.search(r"\{\w+\}", line);
if match:
attribute = match.group(0)[1:-1]
if attribute == 'methods':
resultFile.write( generateServiceMethods( entity ) )
else:
resultFile.write( line.format( **entity ) )
else:
resultFile.write(line)
resultFile.close()
def main():
if len(sys.argv) < 2:
print "Deve ser passado o arquivo de configuração como parâmetro."
return
readConf( sys.argv[1] )
for entity in config['entities']:
generateEntity( entity )
generateRepository( entity )
generateService( entity )
main()