@simplix-boot/cli 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/LICENSE_KO +28 -0
- package/README.md +146 -2
- package/bin/simplix-boot.js +6 -0
- package/package.json +45 -4
- package/src/cli.js +103 -0
- package/src/commands/config.js +129 -0
- package/src/commands/generate.js +291 -0
- package/src/commands/generated.js +125 -0
- package/src/commands/init.js +41 -0
- package/src/commands/promote.js +172 -0
- package/src/commands/templates.js +100 -0
- package/src/commands/version.js +134 -0
- package/src/lib/analysis/fields.js +217 -0
- package/src/lib/analysis/imports.js +76 -0
- package/src/lib/analysis/structure.js +193 -0
- package/src/lib/analysis/types.js +94 -0
- package/src/lib/args.js +69 -0
- package/src/lib/comment-extractor.js +141 -0
- package/src/lib/config/fields.js +321 -0
- package/src/lib/config/parents.js +108 -0
- package/src/lib/config/paths.js +156 -0
- package/src/lib/config/yml.js +61 -0
- package/src/lib/console.js +83 -0
- package/src/lib/controller-name.js +33 -0
- package/src/lib/entity-analyzer.js +49 -0
- package/src/lib/entity-type-resolver.js +80 -0
- package/src/lib/entity-type.js +35 -0
- package/src/lib/errors.js +17 -0
- package/src/lib/export-config.js +74 -0
- package/src/lib/file-utils.js +77 -0
- package/src/lib/generate/file-plan.js +155 -0
- package/src/lib/generate/search-fields.js +111 -0
- package/src/lib/generate/settings.js +111 -0
- package/src/lib/generate/support-classes.js +47 -0
- package/src/lib/generate/template-vars.js +183 -0
- package/src/lib/git-utils.js +18 -0
- package/src/lib/name-field.js +42 -0
- package/src/lib/project.js +121 -0
- package/src/lib/promote-files.js +132 -0
- package/src/lib/rendered-output.js +13 -0
- package/src/lib/search-operators.js +87 -0
- package/src/lib/templates.js +50 -0
- package/src/lib/tracked-render.js +200 -0
- package/src/lib/version-check.js +195 -0
- package/src/lib/version-utils.js +97 -0
- package/templates/controller/rest/EntityRestController.java.template +517 -0
- package/templates/controller/web/EntityWebController.java.template +211 -0
- package/templates/dto/EntityDTOs.java.template +981 -0
- package/templates/repository/EntityRepository.java.template +78 -0
- package/templates/service/EntityService.java.template +790 -0
- package/templates/service/EntityTreeService.java.template +352 -0
- package/templates/test/EntityServiceTest.java.template +937 -0
- package/templates/test/EntityTreeServiceTest.java.template +842 -0
|
@@ -0,0 +1,981 @@
|
|
|
1
|
+
package <%= packagePaths['dto'] %>;
|
|
2
|
+
|
|
3
|
+
<% if (hasEmbeddedId) { %>
|
|
4
|
+
import <%= entityPackage %>.<%= entityName %>.<%= embeddedIdType %>;
|
|
5
|
+
<% } %>
|
|
6
|
+
|
|
7
|
+
<%
|
|
8
|
+
// Parse import statements for reference entities
|
|
9
|
+
const importedTypes = new Set();
|
|
10
|
+
|
|
11
|
+
// Helper: Check if a type is a standard Java library type that should NOT be imported as an entity
|
|
12
|
+
function isStandardJavaType(typeName, importPath) {
|
|
13
|
+
if (!typeName) return false;
|
|
14
|
+
// Skip java.* types (they are already imported via wildcards)
|
|
15
|
+
if (importPath && importPath.startsWith('java.')) return true;
|
|
16
|
+
// Skip common Java types
|
|
17
|
+
const standardTypes = ['Instant', 'LocalDate', 'LocalDateTime', 'ZonedDateTime', 'Duration',
|
|
18
|
+
'BigDecimal', 'BigInteger', 'UUID', 'Optional',
|
|
19
|
+
'List', 'Set', 'Map', 'Collection', 'Iterable'];
|
|
20
|
+
return standardTypes.includes(typeName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Import the main entity class for @UniqueFields annotation
|
|
24
|
+
%>
|
|
25
|
+
import <%= entityPackage %>.<%= entityName %>;
|
|
26
|
+
<%
|
|
27
|
+
// Find reference types from entity fields
|
|
28
|
+
fields.forEach(field => {
|
|
29
|
+
if (field.typeKind === 'entity' || field.isRelation || field.relationType === 'ManyToOne') {
|
|
30
|
+
const entityType = field.isCollection ? field.actualType : field.type;
|
|
31
|
+
|
|
32
|
+
// Skip standard Java types - they should not be treated as entities
|
|
33
|
+
if (isStandardJavaType(entityType, field.importPath)) {
|
|
34
|
+
return; // skip this field
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Always use the correct domain entity package path
|
|
38
|
+
let entityImport;
|
|
39
|
+
if (field.importPath && !field.importPath.includes('jakarta.persistence')) {
|
|
40
|
+
entityImport = field.importPath;
|
|
41
|
+
} else {
|
|
42
|
+
// Default to same module path as current entity
|
|
43
|
+
// Use entityBasePackage if available, otherwise extract from entityPackage
|
|
44
|
+
const entityBasePkg = entityBasePackage || entityPackage.substring(0, entityPackage.lastIndexOf('.'));
|
|
45
|
+
entityImport = `${entityBasePkg}.${modulePath}.${entityType}`;
|
|
46
|
+
}
|
|
47
|
+
if (!importedTypes.has(entityType)) {
|
|
48
|
+
importedTypes.add(entityType); %>
|
|
49
|
+
import <%= entityImport %>;<%
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Handle enum type
|
|
53
|
+
else if (field.typeKind === 'enum') {
|
|
54
|
+
// Use the import path from field analysis
|
|
55
|
+
if (field.importPath && !importedTypes.has(field.type)) {
|
|
56
|
+
importedTypes.add(field.type); %>
|
|
57
|
+
import <%= field.importPath %>;<%
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Handle a value type kept in the row (a record or an embeddable in a JSON column, say)
|
|
61
|
+
else if (field.typeKind === 'value') {
|
|
62
|
+
const valueType = field.isCollection ? field.actualType : field.type;
|
|
63
|
+
if (field.importPath && !importedTypes.has(valueType)) {
|
|
64
|
+
importedTypes.add(valueType); %>
|
|
65
|
+
import <%= field.importPath %>;<%
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}); %>
|
|
69
|
+
import java.util.*;
|
|
70
|
+
import java.time.*;
|
|
71
|
+
import java.math.*;
|
|
72
|
+
import io.swagger.v3.oas.annotations.media.Schema;
|
|
73
|
+
import lombok.Data;
|
|
74
|
+
import lombok.EqualsAndHashCode;
|
|
75
|
+
import lombok.Getter;
|
|
76
|
+
import lombok.Setter;
|
|
77
|
+
import org.springframework.format.annotation.DateTimeFormat;
|
|
78
|
+
import org.hibernate.validator.constraints.Length;
|
|
79
|
+
import jakarta.validation.constraints.*;
|
|
80
|
+
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
81
|
+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
82
|
+
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
|
|
83
|
+
|
|
84
|
+
import dev.simplecore.simplix.core.jackson.annotation.I18nTrans;
|
|
85
|
+
import dev.simplecore.simplix.core.validator.SoftDeleteType;
|
|
86
|
+
import dev.simplecore.simplix.core.validator.Unique;
|
|
87
|
+
import dev.simplecore.simplix.core.validator.UniqueField;
|
|
88
|
+
import dev.simplecore.simplix.core.validator.UniqueFields;
|
|
89
|
+
import dev.simplecore.searchable.core.annotation.SearchableField;
|
|
90
|
+
import dev.simplecore.simplix.core.annotation.FieldLabel;
|
|
91
|
+
|
|
92
|
+
import static dev.simplecore.searchable.core.condition.operator.SearchOperator.*;
|
|
93
|
+
|
|
94
|
+
public class <%= classPrefix %>DTOs {
|
|
95
|
+
<%
|
|
96
|
+
// Build i18n field mapping: baseFieldName -> i18nFieldName
|
|
97
|
+
// Example: description -> descriptionI18n
|
|
98
|
+
const i18nFieldMap = new Map();
|
|
99
|
+
entityFields.forEach(field => {
|
|
100
|
+
if (field.name.endsWith('I18n')) {
|
|
101
|
+
const baseFieldName = field.name.substring(0, field.name.length - 4);
|
|
102
|
+
const baseField = entityFields.find(f => f.name === baseFieldName);
|
|
103
|
+
if (baseField) {
|
|
104
|
+
i18nFieldMap.set(baseFieldName, field.name);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ============== DERIVED I18N COLUMNS ==============
|
|
110
|
+
// A locale map can be neither pattern-matched nor ordered, so a localized field that a list
|
|
111
|
+
// searches or sorts by carries derived columns beside it: <x>Search folds every language into one
|
|
112
|
+
// normalized string, and one <x>Sort<Lang> per language holds what that language's reader sees.
|
|
113
|
+
// I18nDerivedColumnListener fills them on every write; here they are what the SearchDTO points at.
|
|
114
|
+
//
|
|
115
|
+
// The pairing is the listener's, so the two cannot disagree: the locale map has to be there for a
|
|
116
|
+
// <x>Search or <x>Sort<Lang> field to be claimed as derived. Claiming by suffix alone would sweep
|
|
117
|
+
// up ordinary fields - sortKey, sortOrder, searchTerm.
|
|
118
|
+
const derivedI18nFields = new Map();
|
|
119
|
+
const derivedI18nColumnNames = new Set();
|
|
120
|
+
i18nFieldMap.forEach((i18nFieldName, baseFieldName) => {
|
|
121
|
+
// The derived columns are answered through the application's rewrite of the search parameters,
|
|
122
|
+
// which the project configuration names under supportClasses.localizedSearchParams. Without it
|
|
123
|
+
// they are ordinary columns and the SearchDTO points at the base column.
|
|
124
|
+
if (!supportClasses.find('localizedSearchParams')) return;
|
|
125
|
+
|
|
126
|
+
const searchFieldName = `${baseFieldName}Search`;
|
|
127
|
+
const sortPrefix = `${baseFieldName}Sort`;
|
|
128
|
+
|
|
129
|
+
const search = entityFields.some(f => f.name === searchFieldName && f.type === 'String')
|
|
130
|
+
? searchFieldName : null;
|
|
131
|
+
const sorts = entityFields
|
|
132
|
+
.filter(f => f.type === 'String'
|
|
133
|
+
&& f.name.startsWith(sortPrefix)
|
|
134
|
+
&& f.name.length > sortPrefix.length)
|
|
135
|
+
.map(f => ({
|
|
136
|
+
field: f.name,
|
|
137
|
+
language: f.name.substring(sortPrefix.length).toLowerCase()
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
if (!search && sorts.length === 0) return;
|
|
141
|
+
|
|
142
|
+
derivedI18nFields.set(baseFieldName, { search, sorts });
|
|
143
|
+
if (search) derivedI18nColumnNames.add(search);
|
|
144
|
+
sorts.forEach(sort => derivedI18nColumnNames.add(sort.field));
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ============== TREE ENTITY DETECTION ==============
|
|
148
|
+
// Use hasTreeEntity from generator context (entity implements TreeEntity interface)
|
|
149
|
+
// Fallback to parentId field check if hasTreeEntity is not defined
|
|
150
|
+
const isTreeEntity = typeof hasTreeEntity !== 'undefined' ? hasTreeEntity :
|
|
151
|
+
(entityFields.some(f => f.name === 'parentId') ||
|
|
152
|
+
(ymlConfig.fields && ymlConfig.fields['parentId']));
|
|
153
|
+
|
|
154
|
+
// ============== INHERITED FIELD SUPPORT ==============
|
|
155
|
+
// Build entityFields name set for quick lookup
|
|
156
|
+
const entityFieldNames = new Set(entityFields.map(f => f.name));
|
|
157
|
+
|
|
158
|
+
// Build excluded fields set from generator-simplix.json configuration
|
|
159
|
+
const excludedFieldsSet = new Set(dtoExcludeFields || []);
|
|
160
|
+
|
|
161
|
+
// Helper: Check if a field should be excluded from DTO generation
|
|
162
|
+
function isExcludedField(fieldName) {
|
|
163
|
+
// A derived i18n column is never a DTO field of its own. The listener recomputes it from the
|
|
164
|
+
// locale map on every write, so offering it for edit would offer a value the next save throws
|
|
165
|
+
// away, and showing it would put normalized search text on a screen. The SearchDTO names the
|
|
166
|
+
// sort columns explicitly below; nothing else does.
|
|
167
|
+
return excludedFieldsSet.has(fieldName) || derivedI18nColumnNames.has(fieldName);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Helper: Check if a field is a self-reference (references the same entity)
|
|
171
|
+
// Note: FK ID fields (like parentId) should NOT be skipped - they need to be in DTOs for input
|
|
172
|
+
// Only the ManyToOne object field (like parent) should be skipped to avoid circular references
|
|
173
|
+
function isSelfReferenceField(fieldName, ymlField) {
|
|
174
|
+
if (!ymlField?.reference?.entity) return false;
|
|
175
|
+
if (ymlField.reference.entity !== entityName) return false;
|
|
176
|
+
// If this field IS the FK ID field (fieldName === idField), don't skip it
|
|
177
|
+
// FK ID fields like 'parentId' should be included in DTOs for tree structure input
|
|
178
|
+
if (ymlField.reference.idField === fieldName) return false;
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Helper: Check if entity has explicit ID field for a reference field
|
|
183
|
+
// e.g., if entity has both 'channel' (ManyToOne) and 'channelId' (String), skip generating ID from reference
|
|
184
|
+
function hasExplicitIdField(referenceFieldName) {
|
|
185
|
+
const expectedIdFieldName = referenceFieldName + 'Id';
|
|
186
|
+
return entityFields.some(f => f.name === expectedIdFieldName);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Helper: Check if field name already ends with 'Id' (to avoid 'IdId' pattern)
|
|
190
|
+
function fieldNameEndsWithId(fieldName) {
|
|
191
|
+
return fieldName.endsWith('Id') && fieldName !== 'Id';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Helper: whether a referencing field holds the key itself (a scalar column such as grantedBy or
|
|
195
|
+
// userAccountId) rather than an association whose key sits one step further (channel.channelId).
|
|
196
|
+
// The field's own type decides; a name ending in 'Id' is a scalar key whatever the analyzer reports.
|
|
197
|
+
function holdsReferenceKey(fieldName) {
|
|
198
|
+
if (fieldNameEndsWithId(fieldName)) return true;
|
|
199
|
+
const entityField = entityFields.find(f => f.name === fieldName);
|
|
200
|
+
return Boolean(entityField) && entityField.typeKind !== 'entity' && !entityField.isCollection;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Helper: Get ID field name for reference field (avoids 'IdId' pattern)
|
|
204
|
+
function getReferenceIdFieldName(fieldName, isMultiple) {
|
|
205
|
+
if (isMultiple) {
|
|
206
|
+
// For multiple references: field -> fieldIds, fieldId -> fieldIds (not fieldIdIds)
|
|
207
|
+
return fieldNameEndsWithId(fieldName) ? `${fieldName}s` : `${fieldName}Ids`;
|
|
208
|
+
} else {
|
|
209
|
+
// For single reference: field -> fieldId, fieldId -> fieldId (not fieldIdId)
|
|
210
|
+
return holdsReferenceKey(fieldName) ? fieldName : `${fieldName}Id`;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Convert camelCase field name to human-readable comment
|
|
215
|
+
function fieldNameToComment(fieldName) {
|
|
216
|
+
return fieldName.replace(/([A-Z])/g, ' $1').replace(/^./, s => s.toUpperCase()).trim();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Convert one yml searchOperators entry to its SearchOperator enum constant.
|
|
220
|
+
function toSearchOperator(op) {
|
|
221
|
+
// yml names are camelCase; the SearchOperator constants are UPPER_SNAKE, with the
|
|
222
|
+
// inclusive comparisons spelled ..._OR_EQUAL_TO.
|
|
223
|
+
const aliases = {
|
|
224
|
+
greaterThanOrEqual: 'GREATER_THAN_OR_EQUAL_TO', greaterThanOrEquals: 'GREATER_THAN_OR_EQUAL_TO',
|
|
225
|
+
lessThanOrEqual: 'LESS_THAN_OR_EQUAL_TO', lessThanOrEquals: 'LESS_THAN_OR_EQUAL_TO',
|
|
226
|
+
};
|
|
227
|
+
if (aliases[op]) return aliases[op];
|
|
228
|
+
return op.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ============== THE ENTITY ID'S SEARCH CONTRACT ==============
|
|
232
|
+
// The id never reaches entitySearchFields - prepareSearchFields drops it before the template sees
|
|
233
|
+
// anything - so its yml declaration is read here directly, and the two halves below are added
|
|
234
|
+
// whatever that declaration says. Both are mandatory and the scaffold used to emit neither:
|
|
235
|
+
//
|
|
236
|
+
// sortable the frontend's default list sort is `<entityId>.desc`, so without it the very
|
|
237
|
+
// first request a scaffolded list makes is refused and the screen never loads.
|
|
238
|
+
// IN a list's filter FOR this entity resolves the labels of what is selected by asking
|
|
239
|
+
// this entity's own search for those ids at once (`<entityId>.in=a,b,c`). Left out,
|
|
240
|
+
// that filter refuses the whole request the moment a value is picked - on every
|
|
241
|
+
// list that offers it, including other modules', since the filter is shared.
|
|
242
|
+
//
|
|
243
|
+
// The asymmetry that hides it: the same id declared as a FOREIGN key on another entity is
|
|
244
|
+
// routinely {EQUALS, IN} and works, so only the entity's own list carries the defect.
|
|
245
|
+
function idSearchOperators() {
|
|
246
|
+
const declared = (ymlConfig.fields?.[ymlConfig.idField]?.searchOperators || []).map(toSearchOperator);
|
|
247
|
+
const operators = ['EQUALS', 'IN'];
|
|
248
|
+
declared.forEach(op => { if (!operators.includes(op)) operators.push(op); });
|
|
249
|
+
return operators.join(', ');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Infer type from YML field configuration or field name pattern
|
|
253
|
+
function inferFieldType(fieldName, ymlField) {
|
|
254
|
+
// If YML has explicit type, use it
|
|
255
|
+
if (ymlField?.type) {
|
|
256
|
+
return ymlField.type;
|
|
257
|
+
}
|
|
258
|
+
// Infer from searchOperators pattern
|
|
259
|
+
if (ymlField?.searchOperators) {
|
|
260
|
+
const ops = ymlField.searchOperators;
|
|
261
|
+
if (ops.includes('between')) {
|
|
262
|
+
return 'Instant'; // Date/time fields typically use between
|
|
263
|
+
}
|
|
264
|
+
if (ops.includes('greaterThan') && ops.includes('lessThan') && !ops.includes('contains')) {
|
|
265
|
+
// Numeric or version field
|
|
266
|
+
if (fieldName === 'version') return 'Long';
|
|
267
|
+
return 'Long';
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Infer from maxLength (String fields)
|
|
271
|
+
if (ymlField?.maxLength) {
|
|
272
|
+
return 'String';
|
|
273
|
+
}
|
|
274
|
+
// Default to String
|
|
275
|
+
return 'String';
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Helper: Get all fields for a view, including inherited ones from YML
|
|
279
|
+
function getMergedFieldsForView(viewFilter) {
|
|
280
|
+
const result = [];
|
|
281
|
+
const processedFieldNames = new Set();
|
|
282
|
+
|
|
283
|
+
// 1. Add all fields from entityFields that match the view filter
|
|
284
|
+
for (const field of entityFields) {
|
|
285
|
+
// Skip globally excluded fields
|
|
286
|
+
if (isExcludedField(field.name)) continue;
|
|
287
|
+
|
|
288
|
+
const ymlField = ymlConfig.fields[field.name];
|
|
289
|
+
|
|
290
|
+
// Skip self-reference fields (e.g., parent field referencing same entity)
|
|
291
|
+
if (isSelfReferenceField(field.name, ymlField)) continue;
|
|
292
|
+
|
|
293
|
+
if (ymlField && ymlField.views?.includes(viewFilter)) {
|
|
294
|
+
result.push({
|
|
295
|
+
...field,
|
|
296
|
+
ymlField: ymlField,
|
|
297
|
+
isInherited: false
|
|
298
|
+
});
|
|
299
|
+
processedFieldNames.add(field.name);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// 2. Add fields from ymlConfig that are not in entityFields (inherited fields)
|
|
304
|
+
for (const [fieldName, ymlField] of Object.entries(ymlConfig.fields || {})) {
|
|
305
|
+
// Skip globally excluded fields
|
|
306
|
+
if (isExcludedField(fieldName)) continue;
|
|
307
|
+
|
|
308
|
+
// Skip self-reference fields (e.g., parent field referencing same entity)
|
|
309
|
+
if (isSelfReferenceField(fieldName, ymlField)) continue;
|
|
310
|
+
|
|
311
|
+
if (!processedFieldNames.has(fieldName) &&
|
|
312
|
+
!entityFieldNames.has(fieldName) &&
|
|
313
|
+
ymlField.views?.includes(viewFilter)) {
|
|
314
|
+
result.push({
|
|
315
|
+
name: fieldName,
|
|
316
|
+
type: inferFieldType(fieldName, ymlField),
|
|
317
|
+
comment: fieldNameToComment(fieldName),
|
|
318
|
+
ymlField: ymlField,
|
|
319
|
+
isInherited: true,
|
|
320
|
+
isCollection: false
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Helper: Format field type for DTO (handle primitives, etc.)
|
|
329
|
+
function formatDtoFieldType(field, ymlField) {
|
|
330
|
+
let fieldType = ymlField?.type || field.type;
|
|
331
|
+
if (fieldType === 'boolean') fieldType = 'Boolean';
|
|
332
|
+
if (fieldType === 'int') fieldType = 'Integer';
|
|
333
|
+
if (fieldType === 'long') fieldType = 'Long';
|
|
334
|
+
return fieldType;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Helper: Get merged search fields including inherited ones from YML
|
|
338
|
+
function getMergedSearchFields() {
|
|
339
|
+
const result = [];
|
|
340
|
+
const processedFieldNames = new Set();
|
|
341
|
+
|
|
342
|
+
// 1. Add all fields from entitySearchFields
|
|
343
|
+
if (entitySearchFields && entitySearchFields.length > 0) {
|
|
344
|
+
for (const field of entitySearchFields) {
|
|
345
|
+
// Skip globally excluded fields
|
|
346
|
+
if (isExcludedField(field.name)) continue;
|
|
347
|
+
|
|
348
|
+
const ymlField = ymlConfig.fields[field.name];
|
|
349
|
+
|
|
350
|
+
// Skip self-reference fields (e.g., parent field referencing same entity)
|
|
351
|
+
if (isSelfReferenceField(field.name, ymlField)) continue;
|
|
352
|
+
|
|
353
|
+
result.push({
|
|
354
|
+
...field,
|
|
355
|
+
isInherited: false
|
|
356
|
+
});
|
|
357
|
+
processedFieldNames.add(field.name);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// 2. Add fields from ymlConfig that are not in entityFields and have searchOperators
|
|
362
|
+
for (const [fieldName, ymlField] of Object.entries(ymlConfig.fields || {})) {
|
|
363
|
+
// Skip globally excluded fields
|
|
364
|
+
if (isExcludedField(fieldName)) continue;
|
|
365
|
+
|
|
366
|
+
// Skip self-reference fields (e.g., parent field referencing same entity)
|
|
367
|
+
if (isSelfReferenceField(fieldName, ymlField)) continue;
|
|
368
|
+
|
|
369
|
+
if (!processedFieldNames.has(fieldName) &&
|
|
370
|
+
!entityFieldNames.has(fieldName) &&
|
|
371
|
+
ymlField.searchOperators && ymlField.searchOperators.length > 0) {
|
|
372
|
+
result.push({
|
|
373
|
+
name: fieldName,
|
|
374
|
+
type: inferFieldType(fieldName, ymlField),
|
|
375
|
+
comment: fieldNameToComment(fieldName),
|
|
376
|
+
sortable: ymlField.sortable || false,
|
|
377
|
+
searchOperators: ymlField.searchOperators,
|
|
378
|
+
entityField: fieldName,
|
|
379
|
+
isInherited: true
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return result;
|
|
385
|
+
}
|
|
386
|
+
%>
|
|
387
|
+
|
|
388
|
+
//----------------------------------
|
|
389
|
+
// Search
|
|
390
|
+
//----------------------------------
|
|
391
|
+
|
|
392
|
+
@Getter
|
|
393
|
+
@Setter
|
|
394
|
+
public static class <%= classPrefix %>SearchDTO {
|
|
395
|
+
<% if (hasEmbeddedId) {
|
|
396
|
+
if (embeddedIdSearchFields && embeddedIdSearchFields.length > 0) {
|
|
397
|
+
embeddedIdSearchFields.forEach(field => { %>
|
|
398
|
+
@Schema(description = "<%= field.comment %>")
|
|
399
|
+
@FieldLabel("{entities.<%= entityName %>.<%= field.name %>}")
|
|
400
|
+
@SearchableField(entityField = "<%= field.entityField %>", operators = {<%= field.operators %>}<% if (field.sortable) { %>, sortable = true<% } %>)
|
|
401
|
+
private <%= field.type %> <%= field.name %>;
|
|
402
|
+
<% });
|
|
403
|
+
}
|
|
404
|
+
} else {
|
|
405
|
+
const idEntityField = entityFields.find(f => f.name === ymlConfig.idField); %>
|
|
406
|
+
@Schema(description = "<%= idEntityField?.comment?.split(':')[0] || `${entityName} ID` %>")
|
|
407
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
408
|
+
// sortable because the list's default order is this id descending, and IN because the
|
|
409
|
+
// filter for this entity resolves the labels of what is selected by asking for those ids
|
|
410
|
+
// at once. Without either half the list is refused rather than merely narrowed.
|
|
411
|
+
@SearchableField(operators = {<%= idSearchOperators() %>}, sortable = true)
|
|
412
|
+
private <%= idEntityField?.type || idType %> <%= ymlConfig.idField %>;
|
|
413
|
+
<% } %>
|
|
414
|
+
<%
|
|
415
|
+
const mergedSearchFields = getMergedSearchFields();
|
|
416
|
+
mergedSearchFields
|
|
417
|
+
.filter(field => field.name !== ymlConfig.idField && !hasEmbeddedId)
|
|
418
|
+
.forEach(field => {
|
|
419
|
+
const ymlField = ymlConfig.fields[field.name];
|
|
420
|
+
|
|
421
|
+
// Convert searchOperators array to uppercase enum constants
|
|
422
|
+
const operators = field.searchOperators && field.searchOperators.length > 0
|
|
423
|
+
? field.searchOperators.map(toSearchOperator).join(', ')
|
|
424
|
+
: '';
|
|
425
|
+
|
|
426
|
+
// Handle reference fields
|
|
427
|
+
let fieldType = field.type;
|
|
428
|
+
let fieldName = field.name;
|
|
429
|
+
let entityFieldPath = field.entityField || field.name;
|
|
430
|
+
let comment = ymlField?.comment || field.comment || fieldNameToComment(field.name);
|
|
431
|
+
|
|
432
|
+
// Skip reference handling for inherited fields
|
|
433
|
+
if (!field.isInherited) {
|
|
434
|
+
if (ymlField?.reference) {
|
|
435
|
+
// Skip if entity already has explicit ID field (e.g., channelId for channel)
|
|
436
|
+
// The explicit ID field will be processed separately
|
|
437
|
+
if (hasExplicitIdField(field.name)) {
|
|
438
|
+
return; // Skip this reference field to avoid duplicate
|
|
439
|
+
}
|
|
440
|
+
// For reference fields in search, use ID field
|
|
441
|
+
// Use helper to avoid 'IdId' pattern when field already ends with 'Id'
|
|
442
|
+
fieldType = ymlField.reference.idType || 'String';
|
|
443
|
+
fieldName = getReferenceIdFieldName(field.name, false);
|
|
444
|
+
// If field name already ends with 'Id', it's already the FK field - use direct path
|
|
445
|
+
// Otherwise, use nested path (e.g., channel.channelId)
|
|
446
|
+
if (holdsReferenceKey(field.name)) {
|
|
447
|
+
entityFieldPath = field.name; // Direct FK field path
|
|
448
|
+
} else {
|
|
449
|
+
entityFieldPath = `${field.name}.${ymlField.reference.idField}`;
|
|
450
|
+
comment = `${comment} ID`;
|
|
451
|
+
}
|
|
452
|
+
} else if (field.typeKind === 'enum') {
|
|
453
|
+
// Convert enum to String for SearchDTO
|
|
454
|
+
fieldType = 'String';
|
|
455
|
+
} else if (fieldType === 'boolean') {
|
|
456
|
+
// Convert boolean primitive to Boolean wrapper for DTOs
|
|
457
|
+
fieldType = 'Boolean';
|
|
458
|
+
} else if (fieldType === 'int') {
|
|
459
|
+
// Convert int primitive to Integer wrapper for DTOs
|
|
460
|
+
fieldType = 'Integer';
|
|
461
|
+
}
|
|
462
|
+
} else {
|
|
463
|
+
// For inherited fields, ensure proper type
|
|
464
|
+
if (fieldType === 'boolean') fieldType = 'Boolean';
|
|
465
|
+
if (fieldType === 'int') fieldType = 'Integer';
|
|
466
|
+
if (fieldType === 'long') fieldType = 'Long';
|
|
467
|
+
}
|
|
468
|
+
// A localized field is answered by its derived columns rather than by the base
|
|
469
|
+
// column, which holds only whatever language the record was first saved under.
|
|
470
|
+
const derived = derivedI18nFields.get(field.name);
|
|
471
|
+
let searchOperators = operators;
|
|
472
|
+
// A boolean is filtered, never sorted: the paging query aggregates the sort column
|
|
473
|
+
// as max(<column>) and PostgreSQL has no max(boolean), so the list answers 500 the
|
|
474
|
+
// moment anybody sorts by it. The generator drops the flag on entity fields and
|
|
475
|
+
// warns; this covers the yml-only fields it never sees.
|
|
476
|
+
let searchSortable = field.sortable && fieldType !== 'Boolean';
|
|
477
|
+
if (derived) {
|
|
478
|
+
if (derived.search) {
|
|
479
|
+
// EQUALS cannot survive the move: the folded column holds every language
|
|
480
|
+
// at once, so it equals none of them. CONTAINS is the whole point of it.
|
|
481
|
+
entityFieldPath = derived.search;
|
|
482
|
+
searchOperators = 'CONTAINS';
|
|
483
|
+
}
|
|
484
|
+
// The order moves onto the reader's language below. Leaving sortable here
|
|
485
|
+
// would let a list quietly order by the base column instead, and no screen
|
|
486
|
+
// says which language it ordered by.
|
|
487
|
+
searchSortable = false;
|
|
488
|
+
}
|
|
489
|
+
%>
|
|
490
|
+
@Schema(description = "<%= comment %>")
|
|
491
|
+
@FieldLabel("{entities.<%= entityName %>.<%= field.name %>}")
|
|
492
|
+
@SearchableField(entityField = "<%= entityFieldPath %>", operators = {<%= searchOperators %>}<% if (searchSortable) { %>, sortable = true<% } %><% if (ymlField?.searchKeyword) { %>, keyword = true<% } %>)
|
|
493
|
+
private <%= fieldType %> <%= fieldName %>;
|
|
494
|
+
<% if (derived && derived.sorts.length > 0) { %>
|
|
495
|
+
// Sort targets, one per language. A client asks for `sort=<%= field.name %>.asc` as it
|
|
496
|
+
// always has; LocalizedSearchParams moves that onto the reader's language before the
|
|
497
|
+
// search layer sees it. Hidden from the schema because no client names them.
|
|
498
|
+
<% derived.sorts.forEach(sort => { %> @Schema(hidden = true)
|
|
499
|
+
@FieldLabel("{entities.<%= entityName %>.<%= field.name %>}")
|
|
500
|
+
@SearchableField(entityField = "<%= sort.field %>", sortable = true)
|
|
501
|
+
private String <%= sort.field %>;
|
|
502
|
+
|
|
503
|
+
<% }); } %>
|
|
504
|
+
<% });
|
|
505
|
+
%>
|
|
506
|
+
//----------
|
|
507
|
+
|
|
508
|
+
@Schema(description = "Created by")
|
|
509
|
+
@SearchableField(entityField = "createdBy", operators = {EQUALS, CONTAINS})
|
|
510
|
+
private String createdBy;
|
|
511
|
+
|
|
512
|
+
@Schema(description = "Created at")
|
|
513
|
+
@SearchableField(entityField = "createdAt", operators = {GREATER_THAN_OR_EQUAL_TO, LESS_THAN_OR_EQUAL_TO, BETWEEN}, sortable = true)
|
|
514
|
+
private Instant createdAt;
|
|
515
|
+
|
|
516
|
+
@Schema(description = "Updated by")
|
|
517
|
+
@SearchableField(entityField = "updatedBy", operators = {EQUALS, CONTAINS})
|
|
518
|
+
private String updatedBy;
|
|
519
|
+
|
|
520
|
+
@Schema(description = "Updated at")
|
|
521
|
+
@SearchableField(entityField = "updatedAt", operators = {GREATER_THAN_OR_EQUAL_TO, LESS_THAN_OR_EQUAL_TO, BETWEEN}, sortable = true)
|
|
522
|
+
private Instant updatedAt;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
//----------------------------------
|
|
526
|
+
// Create, Update
|
|
527
|
+
//----------------------------------
|
|
528
|
+
<%
|
|
529
|
+
// Helper: Collect unique fields for @UniqueFields annotation
|
|
530
|
+
function getUniqueFieldsForCreate() {
|
|
531
|
+
const uniqueFields = [];
|
|
532
|
+
const createFields = getMergedFieldsForView('edit');
|
|
533
|
+
for (let mergedField of createFields) {
|
|
534
|
+
const field = mergedField;
|
|
535
|
+
const ymlField = mergedField.ymlField;
|
|
536
|
+
if (field.name === ymlConfig.idField) continue;
|
|
537
|
+
if (ymlField?.unique) {
|
|
538
|
+
// Determine the property name (same logic as field generation)
|
|
539
|
+
// Use helper to avoid 'IdId' pattern when field already ends with 'Id'
|
|
540
|
+
let propertyName = field.name;
|
|
541
|
+
if (!mergedField.isInherited && ymlField?.reference) {
|
|
542
|
+
propertyName = getReferenceIdFieldName(field.name, ymlField.reference.multiple);
|
|
543
|
+
}
|
|
544
|
+
uniqueFields.push({
|
|
545
|
+
entityField: field.name,
|
|
546
|
+
property: propertyName
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return uniqueFields;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// Helper: Check if field is a String type
|
|
554
|
+
function isStringType(fieldType) {
|
|
555
|
+
return fieldType === 'String' || fieldType === 'java.lang.String';
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Helper: Generate validation annotations for a field
|
|
559
|
+
function getValidationAnnotations(ymlField, fieldType, isCreateDto = true) {
|
|
560
|
+
const annotations = [];
|
|
561
|
+
|
|
562
|
+
// Required validation
|
|
563
|
+
if (ymlField?.required) {
|
|
564
|
+
if (isStringType(fieldType)) {
|
|
565
|
+
annotations.push('@NotBlank');
|
|
566
|
+
} else {
|
|
567
|
+
annotations.push('@NotNull');
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Length validation (for String fields)
|
|
572
|
+
if (ymlField?.maxLength && isStringType(fieldType)) {
|
|
573
|
+
annotations.push(`@Length(max = ${ymlField.maxLength})`);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Min/Max validation (for numeric fields)
|
|
577
|
+
if (ymlField?.min !== undefined) {
|
|
578
|
+
annotations.push(`@Min(${ymlField.min})`);
|
|
579
|
+
}
|
|
580
|
+
if (ymlField?.max !== undefined) {
|
|
581
|
+
annotations.push(`@Max(${ymlField.max})`);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Pattern validation
|
|
585
|
+
if (ymlField?.pattern) {
|
|
586
|
+
annotations.push(`@Pattern(regexp = "${ymlField.pattern}")`);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return annotations;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const createUniqueFields = getUniqueFieldsForCreate();
|
|
593
|
+
const hasUniqueFields = createUniqueFields.length > 0;
|
|
594
|
+
// Only an entity that declares the soft-delete flag has one; naming it on any other entity makes
|
|
595
|
+
// the uniqueness check query an attribute that does not exist, and every create fails.
|
|
596
|
+
const hasSoftDelete = entityFields.some(f => f.name === 'deleted');
|
|
597
|
+
%>
|
|
598
|
+
@Data<% if (hasUniqueFields) { %>
|
|
599
|
+
@UniqueFields({<%
|
|
600
|
+
createUniqueFields.forEach((uf, index) => { %>
|
|
601
|
+
@UniqueField(entity = <%= entityName %>.class, field = "<%= uf.entityField %>", property = "<%= uf.property %>",
|
|
602
|
+
idField = "<%= ymlConfig.idField %>", idProperty = "<%= ymlConfig.idField %>"<% if (hasSoftDelete) { %>,
|
|
603
|
+
softDeleteField = "deleted", softDeleteType = SoftDeleteType.BOOLEAN<% } %>)<% if (index < createUniqueFields.length - 1) { %>,<% } %><%
|
|
604
|
+
});
|
|
605
|
+
%>
|
|
606
|
+
})<% } %>
|
|
607
|
+
public static class <%= classPrefix %>CreateDTO {
|
|
608
|
+
<% if (hasEmbeddedId) { %>
|
|
609
|
+
<% const idFieldConfig = ymlConfig.fields[ymlConfig.idField]; %>
|
|
610
|
+
<% } %>
|
|
611
|
+
<%
|
|
612
|
+
const createFields = getMergedFieldsForView('edit');
|
|
613
|
+
for (let mergedField of createFields) {
|
|
614
|
+
const field = mergedField;
|
|
615
|
+
const ymlField = mergedField.ymlField;
|
|
616
|
+
if (field.name === ymlConfig.idField) continue;
|
|
617
|
+
|
|
618
|
+
let fieldType = formatDtoFieldType(field, ymlField);
|
|
619
|
+
let fieldName = field.name;
|
|
620
|
+
let comment = ymlField?.comment || (field.comment ? field.comment.split(':')[0] : fieldNameToComment(field.name));
|
|
621
|
+
|
|
622
|
+
// Skip reference/collection handling for inherited fields
|
|
623
|
+
if (!mergedField.isInherited) {
|
|
624
|
+
// For reference field
|
|
625
|
+
if (ymlField?.reference) {
|
|
626
|
+
// Skip if entity already has explicit ID field (e.g., channelId for channel)
|
|
627
|
+
// The explicit ID field will be processed separately
|
|
628
|
+
if (!ymlField.reference.multiple && hasExplicitIdField(field.name)) {
|
|
629
|
+
continue; // Skip this reference field to avoid duplicate
|
|
630
|
+
}
|
|
631
|
+
const collectionType = ymlField.reference.multiple ? 'Set' : null;
|
|
632
|
+
fieldType = collectionType
|
|
633
|
+
? `${collectionType}<${ymlField.reference.idType || 'String'}>`
|
|
634
|
+
: ymlField.reference.idType || 'String';
|
|
635
|
+
// Use helper to avoid 'IdId' pattern when field already ends with 'Id'
|
|
636
|
+
fieldName = getReferenceIdFieldName(field.name, ymlField.reference.multiple);
|
|
637
|
+
// Only append ' ID' to comment if field name doesn't already end with 'Id'
|
|
638
|
+
if (!holdsReferenceKey(field.name)) {
|
|
639
|
+
comment = `${comment} ID`;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
// For collection field
|
|
643
|
+
else if (field.isCollection) {
|
|
644
|
+
if (field.collectionType === 'Map') {
|
|
645
|
+
fieldType = field.type; // Use original type format like Map<String, String>
|
|
646
|
+
} else {
|
|
647
|
+
fieldType = `${field.collectionType || 'List'}<${field.actualType || 'String'}>`;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// Get validation annotations
|
|
653
|
+
const validationAnnotations = getValidationAnnotations(ymlField, fieldType, true);
|
|
654
|
+
%>
|
|
655
|
+
@Schema(description = "<%= comment %>")
|
|
656
|
+
@FieldLabel("{entities.<%= entityName %>.<%= field.name %>}")
|
|
657
|
+
<% validationAnnotations.forEach(annotation => { %> <%= annotation %>
|
|
658
|
+
<% }); %> private <%- fieldType %> <%= fieldName %>;
|
|
659
|
+
<% } %>
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
@Data
|
|
663
|
+
@EqualsAndHashCode(callSuper = true)
|
|
664
|
+
public static class <%= classPrefix %>UpdateDTO extends <%= classPrefix %>CreateDTO {
|
|
665
|
+
<% if (hasEmbeddedId) {
|
|
666
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
667
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %>")
|
|
668
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
669
|
+
@NotNull(message = "ID is required")
|
|
670
|
+
private <%= embeddedIdType %> <%= ymlConfig.idField %>;
|
|
671
|
+
<% } else {
|
|
672
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
673
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %>")
|
|
674
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
675
|
+
@NotBlank(message = "ID is required")
|
|
676
|
+
private <%= idFieldInfo?.type || idType %> <%= ymlConfig.idField %>;
|
|
677
|
+
<% } %>
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
@Data
|
|
681
|
+
@EqualsAndHashCode(callSuper = true)
|
|
682
|
+
public static class <%= classPrefix %>UpdateFormDTO extends <%= classPrefix %>UpdateDTO {
|
|
683
|
+
|
|
684
|
+
@Schema(description = "Created by")
|
|
685
|
+
private String createdBy;
|
|
686
|
+
|
|
687
|
+
@Schema(description = "Created at")
|
|
688
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
689
|
+
private Instant createdAt;
|
|
690
|
+
|
|
691
|
+
@Schema(description = "Updated by")
|
|
692
|
+
private String updatedBy;
|
|
693
|
+
|
|
694
|
+
@Schema(description = "Updated at")
|
|
695
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
696
|
+
private Instant updatedAt;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
@Data
|
|
700
|
+
public static class <%= classPrefix %>BatchUpdateDTO {
|
|
701
|
+
<% if (hasEmbeddedId) {
|
|
702
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
703
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %> list")
|
|
704
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
705
|
+
private Set<<%= embeddedIdType %>> <%= ymlConfig.idField %>s;
|
|
706
|
+
<% } else {
|
|
707
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
708
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %> list")
|
|
709
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
710
|
+
private Set<<%= idFieldInfo?.type || idType %>> <%= ymlConfig.idField %>s;
|
|
711
|
+
<% } %>
|
|
712
|
+
<%
|
|
713
|
+
const batchUpdateFields = getMergedFieldsForView('batchUpdate');
|
|
714
|
+
for (let mergedField of batchUpdateFields) {
|
|
715
|
+
const field = mergedField;
|
|
716
|
+
const ymlField = mergedField.ymlField;
|
|
717
|
+
if (field.name === ymlConfig.idField) continue;
|
|
718
|
+
|
|
719
|
+
let fieldType = formatDtoFieldType(field, ymlField);
|
|
720
|
+
let fieldName = field.name;
|
|
721
|
+
let comment = ymlField?.comment || (field.comment ? field.comment.split(':')[0] : fieldNameToComment(field.name));
|
|
722
|
+
|
|
723
|
+
// Skip reference/collection handling for inherited fields
|
|
724
|
+
if (!mergedField.isInherited) {
|
|
725
|
+
// For reference field
|
|
726
|
+
if (ymlField?.reference) {
|
|
727
|
+
// Skip if entity already has explicit ID field (e.g., channelId for channel)
|
|
728
|
+
// The explicit ID field will be processed separately
|
|
729
|
+
if (!ymlField.reference.multiple && hasExplicitIdField(field.name)) {
|
|
730
|
+
continue; // Skip this reference field to avoid duplicate
|
|
731
|
+
}
|
|
732
|
+
const collectionType = ymlField.reference.multiple ? 'Set' : null;
|
|
733
|
+
fieldType = collectionType
|
|
734
|
+
? `${collectionType}<${ymlField.reference.idType || 'String'}>`
|
|
735
|
+
: ymlField.reference.idType || 'String';
|
|
736
|
+
// Use helper to avoid 'IdId' pattern when field already ends with 'Id'
|
|
737
|
+
fieldName = getReferenceIdFieldName(field.name, ymlField.reference.multiple);
|
|
738
|
+
// Only append ' ID' to comment if field name doesn't already end with 'Id'
|
|
739
|
+
if (!holdsReferenceKey(field.name)) {
|
|
740
|
+
comment = `${comment} ID`;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
// For collection field
|
|
744
|
+
else if (field.isCollection) {
|
|
745
|
+
if (field.collectionType === 'Map') {
|
|
746
|
+
fieldType = field.type; // Use original type format like Map<String, String>
|
|
747
|
+
} else {
|
|
748
|
+
fieldType = `${field.collectionType || 'List'}<${field.actualType || 'String'}>`;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
%>
|
|
753
|
+
@Schema(description = "<%= comment %>")
|
|
754
|
+
@FieldLabel("{entities.<%= entityName %>.<%= field.name %>}")
|
|
755
|
+
private <%- fieldType %> <%= fieldName %>;
|
|
756
|
+
<% } %>
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
//----------------------------------
|
|
760
|
+
// Projection
|
|
761
|
+
//----------------------------------
|
|
762
|
+
|
|
763
|
+
@Data
|
|
764
|
+
public static class <%= classPrefix %>DetailDTO {
|
|
765
|
+
<%
|
|
766
|
+
const detailFields = getMergedFieldsForView('detail');
|
|
767
|
+
// Track already processed field names to avoid duplicates
|
|
768
|
+
const processedDetailFieldNames = new Set();
|
|
769
|
+
|
|
770
|
+
for (let mergedField of detailFields) {
|
|
771
|
+
const field = mergedField;
|
|
772
|
+
const ymlField = mergedField.ymlField;
|
|
773
|
+
|
|
774
|
+
let fieldType = formatDtoFieldType(field, ymlField);
|
|
775
|
+
let fieldName = field.name;
|
|
776
|
+
let comment = ymlField?.comment || (field.comment ? field.comment.split(':')[0] : fieldNameToComment(field.name));
|
|
777
|
+
|
|
778
|
+
// For reference fields, check if we need JsonIncludeProperties for Hibernate proxy
|
|
779
|
+
// Ensure we don't treat standard Java types or String FK fields as reference fields
|
|
780
|
+
// String FK fields (e.g., contentId) should not have @JsonIncludeProperties
|
|
781
|
+
const isYmlReferenceField = ymlField?.reference && !ymlField.reference.multiple &&
|
|
782
|
+
!isStandardJavaType(fieldType, field.importPath) &&
|
|
783
|
+
fieldType !== 'String' && !fieldNameEndsWithId(fieldName);
|
|
784
|
+
|
|
785
|
+
// Check if Entity field has @JsonIncludeProperties annotation (ManyToOne objects)
|
|
786
|
+
const hasEntityJsonIncludeProps = field.jsonIncludeProperties && field.jsonIncludeProperties.length > 0;
|
|
787
|
+
const isManyToOneObject = field.relationType === 'ManyToOne' && field.typeKind === 'entity' &&
|
|
788
|
+
!isStandardJavaType(fieldType, field.importPath);
|
|
789
|
+
|
|
790
|
+
// Determine @JsonIncludeProperties value
|
|
791
|
+
let jsonIncludePropsValue = null;
|
|
792
|
+
if (hasEntityJsonIncludeProps) {
|
|
793
|
+
// Use Entity's @JsonIncludeProperties value
|
|
794
|
+
jsonIncludePropsValue = field.jsonIncludeProperties.map(p => `"${p}"`).join(', ');
|
|
795
|
+
} else if (isYmlReferenceField) {
|
|
796
|
+
// Use YML reference idField
|
|
797
|
+
jsonIncludePropsValue = `"${ymlField.reference.idField}"`;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
const isReferenceField = isYmlReferenceField || hasEntityJsonIncludeProps || isManyToOneObject;
|
|
801
|
+
|
|
802
|
+
// Skip if already processed (avoid duplicates)
|
|
803
|
+
if (processedDetailFieldNames.has(fieldName)) continue;
|
|
804
|
+
processedDetailFieldNames.add(fieldName);
|
|
805
|
+
|
|
806
|
+
// Skip collection handling for inherited fields
|
|
807
|
+
if (!mergedField.isInherited) {
|
|
808
|
+
// For collection field (skip for reference fields already handled)
|
|
809
|
+
if (field.isCollection && !ymlField?.reference) {
|
|
810
|
+
if (field.collectionType === 'Map') {
|
|
811
|
+
fieldType = field.type; // Use original type format like Map<String, String>
|
|
812
|
+
} else {
|
|
813
|
+
fieldType = `${field.collectionType || 'List'}<${field.actualType || 'String'}>`;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Check for i18n fields
|
|
819
|
+
const i18nSourceField = i18nFieldMap.get(fieldName);
|
|
820
|
+
const isI18nField = fieldName.endsWith('I18n');
|
|
821
|
+
%>
|
|
822
|
+
@Schema(description = "<%= comment %>")
|
|
823
|
+
<% if (isReferenceField && jsonIncludePropsValue) { %> @JsonIncludeProperties({<%= jsonIncludePropsValue %>})
|
|
824
|
+
<% } %><% if (i18nSourceField) { %> @I18nTrans(source="<%= i18nSourceField %>")
|
|
825
|
+
<% } %><% if (isI18nField) { %> @JsonIgnore
|
|
826
|
+
<% } %> private <%- fieldType %> <%= fieldName %>;
|
|
827
|
+
<% } %>
|
|
828
|
+
|
|
829
|
+
//----------
|
|
830
|
+
|
|
831
|
+
@Schema(description = "Created by")
|
|
832
|
+
private String createdBy;
|
|
833
|
+
|
|
834
|
+
@Schema(description = "Created at")
|
|
835
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
836
|
+
private Instant createdAt;
|
|
837
|
+
|
|
838
|
+
@Schema(description = "Updated by")
|
|
839
|
+
private String updatedBy;
|
|
840
|
+
|
|
841
|
+
@Schema(description = "Updated at")
|
|
842
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
843
|
+
private Instant updatedAt;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
@Data
|
|
847
|
+
public static class <%= classPrefix %>ListDTO {
|
|
848
|
+
<%
|
|
849
|
+
const listFields = getMergedFieldsForView('list');
|
|
850
|
+
// Track already processed field names to avoid duplicates
|
|
851
|
+
const processedListFieldNames = new Set();
|
|
852
|
+
|
|
853
|
+
for (let mergedField of listFields) {
|
|
854
|
+
const field = mergedField;
|
|
855
|
+
const ymlField = mergedField.ymlField;
|
|
856
|
+
|
|
857
|
+
let fieldType = formatDtoFieldType(field, ymlField);
|
|
858
|
+
let fieldName = field.name;
|
|
859
|
+
let comment = ymlField?.comment || (field.comment ? field.comment.split(':')[0] : fieldNameToComment(field.name));
|
|
860
|
+
|
|
861
|
+
// For reference fields, check if we need JsonIncludeProperties for Hibernate proxy
|
|
862
|
+
// Ensure we don't treat standard Java types or String FK fields as reference fields
|
|
863
|
+
// String FK fields (e.g., contentId) should not have @JsonIncludeProperties
|
|
864
|
+
const isYmlReferenceField = ymlField?.reference && !ymlField.reference.multiple &&
|
|
865
|
+
!isStandardJavaType(fieldType, field.importPath) &&
|
|
866
|
+
fieldType !== 'String' && !fieldNameEndsWithId(fieldName);
|
|
867
|
+
|
|
868
|
+
// Check if Entity field has @JsonIncludeProperties annotation (ManyToOne objects)
|
|
869
|
+
const hasEntityJsonIncludeProps = field.jsonIncludeProperties && field.jsonIncludeProperties.length > 0;
|
|
870
|
+
const isManyToOneObject = field.relationType === 'ManyToOne' && field.typeKind === 'entity' &&
|
|
871
|
+
!isStandardJavaType(fieldType, field.importPath);
|
|
872
|
+
|
|
873
|
+
// Determine @JsonIncludeProperties value
|
|
874
|
+
let jsonIncludePropsValue = null;
|
|
875
|
+
if (hasEntityJsonIncludeProps) {
|
|
876
|
+
// Use Entity's @JsonIncludeProperties value
|
|
877
|
+
jsonIncludePropsValue = field.jsonIncludeProperties.map(p => `"${p}"`).join(', ');
|
|
878
|
+
} else if (isYmlReferenceField) {
|
|
879
|
+
// Use YML reference idField
|
|
880
|
+
jsonIncludePropsValue = `"${ymlField.reference.idField}"`;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
const isReferenceField = isYmlReferenceField || hasEntityJsonIncludeProps || isManyToOneObject;
|
|
884
|
+
|
|
885
|
+
// Skip if already processed (avoid duplicates)
|
|
886
|
+
if (processedListFieldNames.has(fieldName)) continue;
|
|
887
|
+
processedListFieldNames.add(fieldName);
|
|
888
|
+
|
|
889
|
+
// Skip collection handling for inherited fields
|
|
890
|
+
if (!mergedField.isInherited) {
|
|
891
|
+
// For collection field (skip for reference fields already handled)
|
|
892
|
+
if (field.isCollection && !ymlField?.reference) {
|
|
893
|
+
if (field.collectionType === 'Map') {
|
|
894
|
+
fieldType = field.type; // Use original type format like Map<String, String>
|
|
895
|
+
} else {
|
|
896
|
+
fieldType = `${field.collectionType || 'List'}<${field.actualType || 'String'}>`;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// Check for i18n fields
|
|
902
|
+
const i18nSourceField = i18nFieldMap.get(fieldName);
|
|
903
|
+
const isI18nField = fieldName.endsWith('I18n');
|
|
904
|
+
%>
|
|
905
|
+
@Schema(description = "<%= comment %>")
|
|
906
|
+
<% if (isReferenceField && jsonIncludePropsValue) { %> @JsonIncludeProperties({<%= jsonIncludePropsValue %>})
|
|
907
|
+
<% } %><% if (i18nSourceField) { %> @I18nTrans(source="<%= i18nSourceField %>")
|
|
908
|
+
<% } %><% if (isI18nField) { %> @JsonIgnore
|
|
909
|
+
<% } %> private <%- fieldType %> <%= fieldName %>;
|
|
910
|
+
<%
|
|
911
|
+
// If this base field has a corresponding I18n field, add it automatically
|
|
912
|
+
if (i18nSourceField && !processedListFieldNames.has(i18nSourceField)) {
|
|
913
|
+
processedListFieldNames.add(i18nSourceField);
|
|
914
|
+
const i18nField = entityFields.find(f => f.name === i18nSourceField);
|
|
915
|
+
const i18nComment = ymlConfig.fields?.[i18nSourceField]?.comment || (i18nField?.comment ? i18nField.comment.split(':')[0] : fieldNameToComment(i18nSourceField));
|
|
916
|
+
%>
|
|
917
|
+
@Schema(description = "<%= i18nComment %>")
|
|
918
|
+
@JsonIgnore
|
|
919
|
+
private Map<String, String> <%= i18nSourceField %>;
|
|
920
|
+
<% }
|
|
921
|
+
} %>
|
|
922
|
+
|
|
923
|
+
//----------
|
|
924
|
+
|
|
925
|
+
@Schema(description = "Created by")
|
|
926
|
+
private String createdBy;
|
|
927
|
+
|
|
928
|
+
@Schema(description = "Created at")
|
|
929
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
930
|
+
private Instant createdAt;
|
|
931
|
+
|
|
932
|
+
@Schema(description = "Updated by")
|
|
933
|
+
private String updatedBy;
|
|
934
|
+
|
|
935
|
+
@Schema(description = "Updated at")
|
|
936
|
+
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
|
937
|
+
private Instant updatedAt;
|
|
938
|
+
<% if (isTreeEntity) { %>
|
|
939
|
+
|
|
940
|
+
//----------
|
|
941
|
+
// Tree Structure
|
|
942
|
+
|
|
943
|
+
@Schema(description = "Child nodes in tree hierarchy")
|
|
944
|
+
private List<<%= classPrefix %>ListDTO> children;
|
|
945
|
+
<% } %>
|
|
946
|
+
}
|
|
947
|
+
<% if (hasOrderField) {
|
|
948
|
+
const orderFieldName = orderField.name;
|
|
949
|
+
const orderFieldComment = orderField.comment ? orderField.comment.split(':')[0] : fieldNameToComment(orderFieldName);
|
|
950
|
+
const orderFieldType = orderField.type === 'int' ? 'Integer' : (orderField.type === 'long' ? 'Long' : orderField.type);
|
|
951
|
+
%>
|
|
952
|
+
|
|
953
|
+
//----------------------------------
|
|
954
|
+
// Order Update
|
|
955
|
+
//----------------------------------
|
|
956
|
+
|
|
957
|
+
/**
|
|
958
|
+
* DTO for updating the display order of <%= entityName %> entities.
|
|
959
|
+
*/
|
|
960
|
+
@Data
|
|
961
|
+
public static class <%= classPrefix %>OrderUpdateDTO {
|
|
962
|
+
<% if (hasEmbeddedId) {
|
|
963
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
964
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %>")
|
|
965
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
966
|
+
@NotNull(message = "ID is required")
|
|
967
|
+
private <%= embeddedIdType %> <%= ymlConfig.idField %>;
|
|
968
|
+
<% } else {
|
|
969
|
+
const idFieldInfo = entitySearchFields.find(f => f.name === ymlConfig.idField); %>
|
|
970
|
+
@Schema(description = "<%= idFieldInfo?.comment || `${entityName} ID` %>")
|
|
971
|
+
@FieldLabel("{entities.<%= entityName %>.<%= ymlConfig.idField %>}")
|
|
972
|
+
@NotBlank(message = "ID is required")
|
|
973
|
+
private <%= idFieldInfo?.type || idType %> <%= ymlConfig.idField %>;
|
|
974
|
+
<% } %>
|
|
975
|
+
|
|
976
|
+
@Schema(description = "<%= orderFieldComment %>")
|
|
977
|
+
@FieldLabel("{entities.<%= entityName %>.<%= orderFieldName %>}")
|
|
978
|
+
private <%= orderFieldType %> <%= orderFieldName %>;
|
|
979
|
+
}
|
|
980
|
+
<% } %>
|
|
981
|
+
}
|