Skip to content

Commit

Permalink
entity changelog fixes 3
Browse files Browse the repository at this point in the history
  • Loading branch information
seed-master committed Sep 21, 2023
1 parent 9959bc6 commit 862a89e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 51 deletions.
69 changes: 28 additions & 41 deletions src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class EntityChangeLogBuilder extends AbstractChangeLogBuilder<Entity> {
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;
Expand Down Expand Up @@ -134,7 +134,7 @@ else if (nextVersionObject == null) {
else {
// audit state change
if (currentVersionObject.isAudited() != nextVersionObject.isAudited()) {
buildAuditTableChange();
buildAuditChanges();
}

// rename table
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -723,7 +710,7 @@ private void buildStatusFieldChange() {
}
}

private void buildAuditTableChange() {
private void buildAuditChanges() {
if (nextVersionObject.isAudited()) {
addCreateTableChangeSet(currentVersionObject, true);
currentVersionObject.getAllRelations().forEach(
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/seed/core/entity/EntityMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntityRelation> getAllRelations() {
final List<EntityRelation> list = new ArrayList<>();
if (genericEntity != null) {
list.addAll(genericEntity.getAllRelations());
final var list = new ArrayList<EntityRelation>();
if (genericEntity != null && genericEntity.hasRelations()) {
list.addAll(genericEntity.getRelations());
list.forEach(relation -> relation.setDerivedEntity(this));
}
if (hasRelations()) {
list.addAll(getRelations());
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/org/seed/core/entity/EntityRelation.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class EntityRelation extends AbstractOrderedTransferableObject {

private String name;

@Transient
private Entity derivedEntity;

@Transient
@JsonIgnore
private String relatedEntityUid;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down

0 comments on commit 862a89e

Please sign in to comment.