Skip to content

Commit

Permalink
Backend: support for empty input suffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Mar 28, 2023
1 parent a1987b3 commit fe11485
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 31 deletions.
17 changes: 9 additions & 8 deletions plugins/jdl-backend-application-default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ This generator supports the following JDL extensions:

- if any entity is annotated with @aggregate then the following table applies:

| **Annotation** | **Entity** | **@Persistence** | **Repository** | **Id** |
|:--------------------------------|------------|:-----------------|:---------------|:-------|
| **entity** | yes | yes | | yes |
| **@aggregate** | yes | yes | yes | yes |
| **@embedded** | yes | yes | | |
| **@vo** | yes | | | |
| **@searchCriteria(entityName)** | | | | |
| **@skip** | no | | | |
| **Annotation** | **Entity** | **Inbound DTO** | **@Persistence** | **Repository** | **Id** |
|:--------------------------------|------------|----------------------|:-----------------|:---------------|:---------|
| **entity** | yes | | yes | | yes |
| **@aggregate** | yes | | yes | yes | yes |
| **@embedded** | yes | | yes | | only jpa |
| **@vo** | yes | | | | |
| **@input** | no | yes | | | |
| **@searchCriteria(entityName)** | | yes (for entityName) | | | |
| **@skip** | no | | | | |

- **@searchCriteria(entityName)** is used to specify the entity name for the search criteria, if empty will take the same fields as the actual entity.
- **@skip** entities used as search criteria should be marked with @skip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ boolean is(Map<String, Object> model, String... annotations) {
new Object[] {"src/main/java", "core/inbound/dtos/EntityCriteria.java", "core/inbound/dtos/{{criteriaClassName entity }}.java", JAVA, skipSearchCriteria},
new Object[] {"src/main/java", "core/inbound/dtos/EntityInput.java", "core/inbound/dtos/{{entity.className}}{{inputDTOSuffix entity}}.java", JAVA, skipEntityInput},
new Object[] {"src/main/java", "core/inbound/dtos/EntityInput.java", "core/inbound/dtos/{{entity.className}}{{inputDTOSuffix entity}}.java", JAVA, skipInput},
new Object[] {"src/main/java", "core/implementation/mappers/EntityMapper.java", "core/implementation/mappers/{{entity.className}}Mapper.java", JAVA, skipEntityInput, true},
new Object[] {"src/main/java", "core/implementation/mappers/EntityMapper.java", "core/implementation/mappers/{{entity.className}}Mapper.java", JAVA, skipEntity, true},
// new Object[] {"src/main/java", "adapters/web/{{webFlavor}}/EntityResource.java", "adapters/web/{{entity.className}}Resource.java", JAVA, skipEntityResource},
new Object[] {"src/main/java", "core/domain/search/EntityDocument.java", "core/domain/search/{{entity.className}}{{searchDTOSuffix}}.java", JAVA, skipElasticSearch},
new Object[] {"src/main/java", "core/outbound/search/EntitySearchRepository.java", "core/outbound/search/{{entity.className}}SearchRepository.java", JAVA, skipElasticSearch, true},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class {{service.name}}Impl implements {{service.name}} {
private final Logger log = LoggerFactory.getLogger(getClass());

{{#each entities as |entity|}}
{{~#if (inputDTOSuffix entity)}}
private final {{entity.className}}Mapper {{entity.instanceName}}Mapper = Mappers.getMapper({{entity.className}}Mapper.class);
{{~/if}}
{{~#unless (skipEntityRepository this)}}
private final {{entity.className}}Repository {{entity.instanceName}}Repository;
{{~/unless}}
Expand All @@ -53,25 +51,20 @@ public class {{service.name}}Impl implements {{service.name}} {
// {{entity.name}}

@Override
@Transactional(readOnly = false)
public {{entity.className}} create{{entity.className}}({{entity.className}}{{inputDTOSuffix}} input) {
log.debug("Request to save {{entity.className}}: {}", input);
var {{entity.instanceName}} = {{#if (inputDTOSuffix entity)}}{{entity.instanceName}}Mapper.update(new {{entity.className}}(), input);
{{entity.instanceName}} ={{/if}} {{entity.instanceName}}Repository.save({{entity.instanceName}});
var {{entity.instanceName}} = {{entity.instanceName}}Mapper.update(new {{entity.className}}(), input);
{{entity.instanceName}} = {{entity.instanceName}}Repository.save({{entity.instanceName}});
// TODO: you may need to reload the entity here to fetch all the relationships
return {{entity.instanceName}};
}

@Override
@Transactional(readOnly = false)
public Optional<{{entity.className}}> update{{entity.className}}({{idJavaType}} id, {{entity.className}}{{inputDTOSuffix}} input) {
log.debug("Request to update {{entity.className}}: {}", input);
var {{entity.instanceName}} = {{entity.instanceName}}Repository.findById(id);
{{~#if (inputDTOSuffix entity)}}
{{entity.instanceName}} = {{entity.instanceName}}.map(existing{{entity.instanceName}} -> {{entity.instanceName}}Mapper.update(existing{{entity.instanceName}}, input));
{{~/if}}
// saving is unnecessary (jpa save anti-pattern): https://vladmihalcea.com/best-spring-data-jparepository/
// return {{entity.instanceName}}.map({{entity.instanceName}}Repository::save);
// saving is unnecessary: https://vladmihalcea.com/best-spring-data-jparepository/
return {{entity.instanceName}};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class {{service.name}}Impl implements {{service.name}} {
private final Logger log = LoggerFactory.getLogger(getClass());

{{#each entities as |entity|}}
{{~#if (inputDTOSuffix entity)}}
private final {{entity.className}}Mapper {{entity.instanceName}}Mapper = Mappers.getMapper({{entity.className}}Mapper.class);
{{~/if}}
{{~#unless (skipEntityRepository this)}}
private final {{entity.className}}Repository {{entity.instanceName}}Repository;
{{~/unless}}
Expand All @@ -55,11 +53,7 @@ public class {{service.name}}Impl implements {{service.name}} {
@Override
public {{entity.className}} create{{entity.className}}({{entity.className}}{{inputDTOSuffix}} input) {
log.debug("Request to save {{entity.className}}{{inputDTOSuffix}} : {}", input);
{{~#if (inputDTOSuffix entity)}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.save({{entity.instanceName}}Mapper.update(new {{entity.className}}(), input));
{{~else}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.save(input);
{{~/if}}
return {{entity.instanceName}};
}

Expand All @@ -69,9 +63,9 @@ public class {{service.name}}Impl implements {{service.name}} {

var {{entity.instanceName}} = {{entity.instanceName}}Repository
.findById(id)
{{~#if (inputDTOSuffix entity)}}
.map(existing{{entity.className}} -> {{entity.instanceName}}Mapper.update(existing{{entity.className}}, input))
{{~/if}}
.map(existing{{entity.className}} -> {
return {{entity.instanceName}}Mapper.update(existing{{entity.className}}, input);
})
.map({{entity.instanceName}}Repository::save);
return {{entity.instanceName}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ public void test_generator_hexagonal_jpa_imperative() throws Exception {
.withOption("persistence", PersistenceType.jpa)
.withOption("databaseType", DatabaseType.mariadb)
.withOption("style", ProgrammingStyle.imperative)
.withOption("forceOverwrite", true)
.withOption("haltOnFailFormatting", false);

new MainGenerator().generate(plugin);

// Assertions.assertTrue(logs.contains("Writing template with targetFile: io/example/integration/test/api/provider_for_commands_reactive/DoCreateProductConsumer.java"));
// Assertions.assertTrue(logs.contains("Writing template with targetFile: io/example/integration/test/api/provider_for_commands_reactive/DoCreateProductService.java"));

int exitCode = MavenCompiler.compile("src/test/resources/jpa-elasticsearch-scs3-pom.xml", targetFolder);
Assertions.assertEquals(0, exitCode);
}

@Test
public void test_generator_hexagonal_jpa_imperative_no_dto() throws Exception {
String targetFolder = "target/test_generator_hexagonal_jpa_imperative";
Plugin plugin = new JDLBackendApplicationDefaultPlugin()
.withSpecFile("classpath:io/zenwave360/sdk/resources/jdl/orders-model-relational.jdl")
.withTargetFolder(targetFolder)
.withOption("basePackage", "io.zenwave360.example")
.withOption("persistence", PersistenceType.jpa)
.withOption("databaseType", DatabaseType.mariadb)
.withOption("style", ProgrammingStyle.imperative)
.withOption("inputDTOSuffix", "")
.withOption("forceOverwrite", true)
.withOption("haltOnFailFormatting", false);

new MainGenerator().generate(plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,30 @@ public void test_generator_hexagonal_mongodb_imperative() throws Exception {
.withTargetFolder(targetFolder)
.withOption("basePackage", "io.zenwave360.example")
.withOption("persistence", PersistenceType.mongodb)
.withOption("style", ProgrammingStyle.imperative);
.withOption("style", ProgrammingStyle.imperative)
.withOption("forceOverwrite", true);

new MainGenerator().generate(plugin);

List<String> logs = logCaptor.getLogs();
// Assertions.assertTrue(logs.contains("Writing template with targetFile: io/example/integration/test/api/provider_for_commands_reactive/DoCreateProductConsumer.java"));
// Assertions.assertTrue(logs.contains("Writing template with targetFile: io/example/integration/test/api/provider_for_commands_reactive/DoCreateProductService.java"));

int exitCode = MavenCompiler.compile("src/test/resources/mongodb-elasticsearch-scs3-pom.xml", targetFolder);
Assertions.assertEquals(0, exitCode);
}

@Test
public void test_generator_hexagonal_mongodb_imperative_no_dtos() throws Exception {
String targetFolder = "target/test_generator_hexagonal_mongodb_imperative";
Plugin plugin = new JDLBackendApplicationDefaultPlugin()
.withSpecFile("classpath:io/zenwave360/sdk/resources/jdl/orders-model.jdl")
.withTargetFolder(targetFolder)
.withOption("basePackage", "io.zenwave360.example")
.withOption("persistence", PersistenceType.mongodb)
.withOption("inputDTOSuffix", "")
.withOption("style", ProgrammingStyle.imperative)
.withOption("forceOverwrite", true);

new MainGenerator().generate(plugin);

Expand All @@ -74,6 +97,7 @@ public void test_generator_hexagonal_mongodb_imperative() throws Exception {
Assertions.assertEquals(0, exitCode);
}


@Test
// @Disabled
public void test_generator_hexagonal_mongodb_imperative_registry() throws Exception {
Expand All @@ -83,7 +107,8 @@ public void test_generator_hexagonal_mongodb_imperative_registry() throws Except
.withTargetFolder(targetFolder)
.withOption("basePackage", "io.zenwave360.example")
.withOption("persistence", PersistenceType.mongodb)
.withOption("style", ProgrammingStyle.imperative);
.withOption("style", ProgrammingStyle.imperative)
.withOption("forceOverwrite", true);

new MainGenerator().generate(plugin);

Expand All @@ -105,7 +130,8 @@ public void test_generator_hexagonal_mongodb_imperative_registry_only_some_entit
.withOption("basePackage", "io.zenwave360.example")
.withOption("persistence", PersistenceType.mongodb)
.withOption("style", ProgrammingStyle.imperative)
.withOption("entities", List.of("BaseEntity", "Customer"));
.withOption("entities", List.of("BaseEntity", "Customer"))
.withOption("forceOverwrite", true);

new MainGenerator().generate(plugin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.*;
import java.util.stream.Collectors;

import com.github.jknack.handlebars.Handlebars;
import io.zenwave360.sdk.parsers.JDLParser;
import io.zenwave360.sdk.utils.JSONPath;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -111,6 +112,14 @@ public static boolean endsWith(String first, Options options) throws IOException
return StringUtils.endsWith(first, second);
}

public static Object ifTruthy(final Object value, final Options options)
throws IOException {
if (isTruthy(value)) {
return options.param(0, "");
}
return options.param(1, "");
}

public static boolean not(Object value, Options options) throws IOException {
if (value == null) {
return true;
Expand All @@ -130,6 +139,9 @@ private static boolean isTruthy(Object value) {
if (value == null) {
return false;
}
if(value.toString().trim().equals("")) {
return false;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void testHandlebarsEngine() throws IOException {
Assertions.assertTrue(templateOutput.getContent().contains("kebabCase some-camel-case-with-spaces"));
Assertions.assertTrue(templateOutput.getContent().contains("asPackageFolder io/zenwave360/sdk/templating"));
Assertions.assertTrue(templateOutput.getContent().contains("Prefix2Suffix"));
Assertions.assertTrue(templateOutput.getContent().contains("ifTruthy true: true"));
Assertions.assertTrue(templateOutput.getContent().contains("ifTruthy false: false"));
Assertions.assertTrue(templateOutput.getContent().contains("Inside if 1"));
Assertions.assertTrue(templateOutput.getContent().contains("Inside else 2"));
Assertions.assertTrue(templateOutput.getContent().contains("path: 'api/v1/users'"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ asInstanceName {{asInstanceName "tratra-tratra"}}
asJavaTypeName {{asJavaTypeName "tratra-tratra"}}
kebabCase {{kebabCase "Some Camel Case with Spaces"}}
asPackageFolder {{asPackageFolder "io.zenwave360.sdk.templating"}}

ifTruthy true: {{ifTruthy "true" "true" "false"}}
ifTruthy false: {{ifTruthy "false" "true" "false"}}

{{#joinWithTemplate theList}}
Prefix{{.}}Suffix
{{/joinWithTemplate}}
Expand Down

0 comments on commit fe11485

Please sign in to comment.