-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
232 lines (199 loc) · 5.04 KB
/
index.html
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
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
<body>
<table class="tbl-main">
<tr>
<td width="45%">
<textarea id="source" placeholder="Your entity java class"></textarea>
<td class="btn-panel">
<button onclick=generateDto() class="btn">Generate DTO >></button>
<button onclick=generateDto('with.list') class="btn">Generate DTO (with list)>></button>
<button onclick=mappingFromDto() class="btn">Mapping from DTO>></button>
<button onclick=reset() class="btn">Reset</button>
<div class="version">version 0.0.3</div>
<td width="45%">
<textarea id="result" placeholder="" readonly="true"></textarea>
</td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var defaultTypeExpr = /^([bB]oolean|int|Integer|[lL]ong|[fF]loat|[dD]ouble|[sS]hort|[bB]yte|Number|BigInteger|BigDecimal|String|char|Character|Blob|Clob|Date|Time|Timestamp|LocalDate)$/;
function generateDto(generatorType){
saveCache();
var className = getClassName();
if(!className){
alert('Class not found');
return;
}else{
console.log(className + ' FOUND');
}
var fieldMap = getFields();
var arr = [];
var dtoClassName = className + "Dto";
arr.push('public class ' + dtoClassName + " {\n");
for(var key in fieldMap){
var type = fieldMap[key];
var match = defaultTypeExpr.exec(type);
if(match){
arr.push('\tpublic ' + type + ' ' + key + ';\n');
}else{
arr.push('\tpublic ' + type + 'Dto ' + key + ';\n');
arr.push('\tpublic Long ' + key + 'Id;\n');
}
}
arr.push('\n\tpublic ' + dtoClassName + "() {\n");
arr.push('\t\t\n');
arr.push('\t}\n');
var varName = decapitalizeFirstLetter(className);
arr.push('\n\tpublic ' + dtoClassName + "(" + className + " " + varName + ") {\n");
for(var key in fieldMap){
var type = fieldMap[key];
var match = defaultTypeExpr.exec(type);
if(match){
arr.push('\t\tthis.' + key + ' = ' + varName + '.' + detectGetMethod(key,type) + '();\n');
}else{
//arr.push('\t\tthis.' + key + ' = ' + varName + '.' + detectGetMethod(key + 'Dto',type) + '();\n');
//arr.push('\t\tthis.' + key + ' = ' + varName + '.' + detectGetMethod(key + 'Id','Long') + '();\n');
}
}
arr.push('\t}\n\n');
if(generatorType === 'with.list'){
alert('В разработке');
//getGetters();
return;
}
arr.push('}');
setResult(arr.join(''));
}
init();
function init(){
setSource(localStorage.getItem('dtoMapperSource'));
setResult('');
}
function reset(){
setSource('');
setResult('');
}
function getElement(name){
return document.getElementById(name);
}
function getSource(){
return getElement('source').value;
}
function setSource(value){
return getElement('source').value = value;
}
function setResult(value){
var result = getElement('result')
if(value){
result.classList.remove('opacity1');
result.classList.add('opacity2');
}else{
result.classList.remove('opacity2');
result.classList.add('opacity1');
}
return result.value = value;
}
function saveCache(){
localStorage.setItem('dtoMapperSource', getSource());
}
function getClassName(){
var classExpr = /public\s+class\s+(\w+).*/g;
var match = classExpr.exec(getSource());
if(match){
return match[1];
}else{
return null;
}
}
function getFields(){
var classExpr = /private\s+(\w+)\s+(\w+)\s*;.*/g;
var match;
var map = {};
var source = getSource();
while(match = classExpr.exec(source)){
map[match[2]] = match[1];
}
return map;
}
function getGetters(){
console.log('getGetters()');
var source = getSource();
var getterExpr = /public\s+(\w+)\s+(get\w+)\s*(\s*)/g;
var match;
var map = {};
while(match = getterExpr.exec(source)){
map[match[2]] = match[1];
}
return map;
}
function mappingFromDto(){
alert('В разработке');
}
function decapitalizeFirstLetter(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function detectGetMethod(key,type){
if(type === 'boolean' || type === 'Boolean'){
//return key;
//TODO checks
return 'get' + capitalizeFirstLetter(key);
}else{
return 'get' + capitalizeFirstLetter(key);
}
}
</script>
<style type="text/css">
body{
height: 100%;
width: 100%;
border: 0;
margin: 0;
padding: 0;
background-image: url('math_model.jpg');
background-repeat: repeat;
}
.tbl-main {
height: 100%;
width: 100%;
}
.btn {
width: 100%;
font-family: "Courier New", "Lucida Console","Consolas";
font-size: 20pt;
margin-top: 50px;
}
.btn-panel{
vertical-align: top;
padding-top: 25px;
}
textarea{
height: 100%;
width: 100%;
font-family: "Courier New", "Lucida Console","Consolas";
font-size: 20pt;
border: solid 1px silver;
}
#result{
background: #eee;
}
.opacity1{
opacity: 0.5;
}
.opacity2{
opacity: 0.9;
}
.version{
margin-top: 25px;
text-align: center;
color: #36393d;
background: silver;
}
</style>