Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use derived constants in entity-server templates and domain #14659

Merged
merged 7 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions generators/ci-cd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ module.exports = class extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
this._loadPlatformConfig();
},
Expand Down
3 changes: 3 additions & 0 deletions generators/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ module.exports = class JHipsterClientGenerator extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
</a>
</li>
<!-- jhipster-needle-add-element-to-admin-menu - JHipster will add entities to the admin menu here -->
<%_ if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory || devDatabaseTypeCouchbase) { _%>
<%_ if (devDatabaseTypeH2Any || devDatabaseTypeCouchbase) { _%>
<li *ngIf="!inProduction">
<a class="dropdown-item" href="<% if (devDatabaseTypeCouchbase) { %>http://localhost:8091/<% } else if (reactive) { %> http://localhost:8092/ <% } else { %>./h2-console/<% } %>" target="_tab" (click)="collapseNavbar()">
<fa-icon icon="database" [fixedWidth]="true"></fa-icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const Header = (props: IHeaderProps) => {
<Home />
{props.isAuthenticated && <EntitiesMenu />}
{props.isAuthenticated && props.isAdmin &&
<AdminMenu showOpenAPI={props.isOpenAPIEnabled} <% if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory) { %> showDatabase={!props.isInProduction} <%_ } _%> />}
<AdminMenu showOpenAPI={props.isOpenAPIEnabled} <% if (devDatabaseTypeH2Any) { %> showDatabase={!props.isInProduction} <%_ } _%> />}
<%_ if (enableTranslation) { _%>
<LocaleMenu currentLocale={props.currentLocale} onClick={handleLocaleChange} />
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ const openAPIItem = (
<MenuItem icon="book" to="/admin/docs"><Translate contentKey="global.menu.admin.apidocs">API</Translate></MenuItem>
);

<% if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory) { %>
<% if (devDatabaseTypeH2Any) { %>
const databaseItem = (
<DropdownItem tag="a" href="<% if (reactive) { %>http://localhost:8092/<% } else { %>./h2-console/<% } %>" target="_tab">
<FontAwesomeIcon icon="database" fixedWidth /> <Translate contentKey="global.menu.admin.database">Database</Translate>
</DropdownItem>
);
<%_ } _%>

export const AdminMenu = ({ showOpenAPI <% if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory) { %>, showDatabase <%_ } _%>}) => (
export const AdminMenu = ({ showOpenAPI <% if (devDatabaseTypeH2Any) { %>, showDatabase <%_ } _%>}) => (
<NavDropdown icon="users-cog" name={translate('global.menu.admin.main')} id="admin-menu" data-cy="adminMenu">
{adminMenuItems}
{showOpenAPI && openAPIItem}
<% if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory) { %>
<% if (devDatabaseTypeH2Any) { %>
{showDatabase && databaseItem}
<%_ } _%>
</NavDropdown>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<font-awesome-icon icon="book" />
<span v-text="$t('global.menu.admin.apidocs')">API</span>
</b-dropdown-item>
<%_ if (devDatabaseTypeH2Disk || devDatabaseTypeH2Memory || devDatabaseTypeCouchbase) { _%>
<%_ if (devDatabaseTypeH2Any || devDatabaseTypeCouchbase) { _%>
<b-dropdown-item v-if="!inProduction" href='<% if (devDatabaseTypeCouchbase) { %>http://localhost:8092/<% } else if (reactive) { %>http://localhost:8092/<% } else { %>./h2-console/<% } %>' target="_tab">
<font-awesome-icon icon="database" />
<span v-text="$t('global.menu.admin.database')">Database</span>
Expand Down
3 changes: 3 additions & 0 deletions generators/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ module.exports = class JHipsterCommonGenerator extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
},

Expand Down
3 changes: 3 additions & 0 deletions generators/cypress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ module.exports = class extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
},
};
Expand Down
3 changes: 3 additions & 0 deletions generators/database-changelog-liquibase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ module.exports = class extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
},
};
Expand Down
31 changes: 19 additions & 12 deletions generators/entity-server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: [
{
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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: [
{
Expand All @@ -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: [
{
Expand All @@ -159,7 +166,7 @@ const serverFiles = {
],
},
{
condition: generator => generator.dto === 'mapstruct',
condition: generator => generator.dto === MAPSTRUCT,
path: SERVER_MAIN_SRC_DIR,
templates: [
{
Expand Down Expand Up @@ -197,7 +204,7 @@ const serverFiles = {
],
},
{
condition: generator => generator.searchEngine === 'elasticsearch' && !generator.embedded,
condition: generator => generator.searchEngine === ELASTICSEARCH && !generator.embedded,
path: SERVER_TEST_SRC_DIR,
templates: [
{
Expand Down Expand Up @@ -228,7 +235,7 @@ const serverFiles = {
],
},
{
condition: generator => generator.dto === 'mapstruct',
condition: generator => generator.dto === MAPSTRUCT,
path: SERVER_TEST_SRC_DIR,
templates: [
{
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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);
}
}
Expand Down
11 changes: 7 additions & 4 deletions generators/entity-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -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];
},

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +174,7 @@ module.exports = class extends BaseBlueprintGenerator {
}

_generateSqlSafeName(name) {
if (isReservedTableName(name, 'sql')) {
if (isReservedTableName(name, SQL)) {
return `e_${name}`;
}
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
limitations under the License.
-%>
// Initialize the database
<%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%>
<%_ if (!primaryKey.autoGenerate && !databaseTypeSql) { _%>
<%_ if (primaryKey.typeUUID) { _%>
Tcharl marked this conversation as resolved.
Show resolved Hide resolved
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID());
<%_ } _%>
<%_ } _%>
<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>;

Expand All @@ -33,14 +35,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) { _%>
Expand All @@ -60,19 +62,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 %>);
<%_ } } _%>
<%_ } _%>
<%_ } _%>
Loading