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
*
* @param entity Entity
* @param metaClass Entity metaclass
* @param id Entity Id
* @return Snapshot list sorted by snapshotDate desc
*/
List<EntitySnapshot> getSnapshots(Entity entity);
List<EntitySnapshot> 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 -&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

View File

@ -74,11 +74,14 @@ public class EntitySnapshotManager implements EntitySnapshotAPI {
@Inject
protected ReferenceToEntitySupport referenceToEntitySupport;
@Override
public List<EntitySnapshot> getSnapshots(Entity entity) {
checkCompositePrimaryKey(entity);
MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass());
@Inject
protected DataManager dataManager;
@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;
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<Class, Class> classMapping) {
MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass());
public void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping) {
metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
// load snapshots
List<EntitySnapshot> snapshotList = getSnapshots(entity);
List<EntitySnapshot> snapshotList = getSnapshots(metaClass, id);
Class javaClass = metaClass.getJavaClass();
MetaClass mappedMetaClass = null;

View File

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

View File

@ -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<EntitySnapshot> getSnapshots(Entity entity);
List<EntitySnapshot> 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 -&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 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";

View File

@ -48,7 +48,7 @@ public class EntitySnapshotsDatasource extends CustomCollectionDatasource<Entity
protected Collection<EntitySnapshot> getEntities(Map<String, Object> 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();