From 42e45778651f13751700c7c64a179622e8479783 Mon Sep 17 00:00:00 2001 From: Andrey Subbotin Date: Tue, 4 Apr 2017 17:59:57 +0400 Subject: [PATCH] PL-8461 Support composite primary keys and non-UUID pk for Entity Log, Dynamic Attributes, Entity Snapshots (fix review) --- .../cuba/core/app/EntitySnapshotAPI.java | 10 ++++++---- .../cuba/core/app/EntitySnapshotManager.java | 18 ++++++++++-------- .../core/app/EntitySnapshotServiceBean.java | 8 ++++---- .../cuba/core/app/EntitySnapshotService.java | 10 ++++++---- .../core/global/ReferenceToEntitySupport.java | 15 +++++++++++++++ .../entitydiff/EntitySnapshotsDatasource.java | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotAPI.java b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotAPI.java index b64c2ad4b1..bbc1290135 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotAPI.java +++ b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotAPI.java @@ -40,18 +40,20 @@ public interface EntitySnapshotAPI { /** * Load snapshots for entity * - * @param entity Entity + * @param metaClass Entity metaclass + * @param id Entity Id * @return Snapshot list sorted by snapshotDate desc */ - List getSnapshots(Entity entity); + List getSnapshots(MetaClass metaClass, Object id); /** * Translate snapshots for archival classes * - * @param entity Entity + * @param metaClass Metaclass + * @param id Entity Id * @param classMapping Map of [OldClass -> NewClass] for migration */ - void migrateSnapshots(Entity entity, Map classMapping); + void migrateSnapshots(MetaClass metaClass, Object id, Map classMapping); /** * Create snapshot for Entity and store it to database diff --git a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotManager.java b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotManager.java index 9a70b3aa9a..3dddb26a36 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotManager.java +++ b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotManager.java @@ -74,11 +74,14 @@ public class EntitySnapshotManager implements EntitySnapshotAPI { @Inject protected ReferenceToEntitySupport referenceToEntitySupport; - @Override - public List getSnapshots(Entity entity) { - checkCompositePrimaryKey(entity); - MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass()); + @Inject + protected DataManager dataManager; + @Override + public List getSnapshots(MetaClass metaClass, Object id) { + metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass); + Entity entity = dataManager.load(new LoadContext<>(metaClass).setId(id).setView(View.LOCAL)); + checkCompositePrimaryKey(entity); List resultList = null; View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse"); Transaction tx = persistence.createTransaction(); @@ -101,11 +104,10 @@ public class EntitySnapshotManager implements EntitySnapshotAPI { } @Override - public void migrateSnapshots(Entity entity, Map classMapping) { - MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass()); - + public void migrateSnapshots(MetaClass metaClass, Object id, Map classMapping) { + metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass); // load snapshots - List snapshotList = getSnapshots(entity); + List snapshotList = getSnapshots(metaClass, id); Class javaClass = metaClass.getJavaClass(); MetaClass mappedMetaClass = null; diff --git a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotServiceBean.java b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotServiceBean.java index fd3bd83a43..2e87b4f540 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotServiceBean.java +++ b/modules/core/src/com/haulmont/cuba/core/app/EntitySnapshotServiceBean.java @@ -39,8 +39,8 @@ public class EntitySnapshotServiceBean implements EntitySnapshotService { protected EntitySnapshotAPI snapshotAPI; @Override - public List getSnapshots(Entity entity) { - return snapshotAPI.getSnapshots(entity); + public List getSnapshots(MetaClass metaClass, Object id) { + return snapshotAPI.getSnapshots(metaClass, id); } @Override @@ -69,7 +69,7 @@ public class EntitySnapshotServiceBean implements EntitySnapshotService { } @Override - public void migrateSnapshots(Entity entity, Map classMapping) { - snapshotAPI.migrateSnapshots(entity, classMapping); + public void migrateSnapshots(MetaClass metaClass, Object id, Map classMapping) { + snapshotAPI.migrateSnapshots(metaClass, id, classMapping); } } \ No newline at end of file diff --git a/modules/global/src/com/haulmont/cuba/core/app/EntitySnapshotService.java b/modules/global/src/com/haulmont/cuba/core/app/EntitySnapshotService.java index 469ae8ab79..2adcc67dea 100644 --- a/modules/global/src/com/haulmont/cuba/core/app/EntitySnapshotService.java +++ b/modules/global/src/com/haulmont/cuba/core/app/EntitySnapshotService.java @@ -39,10 +39,11 @@ public interface EntitySnapshotService { /** * Get snapshots for entity by id - * @param entity Entity + * @param metaClass Entity meta class + * @param id Entity id * @return Snapshot list */ - List getSnapshots(Entity entity); + List getSnapshots(MetaClass metaClass, Object id); /** * Create snapshot for entity and save it to database @@ -91,8 +92,9 @@ public interface EntitySnapshotService { /** * Translate snapshots for archival classes * - * @param entity Entity + * @param metaClass Metaclass + * @param id Entity Id * @param classMapping Map of [OldClass -> NewClass] for migration */ - void migrateSnapshots(Entity entity, Map classMapping); + void migrateSnapshots(MetaClass metaClass, Object id, Map classMapping); } \ No newline at end of file diff --git a/modules/global/src/com/haulmont/cuba/core/global/ReferenceToEntitySupport.java b/modules/global/src/com/haulmont/cuba/core/global/ReferenceToEntitySupport.java index febeea1d8d..25e29024e0 100644 --- a/modules/global/src/com/haulmont/cuba/core/global/ReferenceToEntitySupport.java +++ b/modules/global/src/com/haulmont/cuba/core/global/ReferenceToEntitySupport.java @@ -26,6 +26,9 @@ import org.springframework.stereotype.Component; import javax.inject.Inject; import java.util.UUID; +/** + * Utility class to provide common functionality for entities with different type of primary keys + */ @Component(ReferenceToEntitySupport.NAME) public class ReferenceToEntitySupport { @@ -34,6 +37,10 @@ public class ReferenceToEntitySupport { @Inject protected Metadata metadata; + /** + * @param entity entity + * @return entity id to store in database + */ public Object getReferenceId(Entity entity) { if (entity instanceof HasUuid) { return ((HasUuid) entity).getUuid(); @@ -45,6 +52,10 @@ public class ReferenceToEntitySupport { return entity.getId(); } + /** + * @param metaClass of entity + * @return metaProperty name for storing corresponding primary key in the database + */ public String getReferenceIdPropertyName(MetaClass metaClass) { if (HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) { return "entityId"; @@ -71,6 +82,10 @@ public class ReferenceToEntitySupport { } } + /** + * @param metaClass of entity + * @return metaProperty name for loading entity from database by primary key stored in the database + */ public String getPrimaryKeyForLoadingEntity(MetaClass metaClass) { if (metadata.getTools().hasCompositePrimaryKey(metaClass) && HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) { return "uuid"; diff --git a/modules/gui/src/com/haulmont/cuba/gui/app/core/entitydiff/EntitySnapshotsDatasource.java b/modules/gui/src/com/haulmont/cuba/gui/app/core/entitydiff/EntitySnapshotsDatasource.java index c731608c8c..50cfcc86c0 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/app/core/entitydiff/EntitySnapshotsDatasource.java +++ b/modules/gui/src/com/haulmont/cuba/gui/app/core/entitydiff/EntitySnapshotsDatasource.java @@ -48,7 +48,7 @@ public class EntitySnapshotsDatasource extends CustomCollectionDatasource getEntities(Map params) { if (entity != null) { EntitySnapshotService snapshotService = AppBeans.get(EntitySnapshotService.NAME); - snapshots = snapshotService.getSnapshots(entity); + snapshots = snapshotService.getSnapshots(entity.getMetaClass(), entity.getId()); return snapshots; } return Collections.emptyList();