Skip to content

Commit

Permalink
Add relationships support to custom entity packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 6, 2021
1 parent f621426 commit 85f963d
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 54 deletions.
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
4 changes: 4 additions & 0 deletions generators/entity/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<%#
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 <%= packageName %>.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 UserSqlHelper {
public static List<Expression> getColumns(Table table, String columnPrefix) {
List<Expression> columns = new ArrayList<>();
columns.add(Column.aliased("id", table, columnPrefix + "_id"));
columns.add(Column.aliased("login", table, columnPrefix + "_login"));
<%_ if (!authenticationTypeOauth2) { _%>
columns.add(Column.aliased("password_hash", table, columnPrefix + "_password"));
<%_ } _%>
columns.add(Column.aliased("first_name", table, columnPrefix + "_first_name"));
columns.add(Column.aliased("last_name", table, columnPrefix + "_last_name"));
columns.add(Column.aliased("email", table, columnPrefix + "_email"));
columns.add(Column.aliased("activated", table, columnPrefix + "_activated"));
columns.add(Column.aliased("lang_key", table, columnPrefix + "_lang_key"));
columns.add(Column.aliased("image_url", table, columnPrefix + "_image_url"));
<%_ if (!authenticationTypeOauth2) { _%>
columns.add(Column.aliased("activation_key", table, columnPrefix + "_activation_key"));
columns.add(Column.aliased("reset_key", table, columnPrefix + "_reset_key"));
columns.add(Column.aliased("reset_date", table, columnPrefix + "_reset_date"));
<%_ } _%>
return columns;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,29 +464,6 @@ class UserRepositoryInternalImpl implements UserRepositoryInternal {
}
}

class UserSqlHelper {
static List<Expression> getColumns(Table table, String columnPrefix) {
List<Expression> columns = new ArrayList<>();
columns.add(Column.aliased("id", table, columnPrefix + "_id"));
columns.add(Column.aliased("login", table, columnPrefix + "_login"));
<%_ if (!authenticationTypeOauth2) { _%>
columns.add(Column.aliased("password_hash", table, columnPrefix + "_password"));
<%_ } _%>
columns.add(Column.aliased("first_name", table, columnPrefix + "_first_name"));
columns.add(Column.aliased("last_name", table, columnPrefix + "_last_name"));
columns.add(Column.aliased("email", table, columnPrefix + "_email"));
columns.add(Column.aliased("activated", table, columnPrefix + "_activated"));
columns.add(Column.aliased("lang_key", table, columnPrefix + "_lang_key"));
columns.add(Column.aliased("image_url", table, columnPrefix + "_image_url"));
<%_ if (!authenticationTypeOauth2) { _%>
columns.add(Column.aliased("activation_key", table, columnPrefix + "_activation_key"));
columns.add(Column.aliased("reset_key", table, columnPrefix + "_reset_key"));
columns.add(Column.aliased("reset_date", table, columnPrefix + "_reset_date"));
<%_ } _%>
return columns;
}
}

<%_ } else if (databaseTypeCassandra) { _%>
@Repository
public class UserRepository {
Expand Down
5 changes: 3 additions & 2 deletions utils/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const { OAUTH2 } = require('../jdl/jhipster/authentication-types');
const { CommonDBTypes } = require('../jdl/jhipster/field-types');

const { BOOLEAN, LONG, STRING, UUID } = CommonDBTypes;
const { MAPSTRUCT } = MapperTypes;
const { NO: NO_DTO, MAPSTRUCT } = MapperTypes;
const { PAGINATION, INFINITE_SCROLL } = PaginationTypes;
const { SERVICE_IMPL } = ServiceTypes;
const NO_SERVICE = ServiceTypes.NO;
Expand All @@ -40,6 +40,7 @@ const NO_MAPPER = MapperTypes.NO;

const BASE_TEMPLATE_DATA = {
primaryKey: undefined,
entityPackage: undefined,
skipUiGrouping: false,
haveFieldWithJavadoc: false,
existingEnum: false,
Expand Down Expand Up @@ -136,7 +137,7 @@ function prepareEntityForTemplates(entityWithConfig, generator) {
entityNamePlural: pluralize(entityName),
});

const dto = entityWithConfig.dto === MAPSTRUCT;
const dto = entityWithConfig.dto && entityWithConfig.dto !== NO_DTO;
if (dto) {
_.defaults(entityWithConfig, {
dtoClass: generator.asDto(entityWithConfig.entityClass),
Expand Down

0 comments on commit 85f963d

Please sign in to comment.