@simplix-boot/cli 0.0.0-stage → 2.0.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.
Files changed (54) hide show
  1. package/LICENSE +25 -0
  2. package/LICENSE_KO +28 -0
  3. package/README.md +144 -2
  4. package/bin/simplix-boot.js +4 -0
  5. package/package.json +44 -4
  6. package/src/cli.js +103 -0
  7. package/src/commands/config.js +129 -0
  8. package/src/commands/generate.js +291 -0
  9. package/src/commands/generated.js +125 -0
  10. package/src/commands/init.js +41 -0
  11. package/src/commands/promote.js +172 -0
  12. package/src/commands/templates.js +100 -0
  13. package/src/commands/version.js +134 -0
  14. package/src/lib/analysis/fields.js +217 -0
  15. package/src/lib/analysis/imports.js +76 -0
  16. package/src/lib/analysis/structure.js +193 -0
  17. package/src/lib/analysis/types.js +94 -0
  18. package/src/lib/args.js +69 -0
  19. package/src/lib/comment-extractor.js +141 -0
  20. package/src/lib/config/fields.js +321 -0
  21. package/src/lib/config/parents.js +108 -0
  22. package/src/lib/config/paths.js +156 -0
  23. package/src/lib/config/yml.js +61 -0
  24. package/src/lib/console.js +70 -0
  25. package/src/lib/controller-name.js +33 -0
  26. package/src/lib/entity-analyzer.js +49 -0
  27. package/src/lib/entity-type-resolver.js +80 -0
  28. package/src/lib/entity-type.js +35 -0
  29. package/src/lib/errors.js +17 -0
  30. package/src/lib/export-config.js +74 -0
  31. package/src/lib/file-utils.js +77 -0
  32. package/src/lib/generate/file-plan.js +155 -0
  33. package/src/lib/generate/search-fields.js +111 -0
  34. package/src/lib/generate/settings.js +111 -0
  35. package/src/lib/generate/support-classes.js +47 -0
  36. package/src/lib/generate/template-vars.js +183 -0
  37. package/src/lib/git-utils.js +18 -0
  38. package/src/lib/name-field.js +42 -0
  39. package/src/lib/project.js +121 -0
  40. package/src/lib/promote-files.js +132 -0
  41. package/src/lib/rendered-output.js +13 -0
  42. package/src/lib/search-operators.js +87 -0
  43. package/src/lib/templates.js +50 -0
  44. package/src/lib/tracked-render.js +200 -0
  45. package/src/lib/version-check.js +194 -0
  46. package/src/lib/version-utils.js +89 -0
  47. package/templates/controller/rest/EntityRestController.java.template +517 -0
  48. package/templates/controller/web/EntityWebController.java.template +211 -0
  49. package/templates/dto/EntityDTOs.java.template +981 -0
  50. package/templates/repository/EntityRepository.java.template +78 -0
  51. package/templates/service/EntityService.java.template +790 -0
  52. package/templates/service/EntityTreeService.java.template +352 -0
  53. package/templates/test/EntityServiceTest.java.template +937 -0
  54. package/templates/test/EntityTreeServiceTest.java.template +842 -0
@@ -0,0 +1,352 @@
1
+ package <%= packagePaths['service'] %>;
2
+
3
+ import <%= packagePaths['dto'] %>.<%= classPrefix %>DTOs.*;
4
+ import <%= entityPackage %>.<%= entityName %>;
5
+ <% if (hasEmbeddedId) { %>
6
+ import <%= entityPackage %>.<%= entityName %>.<%= embeddedIdType %>;
7
+ <% } %>
8
+ <%
9
+ // Parse import statements for reference entities (excluding self-reference)
10
+ // Only import entity classes for immutable reference field validation in updateNode
11
+ const importedEntities = new Set();
12
+
13
+ if (ymlConfig && ymlConfig.fields) {
14
+ Object.entries(ymlConfig.fields).forEach(([fieldName, ymlField]) => {
15
+ if (ymlField.reference && ymlField.reference.entity && ymlField.reference.entity !== entityName) {
16
+ const refEntity = ymlField.reference.entity;
17
+ const refModulePath = ymlField.reference.modulePath;
18
+
19
+ // Import entity class for immutable reference fields (used in updateNode validation)
20
+ if (ymlField.immutable && !importedEntities.has(refEntity)) {
21
+ importedEntities.add(refEntity);
22
+ if (refModulePath) {
23
+ const entityPackagePath = basePackage.replace('.api', '') + '.domain.entity.' + refModulePath.replace(/\//g, '.');
24
+ %>
25
+ import <%= entityPackagePath %>.<%= refEntity %>;<%
26
+ }
27
+ }
28
+ }
29
+ });
30
+ } %>
31
+
32
+ import dev.simplecore.simplix.core.exception.ErrorCode;
33
+ import dev.simplecore.simplix.core.exception.SimpliXGeneralException;
34
+ import dev.simplecore.simplix.core.tree.repository.SimpliXTreeRepository;
35
+ import dev.simplecore.simplix.core.tree.service.SimpliXTreeBaseService;
36
+ import org.modelmapper.ModelMapper;
37
+ import org.springframework.context.MessageSource;
38
+ import org.springframework.context.i18n.LocaleContextHolder;
39
+ import org.springframework.stereotype.Service;
40
+ import org.springframework.transaction.annotation.Transactional;
41
+
42
+ import java.util.*;
43
+ import java.util.stream.Collectors;
44
+
45
+ /**
46
+ * Tree service class for managing <%= entityName %> entities.
47
+ * <p>
48
+ * Provides tree-specific CRUD operations, including parent-child relationship management,
49
+ * tree traversal, and sibling reordering for <%= entityName %> entities.
50
+ * <p>
51
+ * This service extends SimpliXTreeBaseService which provides:
52
+ * <ul>
53
+ * <li>Tree structure retrieval (findRoots, findDirectChildren, getAllDescendants)</li>
54
+ * <li>Tree traversal (findAncestors, getPath)</li>
55
+ * <li>Tree manipulation (move, copy)</li>
56
+ * </ul>
57
+ <% if (hasEmbeddedId) { %>
58
+ * This entity uses a composite key (<%= embeddedIdType %>).
59
+ <% } %>
60
+ *
61
+ * @see SimpliXTreeBaseService
62
+ * @see <%= classPrefix %>Service
63
+ */
64
+ @Service
65
+ @Transactional(readOnly = true)
66
+ public class <%= classPrefix %>TreeService extends SimpliXTreeBaseService<<%= entityName %>, <%= idType %>> {
67
+
68
+ protected final ModelMapper modelMapper;
69
+ private final MessageSource messageSource;
70
+
71
+ /**
72
+ * Constructs a new <%= classPrefix %>TreeService with required dependencies.
73
+ *
74
+ * @param treeRepository The tree repository for <%= entityName %> entities
75
+ * @param modelMapper ModelMapper for DTO-Entity mapping
76
+ * @param messageSource Message source for i18n error messages
77
+ */
78
+ public <%= classPrefix %>TreeService(
79
+ SimpliXTreeRepository<<%= entityName %>, <%= idType %>> treeRepository,
80
+ ModelMapper modelMapper,
81
+ MessageSource messageSource
82
+ ) {
83
+ super(treeRepository);
84
+ this.modelMapper = modelMapper;
85
+ this.messageSource = messageSource;
86
+ }
87
+
88
+ //----------------------------------
89
+ // CRUD Operations for Tree Nodes
90
+ //----------------------------------
91
+
92
+ /**
93
+ * Creates a new <%= entityName %> tree node from the provided DTO.
94
+ * <p>
95
+ * Handles empty parentId conversion to null for root nodes.
96
+ * FK ID fields are mapped directly by ModelMapper from DTO to entity.
97
+ *
98
+ * @param createDTO DTO containing the data for the new <%= entityName %>
99
+ * @return <%= classPrefix %>DetailDTO of the created entity
100
+ */
101
+ @Transactional
102
+ public <%= classPrefix %>DetailDTO createNode(<%= classPrefix %>CreateDTO createDTO) {
103
+ <%= entityName %> entity = new <%= entityName %>();
104
+ modelMapper.map(createDTO, entity);
105
+
106
+ // Handle empty parentId - convert to null for root nodes
107
+ entity.setParentId(normalizeParentId(entity.getParentId()));
108
+
109
+ <%= entityName %> savedEntity = create(entity);
110
+ return modelMapper.map(savedEntity, <%= classPrefix %>DetailDTO.class);
111
+ }
112
+
113
+ /**
114
+ * Updates an existing <%= entityName %> tree node with the provided DTO data.
115
+ * <p>
116
+ * Handles empty parentId conversion to null for root nodes.
117
+ * Validates tree structure integrity (circular reference prevention, parent existence).
118
+ *
119
+ * @param entity The existing <%= entityName %> entity to update
120
+ * @param updateDto DTO containing the updated data
121
+ * @return <%= classPrefix %>DetailDTO of the updated entity
122
+ * @throws SimpliXGeneralException if ID is changed, circular reference detected, or related entities are not found
123
+ */
124
+ @Transactional
125
+ public <%= classPrefix %>DetailDTO updateNode(<%= entityName %> entity, <%= classPrefix %>UpdateDTO updateDto) {
126
+ if (!Objects.equals(entity.get<%= capitalizeFirstLetter(ymlConfig.idField) %>(), updateDto.get<%= capitalizeFirstLetter(ymlConfig.idField) %>())) {
127
+ throw new SimpliXGeneralException(ErrorCode.GEN_CONFLICT, "{error.id.cannot.change}", null);
128
+ }
129
+ <%
130
+ // Generate immutable reference field validation (BEFORE modelMapper.map)
131
+ Object.entries(ymlConfig.fields || {}).forEach(([fieldName, field]) => {
132
+ if (field.reference && !field.reference.multiple && field.reference.entity !== entityName && field.immutable) {
133
+ const upperFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
134
+ const idFieldName = field.reference.idField;
135
+ const upperIdFieldName = idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1);
136
+ %>
137
+
138
+ // Validate <%= fieldName %> cannot be changed after creation (immutable)
139
+ <%= field.reference.entity %> existing<%= upperFieldName %> = entity.get<%= upperFieldName %>();
140
+ <%= field.reference.idType || 'String' %> existing<%= upperFieldName %>Id = existing<%= upperFieldName %> != null ? existing<%= upperFieldName %>.get<%= upperIdFieldName %>() : null;
141
+ if (!Objects.equals(existing<%= upperFieldName %>Id, updateDto.get<%= upperIdFieldName %>())) {
142
+ throw new SimpliXGeneralException(ErrorCode.GEN_CONFLICT, "{error.<%= _.snakeCase(fieldName) %>.cannot.change}", null);
143
+ }
144
+ <%
145
+ }
146
+ });
147
+ %>
148
+
149
+ // Preserve existing parentId for comparison
150
+ <%= idType %> existingParentId = entity.getParentId();
151
+
152
+ // Normalize new parentId
153
+ final <%= idType %> newParentId = normalizeParentId(updateDto.getParentId());
154
+
155
+ // Validate parentId change with tree structure validation
156
+ if (!Objects.equals(existingParentId, newParentId)) {
157
+ if (newParentId != null) {
158
+ // Validate new parent exists
159
+ findById(newParentId).orElseThrow(() -> {
160
+ String message = messageSource.getMessage("error.entity.not.found",
161
+ new Object[]{"Parent", newParentId},
162
+ "Parent not found", LocaleContextHolder.getLocale());
163
+ return new SimpliXGeneralException(ErrorCode.GEN_NOT_FOUND, message, null);
164
+ });
165
+
166
+ // Check for circular reference using base service utility method
167
+ validateNoCircularReference(entity.get<%= capitalizeFirstLetter(ymlConfig.idField) %>(), newParentId,
168
+ () -> new SimpliXGeneralException(ErrorCode.VAL_VALIDATION_FAILED, "{error.tree.circular.reference}", null));
169
+ }
170
+ }
171
+
172
+ modelMapper.map(updateDto, entity);
173
+ <%
174
+ // Generate immutable reference field restoration (AFTER modelMapper.map)
175
+ Object.entries(ymlConfig.fields || {}).forEach(([fieldName, field]) => {
176
+ if (field.reference && !field.reference.multiple && field.reference.entity !== entityName && field.immutable) {
177
+ const upperFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
178
+ %>
179
+
180
+ // Restore <%= fieldName %> (immutable after creation)
181
+ entity.set<%= upperFieldName %>(existing<%= upperFieldName %>);
182
+ <%
183
+ }
184
+ });
185
+ %>
186
+
187
+ // Set validated parentId (overwrite whatever modelMapper set)
188
+ entity.setParentId(newParentId);
189
+
190
+ <%= entityName %> savedEntity = update(entity);
191
+ return modelMapper.map(savedEntity, <%= classPrefix %>DetailDTO.class);
192
+ }
193
+
194
+ /**
195
+ * Deletes a <%= entityName %> tree node by its ID.
196
+ * <p>
197
+ * This operation will fail if the node has children. Move or delete children first.
198
+ * Caller should verify entity exists before calling this method.
199
+ *
200
+ * @param <%= ymlConfig.idField %> The ID of the <%= entityName %> to delete
201
+ * @throws SimpliXGeneralException if the node has children
202
+ */
203
+ @Transactional
204
+ public void deleteNode(<%= idType %> <%= ymlConfig.idField %>) {
205
+ // Check if node has children using base service utility method
206
+ validateNoChildren(<%= ymlConfig.idField %>, () -> {
207
+ String message = messageSource.getMessage("error.tree.has.children",
208
+ null, "Cannot delete node with children. Delete or move children first.",
209
+ LocaleContextHolder.getLocale());
210
+ return new SimpliXGeneralException(ErrorCode.VAL_VALIDATION_FAILED, message, null);
211
+ });
212
+
213
+ deleteById(<%= ymlConfig.idField %>);
214
+ }
215
+
216
+ /**
217
+ * Deletes multiple <%= entityName %> tree nodes by their IDs.
218
+ * <p>
219
+ * Validates that none of the nodes have children before deletion.
220
+ *
221
+ * @param <%= ymlConfig.idField %>s List of <%= entityName %> IDs to delete
222
+ * @throws SimpliXGeneralException if any node has children
223
+ */
224
+ @Transactional
225
+ public void batchDeleteNodes(List<<%= idType %>> <%= ymlConfig.idField %>s) {
226
+ // Validate none have children using base service utility method
227
+ for (<%= idType %> <%= ymlConfig.idField %> : <%= ymlConfig.idField %>s) {
228
+ validateNoChildren(<%= ymlConfig.idField %>, () -> {
229
+ String message = messageSource.getMessage("error.tree.has.children",
230
+ new Object[]{<%= ymlConfig.idField %>},
231
+ "Cannot delete node " + <%= ymlConfig.idField %> + " with children.",
232
+ LocaleContextHolder.getLocale());
233
+ return new SimpliXGeneralException(ErrorCode.VAL_VALIDATION_FAILED, message, null);
234
+ });
235
+ }
236
+ deleteBatch(<%= ymlConfig.idField %>s);
237
+ }
238
+
239
+ //----------------------------------
240
+ // Tree-Specific Operations
241
+ //----------------------------------
242
+
243
+ /**
244
+ * Retrieves the tree structure starting from root nodes.
245
+ *
246
+ * @param fullTree If true, returns full tree with all descendants; if false, returns only root nodes
247
+ * @return List of <%= classPrefix %>ListDTO representing the tree structure
248
+ */
249
+ public List<<%= classPrefix %>ListDTO> findTree(boolean fullTree) {
250
+ return findTree(null, fullTree);
251
+ }
252
+
253
+ /**
254
+ * Retrieves the tree structure starting from a specific node.
255
+ *
256
+ * @param parentId The root node ID to start from (null for top-level roots)
257
+ * @param fullTree If true, returns full subtree with all descendants; if false, returns only immediate children
258
+ * @return List of <%= classPrefix %>ListDTO representing the tree structure
259
+ */
260
+ public List<<%= classPrefix %>ListDTO> findTree(<%= idType %> parentId, boolean fullTree) {
261
+ // Handle empty parentId using base service utility method
262
+ parentId = normalizeParentId(parentId);
263
+
264
+ List<<%= entityName %>> nodes;
265
+ if (fullTree) {
266
+ // Return full tree with all descendants (already structured with children)
267
+ if (parentId == null) {
268
+ nodes = findCompleteHierarchy();
269
+ } else {
270
+ nodes = getAllDescendants(parentId);
271
+ }
272
+ // Convert to DTOs preserving tree structure
273
+ return mapToTreeDtoWithModelMapper(nodes);
274
+ } else {
275
+ // Return only immediate children (or roots) - flat list
276
+ nodes = parentId == null ? findRoots() : findDirectChildren(parentId);
277
+ return nodes.stream()
278
+ .map(node -> modelMapper.map(node, <%= classPrefix %>ListDTO.class))
279
+ .collect(Collectors.toList());
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Converts entity tree to DTO tree using ModelMapper.
285
+ * <p>
286
+ * Uses base class utility method with ModelMapper for entity-to-DTO conversion.
287
+ *
288
+ * @param entities List of entities with children populated
289
+ * @return List of DTOs with children populated
290
+ */
291
+ private List<<%= classPrefix %>ListDTO> mapToTreeDtoWithModelMapper(List<<%= entityName %>> entities) {
292
+ return mapToTreeDto(
293
+ entities,
294
+ entity -> modelMapper.map(entity, <%= classPrefix %>ListDTO.class),
295
+ <%= entityName %>::getChildren,
296
+ <%= classPrefix %>ListDTO::setChildren
297
+ );
298
+ }
299
+ <% if (hasOrderField) {
300
+ const orderFieldName = orderField.name;
301
+ const upperOrderFieldName = orderFieldName.charAt(0).toUpperCase() + orderFieldName.slice(1);
302
+ const upperIdFieldName = ymlConfig.idField.charAt(0).toUpperCase() + ymlConfig.idField.slice(1);
303
+ %>
304
+
305
+ /**
306
+ * Updates the order of sibling nodes under a specific parent.
307
+ * <p>
308
+ * This method reorders children nodes within the same parent. Use null parentId for root nodes.
309
+ *
310
+ * @param parentId The parent node ID (null for root level)
311
+ * @param orderUpdateDtos List of DTOs containing the IDs and new order values
312
+ * @return List of <%= classPrefix %>ListDTO of the reordered entities
313
+ * @throws SimpliXGeneralException if any entity doesn't belong to the specified parent
314
+ */
315
+ @Transactional
316
+ public List<<%= classPrefix %>ListDTO> updateOrders(<%= idType %> parentId, List<<%= classPrefix %>OrderUpdateDTO> orderUpdateDtos) {
317
+ // Handle empty parentId - convert to null for root nodes
318
+ parentId = normalizeParentId(parentId);
319
+
320
+ List<<%= classPrefix %>ListDTO> updatedEntities = new ArrayList<>();
321
+
322
+ for (<%= classPrefix %>OrderUpdateDTO orderUpdateDto : orderUpdateDtos) {
323
+ if (orderUpdateDto.get<%= upperIdFieldName %>() == null) {
324
+ continue;
325
+ }
326
+
327
+ Optional<<%= entityName %>> entityOpt = findById(orderUpdateDto.get<%= upperIdFieldName %>());
328
+ if (entityOpt.isEmpty()) {
329
+ continue; // Skip non-existent entities
330
+ }
331
+
332
+ <%= entityName %> entity = entityOpt.get();
333
+
334
+ // Validate that the entity belongs to the specified parent
335
+ if (!Objects.equals(entity.getParentId(), parentId)) {
336
+ String message = messageSource.getMessage("error.tree.invalid.parent",
337
+ new Object[]{orderUpdateDto.get<%= upperIdFieldName %>(), parentId},
338
+ "Node " + orderUpdateDto.get<%= upperIdFieldName %>() + " does not belong to parent " + parentId,
339
+ LocaleContextHolder.getLocale());
340
+ throw new SimpliXGeneralException(ErrorCode.VAL_VALIDATION_FAILED, message, null);
341
+ }
342
+
343
+ entity.set<%= upperOrderFieldName %>(orderUpdateDto.get<%= upperOrderFieldName %>());
344
+ <%= entityName %> savedEntity = update(entity);
345
+
346
+ updatedEntities.add(modelMapper.map(savedEntity, <%= classPrefix %>ListDTO.class));
347
+ }
348
+
349
+ return updatedEntities;
350
+ }
351
+ <% } %>
352
+ }