/**
* Make an entity data object out of whatever entity is given,
* use the given reference, if there is no id then this will attempt to get one otherwise it will use prefix only
*/
public static EntityData makeEntityData(EntityReference ref, Object entity) {
EntityData ed = null;
if (entity != null) {
if (ref == null) {
throw new IllegalArgumentException("ref must not be null or no entity data object can be created");
}
Class<?> resultClass = entity.getClass();
if (EntityData.class.isAssignableFrom(resultClass)) {
ed = (EntityData) entity;
} else {
if (ref.getId() == null) {
// attempt to get the id if it was not provided
String entityId = getEntityId(entity);
if (entityId != null) {
ref = new EntityReference(ref.getPrefix(), entityId);
}
}
Object entityObject = entity;
if (ActionReturn.class.isAssignableFrom(resultClass)) {
ActionReturn ar = (ActionReturn) entity;
if (ar.entityData == null) {
// make entity data from AR
if (ar.outputString != null) {
entityObject = ar.outputString;
} else if (ar.output != null) {
entityObject = ar.output;
}
} else {
ed = ar.entityData;
}
// removed because it makes a mess of the output, maybe add this in later though -AZ
// } else if (Map.class.isAssignableFrom(resultClass)) {
// props = EntityDataUtils.extractMapProperties((Map)entity);
}
if (ed == null) {
ed = new EntityData(entityObject);
}
}
}
return ed;
}