Skip to content

Commit

Permalink
adjusts for embedded entities
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Apr 26, 2024
1 parent 4320719 commit a3ddefa
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const enumImports = this.generateEntityClientEnumImports(fields);
<%_ if (anyFieldIsDateDerived) { _%>
import dayjs from 'dayjs/esm';
<%_ } _%>
<%_ for (const relationshipsByType of Object.values(differentRelationships).filter(relationshipsByType => relationshipsByType.some(relationship => relationship.otherEntity.entityAngularName !== entityAngularName))) {
<%_ for (const relationshipsByType of Object.values(relationshipsByOtherEntity).filter(relationships => relationships.some(rel => (!embedded || rel.otherEntity.embedded) && rel.otherEntity.entityAngularName !== entityAngularName))) {
const { otherEntity } = relationshipsByType[0];
_%>
import { I<%- otherEntity.entityAngularName %> } from 'app/entities/<%= otherEntity.entityClientRootFolder %><%= otherEntity.entityFolderName %>/<%= otherEntity.entityFileName %>.model';
Expand All @@ -41,7 +41,7 @@ _%>
<%= fieldName %>ContentType<% if (!id /* && !fieldValidationRequired */) { %>?<% } %>: string<% if (!id /* && !fieldValidationRequired */) { %> | null<% } %>,
<%_ } _%>
<%_ } _%>
<%_ for (const relationship of relationships.filter(rel => rel.persistableRelationship || rel.relationshipEagerLoad)) {
<%_ for (const relationship of relationships.filter(rel => (!embedded || rel.otherEntity.embedded) && rel.persistableRelationship || rel.relationshipEagerLoad)) {
const { propertyName, relationshipRequired, otherEntity, collection, otherEntityField } = relationship;
_%>
<%= propertyName %>?: <% if (!otherEntity.embedded && (dtoMapstruct || otherEntity.builtInUser)) { %>Pick<<% } %>I<%= otherEntity.entityAngularName %><% if (!otherEntity.embedded && (dtoMapstruct || otherEntity.builtInUser)) { %>, '<%= otherEntity.primaryKey.name %>'<% if (otherEntity.primaryKey.name !== otherEntityField) { %> | '<%= otherEntityField %>'<% } %>><% } %><% if (collection) { %>[]<% } %> | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const allRelationshipsByEntityNeedingOptions = Object
.values(differentRelationships)
.map(relationships => relationships.filter(rel => rel.persistableRelationship))
.values(relationshipsByOtherEntity)
.map(relationships => relationships.filter(rel => rel.persistableRelationship && !rel.otherEntity.embedded))
.filter(relationships => relationships.length > 0);
const testEntityPrimaryKey0 = this.generateTestEntityPrimaryKey(primaryKey, 0);
const testEntityPrimaryKey1 = this.generateTestEntityPrimaryKey(primaryKey, 1);
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
<%_ } _%>
});

<%_ const trackedRelationships = Object.values(differentRelationships).filter(arr => arr.some(rel => rel.persistableRelationship && rel.otherEntity.primaryKey));
<%_ const trackedRelationships = Object.values(relationshipsByOtherEntity).filter(arr => arr.some(rel => rel.persistableRelationship && !rel.otherEntity.embedded));
if (trackedRelationships.length > 0) {
_%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
-%>
<%_
const allRelationshipsByEntityNeedingOptions = Object
.values(differentRelationships)
.map(relationships => relationships.filter(rel => rel.persistableRelationship))
.values(relationshipsByOtherEntity)
.map(relationships => relationships.filter(rel => rel.persistableRelationship && !rel.otherEntity.embedded))
.filter(relationships => relationships.length > 0);
_%>
import { Component, inject, OnInit<% if (anyFieldHasImageContentType) { %>, ElementRef<% } %> } from '@angular/core';
Expand All @@ -39,18 +39,15 @@ import { AlertError } from 'app/shared/alert/alert-error.model';
import { EventManager, EventWithContent } from 'app/core/util/event-manager.service';
import { DataUtils, FileLoadError } from 'app/core/util/data-util.service';
<%_ } _%>
<%_
Object.keys(differentRelationships).forEach(key => {
if (differentRelationships[key].some(rel => rel.persistableRelationship)) {
const uniqueRel = differentRelationships[key][0];
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) {
const uniqueRel = relationshipsByEntityNeedingOptions[key][0];
if (uniqueRel.otherEntityAngularName !== entityAngularName) {
_%>
import { I<%= uniqueRel.otherEntityAngularName %> } from 'app/entities/<%= uniqueRel.otherEntityPath %>/<%= uniqueRel.otherEntityFileName %>.model';
import { <%= uniqueRel.otherEntityAngularName %>Service } from 'app/entities/<%= uniqueRel.otherEntityPath %>/service/<%= uniqueRel.otherEntityFileName %>.service';
<%_
}
}
});
_%>
<%_ const enumImports = this.generateEntityClientEnumImports(fields); _%>
<%_ enumImports.forEach( (importedPath, importedType) => { _%>
Expand All @@ -70,7 +67,7 @@ export class <%= entityAngularName %>UpdateComponent implements OnInit {
<%- this._.lowerFirst(importedType) %>Values = Object.keys(<%- importedType %>);
<%_ }); _%>

<%_ for (const relationshipsByEntityNeedingOptions of Object.values(differentRelationships).map(relationships => relationships.filter(rel => rel.persistableRelationship)).filter(relationships => relationships.length > 0)) { _%>
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%>
<%_ const relationshipsWithCustomUniqueOptions = relationshipsByEntityNeedingOptions.filter(rel => rel.relationshipOneToOne && rel.otherRelationship); _%>
<%_ if (relationshipsByEntityNeedingOptions.length > relationshipsWithCustomUniqueOptions.length) { _%>
<%_ const otherEntity = relationshipsByEntityNeedingOptions[0].otherEntity _%>
Expand All @@ -87,17 +84,14 @@ export class <%= entityAngularName %>UpdateComponent implements OnInit {
<%_ } _%>
protected <%= entityInstance %>Service = inject(<%= entityAngularName %>Service);
protected <%= entityInstance %>FormService = inject(<%= entityAngularName %>FormService);
<%_
Object.keys(differentRelationships).forEach(key => {
if (differentRelationships[key].some(rel => rel.persistableRelationship)) {
const uniqueRel = differentRelationships[key][0];
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) {
const uniqueRel = relationshipsByEntityNeedingOptions[key][0];
if (uniqueRel.otherEntityAngularName !== entityAngularName) {
_%>
protected <%= uniqueRel.otherEntity.entityInstance %>Service = inject(<%= uniqueRel.otherEntityAngularName %>Service);
<%_
}
}
});
_%>
<%_ if (anyFieldHasImageContentType) { _%>
protected elementRef = inject(ElementRef);
Expand All @@ -107,7 +101,7 @@ _%>
// eslint-disable-next-line @typescript-eslint/member-ordering
editForm: <%= entityAngularName %>FormGroup = this.<%= entityInstance %>FormService.create<%= entityAngularName %>FormGroup();

<%_ for (const relationshipsByEntity of Object.values(differentRelationships).filter(arr => arr.some(rel => rel.persistableRelationship && rel.otherEntity.primaryKey))) {
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) {
const { otherEntity } = relationshipsByEntity[0];
_%>

Expand All @@ -122,7 +116,7 @@ _%>
this.updateForm(<%= entityInstance %>);
}

<%_ if (relationships.filter(rel => rel.persistableRelationship && !rel.otherEntityIsEmbedded).length > 0) { _%>
<%_ if (relationships.filter(rel => rel.persistableRelationship && !rel.otherEntity.embedded).length > 0) { _%>
this.loadRelationshipsOptions();
<%_ } _%>
});
Expand Down Expand Up @@ -225,7 +219,7 @@ _%>
<%_ if (relationships.filter(rel => rel.persistableRelationship && !rel.otherEntityIsEmbedded).length > 0) { _%>
protected loadRelationshipsOptions(): void {
<%_ for (const relationshipsByEntityNeedingOptions of Object.values(differentRelationships).map(relationships => relationships.filter(rel => rel.persistableRelationship)).filter(relationships => relationships.length > 0)) { _%>
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%>
<%_ const relationshipsWithCustomUniqueOptions = relationshipsByEntityNeedingOptions.filter(rel => rel.relationshipOneToOne && rel.otherRelationship); %>
<%_ const relationshipsWithCustomSharedOptions = relationshipsByEntityNeedingOptions.filter(rel => !relationshipsWithCustomUniqueOptions.includes(rel)); %>
<%_ const { otherEntity } = relationshipsByEntityNeedingOptions[0] _%>
Expand Down
4 changes: 2 additions & 2 deletions generators/base-application/support/prepare-relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export default function prepareRelationship(entityWithConfig, relationship, gene
// means that this side should control the reference.
ownerSide: relationshipManyToOne || (relationshipLeftSide && !relationshipOneToMany),
persistableRelationship: ({ ownerSide }) => ownerSide,
relationshipUpdateBackReference: ({ ownerSide, relationshipRightSide }) =>
entityWithConfig.databaseType === 'neo4j' ? relationshipRightSide : !ownerSide,
relationshipUpdateBackReference: ({ ownerSide, relationshipRightSide, otherEntity }) =>
!otherEntity.embedded && (entityWithConfig.databaseType === 'neo4j' ? relationshipRightSide : !ownerSide),

// DB properties
columnName: hibernateSnakeCase(relationshipName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class <%= persistClass %> <&- fragments.extendsSection() -&>implements Se

<%_
// An embedded entity should not reference entities that embed it, unless the other entities are also embedded
for (relationship of relationships.filter(relationship => !relationship.embedded || relationship.otherEntity.embedded || relationship.ownerSide)) {
for (relationship of relationships.filter(rel => !embedded || (rel.otherEntity.embedded && rel.ownerSide))) {
if (typeof relationship.fieldApiDescription) { _%>
<%- relationship.relationshipJavadoc %>
<%_ if (!dtoMapstruct && relationship.relationshipyApiDescription) { _%>
Expand Down Expand Up @@ -222,11 +222,7 @@ for (relationship of relationships.filter(relationship => !relationship.embedded

<&- fragments.classAdditionalFieldsMethodsSection('\n\n') -&>
<%_
for (const relationship of relationships) {
// An embedded entity should not reference entities that embed it, unless the other entities are also embedded
if (embedded && !relationship.otherEntity.embedded && !relationship.ownerSide) {
continue;
}
for (const relationship of relationships.filter(rel => !embedded || (rel.otherEntity.embedded && rel.ownerSide))) {
_%>
<%_ if (relationship.collection) { _%>
public Set<<%= relationship.otherEntity.persistClass %>> get<%= relationship.relationshipNameCapitalizedPlural %>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class <%= persistClass %>Asserts {
<%_ for (const field of fields.filter(field => !field.transient && field.autoGenerate)) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>()))
<%_ } _%>
<%_ for (const relationship of relationships.filter(relationship => relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
<%_ for (const relationship of relationships.filter(relationship => (!embedded || relationship.otherEntity.embedded) && relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
.satisfies(e -> assertThat(e.get<%- relationship.propertyJavaBeanName %>()).as("check <%- relationship.propertyName %>").isEqualTo(actual.get<%- relationship.propertyJavaBeanName %>()))
<%_ } _%>
;
Expand Down Expand Up @@ -121,10 +121,10 @@ public class <%= persistClass %>Asserts {
<%_ if (relationships.some(relationship => relationship.persistableRelationship && !relationship.id && !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
assertThat(expected)
.as("Verify <%- persistClass %> relationships")
<%_ for (const relationship of relationships.filter(relationship => relationship.persistableRelationship && !relationship.id && !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
<%_ for (const relationship of relationships.filter(relationship => (!embedded || relationship.otherEntity.embedded) && relationship.persistableRelationship && !relationship.id && !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
.satisfies(e -> assertThat(e.get<%- relationship.propertyJavaBeanName %>()).as("check <%- relationship.propertyName %>").isEqualTo(actual.get<%- relationship.propertyJavaBeanName %>()))
<%_ } _%>
;
<%_ } _%>
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class <%= persistClass %>Test {
assertThat(<%= persistInstance %>).hasSameHashCodeAs(<%= persistInstance %>1);
}
<%_ } _%>
<%_ for (const relationship of relationships.filter(relationship => !relationship.otherEntity.builtIn)) { _%>
<%_ for (const relationship of relationships.filter(rel => !rel.otherEntity.builtIn && (!embedded || (rel.otherEntity.embedded && rel.ownerSide)))) { _%>

@Test
void <%- relationship.relationshipName %>Test() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion generators/server/support/prepare-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function preparePostEntityServerDerivedProperties(entity) {
.forEach(relationship => {
relationship.ignoreOtherSideProperty =
entity.databaseType !== 'neo4j' &&
!relationship.embedded &&
!entity.embedded &&
!!relationship.otherEntity &&
relationship.otherEntity.relationships.length > 0;
});
Expand Down

0 comments on commit a3ddefa

Please sign in to comment.