PL-8461 Support composite primary keys and non-UUID pk for Entity Log, Dynamic Attributes, Entity Snapshots (fix review)

This commit is contained in:
Andrey Subbotin 2017-04-04 17:59:57 +04:00
parent 09f8edcb5e
commit 42e4577865
6 changed files with 42 additions and 21 deletions

View File

@ -40,18 +40,20 @@ public interface EntitySnapshotAPI {
/** /**
* Load snapshots for entity * Load snapshots for entity
* *
* @param entity Entity * @param metaClass Entity metaclass
* @param id Entity Id
* @return Snapshot list sorted by snapshotDate desc * @return Snapshot list sorted by snapshotDate desc
*/ */
List<EntitySnapshot> getSnapshots(Entity entity); List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id);
/** /**
* Translate snapshots for archival classes * Translate snapshots for archival classes
* *
* @param entity Entity * @param metaClass Metaclass
* @param id Entity Id
* @param classMapping Map of [OldClass -&gt; NewClass] for migration * @param classMapping Map of [OldClass -&gt; NewClass] for migration
*/ */
void migrateSnapshots(Entity entity, Map<Class, Class> classMapping); void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping);
/** /**
* Create snapshot for Entity and store it to database * Create snapshot for Entity and store it to database

View File

@ -74,11 +74,14 @@ public class EntitySnapshotManager implements EntitySnapshotAPI {
@Inject @Inject
protected ReferenceToEntitySupport referenceToEntitySupport; protected ReferenceToEntitySupport referenceToEntitySupport;
@Override @Inject
public List<EntitySnapshot> getSnapshots(Entity entity) { protected DataManager dataManager;
checkCompositePrimaryKey(entity);
MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass());
@Override
public List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id) {
metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
Entity entity = dataManager.load(new LoadContext<>(metaClass).setId(id).setView(View.LOCAL));
checkCompositePrimaryKey(entity);
List<EntitySnapshot> resultList = null; List<EntitySnapshot> resultList = null;
View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse"); View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse");
Transaction tx = persistence.createTransaction(); Transaction tx = persistence.createTransaction();
@ -101,11 +104,10 @@ public class EntitySnapshotManager implements EntitySnapshotAPI {
} }
@Override @Override
public void migrateSnapshots(Entity entity, Map<Class, Class> classMapping) { public void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping) {
MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass()); metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
// load snapshots // load snapshots
List<EntitySnapshot> snapshotList = getSnapshots(entity); List<EntitySnapshot> snapshotList = getSnapshots(metaClass, id);
Class javaClass = metaClass.getJavaClass(); Class javaClass = metaClass.getJavaClass();
MetaClass mappedMetaClass = null; MetaClass mappedMetaClass = null;

View File

@ -39,8 +39,8 @@ public class EntitySnapshotServiceBean implements EntitySnapshotService {
protected EntitySnapshotAPI snapshotAPI; protected EntitySnapshotAPI snapshotAPI;
@Override @Override
public List<EntitySnapshot> getSnapshots(Entity entity) { public List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id) {
return snapshotAPI.getSnapshots(entity); return snapshotAPI.getSnapshots(metaClass, id);
} }
@Override @Override
@ -69,7 +69,7 @@ public class EntitySnapshotServiceBean implements EntitySnapshotService {
} }
@Override @Override
public void migrateSnapshots(Entity entity, Map<Class, Class> classMapping) { public void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping) {
snapshotAPI.migrateSnapshots(entity, classMapping); snapshotAPI.migrateSnapshots(metaClass, id, classMapping);
} }
} }

View File

@ -39,10 +39,11 @@ public interface EntitySnapshotService {
/** /**
* Get snapshots for entity by id * Get snapshots for entity by id
* @param entity Entity * @param metaClass Entity meta class
* @param id Entity id
* @return Snapshot list * @return Snapshot list
*/ */
List<EntitySnapshot> getSnapshots(Entity entity); List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id);
/** /**
* Create snapshot for entity and save it to database * Create snapshot for entity and save it to database
@ -91,8 +92,9 @@ public interface EntitySnapshotService {
/** /**
* Translate snapshots for archival classes * Translate snapshots for archival classes
* *
* @param entity Entity * @param metaClass Metaclass
* @param id Entity Id
* @param classMapping Map of [OldClass -&gt; NewClass] for migration * @param classMapping Map of [OldClass -&gt; NewClass] for migration
*/ */
void migrateSnapshots(Entity entity, Map<Class, Class> classMapping); void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping);
} }

View File

@ -26,6 +26,9 @@ import org.springframework.stereotype.Component;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.UUID; import java.util.UUID;
/**
* Utility class to provide common functionality for entities with different type of primary keys
*/
@Component(ReferenceToEntitySupport.NAME) @Component(ReferenceToEntitySupport.NAME)
public class ReferenceToEntitySupport { public class ReferenceToEntitySupport {
@ -34,6 +37,10 @@ public class ReferenceToEntitySupport {
@Inject @Inject
protected Metadata metadata; protected Metadata metadata;
/**
* @param entity entity
* @return entity id to store in database
*/
public Object getReferenceId(Entity entity) { public Object getReferenceId(Entity entity) {
if (entity instanceof HasUuid) { if (entity instanceof HasUuid) {
return ((HasUuid) entity).getUuid(); return ((HasUuid) entity).getUuid();
@ -45,6 +52,10 @@ public class ReferenceToEntitySupport {
return entity.getId(); return entity.getId();
} }
/**
* @param metaClass of entity
* @return metaProperty name for storing corresponding primary key in the database
*/
public String getReferenceIdPropertyName(MetaClass metaClass) { public String getReferenceIdPropertyName(MetaClass metaClass) {
if (HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) { if (HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
return "entityId"; 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) { public String getPrimaryKeyForLoadingEntity(MetaClass metaClass) {
if (metadata.getTools().hasCompositePrimaryKey(metaClass) && HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) { if (metadata.getTools().hasCompositePrimaryKey(metaClass) && HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
return "uuid"; return "uuid";

View File

@ -48,7 +48,7 @@ public class EntitySnapshotsDatasource extends CustomCollectionDatasource<Entity
protected Collection<EntitySnapshot> getEntities(Map<String, Object> params) { protected Collection<EntitySnapshot> getEntities(Map<String, Object> params) {
if (entity != null) { if (entity != null) {
EntitySnapshotService snapshotService = AppBeans.get(EntitySnapshotService.NAME); EntitySnapshotService snapshotService = AppBeans.get(EntitySnapshotService.NAME);
snapshots = snapshotService.getSnapshots(entity); snapshots = snapshotService.getSnapshots(entity.getMetaClass(), entity.getId());
return snapshots; return snapshots;
} }
return Collections.emptyList(); return Collections.emptyList();