I'm developing a service oriented platform for retrieving, creating and updating entities from DB.
The point here is that every single java entity extends AbstractEntity
, so for example, I have,
MyCar extends AbstractEntity implements Serializable
This AbstractEntity
has common fields (such as id or audit ones).
So I have already developed a GenericReadService
that, receiving a classname and a parameterMap, can read any entity and creates a EntityActionResult<T>
including a List<T extends AbstractEntity>
.
My problem comes when trying to transform that T
type into something like <K extends GenericDTO>
, as the client asking doesn't know AbstractEntity
(obvious) but only GenericDTO
. Doing this for safety and modularization.
So, now, I'm stuck on transforming the ListResponse<T>
to a ReturnList<K extends GenericDTO>
, as I don't find the way for knowing which K
class should apply for each T
.
This is what I actually have written:
private GenericEntityActionResult transform (EntityActionResult result) {
AsnGenericEntityActionResult r = new AsnGenericEntityActionResult();
if (result.getCode() == EntityActionResult.Code.ENTITY || result.getCode() == EntityActionResult.Code.ENTITY_LIST ) {
r.setCode(AsnGenericEntityActionResult.Code.OK);
List <? extends GenericDTO> list = new ArrayList<>();
if (result.getEntity() != null) {
//transform this entity into DTO
//<b>SOMETHING LIKE list.add(result.getEntity());</b>
} else if (result.getList() != null && !result.getList().isEmpty()) {
for (AbstractEntity a:result.getList()) {
//transform this entity into DTO
//<b>SOMETHING LIKE list.add(result.getEntity());</b>
//list.add(result.getEntity());
}
}
r.setList(list);
}
return r;
I´m obviously stuck on the SOMETHING LIKE, but can't find a proper way of doing so.
I thought about creating a abstract <T extends GenericDTO> transformToDTO()
method on AbstractEntity
, but can't do it since there are lots (and i mean hundreds) of Entities extending AbstractEntity
and this client-service approach is a new development for some Entities, not the whole system.
Any clever ideas?