From 8b858180a60b255e0798a5f8f9fb2fcf822ab82c Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Mon, 12 Apr 2021 22:47:25 +0200 Subject: [PATCH] use derived constants in entity-server templates and domain --- generators/entity-server/files.js | 29 +- generators/entity-server/index.js | 11 +- .../partials/it_patch_update.partial.java.ejs | 24 +- .../templates/partials/save_template.ejs | 45 +- .../reactive/partials/save_template.ejs | 6 +- .../java/package/common/delete_template.ejs | 6 +- .../java/package/common/field_validators.ejs | 25 +- .../common/get_all_stream_template.ejs | 37 +- .../java/package/common/get_all_template.ejs | 40 +- .../package/common/get_filtered_template.ejs | 24 +- .../main/java/package/common/get_template.ejs | 2 +- .../java/package/common/inject_template.ejs | 54 +- .../java/package/common/patch_template.ejs | 67 +- .../package/common/search_stream_template.ejs | 22 +- .../java/package/common/search_template.ejs | 40 +- .../main/java/package/domain/Entity.java.ejs | 675 +++++++++--------- .../package/domain/enumeration/Enum.java.ejs | 14 +- .../domain/relationship_validators.ejs | 2 +- generators/generator-base.js | 4 + utils/entity.js | 5 +- utils/field.js | 1 + 21 files changed, 578 insertions(+), 555 deletions(-) diff --git a/generators/entity-server/files.js b/generators/entity-server/files.js index 97bbdb06457..6a723d869e9 100644 --- a/generators/entity-server/files.js +++ b/generators/entity-server/files.js @@ -21,6 +21,13 @@ const chalk = require('chalk'); const fs = require('fs'); const utils = require('../utils'); const constants = require('../generator-constants'); +const { CASSANDRA, COUCHBASE, MONGODB, NEO4J, SQL } = require('../../jdl/jhipster/database-types'); +const { ELASTICSEARCH } = require('../../jdl/jhipster/search-engine-types'); +const { MapperTypes, ServiceTypes } = require('../../jdl/jhipster/entity-options'); +const { EHCACHE, CAFFEINE, INFINISPAN, REDIS } = require('../../jdl/jhipster/cache-types'); + +const { MAPSTRUCT } = MapperTypes; +const { SERVICE_CLASS, SERVICE_IMPL } = ServiceTypes; /* Constants use throughout */ const INTERPOLATE_REGEX = constants.INTERPOLATE_REGEX; @@ -36,7 +43,7 @@ const SERVER_TEST_SRC_DIR = constants.SERVER_TEST_SRC_DIR; const serverFiles = { dbChangelog: [ { - condition: generator => generator.databaseType === 'cassandra' && !generator.skipDbChangelog, + condition: generator => generator.databaseType === CASSANDRA && !generator.skipDbChangelog, path: SERVER_MAIN_RES_DIR, templates: [ { @@ -46,7 +53,7 @@ const serverFiles = { ], }, { - condition: generator => generator.searchEngine === 'couchbase' && !generator.skipDbChangelog, + condition: generator => generator.searchEngine === COUCHBASE && !generator.skipDbChangelog, path: SERVER_MAIN_RES_DIR, templates: [ { @@ -91,7 +98,7 @@ const serverFiles = { ], }, { - condition: generator => generator.searchEngine === 'elasticsearch' && !generator.embedded, + condition: generator => generator.searchEngine === ELASTICSEARCH && !generator.embedded, path: SERVER_MAIN_SRC_DIR, templates: [ { @@ -121,7 +128,7 @@ const serverFiles = { ], }, { - condition: generator => generator.reactive && generator.databaseType === 'sql' && !generator.embedded, + condition: generator => generator.reactive && generator.databaseType === SQL && !generator.embedded, path: SERVER_MAIN_SRC_DIR, templates: [ { @@ -135,7 +142,7 @@ const serverFiles = { ], }, { - condition: generator => generator.service === 'serviceImpl' && !generator.embedded, + condition: generator => generator.service === SERVICE_IMPL && !generator.embedded, path: SERVER_MAIN_SRC_DIR, templates: [ { @@ -149,7 +156,7 @@ const serverFiles = { ], }, { - condition: generator => generator.service === 'serviceClass' && !generator.embedded, + condition: generator => generator.service === SERVICE_CLASS && !generator.embedded, path: SERVER_MAIN_SRC_DIR, templates: [ { @@ -159,7 +166,7 @@ const serverFiles = { ], }, { - condition: generator => generator.dto === 'mapstruct', + condition: generator => generator.dto === MAPSTRUCT, path: SERVER_MAIN_SRC_DIR, templates: [ { @@ -228,7 +235,7 @@ const serverFiles = { ], }, { - condition: generator => generator.dto === 'mapstruct', + condition: generator => generator.dto === MAPSTRUCT, path: SERVER_TEST_SRC_DIR, templates: [ { @@ -238,7 +245,7 @@ const serverFiles = { ], }, { - condition: generator => generator.dto === 'mapstruct' && ['sql', 'mongodb', 'couchbase', 'neo4j'].includes(generator.databaseType), + condition: generator => generator.dto === MAPSTRUCT && [SQL, MONGODB, COUCHBASE, NEO4J].includes(generator.databaseType), path: SERVER_TEST_SRC_DIR, templates: [ { @@ -305,8 +312,8 @@ function writeFiles() { } function customizeFiles() { - if (this.databaseType === 'sql') { - if (['ehcache', 'caffeine', 'infinispan', 'redis'].includes(this.cacheProvider) && this.enableHibernateCache) { + if (this.databaseType === SQL) { + if ([EHCACHE, CAFFEINE, INFINISPAN, REDIS].includes(this.cacheProvider) && this.enableHibernateCache) { this.addEntityToCache(this.asEntity(this.entityClass), this.relationships, this.packageName, this.packageFolder, this.cacheProvider); } } diff --git a/generators/entity-server/index.js b/generators/entity-server/index.js index 774d4d74884..1eeed222cdf 100644 --- a/generators/entity-server/index.js +++ b/generators/entity-server/index.js @@ -21,6 +21,9 @@ const constants = require('../generator-constants'); const { writeFiles, customizeFiles } = require('./files'); const utils = require('../utils'); const BaseBlueprintGenerator = require('../generator-base-blueprint'); +const { GENERATOR_ENTITY_SERVER } = require('../generator-list'); +const { OAUTH2, SESSION } = require('../../jdl/jhipster/authentication-types'); +const { SQL } = require('../../jdl/jhipster/database-types'); const { isReservedTableName } = require('../../jdl/jhipster/reserved-keywords'); /* constants used throughout */ @@ -34,7 +37,7 @@ module.exports = class extends BaseBlueprintGenerator { this.jhipsterContext = opts.jhipsterContext || opts.context; - useBlueprints = !this.fromBlueprint && this.instantiateBlueprints('entity-server', { context: opts.context }); + useBlueprints = !this.fromBlueprint && this.instantiateBlueprints(GENERATOR_ENTITY_SERVER, { context: opts.context }); } // Public API method used by the getter and also by Blueprints @@ -79,7 +82,7 @@ module.exports = class extends BaseBlueprintGenerator { loadConfigIntoGenerator() { utils.copyObjectProps(this, this.entity); - this.testsNeedCsrf = ['oauth2', 'session'].includes(this.entity.authenticationType); + this.testsNeedCsrf = [OAUTH2, SESSION].includes(this.entity.authenticationType); this.officialDatabaseType = constants.OFFICIAL_DATABASE_TYPE_NAMES[this.entity.databaseType]; }, @@ -118,7 +121,7 @@ module.exports = class extends BaseBlueprintGenerator { if (this.primaryKey && this.primaryKey.derived) { this.isUsingMapsId = true; this.mapsIdAssoc = this.relationships.find(rel => rel.id); - this.hasOauthUser = this.mapsIdAssoc.otherEntityName === 'user' && this.authenticationType === 'oauth2'; + this.hasOauthUser = this.mapsIdAssoc.otherEntityName === 'user' && this.authenticationType === OAUTH2; } else { this.isUsingMapsId = false; this.mapsIdAssoc = null; @@ -171,7 +174,7 @@ module.exports = class extends BaseBlueprintGenerator { } _generateSqlSafeName(name) { - if (isReservedTableName(name, 'sql')) { + if (isReservedTableName(name, SQL)) { return `e_${name}`; } return name; diff --git a/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs b/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs index b52e110c7fe..84f7b3e4d93 100644 --- a/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs +++ b/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> // Initialize the database -<%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> +<%_ if (primaryKey.isUuid && !databaseTypeSql) { _%> <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); <%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; @@ -33,14 +33,14 @@ partialUpdated<%= persistClass %>.set<%= primaryKey.nameCapitalized %>(<%= persi .<%= field.fieldName %>(<%= 'UPDATED_' + field.fieldNameUnderscored.toUpperCase() %>)<% if (field.fieldWithContentType) { %> .<%= field.fieldName %>ContentType(<%= 'UPDATED_' + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE)<% } %><% } %>; <%_ } else { _%> -<%_ for (field of fieldsToUpdate) { _%> + <%_ for (field of fieldsToUpdate) { _%> <%_ if (field.includeField) { %> partialUpdated<%= persistClass %>.set<%= field.fieldInJavaBeanMethod %>(<%= field.updateWithValue %>); - <%_ if (field.fieldWithContentType) { _%> + <%_ if (field.fieldWithContentType) { _%> partialUpdated<%= persistClass %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= field.updateWithValue %>_CONTENT_TYPE); - <%_ } _%> + <%_ } _%> <%_ } _%> -<%_ } _%> + <%_ } _%> <%_ } _%> <%_ if (!reactive) { _%> @@ -60,19 +60,21 @@ webTestClient <%_ } _%> // Validate the <%= entityClass %> in the database -<%_ if (databaseType === 'couchbase') { _%> +<%_ if (databaseTypeCouchbase) { _%> SecurityContextHolder.setContext(TestSecurityContextHolder.getContext()); <%_ } _%> List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>; assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeUpdate); <%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1); -<%_ for (field of fields) { if (field.fieldType === 'ZonedDateTime') { _%> +<%_ for (field of fields) { _%> + <%_ if (field.fieldTypeZonedDateTime) { _%> assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>); -<%_ } else if ((field.fieldType === 'byte[]' || field.fieldType === 'ByteBuffer') && field.fieldTypeBlobContent !== 'text') { _%> + <%_ } else if (field.fieldTypeBinary && !field.blobContentTypeText) { _%> assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>); assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>ContentType()).isEqualTo(<%= field.testWithConstant %>_CONTENT_TYPE); -<%_ } else if (field.fieldType === 'BigDecimal'){ _%> + <%_ } else if (field.fieldTypeBigDecimal) { _%> assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualByComparingTo(<%= field.testWithConstant %>); -<%_ } else { _%> + <%_ } else { _%> assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>); -<%_ } } _%> + <%_ } _%> +<%_ } _%> diff --git a/generators/entity-server/templates/partials/save_template.ejs b/generators/entity-server/templates/partials/save_template.ejs index a075a6fdb28..6fe6bd89f7d 100644 --- a/generators/entity-server/templates/partials/save_template.ejs +++ b/generators/entity-server/templates/partials/save_template.ejs @@ -22,7 +22,7 @@ const mapper = entityInstance + 'Mapper'; const dtoToEntity = mapper + '.' + 'toEntity'; const entityToDto = mapper + '.' + 'toDto'; const entityToDtoReference = mapper + '::' + 'toDto'; -const returnPrefix = (returnDirectly && searchEngine !== 'elasticsearch') ? 'return' : instanceType + ' result ='; +const returnPrefix = (returnDirectly && !searchEngine) ? 'return' : instanceType + ' result ='; let resultEntity; let mapsIdEntityInstance; let mapsIdRepoInstance; @@ -32,39 +32,40 @@ if (isUsingMapsId === true) { mapsIdEntityInstance = mapsIdAssoc.otherEntityNameCapitalized.charAt(0).toLowerCase() + mapsIdAssoc.otherEntityNameCapitalized.slice(1); mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`; otherEntityName = mapsIdAssoc.otherEntityName; - if (isController === true) { _%> + if (isController === true) { _%> if (Objects.isNull(<%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>())) { throw new BadRequestAlertException("Invalid association value provided", ENTITY_NAME, "null"); } - <%_ } _%> -<%_ } - if (!viaService) { - if (dto === 'mapstruct') { + <%_ } _%> +<%_ } _%> +<%_ if (!viaService) { _%> + <%_ if (dtoMapstruct) { _%> resultEntity = persistInstance; _%> <%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>); - <%_ if (isUsingMapsId === true) { _%> + <%_ if (isUsingMapsId === true) { _%> <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>(); - <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= persistInstance %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>); - <%_ } _%> + <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= persistInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>); + <%_ } _%> <%= persistInstance %> = <%= entityInstance %>Repository.save(<%= persistInstance %>); <%= returnPrefix %> <%= entityToDto %>(<%= persistInstance %>); - <%_ } else { resultEntity = 'result'; _%> - <%_ if (isUsingMapsId === true) { _%> + <%_ } else { + resultEntity = 'result'; _%> + <%_ if (isUsingMapsId) { _%> <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>(); - <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>); - <%_ } _%> + <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>); + <%_ } _%> <%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>); - <%_ } - if (searchEngine === 'elasticsearch') { _%> + <%_ } _%> + <%_ if (searchEngine) { _%> <%= entityInstance %>SearchRepository.save(<%= resultEntity %>); - <%_ if (returnDirectly) { _%> + <%_ if (returnDirectly) { _%> return result; - <%_ } - } - } else { _%> - <%_ if (isUsingMapsId === true && isController === false) { _%> + <%_ } _%> + <%_ } _%> +<%_ } else { _%> + <%_ if (isUsingMapsId === true && isController === false) { _%> <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= entityInstance %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>(); - <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= entityInstance %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>); - <%_ } _%> + <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= entityInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>); + <%_ } _%> <%= returnPrefix %> <%= entityInstance %>Service.save(<%= instanceName %>); <%_ } _%> diff --git a/generators/entity-server/templates/reactive/partials/save_template.ejs b/generators/entity-server/templates/reactive/partials/save_template.ejs index 7c032d9a110..2b6e0567127 100644 --- a/generators/entity-server/templates/reactive/partials/save_template.ejs +++ b/generators/entity-server/templates/reactive/partials/save_template.ejs @@ -25,11 +25,11 @@ _%> <%_ if (viaService) { _%> return <%= entityInstance %>Service.save(<%= instanceName %>) <%_ } else { _%> - return <%= entityInstance %>Repository.save(<%if (dto === 'mapstruct') { %><%= dtoToEntity %>(<% } %><%= instanceName %>)<%if (dto === 'mapstruct') { %>)<% } -%> - <%_ if (searchEngine === 'elasticsearch') { %> + return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= instanceName %>)<%if (dtoMapstruct) { %>)<% } -%> + <%_ if (searchEngineElasticsearch) { %> .flatMap(<%= entityInstance %>SearchRepository::save) <%_ } -%> - <%_ if (dto === 'mapstruct') { %> + <%_ if (dtoMapstruct) { %> .map(<%= entityToDtoReference %>) <%_ } -%> <%_ if (returnDirectly) { _%>;<%_ } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/delete_template.ejs b/generators/entity-server/templates/src/main/java/package/common/delete_template.ejs index 5975e49a673..1a5a10d1ae2 100644 --- a/generators/entity-server/templates/src/main/java/package/common/delete_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/delete_template.ejs @@ -17,12 +17,12 @@ limitations under the License. -%> <%_ if (reactive) { _%> - return <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id)<%_ if (!viaService && searchEngine === 'elasticsearch') { %> + return <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id)<%_ if (!viaService && searchEngineElasticsearch) { %> .then(<%= entityInstance %>SearchRepository.deleteById(id))<%_ } _%><%_ if (!fromResource) { _%>; <%_ } _%> <%_ } else { _%> <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id); - <%_ if (!viaService && searchEngine === 'elasticsearch') { _%> + <%_ if (!viaService && searchEngineElasticsearch) { _%> <%= entityInstance %>SearchRepository.deleteById(id); - <%_ } _%> + <%_ } _%> <%_ } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/field_validators.ejs b/generators/entity-server/templates/src/main/java/package/common/field_validators.ejs index f0a4d036847..2cc94912d42 100644 --- a/generators/entity-server/templates/src/main/java/package/common/field_validators.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/field_validators.ejs @@ -21,22 +21,21 @@ let result = ''; if (field.fieldValidate === true) { - const rules = field.fieldValidateRules; const validators = []; const MAX_VALUE = 2147483647; - const isBlob = field.fieldType === 'byte[]'; + const isBlob = field.fieldTypeBytes; - if (rules.includes('required') && !isBlob) { + if (field.fieldValidationRequired && !isBlob) { // reactive tests need a default validation message because lookup is blocking validators.push('@NotNull' + (reactive ? '(message = "must not be null")' : '')); } - if (rules.includes('minlength') && !rules.includes('maxlength')) { + if (field.fieldValidationMinLength && !field.fieldValidationMaxLength) { validators.push('@Size(min = ' + field.fieldValidateRulesMinlength + ')'); } - if (rules.includes('maxlength') && !rules.includes('minlength')) { + if (field.fieldValidationMaxLength && !field.fieldValidationMinLength) { validators.push('@Size(max = ' + field.fieldValidateRulesMaxlength + ')'); } - if (rules.includes('minlength') && rules.includes('maxlength')) { + if (field.fieldValidationMinLength && field.fieldValidationMaxLength) { validators.push('@Size(min = ' + field.fieldValidateRulesMinlength + ', max = ' + field.fieldValidateRulesMaxlength + ')'); } // Not supported anymore because the server can't check the size of the blob before downloading it completely. @@ -49,23 +48,23 @@ if (field.fieldValidate === true) { // if (rules.includes('minbytes') && rules.includes('maxbytes')) { // validators.push('@Size(min = ' + field.fieldValidateRulesMinbytes + ', max = ' + field.fieldValidateRulesMaxbytes + ')'); // } - if (rules.includes('min')) { - if (field.fieldType === 'Float' || field.fieldType === 'Double' || field.fieldType === 'BigDecimal') { + if (field.fieldValidationMin) { + if (field.fieldTypeFloat || field.fieldTypeDouble || field.fieldTypeBigDecimal) { validators.push('@DecimalMin(value = "' + field.fieldValidateRulesMin + '")'); } else { - const isLong = (field.fieldValidateRulesMin > MAX_VALUE || field.fieldType == 'Long') ? 'L' : ''; + const isLong = (field.fieldValidateRulesMin > MAX_VALUE || field.fieldTypeLong) ? 'L' : ''; validators.push('@Min(value = ' + field.fieldValidateRulesMin + isLong + ')'); } } - if (rules.includes('max')) { - if (field.fieldType === 'Float' || field.fieldType === 'Double' || field.fieldType === 'BigDecimal') { + if (field.fieldValidationMax) { + if (field.fieldTypeFloat || field.fieldTypeDouble || field.fieldTypeBigDecimal) { validators.push('@DecimalMax(value = "' + field.fieldValidateRulesMax + '")'); } else { - const isLong = (field.fieldValidateRulesMax > MAX_VALUE || field.fieldType == 'Long') ? 'L' : ''; + const isLong = (field.fieldValidateRulesMax > MAX_VALUE || field.fieldTypeLong) ? 'L' : ''; validators.push('@Max(value = ' + field.fieldValidateRulesMax + isLong + ')'); } } - if (rules.includes('pattern')) { + if (field.fieldValidationPattern) { validators.push('@Pattern(regexp = "' + field.fieldValidateRulesPatternJava + '")'); } result = validators.join('\n '); diff --git a/generators/entity-server/templates/src/main/java/package/common/get_all_stream_template.ejs b/generators/entity-server/templates/src/main/java/package/common/get_all_stream_template.ejs index d8f5a0c8ccc..7197bc13035 100644 --- a/generators/entity-server/templates/src/main/java/package/common/get_all_stream_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/get_all_stream_template.ejs @@ -19,31 +19,34 @@ <% const mapper = entityInstance + 'Mapper'; const entityToDtoReference = mapper + '::' + 'toDto'; -const reactiveSql = (databaseType === 'sql' && reactive); +const reactiveSql = (databaseTypeSql && reactive); let toResponseEntityPrefix = ''; let toResponseEntityPostfix = ''; -if (pagination !== 'no') { - if (reactiveSql) { - toResponseEntityPostfix = '.map(ResponseEntity::ok)'; - } else { - toResponseEntityPrefix = 'new ResponseEntity<>('; - toResponseEntityPostfix = ', HttpStatus.OK)'; - } +if (!paginationNo) { + if (reactiveSql) { + toResponseEntityPostfix = '.map(ResponseEntity::ok)'; + } else { + toResponseEntityPrefix = 'new ResponseEntity<>('; + toResponseEntityPostfix = ', HttpStatus.OK)'; + } } -for (const relationship of relationships) { if (relationship.relationshipType === 'one-to-one' && relationship.ownerSide !== true) { %> +for (const relationship of relationships) { + if (relationship.relationshipOneToOne && !relationship.ownerSide) { %> if ("<%= relationship.relationshipName.toLowerCase() %>-is-null".equals(filter)) { log.debug("REST request to get all <%= entityClass %>s where <%= relationship.relationshipName %> is null"); - <%_ if (viaService) { _%> + <%_ if (viaService) { _%> return <%- toResponseEntityPrefix %><%= entityInstance %>Service.findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull()<% if (reactiveSql) { %>.collectList()<% } %><%= toResponseEntityPostfix %>; - <%_ } else { _%> - return <%- toResponseEntityPrefix %><% if (reactiveSql) { %><%= entityInstance %>Repository.findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull()<% if (dto === 'mapstruct') { %>.map(<%= entityToDtoReference %>)<% } %>.collectList()<%= toResponseEntityPostfix %>; + <%_ } else { _%> + return <%- toResponseEntityPrefix %><% if (reactiveSql) { %><%= entityInstance %>Repository.findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull()<% if (dtoMapstruct) { %>.map(<%= entityToDtoReference %>)<% } %>.collectList()<%= toResponseEntityPostfix %>; <%_ } else { _%>StreamSupport .stream(<%= entityInstance %>Repository.findAll().spliterator(), false) - .filter(<%= entityInstance %> -> <%= entityInstance %>.get<%= relationship.relationshipNameCapitalized %>() == null)<% if (dto === 'mapstruct') { %> - .map(<%= entityToDtoReference %>)<% } if (pagination !== 'no') { %> - .collect(Collectors.toList()), HttpStatus.OK);<% } else { if (dto === 'mapstruct') { %> + .filter(<%= entityInstance %> -> <%= entityInstance %>.get<%= relationship.relationshipNameCapitalized %>() == null)<% if (dtoMapstruct) { %> + .map(<%= entityToDtoReference %>)<% } if (!paginationNo) { %> + .collect(Collectors.toList()), HttpStatus.OK);<% } else { if (dtoMapstruct) { %> .collect(Collectors.toCollection(LinkedList::new));<% } else { %> .collect(Collectors.toList());<% }} %> <%_ } _%> - <%_ } _%> - }<% } } %> + <%_ } _%> + } + <%_ } _%> +<%_ } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/get_all_template.ejs b/generators/entity-server/templates/src/main/java/package/common/get_all_template.ejs index df9bb1aa3ef..f45d471728a 100644 --- a/generators/entity-server/templates/src/main/java/package/common/get_all_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/get_all_template.ejs @@ -22,19 +22,19 @@ const mapper = entityInstance + 'Mapper'; const entityListToDtoListReference = mapper + '.' + 'toDto'; const entityToDtoReference = mapper + '::'+ 'toDto'; - const reactiveEntityToDto = (dto === 'mapstruct' && !viaService) ? `.map(${entityToDtoReference})` : ''; + const reactiveEntityToDto = (dtoMapstruct && !viaService) ? `.map(${entityToDtoReference})` : ''; _%> <%_ if (jpaMetamodelFiltering) { _%> - public ResponseEntity>> getAll<%= entityClassPlural %>(<%= entityClass %>Criteria criteria<% if (pagination != 'no') { %>, Pageable pageable<% if (reactive) { %>, ServerHttpRequest request<% } %><% } %>) { + public ResponseEntity>> getAll<%= entityClassPlural %>(<%= entityClass %>Criteria criteria<% if (!paginationNo) { %>, Pageable pageable<% if (reactive) { %>, ServerHttpRequest request<% } %><% } %>) { log.debug("REST request to get <%= entityClassPlural %> by criteria: {}", criteria); - <%_ if (pagination === 'no') { _%> + <%_ if (paginationNo) { _%> List<<%= instanceType %>> entityList = <%= entityInstance %>QueryService.findByCriteria(criteria); return ResponseEntity.ok().body(entityList); - <%_ } else { _%> + <%_ } else { _%> Page<<%= instanceType %>> page = <%= entityInstance %>QueryService.findByCriteria(criteria, pageable); HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page); return ResponseEntity.ok().headers(headers).body(page.getContent()); - <%_ } _%> + <%_ } _%> } /** @@ -49,26 +49,26 @@ _%> return ResponseEntity.ok().body(<%= entityInstance %>QueryService.countByCriteria(criteria)); } <%_ } else { _%> - <%_ if (pagination === 'no') { _%> + <%_ if (paginationNo) { _%> public <% if (reactive) { %>Mono<<% } %>List<<%= instanceType %>><% if (reactive) { %>><% } %> getAll<%= entityClassPlural %>(<% if (fieldsContainNoOwnerOneToOne) { %>@RequestParam(required = false) String filter<% } %><% if (relationshipsContainEagerLoad && fieldsContainNoOwnerOneToOne) { %>,<% } %><% if (relationshipsContainEagerLoad) { %>@RequestParam(required = false, defaultValue = "false") boolean eagerload<% } %>) {<%- include('get_all_stream_template', {viaService: viaService}); -%> log.debug("REST request to get all <%= entityClassPlural %>"); - <%_ if (viaService) { _%> + <%_ if (viaService) { _%> return <%= entityInstance %>Service.findAll()<% if (reactive) { %>.collectList()<% } %>; - <%_ } else if (dto === 'mapstruct') { _%> + <%_ } else if (dtoMapstruct) { _%> <%= reactive ? 'Flux' : 'List' %><<%= persistClass %>> <%= entityInstancePlural %> = <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>(); - <%_ if (!reactive) { _%> + <%_ if (!reactive) { _%> return <%= entityListToDtoListReference %>(<%= entityInstancePlural %>); - <%_ } else { _%> + <%_ } else { _%> return <%= entityInstancePlural %>.map(<%= entityToDtoReference %>).collectList(); - <%_ } _%> - <%_ } else { _%> - return <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>()<% if (reactive) { %>.collectList()<% } %>; - <%_ } _%> + <%_ } _%> <%_ } else { _%> + return <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>()<% if (reactive) { %>.collectList()<% } %>; + <%_ } _%> + <%_ } else { _%> public <% if (reactive) { %>Mono>>><% } else { %>ResponseEntity>><% } %> getAll<%= entityClassPlural %>(Pageable pageable<% if (reactive) { %>, ServerHttpRequest request<% } %><% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (relationshipsContainEagerLoad) { %>, @RequestParam(required = false, defaultValue = "false") boolean eagerload<% } %>) {<%- include('get_all_stream_template', {viaService: viaService}); -%> log.debug("REST request to get a page of <%= entityClassPlural %>"); - <%_ if (!reactive) { _%> - <%_ if (relationshipsContainEagerLoad) { _%> + <%_ if (!reactive) { _%> + <%_ if (relationshipsContainEagerLoad) { _%> Page<<%= instanceType %>> page; if (eagerload) { page = <%= entityInstance %><%= viaService ? 'Service' : 'Repository' %>.findAllWithEagerRelationships(pageable)<%= reactiveEntityToDto %>; @@ -76,12 +76,12 @@ _%> page = <%= entityInstance %><%= viaService ? 'Service' : 'Repository' %>.findAll(pageable)<%= reactiveEntityToDto %>; } HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page); - <%_ } else { _%> + <%_ } else { _%> Page<<%= instanceType %>> page = <%= entityInstance %><%= viaService ? 'Service' : 'Repository' %>.findAll(pageable)<%= reactiveEntityToDto %>; HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page); - <%_ } _%> + <%_ } _%> return ResponseEntity.ok().headers(headers).body(page.getContent()); - <%_ } else { _%> + <%_ } else { _%> return <%= entityInstance %><%= viaService ? 'Service.countAll' : 'Repository.count' %>().zipWith(<%= entityInstance %><%= viaService ? 'Service.findAll' : 'Repository.findAllBy' %>(pageable)<%= reactiveEntityToDto %>.collectList()) .map(countWithEntities -> { return ResponseEntity.ok() @@ -92,7 +92,7 @@ _%> ) ).body(countWithEntities.getT2()); }); - <%_ } _%> <%_ } _%> + <%_ } _%> } <%_ } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/get_filtered_template.ejs b/generators/entity-server/templates/src/main/java/package/common/get_filtered_template.ejs index 9255c7eb712..44fd898f923 100644 --- a/generators/entity-server/templates/src/main/java/package/common/get_filtered_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/get_filtered_template.ejs @@ -20,29 +20,31 @@ const instanceType = restClass; const mapper = entityInstance + 'Mapper'; const entityToDtoReference = mapper + '::' + 'toDto'; -const reactiveSql = (databaseType === 'sql' && reactive); -for (const relationship of relationships) { if (relationship.relationshipType === 'one-to-one' && relationship.ownerSide !== true) { %> +const reactiveSql = (databaseTypeSql && reactive); +for (const relationship of relationships) { if (relationship.relationshipOneToOne && !relationship.ownerSide) { %> /** * Get all the <%= entityInstancePlural %> where <%= relationship.relationshipNameCapitalized %> is {@code null}. * @return the list of entities. - */<% if (databaseType === 'sql') { %> + */ + <%_ if (databaseTypeSql) { _%> @Transactional(readOnly = true) <% } %> public <%= reactiveSql ? 'Flux' : 'List' %><<%= instanceType %>> findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull() { log.debug("Request to get all <%= entityInstancePlural %> where <%= relationship.relationshipNameCapitalized %> is null"); - <%_ if (reactiveSql) { _%> - return <%= entityInstance %>Repository.findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull()<% if (dto === 'mapstruct') { _%> + <%_ if (reactiveSql) { _%> + return <%= entityInstance %>Repository.findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull()<%_ if (dtoMapstruct) { _%> .map(<%= entityToDtoReference %>)<%_ } _%>; - <%_ } else { _%> + <%_ } else { _%> return StreamSupport .stream(<%= entityInstance %>Repository.findAll().spliterator(), false) .filter(<%= entityInstance %> -> <%= entityInstance %>.get<%= relationship.relationshipNameCapitalized %>() == null) - <%_ if (dto === 'mapstruct') { _%> + <%_ if (dtoMapstruct) { _%> .map(<%= entityToDtoReference %>) .collect(Collectors.toCollection(LinkedList::new)); - <%_ } else { _%> + <%_ } else { _%> .collect(Collectors.toList()); - <%_ } _%> - <%_ } _%> + <%_ } _%> + <%_ } _%> } -<% } } %> + <%_ } _%> +<%_ } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/get_template.ejs b/generators/entity-server/templates/src/main/java/package/common/get_template.ejs index 20445fc34fd..61a6498eec0 100644 --- a/generators/entity-server/templates/src/main/java/package/common/get_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/get_template.ejs @@ -24,7 +24,7 @@ const returnPrefix = returnDirectly ? 'return' : `${instanceType} ${instanceName} =`; %> <%_ if (!viaService) { _%> - <%- returnPrefix %> <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findOneWithEagerRelationships(id)<% } else { %>findById(id)<% } %><% if (dto !== 'mapstruct') { %>;<% } else { %> + <%- returnPrefix %> <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findOneWithEagerRelationships(id)<% } else { %>findById(id)<% } %><% if (!dtoMapstruct) { %>;<% } else { %> .map(<%= entityToDtoReference %>);<% } %> <%_ } else { _%> <%- returnPrefix %> <%= entityInstance %>Service.findOne(id); diff --git a/generators/entity-server/templates/src/main/java/package/common/inject_template.ejs b/generators/entity-server/templates/src/main/java/package/common/inject_template.ejs index d44b72c8509..c0274d2e027 100644 --- a/generators/entity-server/templates/src/main/java/package/common/inject_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/inject_template.ejs @@ -17,35 +17,35 @@ limitations under the License. -%> <%_ const beans = []; - if (viaService) { - beans.push({class: `${entityClass}Service`, instance: `${entityInstance}Service`}); - beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`}); - if (queryService) { - beans.push({class: `${entityClass}QueryService`, instance: `${entityInstance}QueryService`}); - } - if (isUsingMapsId === true && isController === false) { - const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized; - const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1); - const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`; - beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance}); - } - } else { - beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`}); - if (dto === 'mapstruct') { - beans.push({class: `${entityClass}Mapper`, instance: `${entityInstance}Mapper`}); - } - if (searchEngine === 'elasticsearch') { - beans.push({class: `${entityClass}SearchRepository`, instance: `${entityInstance}SearchRepository`}); - } - if (isUsingMapsId === true) { - const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized; - const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1); - const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`; - beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance}); - } + if (viaService) { + beans.push({class: `${entityClass}Service`, instance: `${entityInstance}Service`}); + beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`}); + if (queryService) { + beans.push({class: `${entityClass}QueryService`, instance: `${entityInstance}QueryService`}); + } + if (isUsingMapsId === true && isController === false) { + const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized; + const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1); + const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`; + beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance}); + } + } else { + beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`}); + if (dtoMapstruct) { + beans.push({class: `${entityClass}Mapper`, instance: `${entityInstance}Mapper`}); + } + if (searchEngineElasticsearch) { + beans.push({class: `${entityClass}SearchRepository`, instance: `${entityInstance}SearchRepository`}); + } + if (isUsingMapsId) { + const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized; + const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1); + const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`; + beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance}); + } } if (saveUserSnapshot && (viaService || constructorName.endsWith('Resource'))) { - beans.push({class: 'UserRepository', instance: 'userRepository'}); + beans.push({class: 'UserRepository', instance: 'userRepository'}); } _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/patch_template.ejs b/generators/entity-server/templates/src/main/java/package/common/patch_template.ejs index 735b4186ac6..160a177b66a 100644 --- a/generators/entity-server/templates/src/main/java/package/common/patch_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/patch_template.ejs @@ -24,38 +24,43 @@ const mapOrFlatMap = reactive ? 'flatMap' : 'map' const returnPrefix = (isService) ? 'return' : returnType + '<' + instanceType + '> result ='; const mapper = entityInstance + 'Mapper'; _%> -<%_ if(viaService) { %> - <%_ if(!reactive) { %> - Optional<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>); - <%_ } else { %> - Mono<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>); - <%_ } %> +<%_ if(viaService) { _%> + <%_ if(!reactive) { _%> + Optional<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>); + <%_ } else { _%> + Mono<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>); + <%_ } _%> <%_ } else { %> <%- returnPrefix %> <%= entityInstance %>Repository.findById(<%= instanceName %>.get<%= primaryKey.nameCapitalized %>()) .map(existing<%= entityClass %> -> { -<%_ if(dto === 'mapstruct') { %><%= mapper %>.partialUpdate(existing<%= entityClass %>, <%= instanceName %>);<%_ } else { %> -<%_ for (const field of fields.filter(field => !field.id && !field.transient)) { _%> - if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>() != null) { - existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>()); - } - <%_ if (field.fieldWithContentType) { _%> - if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType() != null) { - existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType()); - } - <%_ } _%> -<%_ } %> -<% } %> - return existing<%= entityClass %>; - }) - .<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save)<% if (searchEngine === 'elasticsearch') { %> - .<%= mapOrFlatMap %>(saved<%= entityClass %> -> { - <%= entityInstance %>SearchRepository.save(saved<%= entityClass %>); - <%_ if(!reactive) { %> - return saved<%= entityClass %>; - <%_ } else { %> - return Mono.just(saved<%= entityClass %>); - <%_ } %> - })<% } %><% if (dto !== 'no') { %> - .map(<%= mapper %>::toDto)<% } %><% if (isService && !reactive) { %> - <% } %>; + <%_ if(dtoMapstruct) { _%> + <%= mapper %>.partialUpdate(existing<%= entityClass %>, <%= instanceName %>); + <%_ } else { _%> + <%_ for (const field of fields.filter(field => !field.id && !field.transient)) { _%> + if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>() != null) { + existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>()); + } + <%_ if (field.fieldWithContentType) { _%> + if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType() != null) { + existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType()); + } + <%_ } _%> + <%_ } %> + <% } %> + return existing<%= entityClass %>; + }) + .<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save) + <%_ if (searchEngineElasticsearch) { _%> + .<%= mapOrFlatMap %>(saved<%= entityClass %> -> { + <%= entityInstance %>SearchRepository.save(saved<%= entityClass %>); + <%_ if(!reactive) { %> + return saved<%= entityClass %>; + <%_ } else { %> + return Mono.just(saved<%= entityClass %>); + <%_ } %> + }) + <%_ } _%> + <%_ if (dtoMapstruct) { _%> + .map(<%= mapper %>::toDto) + <%_ } _%>; <%_ } %> diff --git a/generators/entity-server/templates/src/main/java/package/common/search_stream_template.ejs b/generators/entity-server/templates/src/main/java/package/common/search_stream_template.ejs index bbbe8389d9c..c37a17b0f7f 100644 --- a/generators/entity-server/templates/src/main/java/package/common/search_stream_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/search_stream_template.ejs @@ -20,21 +20,21 @@ const mapper = entityInstance + 'Mapper'; const entityToDtoReference = mapper + '::' + 'toDto'; %> <%_ if (!viaService) { _%> - <%_ if (!reactive) { _%> - <%_ if (searchEngine === 'elasticsearch') { _%> + <%_ if (!reactive) { _%> + <%_ if (searchEngineElasticsearch) { _%> return StreamSupport .stream(<%= entityInstance %>SearchRepository.search(queryStringQuery(query)).spliterator(), false) - <%_ } else { _%> - return <%= entityInstance %>Repository.search(<%= entityClass %>.PREFIX, query)<% if (dto !== 'mapstruct') { %>;<% } else { %>.stream()<% } %> - <%_ } _%> - <%_ if (dto === 'mapstruct') { _%> + <%_ } else { _%> + return <%= entityInstance %>Repository.search(<%= entityClass %>.PREFIX, query)<% if (!dtoMapstruct) { %>;<% } else { %>.stream()<% } %> + <%_ } _%> + <%_ if (dtoMapstruct) { _%> .map(<%= entityToDtoReference %>) - <%_ } _%> - <%_ if (dto === 'mapstruct' || searchEngine === 'elasticsearch') { _%> + <%_ } _%> + <%_ if (dtoMapstruct || searchEngineElasticsearch) { _%> .collect(Collectors.toList()); - <%_ } _%> - <%_ } else { _%> - return <%= entityInstance %><% if (searchEngine === 'elasticsearch') { %>Search<% } %>Repository.search(<% if (searchEngine === 'couchbase') { %><%= entityClass %>.PREFIX, <% } %>query)<%_ if (dto === 'mapstruct') { %> + <%_ } _%> + <%_ } else { _%> + return <%= entityInstance %><% if (searchEngineElasticsearch) { %>Search<% } %>Repository.search(<% if (searchEngineCouchbase) { %><%= entityClass %>.PREFIX, <% } %>query)<%_ if (dtoMapstruct) { %> .map(<%= entityToDtoReference %>)<%_ } %><%_ if (fromResource) { _%>.collectList()<%_ } _%>; <%_ } _%> <%_ } else { _%> diff --git a/generators/entity-server/templates/src/main/java/package/common/search_template.ejs b/generators/entity-server/templates/src/main/java/package/common/search_template.ejs index 11096789d51..111d9e614e3 100644 --- a/generators/entity-server/templates/src/main/java/package/common/search_template.ejs +++ b/generators/entity-server/templates/src/main/java/package/common/search_template.ejs @@ -17,33 +17,33 @@ limitations under the License. -%> <%_ - const instanceType = restClass; - const instanceName = restInstance; - const listOrFlux = (reactive === true) ? 'Flux' : 'List'; - const mapper = entityInstance + 'Mapper'; - const entityToDtoReference = mapper + '::' + 'toDto'; - const entityListToDtoListReference = mapper + '.' + 'toDto'; -if (pagination === 'no') { %> + const instanceType = restClass; + const instanceName = restInstance; + const listOrFlux = reactive ? 'Flux' : 'List'; + const mapper = entityInstance + 'Mapper'; + const entityToDtoReference = mapper + '::' + 'toDto'; + const entityListToDtoListReference = mapper + '.' + 'toDto'; +if (paginationNo) { %> public <% if (reactive) { %>Mono<<% } %>List<<%= instanceType %>><% if (reactive) { %>><% } %> search<%= entityClassPlural %>(@RequestParam String query) { log.debug("REST request to search <%= entityClassPlural %> for query {}", query);<%- include('search_stream_template', {viaService: viaService, fromResource: true}); -%> -<% } if (pagination !== 'no') { %> +<% } if (!paginationNo) { %> public <% if (reactive) { %>Mono<<% } %>ResponseEntity<<%= listOrFlux %><<%= instanceType %>>><% if (reactive) { %>><% } %> search<%= entityClassPlural %>(@RequestParam String query, Pageable pageable<% if (reactive) { %>, ServerHttpRequest request<% } %>) { log.debug("REST request to search for a page of <%= entityClassPlural %> for query {}", query); - <%_ if (!reactive) { _%> - <%_ if (viaService) { _%> + <%_ if (!reactive) { _%> + <%_ if (viaService) { _%> Page<<%= instanceType %>> page = <%= entityInstance %>Service.search(query, pageable); - <%_ } else { _%> - Page<<%= persistClass %>> page = <%= entityInstance %><% if (searchEngine == 'elasticsearch') { %>Search<% } %>Repository.search(<% if (searchEngine === 'elasticsearch') { %>queryStringQuery(query)<% } else { %><%= entityClass %>.PREFIX, query<% } %>, pageable); - <%_ } _%> - HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page); - return ResponseEntity.ok().headers(headers).body(<% if (!viaService && dto === 'mapstruct') { %><%= entityListToDtoListReference %>(<% } %>page.getContent()<% if (!viaService && dto === 'mapstruct') { %>)<% } %>); <%_ } else { _%> + Page<<%= persistClass %>> page = <%= entityInstance %><% if (searchEngineElasticsearch) { %>Search<% } %>Repository.search(<% if (searchEngineElasticsearch) { %>queryStringQuery(query)<% } else { %><%= entityClass %>.PREFIX, query<% } %>, pageable); + <%_ } _%> + HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page); + return ResponseEntity.ok().headers(headers).body(<% if (!viaService && dtoMapstruct) { %><%= entityListToDtoListReference %>(<% } %>page.getContent()<% if (!viaService && dtoMapstruct) { %>)<% } %>); + <%_ } else { _%> return <%= entityInstance %><%= viaService ? - 'Service.' + (searchEngine === 'elasticsearch' ? 'searchCount' : 'countAll') : - (searchEngine === 'elasticsearch' ? 'Search' : '') + 'Repository.count' %>() + 'Service.' + (searchEngineElasticsearch ? 'searchCount' : 'countAll') : + (searchEngineElasticsearch ? 'Search' : '') + 'Repository.count' %>() .map(total -> new PageImpl<>(new ArrayList<>(), pageable, total)) .map(page -> PaginationUtil.generatePaginationHttpHeaders(UriComponentsBuilder.fromHttpRequest(request), page)) - .map(headers -> ResponseEntity.ok().headers(headers).body(<%= entityInstance %><%= viaService ? 'Service' : (searchEngine === 'elasticsearch' ? 'Search' : '') + 'Repository' %>.search(<% if (!viaService && searchEngine === 'couchbase') { %><%= entityClass %>.PREFIX, <% } %>query, pageable)<% if (!viaService && dto === 'mapstruct') { %>.map(<%= entityToDtoReference %>)<% } %>)); - <%_ } _%> - <% } -%> + .map(headers -> ResponseEntity.ok().headers(headers).body(<%= entityInstance %><%= viaService ? 'Service' : (searchEngineElasticsearch ? 'Search' : '') + 'Repository' %>.search(<% if (!viaService && searchEngineCouchbase) { %><%= entityClass %>.PREFIX, <% } %>query, pageable)<% if (!viaService && dtoMapstruct) { %>.map(<%= entityToDtoReference %>)<% } %>)); + <%_ } _%> +<% } -%> } diff --git a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs index a172d398599..071bd354401 100644 --- a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs +++ b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs @@ -20,106 +20,106 @@ package <%= packageName %>.domain; <%_ -let hasDto = dto === 'mapstruct'; +let hasDto = dtoMapstruct; let hasTextBlob = false; let hasRelationship = relationships.length !== 0; _%> <%_ for (const field of fields.filter(field => !field.transient)) { - if (prodDatabaseType === 'postgresql' && field.fieldTypeBlobContent === 'text') { - hasTextBlob = true; - break; - } + if (databaseTypePostgres && field.blobContentTypeText) { + hasTextBlob = true; + break; + } } _%> -<%_ if (databaseType === 'cassandra') { _%> +<%_ if (databaseTypeCassandra) { _%> import org.springframework.data.annotation.Id; - <%_ if (fieldsContainBlob) { _%> + <%_ if (fieldsContainBlob) { _%> import org.springframework.data.cassandra.core.mapping.Column; - <%_ } _%> + <%_ } _%> import org.springframework.data.cassandra.core.mapping.Table; <%_ } _%> -<%_ if (relationshipsContainOtherSideIgnore === true) { _%> +<%_ if (relationshipsContainOtherSideIgnore) { _%> import com.fasterxml.jackson.annotation.JsonIgnoreProperties; <%_ } _%> <%_ if (!hasDto && typeof javadoc != 'undefined') { _%> import io.swagger.annotations.ApiModel; <%_ } _%> -<%_ if (!hasDto && importApiModelProperty === true) { _%> +<%_ if (!hasDto && importApiModelProperty) { _%> import io.swagger.annotations.ApiModelProperty; <%_ } _%> <%_ if (enableHibernateCache) { _%> import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; <%_ } _%> -<%_ if (!reactive && (hasTextBlob === true || (fieldsContainUUID === true && ['mysql', 'mariadb'].includes(prodDatabaseType)))) { _%> +<%_ if (!reactive && (hasTextBlob || (fieldsContainUUID && (databaseTypeMysql || databaseTypeMariadb)))) { _%> import org.hibernate.annotations.Type; <%_ } _%> -<%_ if (databaseType === 'mongodb') { - if (!embedded) { _%> +<%_ if (databaseTypeMongodb) { + if (!embedded) { _%> import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; - <%_ } _%> + <%_ } _%> import org.springframework.data.mongodb.core.mapping.Field; - <%_ if (hasRelationship) { _%> + <%_ if (hasRelationship) { _%> import org.springframework.data.mongodb.core.mapping.DBRef; - <%_ } _%> -<%_ } else if (databaseType === 'couchbase') { - if (!embedded) { _%> + <%_ } _%> +<%_ } else if (databaseTypeCouchbase) { + if (!embedded) { _%> import org.springframework.data.annotation.Id; import org.springframework.data.couchbase.core.mapping.Document; import org.springframework.data.couchbase.core.mapping.id.GeneratedValue; import org.springframework.data.couchbase.core.mapping.id.IdPrefix; - <%_ } _%> + <%_ } _%> import com.couchbase.client.java.repository.annotation.Field; - <%_ if (hasRelationship) { _%> + <%_ if (hasRelationship) { _%> import org.springframework.data.couchbase.core.query.FetchType; import org.springframework.data.couchbase.core.query.N1qlJoin; - <%_ } _%> -<%_ } else if (databaseType === 'neo4j') { _%> + <%_ } _%> +<%_ } else if (databaseTypeNeo4j) { _%> import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; import org.springframework.data.neo4j.core.schema.Property; import org.springframework.data.neo4j.core.support.UUIDStringGenerator; -<%_ if (hasRelationship) { _%> + <%_ if (hasRelationship) { _%> import org.springframework.data.neo4j.core.schema.Relationship; -<%_ } _%> -<%_ } if (databaseType === 'sql') { _%> - <%_ if (reactive) { _%> + <%_ } _%> +<%_ } if (databaseTypeSql) { _%> + <%_ if (reactive) { _%> import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.Table; - <%_ } else { _%> + <%_ } else { _%> import javax.persistence.*; - <%_ } _%> + <%_ } _%> <%_ } if (validation) { _%> import javax.validation.constraints.*; <%_ } _%> -<%_ if (searchEngine === 'elasticsearch') { _%> +<%_ if (searchEngineElasticsearch) { _%> import org.springframework.data.elasticsearch.annotations.FieldType; <%_ } _%> import java.io.Serializable; -<%_ if (fieldsContainBigDecimal === true) { _%> +<%_ if (fieldsContainBigDecimal) { _%> import java.math.BigDecimal; -<%_ } if (fieldsContainBlob && databaseType === 'cassandra') { _%> +<%_ } if (fieldsContainBlob && databaseTypeCassandra) { _%> import java.nio.ByteBuffer; -<%_ } if (fieldsContainInstant === true) { _%> +<%_ } if (fieldsContainInstant) { _%> import java.time.Instant; -<%_ } if (fieldsContainLocalDate === true) { _%> +<%_ } if (fieldsContainLocalDate) { _%> import java.time.LocalDate; -<%_ } if (fieldsContainZonedDateTime === true) { _%> +<%_ } if (fieldsContainZonedDateTime) { _%> import java.time.ZonedDateTime; -<%_ } if (fieldsContainDuration === true) { _%> +<%_ } if (fieldsContainDuration) { _%> import java.time.Duration; <%_ } if (entityContainsCollectionField) { _%> import java.util.HashSet; import java.util.Set; -<%_ } if (databaseType === 'couchbase' && hasRelationship) { _%> +<%_ } if (databaseTypeCouchbase && hasRelationship) { _%> import java.util.stream.Collectors; <%_ } _%> <%_ if (fieldsContainUUID) { _%> @@ -130,7 +130,7 @@ Object.keys(uniqueEnums).forEach(function(element) { _%> import <%= packageName %>.domain.enumeration.<%= element %>; <%_ }); _%> -<%_ if (databaseType === 'couchbase' && !embedded) { _%> +<%_ if (databaseTypeCouchbase && !embedded) { _%> import static <%= packageName %>.config.Constants.ID_DELIMITER; import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.UNIQUE; @@ -141,11 +141,11 @@ import static org.springframework.data.couchbase.core.mapping.id.GenerationStrat */ <%_ } else { _%> <%- formatAsClassJavadoc(javadoc) %> - <%_ if (!hasDto) { _%> + <%_ if (!hasDto) { _%> @ApiModel(description = "<%- formatAsApiDescription(javadoc) %>") - <%_ } _%> + <%_ } _%> <%_ } _%> -<%_ if (databaseType === 'sql') { _%> +<%_ if (databaseTypeSql) { _%> <%_ if (reactive) { _%> @Table("<%= entityTableName %>") <%_ } else { _%> @@ -157,189 +157,186 @@ import static org.springframework.data.couchbase.core.mapping.id.GenerationStrat <%_ } _%> <%_ } _%> <%_ if (!embedded) { _%> - <%_ if (databaseType === 'mongodb') { _%> + <%_ if (databaseTypeMongodb) { _%> @Document(collection = "<%= entityTableName %>") - <%_ } else if (databaseType === 'neo4j') { _%> + <%_ } else if (databaseTypeNeo4j) { _%> @Node - <%_ } else if (databaseType === 'couchbase') { _%> + <%_ } else if (databaseTypeCouchbase) { _%> @Document - <%_ } else if (databaseType === 'cassandra') { _%> + <%_ } else if (databaseTypeCassandra) { _%> @Table("<%= entityInstance %>") - <%_ } _%> + <%_ } _%> <%_ } _%> -<%_ if (searchEngine === 'elasticsearch') { _%> +<%_ if (searchEngineElasticsearch) { _%> @org.springframework.data.elasticsearch.annotations.Document(indexName = "<%= entityInstance.toLowerCase() %>") <%_ } _%> public class <%= persistClass %> implements Serializable { private static final long serialVersionUID = 1L; - <%_ if (databaseType === 'couchbase' && !embedded) { _%> +<%_ if (databaseTypeCouchbase && !embedded) { _%> public static final String PREFIX = "<%= entityInstance.toLowerCase() %>"; @SuppressWarnings("unused") @IdPrefix private String prefix = PREFIX; - <%_ } _%> +<%_ } _%> <%_ if (!embedded) { _%> @Id - <%_ if (databaseType === 'sql' && isUsingMapsId === false && !reactive && !primaryKey.composite) { _%> - <%_ if (primaryKey.fields[0].jpaGeneratedValue === 'identity') { _%> + <%_ if (databaseTypeSql && !isUsingMapsId && !reactive && !primaryKey.composite) { _%> + <%_ if (primaryKey.fields[0].jpaGeneratedValue === 'identity') { _%> @GeneratedValue(strategy = GenerationType.IDENTITY) - <%_ } else if (primaryKey.fields[0].jpaGeneratedValue === 'sequence') { _%> + <%_ } else if (primaryKey.fields[0].jpaGeneratedValue === 'sequence') { _%> @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") @SequenceGenerator(name = "sequenceGenerator") - <%_ } else if (primaryKey.fields[0].jpaGeneratedValue) { _%> + <%_ } else if (primaryKey.fields[0].jpaGeneratedValue) { _%> @GeneratedValue - <%_ } _%> - <%_ } else if (databaseType === 'couchbase' && !embedded) { _%> + <%_ } _%> + <%_ } else if (databaseTypeCouchbase && !embedded) { _%> @GeneratedValue(strategy = UNIQUE, delimiter = ID_DELIMITER) - <%_ } else if (databaseType === 'neo4j') { _%> + <%_ } else if (databaseTypeNeo4j) { _%> @GeneratedValue(UUIDStringGenerator.class) - <%_ } _%> + <%_ } _%> private <%= primaryKey.type %> <%= primaryKey.name %>; <%_ } _%> <%_ for (const field of fields.filter(field => !field.id && !field.transient)) { - if (typeof field.javadoc !== 'undefined') { _%> + if (typeof field.javadoc !== 'undefined') { _%> <%- formatAsFieldJavadoc(field.javadoc) %> - <%_ } - let required = false; - let unique = false; - const fieldValidate = field.fieldValidate; - const fieldValidateRules = field.fieldValidateRules; - const fieldValidateRulesMaxlength = field.fieldValidateRulesMaxlength; - const fieldType = field.fieldType; - const fieldTypeBlobContent = field.fieldTypeBlobContent; - const fieldName = field.fieldName; - const fieldNameUnderscored = field.fieldNameUnderscored; - const fieldNameAsDatabaseColumn = field.fieldNameAsDatabaseColumn; - if (fieldValidate === true) { - if (fieldValidateRules.includes('required')) { - required = true; - } - if (fieldValidateRules.includes('unique')) { - unique = true; - } _%> + <%_ } + let required = false; + let unique = false; + const fieldValidate = field.fieldValidate; + const fieldValidateRules = field.fieldValidateRules; + const fieldValidateRulesMaxlength = field.fieldValidateRulesMaxlength; + const fieldType = field.fieldType; + const fieldName = field.fieldName; + const fieldNameUnderscored = field.fieldNameUnderscored; + const fieldNameAsDatabaseColumn = field.fieldNameAsDatabaseColumn; + if (fieldValidate === true) { + if (field.fieldValidationRequired) { + required = true; + } + if (field.fieldValidationUnique) { + unique = true; + } _%> <%- include('../common/field_validators', {field, reactive}); -%> - <%_ } _%> - <%_ if (!hasDto && typeof field.javadoc != 'undefined') { _%> + <%_ } _%> + <%_ if (!hasDto && typeof field.javadoc != 'undefined') { _%> @ApiModelProperty(value = "<%- formatAsApiDescription(field.javadoc) %>"<% if (required) { %>, required = true<% } %>) - <%_ } _%> - <%_ if (databaseType === 'sql' && reactive) { _%> + <%_ } _%> + <%_ if (databaseTypeSql && reactive) { _%> @Column("<%- fieldNameAsDatabaseColumn %>") - <%_ } _%> - <%_ if (databaseType === 'sql' && !reactive) { + <%_ } _%> + <%_ if (databaseTypeSql && !reactive) { if (field.fieldIsEnum) { _%> @Enumerated(EnumType.STRING) - <%_ } - if (fieldType === 'byte[]') { _%> + <%_ } _%> + <%_ if (field.fieldTypeBytes) { _%> @Lob - <%_ if ((prodDatabaseType === 'postgresql' || devDatabaseType === 'postgresql') && fieldTypeBlobContent === 'text' ) { _%> + <%_ if (databaseTypePostgres && field.blobContentTypeText) { _%> @Type(type = "org.hibernate.type.TextType") - <%_ } - } - if (['Instant', 'ZonedDateTime', 'LocalDate', 'Duration'].includes(fieldType)) { _%> + <%_ } _%> + <%_ } _%> + <%_ if (field.fieldTypeTemporal || field.fieldTypeDuration) { _%> @Column(name = "<%- fieldNameAsDatabaseColumn %>"<% if (required) { %>, nullable = false<% } %><% if (unique) { %>, unique = true<% } %>) - <%_ } else if (fieldType === 'BigDecimal') { _%> + <%_ } else if (field.fieldTypeBigDecimal) { _%> @Column(name = "<%- fieldNameAsDatabaseColumn %>", precision = 21, scale = 2<% if (required) { %>, nullable = false<% } %><% if (unique) { %>, unique = true<% } %>) - <%_ } else if (fieldType === 'UUID' && ['mysql', 'mariadb'].includes(prodDatabaseType)) { _%> + <%_ } else if (field.fieldTypeUUID && (databaseTypeMysql || databaseTypeMariadb)) { _%> @Type(type = "uuid-char") @Column(name = "<%- fieldNameAsDatabaseColumn %>", length = 36<% if (required) { %>, nullable = false<% } %><% if (unique) { %>, unique = true<% } %>) - <%_ } else { _%> - @Column(name = "<%- fieldNameAsDatabaseColumn %>"<% if (fieldValidate === true) { %><% if (fieldValidateRules.includes('maxlength')) { %>, length = <%= fieldValidateRulesMaxlength %><% } %><% if (required) { %>, nullable = false<% } %><% if (unique) { %>, unique = true<% } %><% } %>) - <%_ } - } _%> - <%_ if (databaseType === 'mongodb' || databaseType === 'couchbase') { _%> - @Field("<%= fieldNameUnderscored %>") + <%_ } else { _%> + @Column(name = "<%- fieldNameAsDatabaseColumn %>"<% if (fieldValidate === true) { %><% if (field.fieldValidationMaxLength) { %>, length = <%= fieldValidateRulesMaxlength %><% } %><% if (required) { %>, nullable = false<% } %><% if (unique) { %>, unique = true<% } %><% } %>) <%_ } _%> - <%_ if (databaseType === 'neo4j') { _%> + <%_ } _%> + <%_ if (databaseTypeMongodb || databaseTypeCouchbase) { _%> + @Field("<%= fieldNameUnderscored %>") + <%_ } _%> + <%_ if (databaseTypeNeo4j) { _%> @Property("<%=fieldNameUnderscored %>") - <%_ } _%> - <%_ if (fieldTypeBlobContent !== 'text') { _%> + <%_ } _%> + <%_ if (!field.blobContentTypeText) { _%> private <%= fieldType %> <%= fieldName %>; - <%_ } else { _%> + <%_ } else { _%> private String <%= fieldName %>; - <%_ } _%> + <%_ } _%> - <%_ if (field.fieldWithContentType) { _%> - <%_ if ((databaseType === 'sql' && !reactive) || databaseType === 'cassandra') { _%> - @Column(<% if (databaseType !== 'cassandra') { %>name = <% } %>"<%- fieldNameAsDatabaseColumn %>_content_type"<% if (required && databaseType !== 'cassandra') { %>, nullable = false<% } %>) - <%_ if (required && databaseType === 'cassandra') { _%> + <%_ if (field.fieldWithContentType) { _%> + <%_ if ((databaseTypeSql && !reactive) || databaseTypeCassandra) { _%> + @Column(<% if (!databaseTypeCassandra) { %>name = <% } %>"<%- fieldNameAsDatabaseColumn %>_content_type"<% if (required && !databaseTypeCassandra) { %>, nullable = false<% } %>) + <%_ if (required && databaseTypeCassandra) { _%> @NotNull - <%_ } _%> <%_ } _%> - <%_ if (databaseType === 'sql' && reactive) { _%> + <%_ } _%> + <%_ if (databaseTypeSql && reactive) { _%> @Column("<%- fieldNameAsDatabaseColumn %>_content_type") - <%_ if (required) { _%> + <%_ if (required) { _%> @NotNull - <%_ } _%> <%_ } _%> - <%_ if (databaseType === 'mongodb' || databaseType === 'couchbase') { _%> + <%_ } _%> + <%_ if (databaseTypeMongodb || databaseTypeCouchbase) { _%> @Field("<%= fieldNameUnderscored %>_content_type") - <%_ } _%> - <%_ if (databaseType === 'neo4j') { _%> + <%_ } _%> + <%_ if (databaseTypeNeo4j) { _%> @Property("<%=fieldNameUnderscored %>_content_type") - <%_ } _%> + <%_ } _%> private String <%= fieldName %>ContentType; -<%_ } -} + <%_ } _%> +<%_ } _%> -relationships.forEach((relationship, idx) => { - const otherEntityRelationshipName = - databaseType === 'neo4j' - ? "HAS_" + _.toUpper(_.snakeCase(relationship.otherEntityRelationshipName)) - : relationship.otherEntityRelationshipName; - const otherEntityRelationshipNamePlural = relationship.otherEntityRelationshipNamePlural; - const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; - const relationshipName = - databaseType === 'neo4j' - ? "HAS_" + _.toUpper(_.snakeCase(relationship.relationshipName)) - : relationship.relationshipName; - const relationshipFieldName = relationship.relationshipFieldName; - const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; - const relationshipType = relationship.relationshipType; - const relationshipValidate = relationship.relationshipValidate; - const relationshipRequired = relationship.relationshipRequired; - const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; - const ownerSide = relationship.ownerSide || false; - const isUsingMapsId = relationship.id; - if (otherEntityRelationshipName) { - mappedBy = otherEntityRelationshipName.charAt(0).toLowerCase() + otherEntityRelationshipName.slice(1) - } +<%_ relationships.forEach((relationship, idx) => { + const otherEntityRelationshipName = + databaseTypeNeo4j + ? "HAS_" + _.toUpper(_.snakeCase(relationship.otherEntityRelationshipName)) + : relationship.otherEntityRelationshipName; + const otherEntityRelationshipNamePlural = relationship.otherEntityRelationshipNamePlural; + const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; + const relationshipName = + databaseTypeNeo4j + ? "HAS_" + _.toUpper(_.snakeCase(relationship.relationshipName)) + : relationship.relationshipName; + const relationshipFieldName = relationship.relationshipFieldName; + const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; + const relationshipValidate = relationship.relationshipValidate; + const relationshipRequired = relationship.relationshipRequired; + const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; + const ownerSide = relationship.ownerSide || false; + const isUsingMapsId = relationship.id; + if (otherEntityRelationshipName) { + mappedBy = otherEntityRelationshipName.charAt(0).toLowerCase() + otherEntityRelationshipName.slice(1) + } // An embedded entity should not reference entities that embeds it, unless the other entity is also embedded - if (embedded && !otherEntityIsEmbedded && ownerSide === false) { - return; - } + if (embedded && !otherEntityIsEmbedded && ownerSide === false) { + return; + } - if (typeof relationship.javadoc != 'undefined') { _%> + if (typeof relationship.javadoc != 'undefined') { _%> <%- formatAsFieldJavadoc(relationship.javadoc) %> <%_ if (!hasDto) { _%> @ApiModelProperty(value = "<%- formatAsApiDescription(relationship.javadoc) %>") <%_ } _%> -<%_ } - if (relationshipType === 'one-to-many') { - if (databaseType === 'sql' && !reactive) { - _%> + <%_ } _%> + <%_ if (relationship.relationshipOneToMany) { _%> + <%_ if (databaseTypeSql && !reactive) { _%> @OneToMany(mappedBy = "<%= otherEntityRelationshipName %>") - <%_ if (enableHibernateCache) { _%> + <%_ if (enableHibernateCache) { _%> @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) - <%_ } - } else if (databaseType === 'mongodb' || databaseType === 'couchbase') { - if (databaseType === 'mongodb' && !otherEntityIsEmbedded) { _%> + <%_ } _%> + <%_ } else if (databaseTypeMongodb || databaseTypeCouchbase) { + if (databaseTypeMongodb && !otherEntityIsEmbedded) { _%> @DBRef - <%_ } _%> + <%_ } _%> @Field("<%= relationshipFieldName %>") - <%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%> + <%_ if (databaseTypeCouchbase && !otherEntityIsEmbedded) { _%> private Set <%= relationshipFieldName %>Ids = new HashSet<>(); @N1qlJoin(on = "lks.<%= relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE) - <%_ } _%> - <%_ } else if (databaseType === 'neo4j') { _%> + <%_ } _%> + <%_ } else if (databaseTypeNeo4j) { _%> @Relationship("<%= relationshipName %>") - <%_ } else if (databaseType === 'sql' && reactive) { _%> + <%_ } else if (databaseTypeSql && reactive) { _%> @Transient <%_ } _%> <%_ if (relationship.ignoreOtherSideProperty) { _%> @@ -351,54 +348,53 @@ relationships.forEach((relationship, idx) => { <%_ } _%> private Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= relationshipFieldNamePlural %> = new HashSet<>(); -<%_ } else if (relationshipType === 'many-to-one') { - if (databaseType === 'sql' && !reactive) { - _%> + <%_ } else if (relationship.relationshipManyToOne) { + if (databaseTypeSql && !reactive) { _%> @ManyToOne<% if (relationshipRequired) { %>(optional = false)<% } %> - <%_ if (relationshipValidate) { _%> + <%_ if (relationshipValidate) { _%> <%- include('relationship_validators', { relationship }); -%> - <%_ } - } else if ((databaseType === 'mongodb' || databaseType === 'couchbase') && !otherEntityIsEmbedded) { - if (databaseType === 'mongodb') { _%> + <%_ } _%> + <%_ } else if ((databaseTypeMongodb || databaseTypeCouchbase) && !otherEntityIsEmbedded) { + if (databaseTypeMongodb) { _%> @DBRef - <%_ } _%> + <%_ } _%> @Field("<%= relationshipFieldName %>") - <%_ if (databaseType === 'couchbase') { _%> + <%_ if (databaseTypeCouchbase) { _%> private String <%= relationshipFieldName %>Id; @N1qlJoin(on = "lks.<%= relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE) - <%_ } - } else if (databaseType === 'neo4j') { - if (relationship.ignoreOtherSideProperty) { _%> + <%_ } _%> + <%_ } else if (databaseTypeNeo4j) { _%> + <%_ if (relationship.ignoreOtherSideProperty) { _%> @Relationship(value = "<%= otherEntityRelationshipName %>", direction = Relationship.Direction.INCOMING) - <%_ } else {_%> + <%_ } else {_%> @Relationship("<%= relationshipName %>") - <%_ } - } _%> + <%_ } _%> + <%_ } _%> <%_ if (relationship.ignoreOtherSideProperty) { _%> @JsonIgnoreProperties(value = { - <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%> + <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%> "<%= otherRelationship.relationshipReferenceField %>", - <%_ }); _%> + <%_ }); _%> }, allowSetters = true) <%_ } _%> - <%_ if (databaseType === 'sql' && reactive) { _%> + <%_ if (databaseTypeSql && reactive) { _%> @Transient <%_ } _%> private <%= asEntity(otherEntityNameCapitalized) %> <%= relationshipFieldName %>; - <%_ if (databaseType === 'sql' && reactive) { _%> + <%_ if (databaseTypeSql && reactive) { _%> @Column("<%= getColumnName(relationshipName) %>_id") private <%= relationship.otherEntity.primaryKey.type %> <%= relationshipFieldName %>Id; <%_ } _%> -<%_ } else if (relationshipType === 'many-to-many') { - if (databaseType === 'sql' && !reactive) { _%> - @ManyToMany<% if (ownerSide === false) { %>(mappedBy = "<%= otherEntityRelationshipNamePlural %>")<% } %> - <%_ if (enableHibernateCache) {_%> + <%_ } else if (relationship.relationshipManyToMany) { _%> + <%_ if (databaseTypeSql && !reactive) { _%> + @ManyToMany<% if (!ownerSide) { %>(mappedBy = "<%= otherEntityRelationshipNamePlural %>")<% } %> + <%_ if (enableHibernateCache) {_%> @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) - <%_ } if (ownerSide === true) { - if (relationshipValidate) { _%> + <%_ } if (ownerSide) { _%> + <%_ if (relationshipValidate) { _%> <%- include('relationship_validators', { relationship }); -%> <%_ } _%> @JoinTable(name = "<%= relationship.joinTable.name %>", @@ -412,80 +408,80 @@ relationships.forEach((relationship, idx) => { <%= idx === 0 ? '' : ',' %>@JoinColumn(name = "<%= `${relationship.columnName}_${field.columnName}` %>")) <%_ }); _%> <%= relationship.otherEntity.primaryKey.fields.length > 1 ? '}' : '' %> - <%_ } - } else if ((databaseType === 'mongodb' || databaseType === 'couchbase') && !otherEntityIsEmbedded) { - if (databaseType === 'mongodb') { _%> + <%_ } _%> + <%_ } else if ((databaseTypeMongodb || databaseTypeCouchbase) && !otherEntityIsEmbedded) { _%> + <%_ if (databaseTypeMongodb) { _%> @DBRef - <%_ } _%> + <%_ } _%> @Field("<%= relationshipFieldNamePlural %>") - <%_ if (databaseType === 'couchbase') { _%> + <%_ if (databaseTypeCouchbase) { _%> private Set <%= relationshipFieldName %>Ids = new HashSet<>(); @N1qlJoin(on = "lks.<%= relationshipFieldNamePlural %>=meta(rks).id", fetchType = FetchType.IMMEDIATE) - <%_ } - } else if (databaseType === 'neo4j') { _%> + <%_ } _%> + <%_ } else if (databaseTypeNeo4j) { _%> @Relationship(<%_ - if (ownerSide === true) { _%>"<%= relationshipName %>"<%_ - } else { _%>value = "<%= otherEntityRelationshipName %>", direction = Relationship.Direction.INCOMING<%_ } _%>)<%_ - } - if (relationship.ignoreOtherSideProperty) { _%> + if (ownerSide) { _%>"<%= relationshipName %>"<%_ + } else { _%>value = "<%= otherEntityRelationshipName %>", direction = Relationship.Direction.INCOMING<%_ } _%>) + <%_ } _%> + <%_ if (relationship.ignoreOtherSideProperty) { _%> @JsonIgnoreProperties(value = { - <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%> + <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%> "<%= otherRelationship.relationshipReferenceField %>", - <%_ }); _%> + <%_ }); _%> }, allowSetters = true) <%_ } _%> - <%_ if (databaseType === 'sql' && reactive) { _%> + <%_ if (databaseTypeSql && reactive) { _%> @Transient <%_ } _%> private Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= relationshipFieldNamePlural %> = new HashSet<>(); -<%_ } else { - if (databaseType === 'sql' && !reactive) { - if (relationship.ignoreOtherSideProperty) { _%> + <%_ } else { + if (databaseTypeSql && !reactive) { + if (relationship.ignoreOtherSideProperty) { _%> @JsonIgnoreProperties(value = { <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%> "<%= otherRelationship.relationshipReferenceField %>", <%_ }); _%> }, allowSetters = true) - <%_ } - if (ownerSide) { _%> + <%_ } _%> + <%_ if (ownerSide) { _%> @OneToOne<% if (relationshipRequired) { %>(optional = false)<% } %> - <%_ if (relationshipValidate) { _%> + <%_ if (relationshipValidate) { _%> <%- include('relationship_validators', { relationship }); -%> - <%_ } _%> - <%_ if (isUsingMapsId === true) { %> + <%_ } _%> + <%_ if (isUsingMapsId) { %> @MapsId @JoinColumn(name = "<%= relationship.otherEntity.primaryKey.fields[0].columnName %>") - <%_ } else { _%> - @JoinColumn(unique = true) - <%_ } _%> <%_ } else { _%> + @JoinColumn(unique = true) + <%_ } _%> + <%_ } else { _%> @OneToOne(mappedBy = "<%= otherEntityRelationshipName %>") - <%_ } - } else if ((databaseType === 'mongodb' || databaseType === 'couchbase') && !otherEntityIsEmbedded) { - if (databaseType === 'mongodb') { _%> + <%_ } _%> + <%_ } else if ((databaseTypeMongodb || databaseTypeCouchbase) && !otherEntityIsEmbedded) { _%> + <%_ if (databaseTypeMongodb) { _%> @DBRef - <%_ } _%> + <%_ } _%> @Field("<%= relationshipFieldName %>") - <%_ if (databaseType === 'couchbase') { _%> + <%_ if (databaseTypeCouchbase) { _%> private String <%= relationshipFieldName %>Id; @N1qlJoin(on = "lks.<%= relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE) - <%_ } - } else if (databaseType === 'sql' && reactive) { _%> - <%_ if (ownerSide === true && isUsingMapsId !== true) { _%> + <%_ } _%> + <%_ } else if (databaseTypeSql && reactive) { _%> + <%_ if (ownerSide && !isUsingMapsId) { _%> private <%= relationship.otherEntity.primaryKey.type %> <%= relationshipFieldName %>Id; - <%_ } _%> + <%_ } _%> @Transient - <%_ } else if (databaseType === 'neo4j') { _%> + <%_ } else if (databaseTypeNeo4j) { _%> @Relationship("<%= relationshipName %>") - <%_ } _%> + <%_ } _%> private <%= asEntity(otherEntityNameCapitalized) %> <%= relationshipFieldName %>; -<%_ } -}); _%> + <%_ } _%> +<%_ }); _%> // jhipster-needle-entity-add-field - JHipster will add fields here <%_ if (!embedded) { _%> public <%= primaryKey.type %> get<%= primaryKey.nameCapitalized %>() { @@ -496,54 +492,53 @@ relationships.forEach((relationship, idx) => { this.<%= primaryKey.name %> = <%= primaryKey.name %>; } - <%_ if (fluentMethods) { _%> + <%_ if (fluentMethods) { _%> public <%= persistClass %> <%= primaryKey.name %>(<%= primaryKey.type %> <%= primaryKey.name %>) { this.<%= primaryKey.name %> = <%= primaryKey.name %>; return this; } - <%_ } _%> + <%_ } _%> <%_ } _%> <%_ for (const field of fields.filter(field => !field.id && !field.transient)) { - const fieldType = field.fieldType; - const fieldTypeBlobContent = field.fieldTypeBlobContent; - const fieldName = field.fieldName; - const fieldInJavaBeanMethod = field.fieldInJavaBeanMethod; _%> + const fieldType = field.fieldType; + const fieldName = field.fieldName; + const fieldInJavaBeanMethod = field.fieldInJavaBeanMethod; _%> - <%_ if (fieldTypeBlobContent !== 'text') { _%> + <%_ if (!field.blobContentTypeText) { _%> public <%= fieldType %> get<%= fieldInJavaBeanMethod %>() { - <%_ } else { _%> + <%_ } else { _%> public String get<%= fieldInJavaBeanMethod %>() { - <%_ } _%> + <%_ } _%> return this.<%= fieldName %>; } - <%_ if (fluentMethods) { _%> + <%_ if (fluentMethods) { _%> - <%_ if (fieldTypeBlobContent !== 'text') { _%> + <%_ if (!field.blobContentTypeText) { _%> public <%= persistClass %> <%= fieldName %>(<%= fieldType %> <%= fieldName %>) { - <%_ } else { _%> + <%_ } else { _%> public <%= persistClass %> <%= fieldName %>(String <%= fieldName %>) { - <%_ } _%> - <%_ if (fieldType === 'BigDecimal' && databaseType === 'sql' && reactive) { _%> + <%_ } _%> + <%_ if (field.fieldTypeBigDecimal && databaseTypeSql && reactive) { _%> this.<%= fieldName %> = <%= fieldName %> != null ? <%= fieldName %>.stripTrailingZeros() : null; - <%_ } else { _%> + <%_ } else { _%> this.<%= fieldName %> = <%= fieldName %>; - <%_ } _%> + <%_ } _%> return this; } - <%_ } _%> + <%_ } _%> - <%_ if (fieldTypeBlobContent !== 'text') { _%> + <%_ if (!field.blobContentTypeText) { _%> public void set<%= fieldInJavaBeanMethod %>(<%= fieldType %> <%= fieldName %>) { - <%_ } else { _%> + <%_ } else { _%> public void set<%= fieldInJavaBeanMethod %>(String <%= fieldName %>) { - <%_ } _%> - <%_ if (fieldType === 'BigDecimal' && databaseType === 'sql' && reactive) { _%> + <%_ } _%> + <%_ if (field.fieldTypeBigDecimal && databaseTypeSql && reactive) { _%> this.<%= fieldName %> = <%= fieldName %> != null ? <%= fieldName %>.stripTrailingZeros() : null; - <%_ } else { _%> + <%_ } else { _%> this.<%= fieldName %> = <%= fieldName %>; - <%_ } _%> + <%_ } _%> } - <%_ if (field.fieldWithContentType) { _%> + <%_ if (field.fieldWithContentType) { _%> public String get<%= fieldInJavaBeanMethod %>ContentType() { return this.<%= fieldName %>ContentType; @@ -559,153 +554,152 @@ relationships.forEach((relationship, idx) => { public void set<%= fieldInJavaBeanMethod %>ContentType(String <%= fieldName %>ContentType) { this.<%= fieldName %>ContentType = <%= fieldName %>ContentType; } - <%_ } _%> + <%_ } _%> <%_ } _%> <%_ - for (const relationship of relationships) { - const relationshipFieldName = relationship.relationshipFieldName; - const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; - const relationshipType = relationship.relationshipType; - const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; - const relationshipNameCapitalized = relationship.relationshipNameCapitalized; - const relationshipNameCapitalizedPlural = relationship.relationshipNameCapitalizedPlural; - const otherEntityName = relationship.otherEntityName; - const otherEntityNamePlural = relationship.otherEntityNamePlural; - const otherEntityRelationshipNameCapitalized = relationship.otherEntityRelationshipNameCapitalized; - const otherEntityRelationshipNameCapitalizedPlural = relationship.otherEntityRelationshipNameCapitalizedPlural; - const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; - const ownerSide = relationship.ownerSide || false; +for (const relationship of relationships) { + const relationshipFieldName = relationship.relationshipFieldName; + const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; + const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; + const relationshipNameCapitalized = relationship.relationshipNameCapitalized; + const relationshipNameCapitalizedPlural = relationship.relationshipNameCapitalizedPlural; + const otherEntityName = relationship.otherEntityName; + const otherEntityNamePlural = relationship.otherEntityNamePlural; + const otherEntityRelationshipNameCapitalized = relationship.otherEntityRelationshipNameCapitalized; + const otherEntityRelationshipNameCapitalizedPlural = relationship.otherEntityRelationshipNameCapitalizedPlural; + const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; + const ownerSide = relationship.ownerSide || false; // An embedded entity should not reference entities that embeds it, unless the other entity is also embedded - if (embedded && !otherEntityIsEmbedded && ownerSide === false) { - continue; - } + if (embedded && !otherEntityIsEmbedded && !ownerSide) { + continue; + } - const id = relationship.id; - const reactiveRelationshipWithId = (databaseType === 'sql' && reactive && !(relationshipType === 'one-to-one' && - (ownerSide === false || (ownerSide === true && id === true)))); - _%> - <%_ if (relationshipType === 'one-to-many' || relationshipType === 'many-to-many') { _%> + const id = relationship.id; + const reactiveRelationshipWithId = (databaseTypeSql && reactive && !(relationship.relationshipOneToOne && + (!ownerSide || (ownerSide && id)))); +_%> + <%_ if (relationship.relationshipOneToMany || relationship.relationshipManyToMany) { _%> public Set<<%= asEntity(otherEntityNameCapitalized) %>> get<%= relationshipNameCapitalizedPlural %>() { return this.<%= relationshipFieldNamePlural %>; } - <%_ if (fluentMethods) { _%> + <%_ if (fluentMethods) { _%> public <%= persistClass %> <%= relationshipFieldNamePlural %>(Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= otherEntityNamePlural %>) { this.set<%= relationshipNameCapitalizedPlural %>(<%= otherEntityNamePlural %>); - <%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%> + <%_ if (databaseTypeCouchbase && !otherEntityIsEmbedded) { _%> this.<%= relationshipFieldName %>Ids = <%= otherEntityNamePlural %>.stream() .map(<%= asEntity(otherEntityNameCapitalized) %>::getId) .collect(Collectors.toSet()); - <%_ } _%> + <%_ } _%> return this; } public <%= persistClass %> add<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) { this.<%= relationshipFieldNamePlural %>.add(<%= otherEntityName %>); - <%_ if (!otherEntityIsEmbedded || embedded && ownerSide === true) { _%> - <%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%> + <%_ if (!otherEntityIsEmbedded || embedded && ownerSide) { _%> + <%_ if (databaseTypeCouchbase && !otherEntityIsEmbedded) { _%> this.<%= relationshipFieldName %>Ids.add(<%= otherEntityName %>.get<%= primaryKey.nameCapitalized %>()); - <%_ } _%> - <%_ if (relationship.otherRelationship) { - if (relationshipType === 'one-to-many') { - if (databaseType !== 'neo4j') { _%> + <%_ } _%> + <%_ if (relationship.otherRelationship) { _%> + <%_ if (relationship.relationshipOneToMany) { _%> + <%_ if (!databaseTypeNeo4j) { _%> <%= otherEntityName %>.set<%= otherEntityRelationshipNameCapitalized %>(this); - <%_ } _%> - <%_ } else if (otherEntityRelationshipNameCapitalizedPlural && asEntity(otherEntityNameCapitalized)!=='User' && relationshipType === 'many-to-many') { - // JHipster version < 3.6.0 didn't ask for this relationship name _%> - <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().add(this); - <%_ } _%> <%_ } _%> + <%_ } else if (otherEntityRelationshipNameCapitalizedPlural && !otherEntityUser && relationship.relationshipManyToMany) { + /* JHipster version < 3.6.0 didn't ask for this relationship name */ _%> + <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().add(this); + <%_ } _%> <%_ } _%> + <%_ } _%> return this; } public <%= persistClass %> remove<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) { this.<%= relationshipFieldNamePlural %>.remove(<%= otherEntityName %>); - <%_ if (!otherEntityIsEmbedded || (embedded && ownerSide === true)) { _%> - <%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%> + <%_ if (!otherEntityIsEmbedded || (embedded && ownerSide)) { _%> + <%_ if (databaseTypeCouchbase && !otherEntityIsEmbedded) { _%> this.<%= relationshipFieldName %>Ids.remove(<%= otherEntityName %>.get<%= primaryKey.nameCapitalized %>()); - <%_ } _%> - <%_ if (relationship.otherRelationship) { - if (relationshipType === 'one-to-many') { - if (databaseType !== 'neo4j') { _%> + <%_ } _%> + <%_ if (relationship.otherRelationship) { _%> + <%_ if (relationship.relationshipOneToMany) { _%> + <%_ if (!databaseTypeNeo4j) { _%> <%= otherEntityName %>.set<%= otherEntityRelationshipNameCapitalized %>(null); - <%_ } _%> - <%_ } else if (otherEntityRelationshipNameCapitalizedPlural !== '' && asEntity(otherEntityNameCapitalized)!=='User' && relationshipType === 'many-to-many') { - // JHipster version < 3.6.0 didn't ask for this relationship name _%> - <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().remove(this); - <%_ } _%> <%_ } _%> + <%_ } else if (otherEntityRelationshipNameCapitalizedPlural && !otherEntityUser && relationship.relationshipManyToMany) { + /* JHipster version < 3.6.0 didn't ask for this relationship name */ _%> + <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().remove(this); + <%_ } _%> <%_ } _%> + <%_ } _%> return this; } - <%_ } _%> + <%_ } _%> public void set<%= relationshipNameCapitalizedPlural %>(Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= otherEntityNamePlural %>) { - <%_ if (relationship.otherRelationship && !relationship.reference.owned) { _%> + <%_ if (relationship.otherRelationship && !relationship.reference.owned) { _%> if (this.<%= relationshipFieldNamePlural %> != null) { - <%_ if (relationship.otherRelationship.reference.collection) { _%> + <%_ if (relationship.otherRelationship.reference.collection) { _%> this.<%= relationshipFieldNamePlural %>.forEach(i -> i.remove<%= otherEntityRelationshipNameCapitalized %>(this)); - <%_ } else { _%> + <%_ } else { _%> this.<%= relationshipFieldNamePlural %>.forEach(i -> i.set<%= otherEntityRelationshipNameCapitalized %>(null)); - <%_ } _%> + <%_ } _%> } if (<%= otherEntityNamePlural %> != null) { - <%_ if (relationship.otherRelationship.reference.collection) { _%> + <%_ if (relationship.otherRelationship.reference.collection) { _%> <%= otherEntityNamePlural %>.forEach(i -> i.add<%= otherEntityRelationshipNameCapitalized %>(this)); - <%_ } else { _%> + <%_ } else { _%> <%= otherEntityNamePlural %>.forEach(i -> i.set<%= otherEntityRelationshipNameCapitalized %>(this)); - <%_ } _%> + <%_ } _%> } - <%_ } _%> + <%_ } _%> this.<%= relationshipFieldNamePlural %> = <%= otherEntityNamePlural %>; - <%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%> + <%_ if (databaseTypeCouchbase && !otherEntityIsEmbedded) { _%> this.<%= relationshipFieldName %>Ids = <%= otherEntityNamePlural %>.stream() .map(<%= asEntity(otherEntityNameCapitalized) %>::getId) .collect(Collectors.toSet()); - <%_ } _%> + <%_ } _%> } - <%_ } else { _%> + <%_ } else { _%> public <%= asEntity(otherEntityNameCapitalized) %> get<%= relationshipNameCapitalized %>() { return this.<%= relationshipFieldName %>; } - <%_ if (fluentMethods) { _%> + <%_ if (fluentMethods) { _%> public <%= persistClass %> <%= relationshipFieldName %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) { this.set<%= relationshipNameCapitalized %>(<%= otherEntityName %>); - <%_ if ((databaseType === 'couchbase' && !otherEntityIsEmbedded) || reactiveRelationshipWithId) { _%> + <%_ if ((databaseTypeCouchbase && !otherEntityIsEmbedded) || reactiveRelationshipWithId) { _%> this.<%= relationshipFieldName %>Id = <%= otherEntityName %> != null ? <%= otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>() : null; - <%_ } _%> + <%_ } _%> return this; } - <%_ } _%> + <%_ } _%> public void set<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) { - <%_ if (relationship.otherRelationship && !relationship.reference.owned) { _%> + <%_ if (relationship.otherRelationship && !relationship.reference.owned) { _%> if (this.<%= relationshipFieldName %> != null) { - <%_ if (relationship.otherRelationship.reference.collection) { _%> + <%_ if (relationship.otherRelationship.reference.collection) { _%> this.<%= relationshipFieldName %>.remove<%= otherEntityRelationshipNameCapitalized %>(this); - <%_ } else { _%> + <%_ } else { _%> this.<%= relationshipFieldName %>.set<%= otherEntityRelationshipNameCapitalized %>(null); - <%_ } _%> + <%_ } _%> } if (<%= relationshipFieldName %> != null) { - <%_ if (relationship.otherRelationship.reference.collection) { _%> + <%_ if (relationship.otherRelationship.reference.collection) { _%> <%= relationshipFieldName %>.add<%= otherEntityRelationshipNameCapitalized %>(this); - <%_ } else { _%> + <%_ } else { _%> <%= relationshipFieldName %>.set<%= otherEntityRelationshipNameCapitalized %>(this); - <%_ } _%> + <%_ } _%> } - <%_ } _%> + <%_ } _%> this.<%= relationshipFieldName %> = <%= otherEntityName %>; - <%_ if ((databaseType === 'couchbase' && !otherEntityIsEmbedded) || reactiveRelationshipWithId) { _%> + <%_ if ((databaseTypeCouchbase && !otherEntityIsEmbedded) || reactiveRelationshipWithId) { _%> this.<%= relationshipFieldName %>Id = <%= otherEntityName %> != null ? <%= otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>() : null; - <%_ } _%> + <%_ } _%> } - <%_ if (reactiveRelationshipWithId) { _%> + <%_ if (reactiveRelationshipWithId) { _%> public <%= relationship.otherEntity.primaryKey.type %> get<%= relationshipNameCapitalized %>Id() { return this.<%= relationshipFieldName %>Id; @@ -714,8 +708,8 @@ relationships.forEach((relationship, idx) => { public void set<%= relationshipNameCapitalized %>Id(<%= relationship.otherEntity.primaryKey.type %> <%= otherEntityName %>) { this.<%= relationshipFieldName %>Id = <%= otherEntityName %>; } - <%_ } _%> <%_ } _%> + <%_ } _%> <%_ } _%> // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here @@ -727,11 +721,11 @@ relationships.forEach((relationship, idx) => { if (!(o instanceof <%= persistClass %>)) { return false; } - <%_ if (!embedded) { _%> +<%_ if (!embedded) { _%> return <%= primaryKey.name %> != null && <%= primaryKey.name %>.equals(((<%= persistClass %>) o).<%= primaryKey.name %>); - <%_ } else { _%> +<%_ } else { _%> return false; - <%_ } _%> +<%_ } _%> } @Override @@ -744,34 +738,33 @@ relationships.forEach((relationship, idx) => { @Override public String toString() { return "<%= persistClass %>{" + - <%_ if (!embedded) { _%> +<%_ if (!embedded) { _%> "<%= primaryKey.name %>=" + get<%= primaryKey.nameCapitalized %>() + - <%_ } _%> - <%_ for (const field of fields.filter(field => !field.id && !field.transient)) { - const fieldType = field.fieldType; - const fieldTypeBlobContent = field.fieldTypeBlobContent; - const fieldName = field.fieldName; - const fieldInJavaBeanMethod = field.fieldInJavaBeanMethod; - const isNumeric = ['integer', 'long', 'float', 'double', 'bigdecimal'].includes(fieldType.toLowerCase()); _%> +<%_ } _%> +<%_ for (const field of fields.filter(field => !field.id && !field.transient)) { + const fieldName = field.fieldName; + const fieldInJavaBeanMethod = field.fieldInJavaBeanMethod; + const isNumeric = field.fieldTypeNumeric; _%> ", <%= fieldName %>=<% if (! isNumeric) { %>'<% } %>" + get<%= fieldInJavaBeanMethod %>() <% if (! isNumeric) { %>+ "'" <% } %>+ - <%_ if (field.fieldWithContentType) { _%> + <%_ if (field.fieldWithContentType) { _%> ", <%= fieldName %>ContentType='" + get<%= fieldInJavaBeanMethod %>ContentType() + "'" + - <%_ } _%> - <%_ } _%> - <%_ for (const relationship of relationships) { - const relationshipType = relationship.relationshipType; - const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; - const relationshipFieldName = relationship.relationshipFieldName; - const relationshipNameCapitalized = relationship.relationshipNameCapitalized; - const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; - const relationshipNameCapitalizedPlural = relationship.relationshipNameCapitalizedPlural; - const ownerSide = relationship.ownerSide; _%> - <%_ if (otherEntityIsEmbedded) { - if (relationshipType === 'many-to-one') { _%> + <%_ } _%> +<%_ } _%> +<%_ for (const relationship of relationships) { + const otherEntityIsEmbedded = relationship.otherEntityIsEmbedded; + const relationshipFieldName = relationship.relationshipFieldName; + const relationshipNameCapitalized = relationship.relationshipNameCapitalized; + const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural; + const relationshipNameCapitalizedPlural = relationship.relationshipNameCapitalizedPlural; + const ownerSide = relationship.ownerSide; _%> + <%_ if (otherEntityIsEmbedded) { + if (relationship.relationshipManyToOne) { _%> ", <%= relationshipFieldNamePlural %>='" + get<%= relationshipNameCapitalizedPlural %>() + "'" + - <%_ } else if (relationshipType === 'one-to-one' && ownerSide === true) { _%> + <%_ } else if (relationship.relationshipOneToOne && ownerSide) { _%> ", <%= relationshipFieldName %>='" + get<%= relationshipNameCapitalized %>() + "'" + - <%_ } } } _%> + <%_ } _%> + <%_ } _%> +<%_ } _%> "}"; } } diff --git a/generators/entity-server/templates/src/main/java/package/domain/enumeration/Enum.java.ejs b/generators/entity-server/templates/src/main/java/package/domain/enumeration/Enum.java.ejs index 8c1c5076164..818be1c8274 100644 --- a/generators/entity-server/templates/src/main/java/package/domain/enumeration/Enum.java.ejs +++ b/generators/entity-server/templates/src/main/java/package/domain/enumeration/Enum.java.ejs @@ -25,19 +25,19 @@ public enum <%= enumName %> { <%_ if (withoutCustomValues) { _%> <%= enumValues.map(enumValue => enumValue.name).join(', ') %> <%_ } else { - enumValues.forEach((enumWithCustomValue, index) => { - if (enumWithCustomValue.name === enumWithCustomValue.value) { _%> + enumValues.forEach((enumWithCustomValue, index) => { + if (enumWithCustomValue.name === enumWithCustomValue.value) { _%> <%= enumWithCustomValue.name %><% if (index < enumValues.length - 1) { %>,<% } else { %>;<% } %> - <%_ } else { _%> + <%_ } else { _%> <%= enumWithCustomValue.name %>("<%= enumWithCustomValue.value %>")<% if (index < enumValues.length - 1) { %>,<% } else { %>;<% } %> - <%_ } - }); _%> + <%_ } + }); _%> private<% if (withCustomValues) { %> final<% } %> String value; -<%_ if (withSomeCustomValues) { _%> + <%_ if (withSomeCustomValues) { _%> <%= enumName %>() {} -<%_ } _%> + <%_ } _%> <%= enumName %>(String value) { this.value = value; diff --git a/generators/entity-server/templates/src/main/java/package/domain/relationship_validators.ejs b/generators/entity-server/templates/src/main/java/package/domain/relationship_validators.ejs index 46c357acd7f..5f619002b7d 100644 --- a/generators/entity-server/templates/src/main/java/package/domain/relationship_validators.ejs +++ b/generators/entity-server/templates/src/main/java/package/domain/relationship_validators.ejs @@ -19,7 +19,7 @@ <%# Returns a string of all validator annotations for the entity relationships. -%> <% let result = ''; -if (relationship.relationshipValidate === true) { +if (relationship.relationshipValidate) { const validators = []; if (relationship.relationshipRequired) { validators.push('@NotNull'); diff --git a/generators/generator-base.js b/generators/generator-base.js index 2616f23ff15..52191fad4f4 100644 --- a/generators/generator-base.js +++ b/generators/generator-base.js @@ -2586,7 +2586,10 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`; dest.databaseTypeCassandra = config.databaseType === CASSANDRA; dest.databaseTypeCouchbase = config.databaseType === COUCHBASE; dest.databaseTypeMongodb = config.databaseType === MONGODB; + dest.databaseTypeMysql = config.databaseType === MYSQL; + dest.databaseTypeMariadb = config.databaseType === MARIADB; dest.databaseTypeNeo4j = config.databaseType === NEO4J; + dest.databaseTypePostgres = config.databaseType === POSTGRESQL; dest.devDatabaseTypeH2Disk = config.devDatabaseType === H2_DISK; dest.devDatabaseTypeH2Memory = config.devDatabaseType === H2_MEMORY; @@ -2605,6 +2608,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`; dest.serviceDiscoveryEureka = config.serviceDiscoveryType === EUREKA; dest.searchEngineElasticsearch = config.searchEngine === ELASTICSEARCH; + dest.searchEngineCouchbase = config.searchEngine === COUCHBASE; dest.reactiveSqlTestContainers = config.reactive && [MYSQL, POSTGRESQL, MSSQL, MARIADB].includes(config.prodDatabaseType); } diff --git a/utils/entity.js b/utils/entity.js index d5d9d66bfd4..19590218b3c 100644 --- a/utils/entity.js +++ b/utils/entity.js @@ -30,7 +30,7 @@ const { MapperTypes } = require('../jdl/jhipster/entity-options'); const { OAUTH2 } = require('../jdl/jhipster/authentication-types'); const { CommonDBTypes } = require('../jdl/jhipster/field-types'); -const { BOOLEAN } = CommonDBTypes; +const { BOOLEAN, UUID } = CommonDBTypes; const { MAPSTRUCT } = MapperTypes; const { PAGINATION, INFINITE_SCROLL } = PaginationTypes; const NO_PAGINATION = PaginationTypes.NO; @@ -90,10 +90,12 @@ const BASE_TEMPLATE_DATA = { function _derivedProperties(entityWithConfig) { const pagination = entityWithConfig.pagination; + const dto = entityWithConfig.dto; _.defaults(entityWithConfig, { paginationPagination: pagination === PAGINATION, paginationInfiniteScroll: pagination === INFINITE_SCROLL, paginationNo: pagination === NO_PAGINATION, + dtoMapstruct: dto === MAPSTRUCT, }); } @@ -358,6 +360,7 @@ function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator, enable type: primaryKeyType, tsType: generator.getTypescriptKeyType(primaryKeyType), composite, + isUuid: primaryKeyType === UUID, relationships: idRelationships, // Fields declared in this entity ownFields: idFields, diff --git a/utils/field.js b/utils/field.js index 4405f24e851..a7d2727f6c2 100644 --- a/utils/field.js +++ b/utils/field.js @@ -221,6 +221,7 @@ function derivedProperties(field) { fieldValidationMax: validationRules.includes(MAX), fieldValidationMaxLength: validationRules.includes(MAXLENGTH), fieldValidationPattern: validationRules.includes(PATTERN), + fieldValidationUnique: validationRules.includes(UNIQUE), }); }