-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
432 lines (377 loc) · 21.5 KB
/
Copy pathindex.html
File metadata and controls
432 lines (377 loc) · 21.5 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ultimate JPA SE Generator — Blueprint Edition</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Shrikhand&display=swap');
:root {
--bg-dark: #09090b;
--panel-bg: #18181b;
--surface: #27272a;
--border: #3f3f46;
--groovy-purple: #c084fc;
--neon-pink: #f472b6;
--neon-cyan: #22d3ee;
--neon-green: #4ade80;
--amber: #fbbf24;
--text-main: #f4f4f5;
--text-muted: #a1a1aa;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg-dark); color: var(--text-main);
font-family: 'Space Mono', monospace; font-size: 13px; padding: 20px;
background-image: linear-gradient(rgba(34, 211, 238, 0.02) 1px, transparent 1px),
linear-gradient(90deg, rgba(34, 211, 238, 0.02) 1px, transparent 1px);
background-size: 20px 20px;
}
header { margin-bottom: 24px; text-align: center; }
h1 {
font-family: 'Shrikhand', cursive; color: var(--groovy-purple);
font-size: 2.3rem; letter-spacing: 2px;
text-shadow: 3px 3px 0px var(--neon-cyan), -1px -1px 0px var(--neon-pink);
margin-bottom: 8px;
}
.subtitle { color: var(--neon-cyan); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; }
.cfg-bar {
display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px;
background: rgba(24, 24, 27, 0.8); border: 2px solid var(--groovy-purple);
padding: 16px; border-radius: 8px; margin-bottom: 24px;
box-shadow: 0 0 15px rgba(192, 132, 252, 0.15);
}
.cfg-bar label { font-size: 10px; color: var(--neon-pink); text-transform: uppercase; font-weight: bold; margin-bottom: 6px; display: block; }
.cfg-bar input {
width: 100%; background: var(--bg-dark); border: 1px solid var(--border);
color: var(--neon-green); padding: 8px; border-radius: 4px; font-family: 'Space Mono', monospace;
}
.workspace { display: grid; grid-template-columns: 1fr 1.5fr; gap: 20px; }
.panel {
background: rgba(24, 24, 27, 0.9); border: 1px solid var(--border);
border-radius: 8px; padding: 20px; display: flex; flex-direction: column; gap: 12px;
}
.panel-title { font-size: 15px; font-weight: bold; color: var(--amber); border-bottom: 2px dashed var(--border); padding-bottom: 8px; }
textarea {
width: 100%; height: 260px; background: #000; color: var(--neon-green);
border: 1px solid var(--border); border-radius: 4px; padding: 12px;
font-family: 'Space Mono', monospace; font-size: 11px; resize: vertical; line-height: 1.6;
}
.btn-run {
background: transparent; color: var(--groovy-purple);
border: 2px solid var(--groovy-purple); padding: 12px;
font-family: 'Space Mono', monospace; font-weight: bold; font-size: 13px;
text-transform: uppercase; cursor: pointer; position: relative;
box-shadow: 4px 4px 0px var(--groovy-purple);
transition: all 0.1s;
}
.btn-run:active { box-shadow: 0px 0px 0px var(--groovy-purple); transform: translate(4px, 4px); }
.table-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.t-btn {
background: var(--surface); color: var(--text-main);
border: 1px solid var(--border); padding: 6px 12px; border-radius: 4px;
cursor: pointer; font-family: inherit; font-size: 11px;
}
.t-btn.active { background: var(--neon-cyan); color: #000; font-weight: bold; border-color: var(--neon-cyan); }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; border-bottom: 2px solid var(--border); }
.tab {
background: transparent; color: var(--text-muted); border: none;
padding: 10px 14px; cursor: pointer; font-size: 11px; font-weight: bold;
border-bottom: 3px solid transparent; text-transform: uppercase;
}
.tab.active { color: var(--neon-cyan); border-bottom: 3px solid var(--neon-cyan); }
.blueprint-title-bar {
background: #27272a;
padding: 8px 12px;
border-radius: 4px 4px 0 0;
border-bottom: 1px solid var(--border);
font-size: 11px;
color: var(--amber);
display: flex;
justify-content: space-between;
align-items: center;
}
.code-wrap { position: relative; flex: 1; background: #000; border: 1px solid var(--border); border-radius: 4px; margin-top: 12px;}
.code-view {
width: 100%; height: 560px; color: #e2e8f0; padding: 16px;
font-family: 'Space Mono', monospace; font-size: 11px; white-space: pre; overflow: auto; line-height: 1.6;
}
.btn-copy {
background: var(--surface); color: var(--neon-green); border: 1px solid var(--neon-green);
padding: 3px 10px; font-size: 10px; border-radius: 4px; cursor: pointer; font-weight: bold;
}
.btn-copy:hover { background: rgba(74, 222, 128, 0.1); }
</style>
</head>
<body>
<header>
<h1>JPA ARCHITECTURE ENGINE</h1>
<p class="subtitle">Jakarta JPA SE Copy-Paste Deployment Pipeline</p>
</header>
<div class="cfg-bar">
<div><label>Package Name</label><input id="pkg" value="com.ku.application"></div>
<div><label>Persistence Unit</label><input id="pu" value="SiriwiriyaPU"></div>
<div><label>DB URL</label><input id="dbUrl" value="jdbc:mysql://localhost:3306/practice_db"></div>
<div><label>Username</label><input id="dbUser" value="root"></div>
<div><label>Password</label><input id="dbPass" value="password123"></div>
</div>
<div class="workspace">
<div class="panel">
<div class="panel-title">📡 INPUT DATA SOURCE (SQL SCRIPT)</div>
<textarea id="sqlIn" placeholder="วางสคริปต์ SQL CREATE TABLE ทั้งหมดลงที่นี่..."></textarea>
<button class="btn-run" onclick="runPipeline()">⚡ Compile & Build Stack</button>
<div>
<div style="font-size:11px;color:var(--text-muted);margin-bottom:6px">> Select Entity Node:</div>
<div id="tblBtns" class="table-btns"></div>
</div>
</div>
<div class="panel">
<div class="panel-title">⚙️ GENERATED SYSTEM STACK</div>
<div class="tabs">
<button class="tab active" onclick="switchTab('blueprint',this)">📦 Deployment Blueprint</button>
<button class="tab" onclick="switchTab('entity',this)">Entity</button>
<button class="tab" onclick="switchTab('datarepo',this)">DataRepository</button>
<button class="tab" onclick="switchTab('repo',this)">Repository</button>
<button class="tab" onclick="switchTab('service',this)">Service</button>
<button class="tab" onclick="switchTab('main',this)">Main.java</button>
<button class="tab" onclick="switchTab('config',this)">persistence.xml</button>
</div>
<div id="blueprintHeader" class="blueprint-title-bar">
<span>🎯 TARGET FILE PATH: <strong id="targetPathDisplay" style="color:var(--neon-cyan)">-</strong></span>
</div>
<div class="code-wrap">
<button class="btn-copy" style="position: absolute; top: 12px; right: 12px;" onclick="copyCode()">COPY CODE</button>
<div id="out" class="code-view">// System idle. Please load SQL script and press compile...</div>
</div>
</div>
</div>
<script>
let parsedSchema = {}, currentTable = '', currentActiveTab = 'blueprint';
const typeMapping = (sqlType) => {
const t = sqlType.toUpperCase().trim();
if(t.includes('BIGINT')) return { type: 'Long', isNum: true };
if(t.match(/TINYINT\s*\(\s*1\s*\)/) || t==='BOOLEAN') return { type: 'Boolean', isBool: true };
if(t.match(/INT|INTEGER/)) return { type: 'Integer', isNum: true };
if(t.match(/DECIMAL|NUMERIC/)) return { type: 'BigDecimal', isNum: true, imp: 'java.math.BigDecimal' };
if(t.match(/DOUBLE|FLOAT/)) return { type: 'Double', isNum: true };
if(t.match(/TIMESTAMP|DATETIME/)) return { type: 'Instant', isDate: true, imp: 'java.time.Instant' };
if(t.match(/DATE/)) return { type: 'LocalDate', isDate: true, imp: 'java.time.LocalDate' };
return { type: 'String', isStr: true };
};
const toCamel = s => s.toLowerCase().replace(/_([a-z])/g, (_,c) => c.toUpperCase());
const toPascal = s => { const c = toCamel(s); return c[0].toUpperCase() + c.slice(1); };
function runPipeline() {
const sql = document.getElementById('sqlIn').value;
if(!sql.trim()) return;
parsedSchema = {};
const regex = /CREATE\s+TABLE\s+[`"]?(\w+)[`"]?\s*\(([\s\S]*?)\)\s*(?:ENGINE[^;]*)?;/gi;
let match, found = false;
while((match = regex.exec(sql)) !== null) {
parseTableLogic(match[1], match[2]);
found = true;
}
if(found) renderTableSelectors();
}
function parseTableLogic(name, body) {
const lines = body.split('\n');
let fields = [], imports = new Set();
lines.forEach(ln => {
let clean = ln.trim().replace(/--.*$/, '').replace(/`/g, '').replace(/,\s*$/, '');
if(!clean || ['PRIMARY','CONSTRAINT','FOREIGN','UNIQUE'].some(k => clean.toUpperCase().startsWith(k))) return;
const parts = clean.split(/\s+/);
if(parts.length < 2) return;
const col = parts[0].replace(/[`"']/g,'');
const isPk = clean.toUpperCase().includes('PRIMARY KEY');
const metaInfo = typeMapping(parts[1]);
if(metaInfo.imp) imports.add(metaInfo.imp);
fields.push({ col, name: toCamel(col), cap: toPascal(col), ...metaInfo, isPk });
});
if(!fields.some(f => f.isPk) && fields.length > 0) fields[0].isPk = true;
parsedSchema[name] = { name, cls: toPascal(name), fields, imports };
}
function renderTableSelectors() {
const container = document.getElementById('tblBtns'); container.innerHTML = '';
const keys = Object.keys(parsedSchema);
keys.forEach((k, i) => {
const btn = document.createElement('button');
btn.className = 't-btn' + (i === 0 ? ' active' : '');
btn.textContent = k;
btn.onclick = () => {
document.querySelectorAll('.t-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentTable = k;
renderArtifact();
};
container.appendChild(btn);
});
if(keys.length) { currentTable = keys[0]; renderArtifact(); }
}
function switchTab(tab, btn) {
currentActiveTab = tab;
document.querySelectorAll('.tab').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
renderArtifact();
}
function renderArtifact() {
if(!currentTable) return;
const pkg = document.getElementById('pkg').value;
const pathSlug = pkg.replace(/\./g, '/');
const pu = document.getElementById('pu').value;
const url = document.getElementById('dbUrl').value;
const user = document.getElementById('dbUser').value;
const pass = document.getElementById('dbPass').value;
const t = parsedSchema[currentTable];
let code = '', finalPath = '';
// ลอจิกการคำนวณ Path ตามโฟลเดอร์โครงสร้างจริงของคุณ
if (currentActiveTab === 'blueprint') {
finalPath = "SUMMARY VIEW (พิมพ์เขียวการสร้างไฟล์และก๊อปวางรวดเดียว)";
code = genBlueprintView(t, pkg, pathSlug, pu, url, user, pass);
} else if (currentActiveTab === 'entity') {
finalPath = `src/main/java/${pathSlug}/entity/${t.cls}.java`;
code = genEntity(t, pkg);
} else if (currentActiveTab === 'datarepo') {
finalPath = `src/main/java/${pathSlug}/repository/DataRepository.java`;
code = genDataRepo(pkg);
} else if (currentActiveTab === 'repo') {
finalPath = `src/main/java/${pathSlug}/repository/${t.cls}Repository.java`;
code = genRepoArsenal(t, pkg);
} else if (currentActiveTab === 'service') {
finalPath = `src/main/java/${pathSlug}/service/${t.cls}Service.java`;
code = genService(t, pkg);
} else if (currentActiveTab === 'main') {
finalPath = `src/main/java/${pathSlug}/Main.java`;
code = genMain(t, pkg);
} else if (currentActiveTab === 'config') {
finalPath = `src/main/resources/META-INF/persistence.xml`;
code = genConfig(pkg, pu, url, user, pass, Object.values(parsedSchema));
}
document.getElementById('targetPathDisplay').textContent = finalPath;
document.getElementById('out').textContent = code;
}
// ฟังก์ชันหัวใจใหม่: เจนแผนภาพสรุปให้เห็นชื่อไฟล์และโฟลเดอร์สำหรับก๊อปปี้แบบม้วนเดียวจบ
function genBlueprintView(t, pkg, pathSlug, pu, url, user, pass) {
let borderStr = "=".repeat(80);
let view = `// 📦 JAKARTA JPA SE ARCHITECTURE BLUEPRINT (FULL STACK FILE GUIDE)\n`;
view += `// ทำการสร้างไฟล์ตามโครงสร้างด้านล่างนี้ใน IntelliJ แล้วก๊อปโค้ดจากแต่ละแท็บไปวางได้เลยครับ\n`;
view += `${borderStr}\n\n`;
view += `📁 [ROOT PROJECT FOLDER]\n`;
view += `└── 📁 src\n`;
view += ` └── 📁 main\n`;
view += ` ├── 📁 java\n`;
view += ` │ └── 📁 ${pkg.replace(/\./g, ' ➔ 📁 ')}\n`;
view += ` │ ├── 📁 entity\n`;
view += ` │ │ └── 📄 ${t.cls}.java ➔ (สลับไปแท็บ Entity เพื่อก๊อปโค้ด)\n`;
view += ` │ ├── 📁 repository\n`;
view += ` │ │ ├── 📄 DataRepository.java ➔ (สลับไปแท็บ DataRepository เพื่อก๊อปโค้ด)\n`;
view += ` │ │ └── 📄 ${t.cls}Repository.java ➔ (สลับไปแท็บ Repository เพื่อก๊อปโค้ด)\n`;
view += ` │ ├── 📁 service\n`;
view += ` │ │ └── 📄 ${t.cls}Service.java ➔ (สลับไปแท็บ Service เพื่อก๊อปโค้ด)\n`;
view += ` │ └── 📄 Main.java ➔ (สลับไปแท็บ Main.java เพื่อก๊อปโค้ด)\n`;
view += ` └── 📁 resources\n`;
view += ` └── 📁 META-INF\n`;
view += ` └── 📄 persistence.xml ➔ (สลับไปแท็บ Config เพื่อก๊อปโค้ด)\n\n`;
view += `${borderStr}\n`;
view += `💡 [TIPS] กดสลับแท็บเมนูด้านบน เพื่อดึงโค้ดเต็มของแต่ละไฟล์ออกมาคัดลอกได้ทันทีครับ!`;
return view;
}
// 1. ENTITY
function genEntity(t, pkg) {
let code = `package ${pkg}.entity;\n\nimport jakarta.persistence.*;\nimport lombok.*;\n`;
t.imports.forEach(i => code += `import ${i};\n`);
code += `\n@Entity\n@Table(name = "${t.name}")\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\npublic class ${t.cls} {\n\n`;
t.fields.forEach(f => {
if(f.isPk) code += ` @Id @GeneratedValue(strategy = GenerationType.IDENTITY)\n`;
code += ` @Column(name = "${f.col}")\n private ${f.type} ${f.name};\n\n`;
});
return code + `}`;
}
// 2. GENERIC REPO
function genDataRepo(pkg) {
return `package ${pkg}.repository;
import jakarta.persistence.EntityManager;
import java.util.List;
import java.util.Optional;
public interface DataRepository<E, T> {
Class<E> getEntityClass();
EntityManager getEntityManager();
default E save(E entity) {
getEntityManager().persist(entity);
return entity;
}
default E update(E entity) {
return getEntityManager().merge(entity);
}
default Optional<E> findById(T id) {
return Optional.ofNullable(getEntityManager().find(getEntityClass(), id));
}
default List<E> findAll() {
return getEntityManager().createQuery(
"SELECT e FROM " + getEntityClass().getSimpleName() + " e", getEntityClass()
).getResultList();
}
default void delete(E entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
}`;
}
// 3. REPOSITORY IMPL WITH ARSENAL
function genRepoArsenal(t, pkg) {
const pkType = t.fields.find(f => f.isPk).type;
let code = `package ${pkg}.repository;\n\nimport ${pkg}.entity.${t.cls};\nimport jakarta.persistence.EntityManager;\nimport java.util.List;\n\npublic class ${t.cls}Repository implements DataRepository<${t.cls}, ${pkType}> {\n\n`;
code += ` private final EntityManager em;\n\n public ${t.cls}Repository(EntityManager em) {\n this.em = em;\n }\n\n`;
code += ` @Override\n public Class<${t.cls}> getEntityClass() { return ${t.cls}.class; }\n\n`;
code += ` @Override\n public EntityManager getEntityManager() { return this.em; }\n\n`;
code += ` // === DYNAMIC EXAM ARSENAL METHODS ===\n\n`;
t.fields.forEach(f => {
if(f.isPk) return;
if(f.isStr) {
code += ` public List<${t.cls}> findBy${f.cap}Containing(String keyword) {\n`;
code += ` return em.createQuery("SELECT e FROM ${t.cls} e WHERE e.${f.name} LIKE :kw", ${t.cls}.class)\n`;
code += ` .setParameter("kw", "%" + keyword + "%").getResultList();\n }\n\n`;
}
if(f.isNum) {
code += ` public List<${t.cls}> findBy${f.cap}Between(${f.type} min, ${f.type} max) {\n`;
code += ` return em.createQuery("SELECT e FROM ${t.cls} e WHERE e.${f.name} BETWEEN :min AND :max", ${t.cls}.class)\n`;
code += ` .setParameter("min", min).setParameter("max", max).getResultList();\n }\n\n`;
}
});
return code + `}`;
}
// 4. SERVICE LAYER (TRANSACTION WRAPPER)
function genService(t, pkg) {
let code = `package ${pkg}.service;\n\nimport ${pkg}.entity.${t.cls};\nimport ${pkg}.repository.${t.cls}Repository;\nimport jakarta.persistence.EntityManager;\nimport jakarta.persistence.EntityManagerFactory;\nimport jakarta.persistence.EntityTransaction;\nimport jakarta.persistence.Persistence;\nimport java.util.List;\n\npublic class ${t.cls}Service {\n\n`;
code += ` private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("SiriwiriyaPU");\n\n`;
code += ` public void create(${t.cls} entity) {\n`;
code += ` EntityManager em = emf.createEntityManager();\n`;
code += ` EntityTransaction tx = em.getTransaction();\n`;
code += ` try {\n tx.begin();\n`;
code += ` new ${t.cls}Repository(em).save(entity);\n`;
code += ` tx.commit();\n } catch(Exception e) {\n if(tx.isActive()) tx.rollback();\n throw e;\n } finally {\n em.close();\n }\n }\n\n`;
code += ` public List<${t.cls}> getAll() {\n`;
code += ` EntityManager em = emf.createEntityManager();\n`;
code += ` try { return new ${t.cls}Repository(em).findAll(); } finally { em.close(); }\n`;
code += ` }\n}`;
return code;
}
// 5. MAIN CONSOLE MENU
function genMain(t, pkg) {
let code = `package ${pkg};\n\nimport ${pkg}.entity.${t.cls};\nimport ${pkg}.service.${t.cls}Service;\nimport java.util.Scanner;\n\npublic class Main {\n`;
code += ` public static void main(String[] args) {\n`;
code += ` ${t.cls}Service service = new ${t.cls}Service();\n`;
code += ` Scanner scanner = new Scanner(System.in);\n\n`;
code += ` System.out.println("=== EXECUTION SUCCESSFUL ===");\n`;
code += ` service.getAll().forEach(System.out::println);\n`;
code += ` }\n}`;
return code;
}
// 6. PERSISTENCE XML CONFIG
function genConfig(pkg, pu, url, user, pass, allTables) {
let classes = allTables.map(t => ` <class>${pkg}.entity.${t.cls}</class>`).join('\n');
return `<?xml version="1.0" encoding="UTF-8" ?>\n<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.0">\n <persistence-unit name="${pu}" transaction-type="RESOURCE_LOCAL">\n <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>\n${classes}\n <properties>\n <property name="jakarta.persistence.jdbc.url" value="${url}"/>\n <property name="jakarta.persistence.jdbc.user" value="${user}"/>\n <property name="jakarta.persistence.jdbc.password" value="${pass}"/>\n <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>\n <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>\n <property name="hibernate.show_sql" value="true"/>\n </properties>\n </persistence-unit>\n</persistence>`;
}
function copyCode() {
navigator.clipboard.writeText(document.getElementById('out').textContent);
}
</script>
</body>
</html>