Skip to content

Commit

Permalink
generic entity fixes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
seed-master committed Oct 2, 2023
1 parent 98758b9 commit e032c3b
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public List<IDataSource> findUsage(Entity entity, Session session) {
}

@Override
public List<IDataSource> findUsage(EntityField entityField, Session session) {
public List<IDataSource> findUsage(Entity entity, EntityField entityField, Session session) {
return Collections.emptyList();
}

@Override
public List<IDataSource> findUsage(EntityFieldGroup fieldGroup) {
public List<IDataSource> findUsage(Entity entity, EntityFieldGroup fieldGroup) {
return Collections.emptyList();
}

Expand All @@ -121,7 +121,7 @@ public List<IDataSource> findUsage(NestedEntity nestedEntity, Session session) {
}

@Override
public List<IDataSource> findUsage(EntityRelation entityRelation, Session session) {
public List<IDataSource> findUsage(Entity entity, EntityRelation entityRelation, Session session) {
return Collections.emptyList();
}

Expand Down
59 changes: 45 additions & 14 deletions src/main/java/org/seed/core/data/dbobject/DBObjectServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;

import org.hibernate.Session;
import org.hibernate.Transaction;

Expand All @@ -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;
Expand Down Expand Up @@ -112,31 +115,54 @@ public void initObject(DBObject dbObject) throws ValidationException {
public List<DBObject> 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
public List<DBObject> 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<DBObject> findUsage(EntityField entityField, Session session) {
public List<DBObject> 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<DBObject> 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<DBObject> findUsage(EntityFieldGroup fieldGroup) {
public List<DBObject> findUsage(Entity entity, EntityFieldGroup fieldGroup) {
return Collections.emptyList();
}

Expand All @@ -155,11 +181,6 @@ public List<DBObject> findUsage(NestedEntity nestedEntity, Session session) {
return Collections.emptyList();
}

@Override
public List<DBObject> findUsage(EntityRelation entityRelation, Session session) {
return Collections.emptyList();
}

@Override
protected void analyzeNextVersionObjects(ImportAnalysis analysis, Module currentVersionModule) {
if (analysis.getModule().getDBObjects() != null) {
Expand Down Expand Up @@ -314,6 +335,16 @@ public void saveObject(DBObject dbObject) throws ValidationException {
updateConfiguration();
}

private List<DBObject> 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)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/seed/core/data/revision/RevisionEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/seed/core/entity/EntityChangeLogBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class EntityChangeLogBuilder extends AbstractChangeLogBuilder<Entity> {
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/seed/core/entity/EntityDependent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public interface EntityDependent<T extends SystemEntity> {

List<T> findUsage(Entity entity, Session session);

List<T> findUsage(EntityField entityField, Session session);
List<T> findUsage(Entity entity, EntityField entityField, Session session);

List<T> findUsage(EntityFieldGroup fieldGroup);
List<T> findUsage(Entity entity, EntityFieldGroup fieldGroup);

List<T> findUsage(Entity entity, EntityRelation entityRelation, Session session);

List<T> findUsage(EntityStatus entityStatus, Session session);

List<T> findUsage(EntityFunction entityFunction, Session session);

List<T> findUsage(NestedEntity nestedEntity, Session session);

List<T> findUsage(EntityRelation entityRelation, Session session);

}
8 changes: 4 additions & 4 deletions src/main/java/org/seed/core/entity/EntityRelation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 48 additions & 20 deletions src/main/java/org/seed/core/entity/EntityServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ public List<Entity> findUsage(Entity entity, Session session) {
}

@Override
public List<Entity> findUsage(EntityFieldGroup fieldGroup) {
public List<Entity> 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())))
Expand Down Expand Up @@ -318,18 +318,19 @@ public List<Entity> findUsage(UserGroup userGroup, Session session) {
}

@Override
public List<Entity> findUsage(EntityField entityField, Session session) {
public List<Entity> 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<Entity> findUsage(EntityRelation entityRelation, Session session) {
public List<Entity> findUsage(Entity entity, EntityRelation entityRelation, Session session) {
return Collections.emptyList();
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -465,14 +475,44 @@ 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()),
field -> field.setFieldGroup(null));
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 {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit e032c3b

Please sign in to comment.