Search in sources :

Example 1 with EntityEventWrapper

use of com.kloia.eventapis.view.EntityEventWrapper in project eventapis by kloiasoft.

the class CassandraViewQuery method queryEntityInternal.

private E queryEntityInternal(String entityId, Select select, E previousEntity) throws EventStoreException {
    E currentEntity = previousEntity;
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    for (Row entityEventData : entityEventDatas) {
        EntityEvent entityEvent = convertToEntityEvent(entityEventData);
        if (entityEvent.getStatus() == EventState.CREATED) {
            EntityFunctionSpec<E, ?> functionSpec = functionMap.get(entityEvent.getEventType());
            if (functionSpec != null) {
                EntityEventWrapper eventWrapper = new EntityEventWrapper<>(functionSpec.getQueryType(), objectMapper, entityEvent);
                EntityFunction<E, ?> entityFunction = functionSpec.getEntityFunction();
                currentEntity = (E) entityFunction.apply(currentEntity, eventWrapper);
            } else
                log.trace("Function Spec is not available for " + entityEvent.getEventType() + " EntityId:" + entityId + " Table:" + tableName);
        }
        if (currentEntity != null) {
            currentEntity.setId(entityId);
            currentEntity.setVersion(entityEvent.getEventKey().getVersion());
        }
    }
    return (currentEntity == null || currentEntity.getId() == null) ? null : currentEntity;
}
Also used : PagingIterable(com.datastax.driver.core.PagingIterable) EntityEventWrapper(com.kloia.eventapis.view.EntityEventWrapper) Row(com.datastax.driver.core.Row)

Example 2 with EntityEventWrapper

use of com.kloia.eventapis.view.EntityEventWrapper in project eventapis by kloiasoft.

the class CassandraViewQuery method queryEntityInternal.

private E queryEntityInternal(String entityId, Select select) throws EventStoreException {
    E initialInstance;
    E result = null;
    try {
        initialInstance = entityType.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        log.error(e.getMessage(), e);
        throw new EventStoreException(e);
    }
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    for (Row entityEventData : entityEventDatas) {
        EntityEvent entityEvent = convertToEntityEvent(entityEventData);
        if (entityEvent.getStatus() == EventState.CREATED) {
            EntityFunctionSpec<E, ?> functionSpec = functionMap.get(entityEvent.getEventType());
            if (functionSpec != null) {
                EntityEventWrapper eventWrapper = new EntityEventWrapper<>(functionSpec.getQueryType(), objectMapper, entityEvent);
                EntityFunction<E, ?> entityFunction = functionSpec.getEntityFunction();
                result = (E) entityFunction.apply(result == null ? initialInstance : result, eventWrapper);
            } else
                log.trace("Function Spec is not available for " + entityEvent.getEventType() + " EntityId:" + entityId + " Table:" + tableName);
        }
        if (result != null) {
            result.setId(entityId);
            result.setVersion(entityEvent.getEventKey().getVersion());
        }
    }
    return (result == null || result.getId() == null) ? null : result;
}
Also used : EventStoreException(com.kloia.eventapis.exception.EventStoreException) PagingIterable(com.datastax.driver.core.PagingIterable) EntityEventWrapper(com.kloia.eventapis.view.EntityEventWrapper) Row(com.datastax.driver.core.Row)

Aggregations

PagingIterable (com.datastax.driver.core.PagingIterable)2 Row (com.datastax.driver.core.Row)2 EntityEventWrapper (com.kloia.eventapis.view.EntityEventWrapper)2 EventStoreException (com.kloia.eventapis.exception.EventStoreException)1