From 862a89e04d489d9a36ff4d590f730ba9ce27bed9 Mon Sep 17 00:00:00 2001 From: seed-master Date: Thu, 21 Sep 2023 22:20:57 +0200 Subject: [PATCH] entity changelog fixes 3 --- .../core/entity/EntityChangeLogBuilder.java | 69 ++++++++----------- .../org/seed/core/entity/EntityMetadata.java | 14 ++-- .../org/seed/core/entity/EntityRelation.java | 38 ++++++++-- 3 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java b/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java index b037d067a..4c4f4b843 100644 --- a/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java +++ b/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java @@ -83,7 +83,7 @@ class EntityChangeLogBuilder extends AbstractChangeLogBuilder { EntityChangeLogBuilder(DatabaseInfo databaseInfo, Limits limits) { entityUsage = Seed.getBean(EntityUsage.class); Assert.stateAvailable(entityUsage, "entity usage"); - Assert.notNull(databaseInfo, "databaseInfo"); + Assert.notNull(databaseInfo, "database info"); Assert.notNull(limits, "limits"); this.databaseInfo = databaseInfo; @@ -134,7 +134,7 @@ else if (nextVersionObject == null) { else { // audit state change if (currentVersionObject.isAudited() != nextVersionObject.isAudited()) { - buildAuditTableChange(); + buildAuditChanges(); } // rename table @@ -162,37 +162,32 @@ else if (nextVersionObject == null) { private void createEntityTables() { // entity table addCreateTableChangeSet(nextVersionObject, false); - // audit table if (nextVersionObject.isAudited()) { addCreateTableChangeSet(nextVersionObject, true); } - // relation tables - if (nextVersionObject.hasAllRelations()) { - for (EntityRelation relation : nextVersionObject.getAllRelations()) { - addCreateTableChangeSet(relation, false); - // relation audit table - if (nextVersionObject.isAudited()) { - addCreateTableChangeSet(relation, true); - } + for (EntityRelation relation : nextVersionObject.getAllRelations()) { + addCreateTableChangeSet(relation, false); + // relation audit table + if (nextVersionObject.isAudited()) { + addCreateTableChangeSet(relation, true); } } } private void dropEntityTables() { // drop relation tables - if (currentVersionObject.hasAllRelations()) { - for (EntityRelation relation : currentVersionObject.getAllRelations()) { - addDropTableChangeSet(relation, false); - // drop relation audit table - if (currentVersionObject.isAudited()) { - addDropTableChangeSet(relation, true); - } + for (EntityRelation relation : currentVersionObject.getAllRelations()) { + addDropTableChangeSet(relation, false); + // drop relation audit table + if (currentVersionObject.isAudited()) { + addDropTableChangeSet(relation, true); } } - // drop audit table + // drop entity table addDropTableChangeSet(currentVersionObject, false); + // drop audit table if (currentVersionObject.isAudited()) { addDropTableChangeSet(currentVersionObject, true); } @@ -210,18 +205,14 @@ private void renameEntity() { if (nextVersionObject.isAudited()) { addRenameTableChange(currentVersionObject, nextVersionObject, true); } - // rename relation tables - if (nextVersionObject.hasAllRelations()) { - for (EntityRelation relation : nextVersionObject.getAllRelations()) { - final EntityRelation currentVersionRelation = - currentVersionObject.getRelationByUid(relation.getUid()); - if (currentVersionRelation != null) { - renameRelation(null, currentVersionRelation, relation); - } + for (EntityRelation relation : nextVersionObject.getAllRelations()) { + final EntityRelation currentVersionRelation = + currentVersionObject.getRelationByUid(relation.getUid()); + if (currentVersionRelation != null) { + renameRelation(null, currentVersionRelation, relation); } } - // rename inverse relation tables if (inverseRelateds != null) { for (Entity inverseRelated : inverseRelateds) { @@ -234,7 +225,7 @@ private void renameEntity() { private void renameRelation(Entity related, EntityRelation oldRelation, EntityRelation newRelation) { addRenameRelationTableChanges(related, oldRelation, newRelation, false); - if (newRelation.getEntity().isAudited()) { + if (newRelation.isEntityAudited()) { addRenameRelationTableChanges(related, oldRelation, newRelation, true); } } @@ -262,13 +253,10 @@ private void addCreateTableChangeSet(Entity entity, boolean isAuditTable) { createTableChange.addColumn(columnStatus); } // fields - if (entity.hasAllFields()) { - entity.getAllFields().forEach(field -> - createTableChange.addColumn(initColumn(new ColumnConfig(), entity, field, isAuditTable))); - } + entity.getAllFields().forEach(field -> + createTableChange.addColumn(initColumn(new ColumnConfig(), entity, field, isAuditTable))); addChange(createTableChange); - if (isAuditTable) { buildRevisionFieldConstraint(entity); } @@ -537,7 +525,7 @@ private void buildRelationChangesCurrentVersion(EntityRelation relation) { if (descendants != null) { // build changes for all implementing entities for (Entity descendant : descendants) { addDropTableChangeSet(relation.createDescendantRelation(descendant), false); - if (relation.getEntity().isAudited()) { + if (descendant.isAudited()) { addDropTableChangeSet(relation.createDescendantRelation(descendant), true); } } @@ -556,7 +544,7 @@ private void buildRelationChangesNextVersion(EntityRelation relation) { if (descendants != null) { // build changes for all implementing entities for (Entity descendant : descendants) { addCreateTableChangeSet(relation.createDescendantRelation(descendant), false); - if (relation.getEntity().isAudited()) { + if (descendant.isAudited()) { addCreateTableChangeSet(relation.createDescendantRelation(descendant), true); } } @@ -571,9 +559,8 @@ private void buildRelationChangesNextVersion(EntityRelation relation) { } private void buildFieldChanges() { - if (currentVersionObject.hasAllFields()) { - currentVersionObject.getAllFields().forEach(this::buildFieldChangesCurrentVersion); - } + currentVersionObject.getAllFields().forEach(this::buildFieldChangesCurrentVersion); + if (nextVersionObject.hasAllFields()) { if (isGeneric()) { if (descendants != null) { // build changes for all implementing entities @@ -592,7 +579,7 @@ private void buildFieldChangesCurrentVersion(EntityField field) { return; } - final EntityField nextVersionField = nextVersionObject.getFieldById(field.getId()); + final var nextVersionField = nextVersionObject.getFieldById(field.getId()); // generic if (isGeneric()) { if (descendants != null) { // build changes for all implementing entities @@ -723,7 +710,7 @@ private void buildStatusFieldChange() { } } - private void buildAuditTableChange() { + private void buildAuditChanges() { if (nextVersionObject.isAudited()) { addCreateTableChangeSet(currentVersionObject, true); currentVersionObject.getAllRelations().forEach( diff --git a/src/main/java/org/seed/core/entity/EntityMetadata.java b/src/main/java/org/seed/core/entity/EntityMetadata.java index 9270bb20f..9f16e00f9 100644 --- a/src/main/java/org/seed/core/entity/EntityMetadata.java +++ b/src/main/java/org/seed/core/entity/EntityMetadata.java @@ -465,16 +465,17 @@ public boolean hasRelations() { @Override public boolean hasAllRelations() { - return (genericEntity != null && genericEntity.hasAllRelations()) || hasRelations(); + return (genericEntity != null && genericEntity.hasRelations()) || hasRelations(); } // includes generic relations @Override @JsonIgnore public List getAllRelations() { - final List list = new ArrayList<>(); - if (genericEntity != null) { - list.addAll(genericEntity.getAllRelations()); + final var list = new ArrayList(); + if (genericEntity != null && genericEntity.hasRelations()) { + list.addAll(genericEntity.getRelations()); + list.forEach(relation -> relation.setDerivedEntity(this)); } if (hasRelations()) { list.addAll(getRelations()); @@ -489,7 +490,7 @@ public boolean hasFields() { @Override public boolean hasAllFields() { - return (genericEntity != null && genericEntity.hasAllFields()) || hasFields(); + return (genericEntity != null && genericEntity.hasFields()) || hasFields(); } // includes nested fields @@ -610,6 +611,9 @@ public EntityRelation getRelationByUid(String uid) { EntityRelation relation = null; if (genericEntity != null) { relation = genericEntity.getRelationByUid(uid); + if (relation != null) { + relation.setDerivedEntity(this); + } } return relation != null ? relation : getObjectByUid(getRelations(), uid); } diff --git a/src/main/java/org/seed/core/entity/EntityRelation.java b/src/main/java/org/seed/core/entity/EntityRelation.java index fc63fdd36..e8d8658bd 100644 --- a/src/main/java/org/seed/core/entity/EntityRelation.java +++ b/src/main/java/org/seed/core/entity/EntityRelation.java @@ -56,6 +56,9 @@ public class EntityRelation extends AbstractOrderedTransferableObject { private String name; + @Transient + private Entity derivedEntity; + @Transient @JsonIgnore private String relatedEntityUid; @@ -108,13 +111,12 @@ public String getJoinTableName() { @JsonIgnore public String getJoinTableName(Entity related) { - return entity.getEffectiveTableName() + '_' + - related.getEffectiveTableName(); + return getEntityTableName() + '_' + related.getEffectiveTableName(); } @JsonIgnore public String getJoinColumnName() { - return entity.getEffectiveTableName().concat(SUFFIX_ID); + return getEntityTableName().concat(SUFFIX_ID); } @JsonIgnore @@ -144,7 +146,11 @@ public boolean isEqual(Object other) { public EntityRelation createInverseRelation(Entity relatedEntity) { Assert.notNull(relatedEntity, "related entity"); - + if (entity.isGeneric()) { + Assert.stateAvailable(derivedEntity, "derived entity"); + + return createRelation(derivedEntity, relatedEntity); + } return createRelation(entity, relatedEntity); } @@ -154,8 +160,30 @@ public EntityRelation createDescendantRelation(Entity descendantEntity) { return createRelation(descendantEntity, relatedEntity); } + void setDerivedEntity(Entity realEntity) { + this.derivedEntity = realEntity; + } + + boolean isEntityAudited() { + if (entity.isGeneric()) { + Assert.stateAvailable(derivedEntity, "derived entity"); + + return derivedEntity.isAudited(); + } + return entity.isAudited(); + } + + private String getEntityTableName() { + if (entity.isGeneric()) { + Assert.stateAvailable(derivedEntity, "derived entity"); + + return derivedEntity.getEffectiveTableName(); + } + return entity.getEffectiveTableName(); + } + private static EntityRelation createRelation(Entity entity, Entity relatedEntity) { - final EntityRelation relation = new EntityRelation(); + final var relation = new EntityRelation(); relation.setEntity(entity); relation.setRelatedEntity(relatedEntity); return relation;