From e032c3bee01435f947e07f63cab0fba64deefe51 Mon Sep 17 00:00:00 2001 From: seed-master Date: Mon, 2 Oct 2023 22:41:26 +0200 Subject: [PATCH] generic entity fixes 2 --- .../datasource/DataSourceServiceImpl.java | 6 +- .../data/dbobject/DBObjectServiceImpl.java | 59 ++++++++++++---- .../core/data/revision/RevisionEntity.java | 2 + .../core/entity/EntityChangeLogBuilder.java | 11 ++- .../org/seed/core/entity/EntityDependent.java | 8 +-- .../org/seed/core/entity/EntityRelation.java | 8 +-- .../seed/core/entity/EntityServiceImpl.java | 68 +++++++++++++------ .../org/seed/core/entity/EntityValidator.java | 45 +++++++----- .../core/entity/filter/FilterServiceImpl.java | 15 ++-- .../entity/transfer/TransferServiceImpl.java | 7 +- .../transform/TransformerServiceImpl.java | 7 +- .../entity/value/ValueObjectServiceImpl.java | 6 +- .../org/seed/core/form/FormActionType.java | 2 +- .../org/seed/core/form/FormServiceImpl.java | 18 ++--- .../org/seed/core/util/CollectionUtils.java | 12 ++++ .../resources/metainfo/zk-label.properties | 1 + .../resources/metainfo/zk-label_de.properties | 1 + .../integration/entity/DeleteEntityTest.java | 2 - .../test/unit/entity/EntityServiceTest.java | 10 +-- .../test/unit/util/CollectionUtilsTest.java | 17 +++++ 20 files changed, 203 insertions(+), 102 deletions(-) diff --git a/src/main/java/org/seed/core/data/datasource/DataSourceServiceImpl.java b/src/main/java/org/seed/core/data/datasource/DataSourceServiceImpl.java index 0b6bcaffe..26cf18cea 100644 --- a/src/main/java/org/seed/core/data/datasource/DataSourceServiceImpl.java +++ b/src/main/java/org/seed/core/data/datasource/DataSourceServiceImpl.java @@ -96,12 +96,12 @@ public List findUsage(Entity entity, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { return Collections.emptyList(); } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -121,7 +121,7 @@ public List findUsage(NestedEntity nestedEntity, Session session) { } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } diff --git a/src/main/java/org/seed/core/data/dbobject/DBObjectServiceImpl.java b/src/main/java/org/seed/core/data/dbobject/DBObjectServiceImpl.java index e2b49e76b..a8c4897f0 100644 --- a/src/main/java/org/seed/core/data/dbobject/DBObjectServiceImpl.java +++ b/src/main/java/org/seed/core/data/dbobject/DBObjectServiceImpl.java @@ -23,6 +23,8 @@ import java.util.Collections; import java.util.List; +import javax.annotation.Nullable; + import org.hibernate.Session; import org.hibernate.Transaction; @@ -37,6 +39,7 @@ import org.seed.core.config.changelog.ChangeLog; import org.seed.core.data.AbstractSystemObject; import org.seed.core.data.ValidationException; +import org.seed.core.data.revision.RevisionEntity; import org.seed.core.entity.Entity; import org.seed.core.entity.EntityDependent; import org.seed.core.entity.EntityField; @@ -112,7 +115,8 @@ public void initObject(DBObject dbObject) throws ValidationException { public List findUsage(DBObject dbObject) { Assert.notNull(dbObject, C.DBOBJECT); - return subList(repository.find(), object -> object.isEnabled() && object.contains(dbObject)); + return subList(repository.find(), object -> object.isEnabled() && + object.contains(dbObject)); } @Override @@ -120,23 +124,45 @@ public List findUsage(Entity entity, Session session) { Assert.notNull(entity, C.ENTITY); Assert.notNull(session, C.SESSION); - return convertedList(schemaManager.findDependencies(session, entity.getEffectiveTableName(), null), - DBObjectServiceImpl::createDummyObject); + final var result = findDependencies(session, entity.getEffectiveTableName(), null, false); + return entity.isAudited() + ? combinedList(result, findDependencies(session, + entity.getEffectiveTableName(), null, true)) + : result; } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); - return convertedList(schemaManager.findDependencies(session, - entityField.getEntity().getEffectiveTableName(), - entityField.getEffectiveColumnName()), - DBObjectServiceImpl::createDummyObject); + final var result = findDependencies(session, entity.getEffectiveTableName(), + entityField.getEffectiveColumnName(), false); + return entity.isAudited() + ? combinedList(result, findDependencies(session, entity.getEffectiveTableName(), + entityField.getEffectiveColumnName(), true)) + : result; + } + + @Override + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { + Assert.notNull(entity, C.ENTITY); + Assert.notNull(entityRelation, "entity relation"); + Assert.notNull(session, C.SESSION); + + if (entityRelation.getEntity().isGeneric()) { + entityRelation.setDerivedEntity(entity); + } + final var result = findDependencies(session, entityRelation.getJoinTableName(), null, false); + return entity.isAudited() + ? combinedList(result, findDependencies(session, + entityRelation.getJoinTableName(), null, true)) + : result; } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -155,11 +181,6 @@ public List findUsage(NestedEntity nestedEntity, Session session) { return Collections.emptyList(); } - @Override - public List findUsage(EntityRelation entityRelation, Session session) { - return Collections.emptyList(); - } - @Override protected void analyzeNextVersionObjects(ImportAnalysis analysis, Module currentVersionModule) { if (analysis.getModule().getDBObjects() != null) { @@ -314,6 +335,16 @@ public void saveObject(DBObject dbObject) throws ValidationException { updateConfiguration(); } + private List findDependencies(Session session, String objectName, + @Nullable String fieldName, boolean isAuditTable) { + return convertedList(schemaManager.findDependencies(session, + isAuditTable + ? objectName.concat(RevisionEntity.SUFFIX_AUDIT) + : objectName, + fieldName), + DBObjectServiceImpl::createDummyObject); + } + private static ChangeLog createChangeLog(DBObject currentVersionObject, DBObject nextVersionObject) { return new DBObjectChangeLogBuilder() .setCurrentVersionObject(currentVersionObject) diff --git a/src/main/java/org/seed/core/data/revision/RevisionEntity.java b/src/main/java/org/seed/core/data/revision/RevisionEntity.java index 66b4a6360..4d588ed12 100644 --- a/src/main/java/org/seed/core/data/revision/RevisionEntity.java +++ b/src/main/java/org/seed/core/data/revision/RevisionEntity.java @@ -38,6 +38,8 @@ @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class RevisionEntity implements Revision { + public static final String SUFFIX_AUDIT = "_aud"; + @Id @SequenceGenerator(name="seqGen", sequenceName="seed_id_seq", initialValue=1000, allocationSize=1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqGen") diff --git a/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java b/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java index 7b33e536e..48e69d734 100644 --- a/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java +++ b/src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java @@ -65,7 +65,6 @@ class EntityChangeLogBuilder extends AbstractChangeLogBuilder { private static final String PREFIX_UNIQUE_KEY = "uni_"; private static final String PREFIX_INDEX = "idx_"; private static final String SUFFIX_STATUS = "_status"; - private static final String SUFFIX_AUDIT = "_aud"; private static final String SUFFIX_REVISION = "_rev"; private final EntityUsage entityUsage; @@ -669,7 +668,7 @@ else if (field.isIndexed() && !nextVersionField.isIndexed()) { private void buildRevisionFieldConstraint(Entity entity) { final var addFKConstraintChange = new AddForeignKeyConstraintChange(); addFKConstraintChange.setConstraintName(getRevisionForeignKeyConstraintName(entity)); - addFKConstraintChange.setBaseTableName(entity.getEffectiveTableName().concat(SUFFIX_AUDIT)); + addFKConstraintChange.setBaseTableName(entity.getEffectiveTableName().concat(RevisionEntity.SUFFIX_AUDIT)); addFKConstraintChange.setBaseColumnNames(RevisionField.REV.columName); addFKConstraintChange.setReferencedTableName(RevisionEntity.class.getAnnotation(Table.class).name()); addFKConstraintChange.setReferencedColumnNames(SystemField.ID.columName); @@ -904,7 +903,7 @@ private int getLimit(String limitName) { private static String getTableName(Entity entity, boolean isAudit) { final String tableName = entity.getEffectiveTableName(); - return isAudit ? tableName.concat(SUFFIX_AUDIT) : tableName; + return isAudit ? tableName.concat(RevisionEntity.SUFFIX_AUDIT) : tableName; } private static String getTableName(EntityRelation relation, boolean isAudit) { @@ -913,18 +912,18 @@ private static String getTableName(EntityRelation relation, boolean isAudit) { private static String getTableName(Entity related, EntityRelation relation, boolean isAudit) { final String tableName = related != null ? relation.getJoinTableName(related) : relation.getJoinTableName(); - return isAudit ? tableName.concat(SUFFIX_AUDIT) : tableName; + return isAudit ? tableName.concat(RevisionEntity.SUFFIX_AUDIT) : tableName; } private static String getPrimaryKeyConstraintName(Entity entity, boolean isAudit) { final String constraintName = PREFIX_PRIMARY_KEY.concat(TinyId.get(entity.getId())); - return isAudit ? constraintName.concat(SUFFIX_AUDIT) : constraintName; + return isAudit ? constraintName.concat(RevisionEntity.SUFFIX_AUDIT) : constraintName; } private static String getPrimaryKeyConstraintName(EntityRelation relation, boolean isAudit) { final String pkName = PREFIX_PRIMARY_KEY + TinyId.get(relation.getEntity().getId()) + '_' + TinyId.get(relation.getRelatedEntity().getId()); - return isAudit ? pkName.concat(SUFFIX_AUDIT) : pkName; + return isAudit ? pkName.concat(RevisionEntity.SUFFIX_AUDIT) : pkName; } private static String getUniqueConstraintName(Entity entity, EntityField field) { diff --git a/src/main/java/org/seed/core/entity/EntityDependent.java b/src/main/java/org/seed/core/entity/EntityDependent.java index a70bac2fe..de2808009 100644 --- a/src/main/java/org/seed/core/entity/EntityDependent.java +++ b/src/main/java/org/seed/core/entity/EntityDependent.java @@ -27,9 +27,11 @@ public interface EntityDependent { List findUsage(Entity entity, Session session); - List findUsage(EntityField entityField, Session session); + List findUsage(Entity entity, EntityField entityField, Session session); - List findUsage(EntityFieldGroup fieldGroup); + List findUsage(Entity entity, EntityFieldGroup fieldGroup); + + List findUsage(Entity entity, EntityRelation entityRelation, Session session); List findUsage(EntityStatus entityStatus, Session session); @@ -37,6 +39,4 @@ public interface EntityDependent { List findUsage(NestedEntity nestedEntity, Session session); - List findUsage(EntityRelation entityRelation, Session session); - } diff --git a/src/main/java/org/seed/core/entity/EntityRelation.java b/src/main/java/org/seed/core/entity/EntityRelation.java index 44284d5b4..134a251fe 100644 --- a/src/main/java/org/seed/core/entity/EntityRelation.java +++ b/src/main/java/org/seed/core/entity/EntityRelation.java @@ -174,16 +174,16 @@ String getEntityTableName() { : entity.getEffectiveTableName(); } - void setDerivedEntity(Entity derivedEntity) { - this.derivedEntity = derivedEntity; - } - private Entity getDerivedEntity() { Assert.stateAvailable(derivedEntity, "derived entity"); return derivedEntity; } + public void setDerivedEntity(Entity derivedEntity) { + this.derivedEntity = derivedEntity; + } + private static EntityRelation createRelation(Entity entity, Entity relatedEntity) { final var relation = new EntityRelation(); relation.setEntity(entity); diff --git a/src/main/java/org/seed/core/entity/EntityServiceImpl.java b/src/main/java/org/seed/core/entity/EntityServiceImpl.java index 3ef2fc690..811912462 100644 --- a/src/main/java/org/seed/core/entity/EntityServiceImpl.java +++ b/src/main/java/org/seed/core/entity/EntityServiceImpl.java @@ -260,9 +260,9 @@ public List findUsage(Entity entity, Session session) { } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(fieldGroup, C.FIELDGROUP); - final Entity entity = fieldGroup.getEntity(); return (anyMatch(entity.getFields(), field -> fieldGroup.equals(field.getFieldGroup())) || anyMatch(entity.getFieldConstraints(), constr -> fieldGroup.equals(constr.getFieldGroup()))) @@ -318,18 +318,19 @@ public List findUsage(UserGroup userGroup, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); return entityField.isReferenceField() - ? subList(getObjects(session), entity -> anyMatch(entity.getNesteds(), - nested -> nested.getReferenceField().equals(entityField))) + ? subList(getObjects(session), entityObj -> anyMatch(entityObj.getNesteds(), + nested -> nested.getReferenceField().equals(entityField))) : Collections.emptyList(); } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } @@ -453,7 +454,16 @@ public void removeField(Entity entity, EntityField field) throws ValidationExcep Assert.notNull(field, C.FIELD); if (!field.isNew()) { - entityValidator.validateRemoveField(field); + if (entity.isGeneric()) { + try (Session session = entityRepository.getSession()) { + for (Entity descendant : findDescendants(entity, session)) { + entityValidator.validateRemoveField(descendant, field); + } + } + } + else { + entityValidator.validateRemoveField(entity, field); + } } entity.removeField(field); } @@ -465,7 +475,16 @@ public void removeFieldGroup(Entity entity, EntityFieldGroup fieldGroup) throws Assert.notNull(fieldGroup, C.FIELDGROUP); if (!fieldGroup.isNew()) { - entityValidator.validateRemoveFieldGroup(fieldGroup); + if (entity.isGeneric()) { + try (Session session = entityRepository.getSession()) { + for (Entity descendant : findDescendants(entity, session)) { + entityValidator.validateRemoveFieldGroup(descendant, fieldGroup); + } + } + } + else { + entityValidator.validateRemoveFieldGroup(entity, fieldGroup); + } } filterAndForEach(entity.getFields(), field -> fieldGroup.equals(field.getFieldGroup()), @@ -473,6 +492,27 @@ public void removeFieldGroup(Entity entity, EntityFieldGroup fieldGroup) throws entity.removeFieldGroup(fieldGroup); } + @Override + @Secured("ROLE_ADMIN_ENTITY") + public void removeRelation(Entity entity, EntityRelation relation) throws ValidationException { + Assert.notNull(entity, C.ENTITY); + Assert.notNull(relation, C.RELATION); + + if (!relation.isNew()) { + if (entity.isGeneric()) { + try (Session session = entityRepository.getSession()) { + for (Entity descendant : findDescendants(entity, session)) { + entityValidator.validateRemoveRelation(descendant, relation); + } + } + } + else { + entityValidator.validateRemoveRelation(entity, relation); + } + } + entity.removeRelation(relation); + } + @Override @Secured("ROLE_ADMIN_ENTITY") public void removeFunction(Entity entity, EntityFunction function) throws ValidationException { @@ -518,18 +558,6 @@ public void removeNested(Entity entity, NestedEntity nested) throws ValidationEx entity.removeNested(nested); } - @Override - @Secured("ROLE_ADMIN_ENTITY") - public void removeRelation(Entity entity, EntityRelation relation) throws ValidationException { - Assert.notNull(entity, C.ENTITY); - Assert.notNull(relation, C.RELATION); - - if (!relation.isNew()) { - entityValidator.validateRemoveRelation(relation); - } - entity.removeRelation(relation); - } - @Override protected void analyzeNextVersionObjects(ImportAnalysis analysis, Module currentVersionModule) { if (analysis.getModule().getEntities() != null) { diff --git a/src/main/java/org/seed/core/entity/EntityValidator.java b/src/main/java/org/seed/core/entity/EntityValidator.java index 3705c9d29..2deba7235 100644 --- a/src/main/java/org/seed/core/entity/EntityValidator.java +++ b/src/main/java/org/seed/core/entity/EntityValidator.java @@ -150,16 +150,16 @@ public void validateDelete(Entity entity) throws ValidationException { validate(errors); } - public void validateRemoveField(EntityField field) throws ValidationException { + public void validateRemoveField(Entity entity, EntityField field) throws ValidationException { + Assert.notNull(entity, C.ENTITY); Assert.notNull(field, C.FIELD); - final var errors = createValidationErrors(field.getEntity()); + final var errors = createValidationErrors(entity); // reload field - final var entity = repository.get(field.getEntity().getId()); - field = entity.getFieldById(field.getId()); + field = repository.get(field.getEntity().getId()).getFieldById(field.getId()); // check if field is used in field formula - for (EntityField entityField : field.getEntity().getFields()) { + for (EntityField entityField : entity.getAllFields()) { if (!entityField.equals(field) && entityField.getFormula() != null && entityField.getFormula().contains(field.getEffectiveColumnName())) { @@ -168,19 +168,20 @@ public void validateRemoveField(EntityField field) throws ValidationException { } try (Session session = repository.getSession()) { for (var dependent : getEntityDependents()) { - validateRemoveFieldDependent(field, dependent, errors, session); + validateRemoveFieldDependent(entity, field, dependent, errors, session); } } validate(errors); } - public void validateRemoveFieldGroup(EntityFieldGroup fieldGroup) throws ValidationException { + public void validateRemoveFieldGroup(Entity entity, EntityFieldGroup fieldGroup) throws ValidationException { + Assert.notNull(entity, C.ENTITY); Assert.notNull(fieldGroup, C.FIELDGROUP); final var errors = createValidationErrors(fieldGroup.getEntity()); for (var dependent : getEntityDependents()) { if (dependent instanceof EntityService) { - for (SystemEntity systemEntity : dependent.findUsage(fieldGroup)) { + for (SystemEntity systemEntity : dependent.findUsage(entity, fieldGroup)) { errors.addError("val.inuse.fieldgroupentity", systemEntity.getName()); } break; @@ -260,18 +261,25 @@ public void validateRemoveNested(NestedEntity nestedEntity) throws ValidationExc validate(errors); } - public void validateRemoveRelation(EntityRelation relation) throws ValidationException { + public void validateRemoveRelation(Entity entity, EntityRelation relation) throws ValidationException { + Assert.notNull(entity, C.ENTITY); Assert.notNull(relation, C.RELATION); - final var errors = createValidationErrors(relation.getEntity()); + final var errors = createValidationErrors(entity); try (Session session = repository.getSession()) { for (var dependent : getEntityDependents()) { - for (SystemEntity systemEntity : dependent.findUsage(relation, session)) { - if (C.FORM.equals(getEntityType(systemEntity))) { - errors.addError("val.inuse.relationform", systemEntity.getName()); - } - else { - unhandledEntity(systemEntity); + for (SystemEntity systemEntity : dependent.findUsage(entity, relation, session)) { + switch (getEntityType(systemEntity)) { + case "dbobject": + errors.addError("val.inuse.relationdbobject", systemEntity.getName()); + break; + + case C.FORM: + errors.addError("val.inuse.relationform", systemEntity.getName()); + break; + + default: + unhandledEntity(systemEntity); } } } @@ -699,9 +707,10 @@ private void validateDeleteEntityDependent(Entity entity, EntityDependent dependent, + private void validateRemoveFieldDependent(Entity entity, EntityField field, + EntityDependent dependent, ValidationErrors errors, Session session) { - for (SystemEntity systemEntity : dependent.findUsage(field, session)) { + for (SystemEntity systemEntity : dependent.findUsage(entity, field, session)) { switch (getEntityType(systemEntity)) { case "dbobject": errors.addError("val.inuse.fieldview", systemEntity.getName()); diff --git a/src/main/java/org/seed/core/entity/filter/FilterServiceImpl.java b/src/main/java/org/seed/core/entity/filter/FilterServiceImpl.java index a1302a368..343a47e38 100644 --- a/src/main/java/org/seed/core/entity/filter/FilterServiceImpl.java +++ b/src/main/java/org/seed/core/entity/filter/FilterServiceImpl.java @@ -324,15 +324,16 @@ public List findUsage(Entity entity, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); - return entityField.getEntity().isGeneric() + return entity.isGeneric() ? Collections.emptyList() - : subList(findFilters(entityField.getEntity(), session), - filter -> anyMatch(filter.getCriteria(), - criterion -> entityField.equals(criterion.getEntityField()))); + : subList(findFilters(entity, session), + filter -> anyMatch(filter.getCriteria(), + criterion -> entityField.equals(criterion.getEntityField()))); } @Override @@ -366,7 +367,7 @@ public List findUsage(EntityStatus entityStatus, Session session) { } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -381,7 +382,7 @@ public List findUsage(NestedEntity nestedEntity, Session session) { } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } diff --git a/src/main/java/org/seed/core/entity/transfer/TransferServiceImpl.java b/src/main/java/org/seed/core/entity/transfer/TransferServiceImpl.java index 55996f1bb..16682293c 100644 --- a/src/main/java/org/seed/core/entity/transfer/TransferServiceImpl.java +++ b/src/main/java/org/seed/core/entity/transfer/TransferServiceImpl.java @@ -343,7 +343,8 @@ public List findUsage(NestedEntity nestedEntity, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); @@ -359,12 +360,12 @@ public List findUsage(UserGroup userGroup, Session session) { } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } diff --git a/src/main/java/org/seed/core/entity/transform/TransformerServiceImpl.java b/src/main/java/org/seed/core/entity/transform/TransformerServiceImpl.java index d6246e397..2494b5419 100644 --- a/src/main/java/org/seed/core/entity/transform/TransformerServiceImpl.java +++ b/src/main/java/org/seed/core/entity/transform/TransformerServiceImpl.java @@ -250,7 +250,8 @@ public void deleteObject(Transformer transformer) throws ValidationException { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); @@ -285,7 +286,7 @@ public List findUsage(NestedEntity nestedEntity, Session session) { } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -295,7 +296,7 @@ public List findUsage(EntityFunction entityFunction, Session sessio } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } diff --git a/src/main/java/org/seed/core/entity/value/ValueObjectServiceImpl.java b/src/main/java/org/seed/core/entity/value/ValueObjectServiceImpl.java index 645e08ebf..ffb2b9751 100644 --- a/src/main/java/org/seed/core/entity/value/ValueObjectServiceImpl.java +++ b/src/main/java/org/seed/core/entity/value/ValueObjectServiceImpl.java @@ -676,12 +676,12 @@ public List findUsage(Entity entity, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { return Collections.emptyList(); } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -708,7 +708,7 @@ public List findUsage(NestedEntity nestedEntity, Session session) } @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { return Collections.emptyList(); } diff --git a/src/main/java/org/seed/core/form/FormActionType.java b/src/main/java/org/seed/core/form/FormActionType.java index be28c1ff3..71144277d 100644 --- a/src/main/java/org/seed/core/form/FormActionType.java +++ b/src/main/java/org/seed/core/form/FormActionType.java @@ -23,7 +23,7 @@ public enum FormActionType { // comes first (if default is true) - // default defsel visibility tmpl-list tmpl-detail icon + // default defsel visibility template-list template-detail icon CUSTOM (false, false, detail()|sub(), Template.SELECT, null, "z-icon-exclamation"), OVERVIEW (true, true, detail(), null, null, "z-icon-arrow-left"), BACKSEARCH (true, true, list(), Template.SEARCH, null, "z-icon-arrow-left"), diff --git a/src/main/java/org/seed/core/form/FormServiceImpl.java b/src/main/java/org/seed/core/form/FormServiceImpl.java index 1edc53451..bd5ffb283 100644 --- a/src/main/java/org/seed/core/form/FormServiceImpl.java +++ b/src/main/java/org/seed/core/form/FormServiceImpl.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Set; -import org.apache.commons.collections.ListUtils; import org.hibernate.Session; import org.hibernate.Transaction; @@ -225,7 +224,8 @@ public List
findUsage(Entity entity, Session session) { } @Override - public List findUsage(EntityField entityField, Session session) { + public List findUsage(Entity entity, EntityField entityField, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityField, C.ENTITYFIELD); Assert.notNull(session, C.SESSION); @@ -239,7 +239,8 @@ public List findUsage(EntityField entityField, Session session) { @Override - public List findUsage(EntityRelation entityRelation, Session session) { + public List findUsage(Entity entity, EntityRelation entityRelation, Session session) { + Assert.notNull(entity, C.ENTITY); Assert.notNull(entityRelation, C.RELATION); Assert.notNull(session, C.SESSION); @@ -249,7 +250,7 @@ public List findUsage(EntityRelation entityRelation, Session session) { } @Override - public List findUsage(EntityFieldGroup fieldGroup) { + public List findUsage(Entity entity, EntityFieldGroup fieldGroup) { return Collections.emptyList(); } @@ -579,7 +580,7 @@ public void notifyBeforeChange(Entity entity, Session session) { Assert.notNull(entity, C.ENTITY); Assert.notNull(session, C.SESSION); - notifyBeforeFields(entity, session); + notifyBeforeChangeFields(entity, session); if (!entity.isGeneric()) { for (Form form : formRepository.find(session, queryParam(C.ENTITY, entity))) { @@ -593,7 +594,7 @@ public void notifyBeforeChange(Entity entity, Session session) { } } - private void notifyBeforeFields(Entity entity, Session session) { + private void notifyBeforeChangeFields(Entity entity, Session session) { final var currentVersionEntity = entityService.getObject(entity.getId()); final var query = session.createQuery("from FormField where entityField = :field"); // delete form field if entity field no longer exist @@ -1013,9 +1014,8 @@ private void cleanupSubForms(Form form) { } private static List createFormFields(Form form) { - return MiscUtils.castList( - ListUtils.union(createEntityFields(form), - createSystemFields(form))); + return combinedList(createEntityFields(form), + createSystemFields(form)); } private static List createEntityFields(Form form) { diff --git a/src/main/java/org/seed/core/util/CollectionUtils.java b/src/main/java/org/seed/core/util/CollectionUtils.java index 73a25aabf..45a47d58f 100644 --- a/src/main/java/org/seed/core/util/CollectionUtils.java +++ b/src/main/java/org/seed/core/util/CollectionUtils.java @@ -124,6 +124,18 @@ static void forEach(@Nullable T[] array, Consumer action) { } } + static List combinedList(@Nullable Collection col1, @Nullable Collection col2) { + if (notEmpty(col1)) { + return notEmpty(col2) + ? Stream.concat(col1.stream(), col2.stream()).collect(Collectors.toList()) + : col1.stream().collect(Collectors.toList()); + } + else if (notEmpty(col2)) { + return col2.stream().collect(Collectors.toList()); + } + return Collections.emptyList(); + } + static Collector> linkedMapCollector(Function keyFunction, Function valueFunction) { return Collectors.toMap(keyFunction, valueFunction, diff --git a/src/main/resources/metainfo/zk-label.properties b/src/main/resources/metainfo/zk-label.properties index 594f72f8c..de81d4a3a 100644 --- a/src/main/resources/metainfo/zk-label.properties +++ b/src/main/resources/metainfo/zk-label.properties @@ -1103,6 +1103,7 @@ val.inuse.nestedconstraint = Entity is still used in a field constrint val.inuse.nestedform = Subobject is still used in Form "{0}" val.inuse.nestedtransform = Subobject is still used in transformation "{0}" val.inuse.nestedtransfer = Subobject is still used in Import / Export "{0}" +val.inuse.relationdbobject = Relation is still in use in database item "{0}" val.inuse.relationform = Relation is still in use in form "{0}" val.inuse.status = Status is still used in at least one object val.inuse.statusentity = Status is still used in an field constraint diff --git a/src/main/resources/metainfo/zk-label_de.properties b/src/main/resources/metainfo/zk-label_de.properties index 019b783fe..b396242f7 100644 --- a/src/main/resources/metainfo/zk-label_de.properties +++ b/src/main/resources/metainfo/zk-label_de.properties @@ -1103,6 +1103,7 @@ val.inuse.nestedconstraint = Ein Feld des Unterobjekts wird noch in einer Feldei val.inuse.nestedform = Das Unterobjekt wird noch im Formular "{0}" verwendet val.inuse.nestedtransform = Das Unterobjekt wird noch in der Transformation "{0}" verwendet val.inuse.nestedtransfer = Das Unterobjekt wird noch im Import / Export "{0}" verwendet +val.inuse.relationdbobject = Die Beziehung wird noch im Datenbankelement "{0}" verwendet val.inuse.relationform = Die Beziehung wird noch im Formular "{0}" verwendet val.inuse.status = Der Status wird noch in mindestens einem Objekt verwendet val.inuse.statusentity = Der Status wird noch in einer Feldeinschränkung verwendet diff --git a/src/test/java/org/seed/test/integration/entity/DeleteEntityTest.java b/src/test/java/org/seed/test/integration/entity/DeleteEntityTest.java index 5138d3e78..4e6ff7c6e 100644 --- a/src/test/java/org/seed/test/integration/entity/DeleteEntityTest.java +++ b/src/test/java/org/seed/test/integration/entity/DeleteEntityTest.java @@ -187,8 +187,6 @@ void testRemoveGenericField() { clickListItem(tabpanelFields, "generictext"); clickButton(tabpanelFields, "remove"); saveEntity(tabpanel); - - pause(5000); } @Test diff --git a/src/test/java/org/seed/test/unit/entity/EntityServiceTest.java b/src/test/java/org/seed/test/unit/entity/EntityServiceTest.java index 4daafbdf2..4d95fce6f 100644 --- a/src/test/java/org/seed/test/unit/entity/EntityServiceTest.java +++ b/src/test/java/org/seed/test/unit/entity/EntityServiceTest.java @@ -136,16 +136,16 @@ void testFindUsageFieldGroup() { final EntityField field = service.createField(entity); final EntityFieldConstraint constraint = service.createFieldConstraint(entity); final EntityFieldGroup group = service.createFieldGroup(entity); - assertTrue(service.findUsage(group).isEmpty()); + assertTrue(service.findUsage(entity, group).isEmpty()); field.setFieldGroup(group); - assertFalse(service.findUsage(group).isEmpty()); - assertSame(entity, service.findUsage(group).get(0)); + assertFalse(service.findUsage(entity, group).isEmpty()); + assertSame(entity, service.findUsage(entity, group).get(0)); field.setFieldGroup(null); constraint.setFieldGroup(group); - assertFalse(service.findUsage(group).isEmpty()); - assertSame(entity, service.findUsage(group).get(0)); + assertFalse(service.findUsage(entity, group).isEmpty()); + assertSame(entity, service.findUsage(entity, group).get(0)); } @Test diff --git a/src/test/java/org/seed/test/unit/util/CollectionUtilsTest.java b/src/test/java/org/seed/test/unit/util/CollectionUtilsTest.java index 037a53642..9e35502dc 100644 --- a/src/test/java/org/seed/test/unit/util/CollectionUtilsTest.java +++ b/src/test/java/org/seed/test/unit/util/CollectionUtilsTest.java @@ -150,6 +150,23 @@ void testForEach() { assertSame(3, result.size()); } + @Test + void testCombinedList() { + final List list1 = new ArrayList<>(); + final List list2 = new ArrayList<>(); + assertTrue(CollectionUtils.combinedList(null, null).isEmpty()); + assertTrue(CollectionUtils.combinedList(list1, null).isEmpty()); + assertTrue(CollectionUtils.combinedList(null, list2).isEmpty()); + assertTrue(CollectionUtils.combinedList(list1, list2).isEmpty()); + + list1.add("test"); + assertSame(1, CollectionUtils.combinedList(list1, null).size()); + assertSame(1, CollectionUtils.combinedList(list1, list2).size()); + + list2.add("test"); + assertSame(2, CollectionUtils.combinedList(list1, list2).size()); + } + @Test void testFilterAndForEach() { final List list = new ArrayList<>();