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

Add relationships and reactive support to custom entity packages. #16592

Merged
merged 3 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 26 additions & 14 deletions .github/workflows/webflux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
id: build
uses: ./.github/actions/build-matrix
applications:
name: ${{ matrix.app-sample }}
name: ${{ matrix.suite }}
runs-on: ${{ matrix.os }}
needs: build-matrix
defaults:
Expand All @@ -80,59 +80,70 @@ jobs:
matrix:
os: [ubuntu-20.04]
cache: [angular]
app-sample:
suite:
- webflux-mongodb
- webflux-mongodb-es-session
- webflux-mongodb-oauth2
- webflux-gateway-jwt
- webflux-gateway-oauth2
- webflux-psql
- webflux-psql-additional
- webflux-mariadb-gradle
- webflux-couchbase
include:
- app-sample: webflux-mongodb
- suite: webflux-mongodb
app-sample: webflux-mongodb
entity: mongodb
environment: prod
war: 0
e2e: 1
testcontainers: 1
- app-sample: webflux-mongodb-es-session
- suite: webflux-mongodb-es-session
app-sample: webflux-mongodb-es-session
entity: mongodb
environment: prod
war: 0
e2e: 1
testcontainers: 1
- app-sample: webflux-mongodb-oauth2
- suite: webflux-mongodb-oauth2
app-sample: webflux-mongodb-oauth2
entity: mongodb
environment: prod
war: 0
e2e: 1
testcontainers: 1
- app-sample: webflux-gateway-jwt
- suite: webflux-gateway-jwt
app-sample: webflux-gateway-jwt
entity: none
environment: prod
war: 0
e2e: 1
testcontainers: 0
- app-sample: webflux-gateway-oauth2
- suite: webflux-gateway-oauth2
app-sample: webflux-gateway-oauth2
entity: none
environment: prod
war: 0
e2e: 1
testcontainers: 1
- app-sample: webflux-psql
- suite: webflux-psql
app-sample: webflux-psql
entity: sql
environment: prod
war: 0
e2e: 1
testcontainers: 0
- app-sample: webflux-mariadb-gradle
- suite: webflux-psql-additional
jdl-sample: webflux-psql,custom-domain
- suite: webflux-mariadb-gradle
app-sample: webflux-mariadb-gradle
entity: sqllight
environment: prod
war: 0
e2e: 1
testcontainers: 0
- app-sample: webflux-couchbase
- suite: webflux-couchbase
app-sample: webflux-couchbase
entity: couchbase
environment: prod
war: 0
Expand All @@ -151,11 +162,12 @@ jobs:
id: setup
uses: ./generator-jhipster/.github/actions/setup
with:
entities-sample: ${{ matrix.entity }}
application-sample: ${{ matrix.app-sample }}
application-environment: ${{ matrix.environment }}
entities-sample: ${{ matrix.entity || 'none' }}
jdl-sample: ${{ matrix.jdl-sample || '' }}
application-sample: ${{ matrix.app-sample || 'jdl' }}
application-environment: ${{ matrix.environment || 'prod' }}
application-packaging: ${{ (matrix.war == 1 && 'war') || 'jar' }}
enable-testcontainers: ${{ matrix.testcontainers == 1 }}
enable-testcontainers: ${{ matrix.testcontainers != 0 }}
- uses: actions/[email protected]
with:
node-version: ${{ steps.setup.outputs.node-version }}
Expand Down
1 change: 1 addition & 0 deletions generators/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ module.exports = class extends BaseGenerator {
relationships: [],
changelogDate,
fields: userEntityDefinition ? userEntityDefinition.fields || [] : [],
dto: true,
};

loadRequiredConfigIntoEntity(user, this.jhipsterConfig);
Expand Down
4 changes: 4 additions & 0 deletions generators/entity-server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ const serverFiles = {
file: 'package/repository/EntityRepositoryInternalImpl_reactive.java',
renameTo: generator => `${generator.entityAbsoluteFolder}/repository/${generator.entityClass}RepositoryInternalImpl.java`,
},
{
file: 'package/repository/EntitySqlHelper_reactive.java',
renameTo: generator => `${generator.entityAbsoluteFolder}/repository/${generator.entityClass}SqlHelper.java`,
},
{
file: 'package/repository/rowmapper/EntityRowMapper.java',
renameTo: generator => `${generator.entityAbsoluteFolder}/repository/rowmapper/${generator.entityClass}RowMapper.java`,
Expand Down
1 change: 1 addition & 0 deletions generators/entity-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ module.exports = class extends BaseBlueprintGenerator {
},

processUniqueEntityTypes() {
this.reactiveOtherEntities = new Set(this.reactiveEagerRelations.map(rel => rel.otherEntity));
this.reactiveUniqueEntityTypes = new Set(this.reactiveEagerRelations.map(rel => rel.otherEntityNameCapitalized));
this.reactiveUniqueEntityTypes.add(this.entityClass);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Object.keys(uniqueEnums).forEach(function(element) { _%>
import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>

<%_ for (const otherEntity of otherEntities.filter(otherEntity => otherEntity.entityPackage !== entityPackage)) { _%>
import <%= otherEntity.entityAbsoluteClass %>;
<%_ } _%>

<%_ if (typeof javadoc === 'undefined') { _%>
/**
* A <%= persistClass %>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,23 @@ import org.springframework.r2dbc.core.RowsFetchSpec;
import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<% relationships.forEach(function(rel) {
if (rel.relationshipManyToMany && rel.ownerSide) { _%>
import <%= entityAbsolutePackage %>.domain.<%= asEntity(rel.otherEntityNameCapitalized) %>;
import <%= rel.otherEntity.entityAbsolutePackage %>.domain.<%= rel.otherEntity.persistClass %>;
<%_ } _%>
<%_ }); _%>
<%_ Object.keys(uniqueEnums).forEach(function(element) { _%>

import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>

<%_ reactiveUniqueEntityTypes.forEach(function(element) { _%>
import <%= entityAbsolutePackage %>.repository.rowmapper.<%= element %>RowMapper;
<%_ [...reactiveOtherEntities, entity].forEach(otherEntity => { _%>
import <%= otherEntity.entityAbsolutePackage %>.repository.rowmapper.<%= otherEntity.entityClass %>RowMapper;
<%_ if (otherEntity.entityPackage !== entityPackage) { _%>
import <%= otherEntity.entityAbsolutePackage %>.repository.<%= otherEntity.entityClass %>SqlHelper;
<%_ } _%>
<%_ }); _%>
import <%= entityAbsolutePackage %>.service.EntityManager;
import <%= packageName %>.service.EntityManager;
<%_ if (fieldsContainOwnerManyToMany) { _%>
import <%= entityAbsolutePackage %>.service.EntityManager.LinkTable;
import <%= packageName %>.service.EntityManager.LinkTable;
<%_ } _%>

import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -247,23 +250,3 @@ _%>

<%_ } _%>
}

class <%= entityClass %>SqlHelper {
static List<Expression> getColumns(Table table, String columnPrefix) {
List<Expression> columns = new ArrayList<>();
<%_ fields.forEach(function(field) {
let col = field.fieldNameAsDatabaseColumn;
_%>
columns.add(Column.aliased("<%= col %>", table, columnPrefix + "_<%= col %>"));
<%_ if ((field.fieldTypeBinary) && !field.blobContentTypeText) { _%>
columns.add(Column.aliased("<%= col %>_content_type", table, columnPrefix + "_<%= col %>_content_type"));
<%_ } _%>
<%_ }); _%>

<%_ reactiveRegularEagerRelations.forEach(function(rel) { _%>
columns.add(Column.aliased("<%= getColumnName(rel.relationshipName) %>_id", table, columnPrefix + "_<%= getColumnName(rel.relationshipName) %>_id"));
<%_ }); _%>
return columns;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<%#
Copyright 2013-2021 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= entityAbsolutePackage %>.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Table;

public class <%= entityClass %>SqlHelper {

public static List<Expression> getColumns(Table table, String columnPrefix) {
List<Expression> columns = new ArrayList<>();
<%_ fields.forEach(function(field) {
let col = field.fieldNameAsDatabaseColumn;
_%>
columns.add(Column.aliased("<%= col %>", table, columnPrefix + "_<%= col %>"));
<%_ if ((field.fieldTypeBinary) && !field.blobContentTypeText) { _%>
columns.add(Column.aliased("<%= col %>_content_type", table, columnPrefix + "_<%= col %>_content_type"));
<%_ } _%>
<%_ }); _%>

<%_ reactiveRegularEagerRelations.forEach(function(rel) { _%>
columns.add(Column.aliased("<%= getColumnName(rel.relationshipName) %>_id", table, columnPrefix + "_<%= getColumnName(rel.relationshipName) %>_id"));
<%_ }); _%>
return columns;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<%_ Object.keys(uniqueEnums).forEach(function(element) { _%>
import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>
import <%= entityAbsolutePackage %>.service.ColumnConverter;
import <%= packageName %>.service.ColumnConverter;

import io.r2dbc.spi.Row;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ import javax.persistence.Lob;
import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>

<%_ for (const otherEntity of otherEntities.filter(otherEntity => otherEntity.entityPackage !== entityPackage)) { _%>
import <%= `${otherEntity.entityAbsolutePackage}.service.dto.${otherEntity.dtoClass}` %>;
<%_ } _%>

/**
* A DTO for the {@link <%= entityAbsolutePackage %>.domain.<%= persistClass %>} entity.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ _%>
import java.util.Set;
<%_ } _%>

import <%= entityAbsolutePackage %>.domain.*;
import <%= entityAbsoluteClass %>;
import <%= entityAbsolutePackage %>.service.dto.<%= dtoClass %>;

<%_ for (const otherEntity of _.uniq(dtoReferences.filter(ref => ref.relationship).map(ref => ref.relationship.otherEntity).filter(otherEntity => otherEntity.entityPackage !== entityPackage))) { _%>
import <%= `${otherEntity.entityAbsolutePackage}.service.mapper.${otherEntity.entityClass}Mapper` %>;
<%_ } _%>

import org.mapstruct.*;
<%_ if (uuidMapMethod) { _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import <%= entityAbsolutePackage %>.repository.UserRepository;
<%_ } _%>
import <%= entityAbsolutePackage %>.repository.<%= entityClass %>Repository;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= entityAbsolutePackage %>.service.EntityManager;
import <%= packageName %>.service.EntityManager;
<%_ } _%>
<%_ if (isUsingMapsId && (!dtoMapstruct && serviceNo)) { _%>
import <%= entityAbsolutePackage %>.repository.<%= mapsIdAssoc.otherEntityNameCapitalized %>Repository;
Expand Down
6 changes: 5 additions & 1 deletion generators/entity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class EntityGenerator extends BaseBlueprintGenerator {
let { entityAbsolutePackage = packageName, entityAbsoluteFolder = packageFolder } = entity;
if (entityPackage) {
entityAbsolutePackage = [packageName, entityPackage].join('.');
entityAbsoluteFolder = path.join(packageFolder, entityPackage);
entityAbsoluteFolder = path.join(packageFolder, entityPackage.replace(/\./g, '/'));
}
entity.entityAbsolutePackage = entityAbsolutePackage;
entity.entityAbsoluteFolder = entityAbsoluteFolder;
Expand Down Expand Up @@ -752,6 +752,10 @@ class EntityGenerator extends BaseBlueprintGenerator {
this.context.user = this.configOptions.sharedEntities.User;
},

loadOtherEntities() {
this.context.otherEntities = _.uniq(this.context.relationships.map(rel => rel.otherEntity));
},

processOtherReferences() {
this.context.otherReferences = this.context.otherRelationships.map(relationship => relationship.reference);
this.context.allReferences
Expand Down
52 changes: 52 additions & 0 deletions generators/server/files-sql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2013-2021 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { SERVER_MAIN_SRC_DIR } = require('../generator-constants');

const sqlFiles = {
reactiveJavaUserManagement: [
{
condition: generator => generator.reactive && (!generator.skipUserManagement || generator.authenticationTypeOauth2),
path: SERVER_MAIN_SRC_DIR,
templates: [
{
file: 'package/repository/UserSqlHelper.java',
renameTo: generator => `${generator.javaDir}repository/UserSqlHelper.java`,
},
],
},
],
};

function writeSqlFiles() {
return {
async writeSqlFiles() {
if (!this.databaseTypeSql) return;

await this.writeFiles({
sections: sqlFiles,
rootTemplatesPath: ['sql/reactive', 'sql'],
});
},
};
}

module.exports = {
sqlFiles,
writeSqlFiles,
};
3 changes: 3 additions & 0 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const { KAFKA } = require('../../jdl/jhipster/message-broker-types');
const { CONSUL, EUREKA } = require('../../jdl/jhipster/service-discovery-types');
const { addSectionsCondition, mergeSections } = require('../utils');
const { writeCouchbaseFiles } = require('./files-couchbase');
const { writeSqlFiles } = require('./files-sql');

/* Constants use throughout */
const NO_DATABASE = databaseTypes.NO;
Expand Down Expand Up @@ -1736,6 +1737,8 @@ function writeFiles() {
},

...writeCouchbaseFiles(),

...writeSqlFiles(),
};
}

Expand Down
10 changes: 9 additions & 1 deletion generators/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,17 @@ module.exports = class JHipsterServerGenerator extends BaseBlueprintGenerator {
...super._missingPreDefault(),

loadUserManagementEntities() {
// TODO v8 move to preparingEntities priority.
if (!this.configOptions.sharedEntities) return;
// Make user entity available to templates.
this.user = this.configOptions.sharedEntities.User;
const user = (this.user = this.configOptions.sharedEntities.User);
if (!user) return;

const { packageName, packageFolder } = this;
const { persistClass } = user;
user.entityAbsolutePackage = packageName;
user.entityAbsoluteFolder = packageFolder;
user.entityAbsoluteClass = `${packageName}.domain.${persistClass}`;
},

loadDomains() {
Expand Down
Loading