5uck1t commited on
Commit
1fbf4e0
·
verified ·
1 Parent(s): a92e9af

okay now next to add button for inputs add a button that will say add relation and it will have 3 inputs first 2 as before name and type but the last will be dropdown that will have 2 options "Parent" and "Child" if child is selected that field will have

Browse files

"@ManyToOne // Many Employees to One Department
@JoinColumn(name = "department_id")
private Department department;"
above it in this case department is field name from the input so do fieldname_id, and if the parent is selected another input appears ,
that will have
"@OneToMany(mappedBy = "department") "
above it and in this case department is from the 4th input that appears when the parent is selected

Files changed (3) hide show
  1. index.html +9 -6
  2. script.js +89 -12
  3. style.css +3 -1
index.html CHANGED
@@ -39,15 +39,18 @@
39
  <label for="class-name" class="block text-sm font-medium text-gray-300 mb-1">Class Name</label>
40
  <input type="text" id="class-name" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="MyClass">
41
  </div>
42
-
43
  <div class="flex justify-between items-center mb-4">
44
  <h2 class="text-xl font-semibold text-primary-500">Input Fields</h2>
45
- <button id="add-input" class="bg-primary-500 hover:bg-primary-600 text-white px-3 py-1 rounded-lg flex items-center">
46
- <i data-feather="plus" class="w-4 h-4 mr-1"></i> Add
47
- </button>
 
 
 
 
 
48
  </div>
49
-
50
- <div id="input-container" class="space-y-4">
51
  <!-- Input fields will be added here -->
52
  <div class="input-group">
53
  <div class="flex gap-2">
 
39
  <label for="class-name" class="block text-sm font-medium text-gray-300 mb-1">Class Name</label>
40
  <input type="text" id="class-name" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="MyClass">
41
  </div>
 
42
  <div class="flex justify-between items-center mb-4">
43
  <h2 class="text-xl font-semibold text-primary-500">Input Fields</h2>
44
+ <div class="flex gap-2">
45
+ <button id="add-input" class="bg-primary-500 hover:bg-primary-600 text-white px-3 py-1 rounded-lg flex items-center">
46
+ <i data-feather="plus" class="w-4 h-4 mr-1"></i> Add Field
47
+ </button>
48
+ <button id="add-relation" class="bg-secondary-500 hover:bg-secondary-600 text-white px-3 py-1 rounded-lg flex items-center">
49
+ <i data-feather="link" class="w-4 h-4 mr-1"></i> Add Relation
50
+ </button>
51
+ </div>
52
  </div>
53
+ <div id="input-container" class="space-y-4">
 
54
  <!-- Input fields will be added here -->
55
  <div class="input-group">
56
  <div class="flex gap-2">
script.js CHANGED
@@ -2,9 +2,9 @@
2
  document.addEventListener('DOMContentLoaded', function() {
3
  // Initialize highlight.js
4
  hljs.highlightAll();
5
-
6
- // Add input field
7
  const addInputBtn = document.getElementById('add-input');
 
8
  const inputContainer = document.getElementById('input-container');
9
 
10
  addInputBtn.addEventListener('click', () => {
@@ -18,7 +18,7 @@ document.addEventListener('DOMContentLoaded', function() {
18
  <button class="remove-input mt-1 text-red-400 hover:text-red-300 text-sm flex items-center">
19
  <i data-feather="trash-2" class="w-3 h-3 mr-1"></i> Remove
20
  </button>
21
- `;
22
  inputContainer.appendChild(inputGroup);
23
  feather.replace();
24
 
@@ -29,6 +29,57 @@ document.addEventListener('DOMContentLoaded', function() {
29
  });
30
  });
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  // Remove input field
33
  document.addEventListener('click', function(e) {
34
  if (e.target.classList.contains('remove-input') || e.target.closest('.remove-input')) {
@@ -36,20 +87,45 @@ document.addEventListener('DOMContentLoaded', function() {
36
  btn.parentElement.remove();
37
  }
38
  });
39
-
40
  // Generate output
41
  const generateBtn = document.getElementById('generate-btn');
42
  const outputTextarea = document.getElementById('output-text');
 
 
 
 
 
 
 
 
 
43
  generateBtn.addEventListener('click', () => {
44
  const className = document.getElementById('class-name').value || 'MyClass';
45
  const inputGroups = Array.from(document.querySelectorAll('.input-group'));
46
  const fields = inputGroups.map(group => {
47
  const inputs = group.querySelectorAll('input');
48
- return {
49
- name: inputs[0].value || 'field',
50
- type: inputs[1].value || 'String'
51
- };
52
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  // Generate JPA Entity
55
  let jpaCode = `package com.example.accessingdatajpa;\n\n`;
@@ -59,15 +135,16 @@ generateBtn.addEventListener('click', () => {
59
  jpaCode += `import jakarta.persistence.Id;\n\n`;
60
  jpaCode += `@Entity\n`;
61
  jpaCode += `public class ${className} {\n\n`;
62
-
63
  // Add fields with annotations if needed
64
  fields.forEach(field => {
65
- if (field.name.toLowerCase() === 'id') {
 
 
66
  jpaCode += ` @Id\n`;
67
  jpaCode += ` @GeneratedValue(strategy=GenerationType.IDENTITY)\n`;
68
  }
69
  jpaCode += ` private ${field.type} ${field.name};\n`;
70
- });
71
 
72
  // Add default constructor
73
  jpaCode += `\n protected ${className}() {}\n\n`;
 
2
  document.addEventListener('DOMContentLoaded', function() {
3
  // Initialize highlight.js
4
  hljs.highlightAll();
5
+ // Add input field
 
6
  const addInputBtn = document.getElementById('add-input');
7
+ const addRelationBtn = document.getElementById('add-relation');
8
  const inputContainer = document.getElementById('input-container');
9
 
10
  addInputBtn.addEventListener('click', () => {
 
18
  <button class="remove-input mt-1 text-red-400 hover:text-red-300 text-sm flex items-center">
19
  <i data-feather="trash-2" class="w-3 h-3 mr-1"></i> Remove
20
  </button>
21
+ `;
22
  inputContainer.appendChild(inputGroup);
23
  feather.replace();
24
 
 
29
  });
30
  });
31
 
32
+ // Add relation field
33
+ addRelationBtn.addEventListener('click', () => {
34
+ const inputGroup = document.createElement('div');
35
+ inputGroup.className = 'input-group';
36
+ inputGroup.innerHTML = `
37
+ <div class="flex flex-col gap-2">
38
+ <div class="flex gap-2">
39
+ <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Name">
40
+ <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Type">
41
+ </div>
42
+ <select class="relation-type bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500">
43
+ <option value="child">Child</option>
44
+ <option value="parent">Parent</option>
45
+ </select>
46
+ <input type="text" class="parent-field mt-2 hidden bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Parent Field Name">
47
+ </div>
48
+ <button class="remove-input mt-1 text-red-400 hover:text-red-300 text-sm flex items-center">
49
+ <i data-feather="trash-2" class="w-3 h-3 mr-1"></i> Remove
50
+ </button>
51
+ `;
52
+ inputContainer.appendChild(inputGroup);
53
+ feather.replace();
54
+
55
+ // Toggle parent field input based on relation type
56
+ const relationType = inputGroup.querySelector('.relation-type');
57
+ const parentField = inputGroup.querySelector('.parent-field');
58
+
59
+ relationType.addEventListener('change', () => {
60
+ if (relationType.value === 'parent') {
61
+ parentField.classList.remove('hidden');
62
+ } else {
63
+ parentField.classList.add('hidden');
64
+ }
65
+ });
66
+
67
+ // Add event listener to remove button
68
+ const removeBtn = inputGroup.querySelector('.remove-input');
69
+ removeBtn.addEventListener('click', () => {
70
+ inputGroup.remove();
71
+ });
72
+ });
73
+ inputContainer.appendChild(inputGroup);
74
+ feather.replace();
75
+
76
+ // Add event listener to new remove button
77
+ const removeBtn = inputGroup.querySelector('.remove-input');
78
+ removeBtn.addEventListener('click', () => {
79
+ inputGroup.remove();
80
+ });
81
+ });
82
+
83
  // Remove input field
84
  document.addEventListener('click', function(e) {
85
  if (e.target.classList.contains('remove-input') || e.target.closest('.remove-input')) {
 
87
  btn.parentElement.remove();
88
  }
89
  });
 
90
  // Generate output
91
  const generateBtn = document.getElementById('generate-btn');
92
  const outputTextarea = document.getElementById('output-text');
93
+
94
+ function getRelationAnnotation(fieldName, relationType, parentField) {
95
+ if (relationType === 'child') {
96
+ return `@ManyToOne\n @JoinColumn(name = "${fieldName}_id")\n`;
97
+ } else if (relationType === 'parent') {
98
+ return `@OneToMany(mappedBy = "${parentField}")\n`;
99
+ }
100
+ return '';
101
+ }
102
  generateBtn.addEventListener('click', () => {
103
  const className = document.getElementById('class-name').value || 'MyClass';
104
  const inputGroups = Array.from(document.querySelectorAll('.input-group'));
105
  const fields = inputGroups.map(group => {
106
  const inputs = group.querySelectorAll('input');
107
+ const select = group.querySelector('select');
108
+
109
+ if (select) {
110
+ // This is a relation field
111
+ const field = {
112
+ name: inputs[0].value || 'field',
113
+ type: inputs[1].value || 'String',
114
+ relationType: select.value
115
+ };
116
+
117
+ if (select.value === 'parent') {
118
+ field.parentField = inputs[2]?.value || 'parentField';
119
+ }
120
+ return field;
121
+ } else {
122
+ // Regular field
123
+ return {
124
+ name: inputs[0].value || 'field',
125
+ type: inputs[1].value || 'String'
126
+ };
127
+ }
128
+ });
129
 
130
  // Generate JPA Entity
131
  let jpaCode = `package com.example.accessingdatajpa;\n\n`;
 
135
  jpaCode += `import jakarta.persistence.Id;\n\n`;
136
  jpaCode += `@Entity\n`;
137
  jpaCode += `public class ${className} {\n\n`;
 
138
  // Add fields with annotations if needed
139
  fields.forEach(field => {
140
+ if (field.relationType) {
141
+ jpaCode += ` ${getRelationAnnotation(field.name, field.relationType, field.parentField)}`;
142
+ } else if (field.name.toLowerCase() === 'id') {
143
  jpaCode += ` @Id\n`;
144
  jpaCode += ` @GeneratedValue(strategy=GenerationType.IDENTITY)\n`;
145
  }
146
  jpaCode += ` private ${field.type} ${field.name};\n`;
147
+ });
148
 
149
  // Add default constructor
150
  jpaCode += `\n protected ${className}() {}\n\n`;
style.css CHANGED
@@ -59,11 +59,13 @@ textarea {
59
  from { opacity: 0; transform: translateY(10px); }
60
  to { opacity: 1; transform: translateY(0); }
61
  }
62
-
63
  .input-group {
64
  animation: fadeIn 0.3s ease-out;
65
  }
66
 
 
 
 
67
  /* Preview content styles */
68
  .preview-content {
69
  line-height: 1.6;
 
59
  from { opacity: 0; transform: translateY(10px); }
60
  to { opacity: 1; transform: translateY(0); }
61
  }
 
62
  .input-group {
63
  animation: fadeIn 0.3s ease-out;
64
  }
65
 
66
+ .hidden {
67
+ display: none;
68
+ }
69
  /* Preview content styles */
70
  .preview-content {
71
  line-height: 1.6;