Search in sources :

Example 6 with PagingIterable

use of com.datastax.driver.core.PagingIterable 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)6 Row (com.datastax.driver.core.Row)6 Select (com.datastax.driver.core.querybuilder.Select)4 EventStoreException (com.kloia.eventapis.exception.EventStoreException)2 EntityEventWrapper (com.kloia.eventapis.view.EntityEventWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ResultSet (com.datastax.driver.core.ResultSet)1 Insert (com.datastax.driver.core.querybuilder.Insert)1 QueryBuilder (com.datastax.driver.core.querybuilder.QueryBuilder)1 Update (com.datastax.driver.core.querybuilder.Update)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IUserContext (com.kloia.eventapis.api.IUserContext)1 IdCreationStrategy (com.kloia.eventapis.api.IdCreationStrategy)1 Views (com.kloia.eventapis.api.Views)1 UUIDCreationStrategy (com.kloia.eventapis.api.impl.UUIDCreationStrategy)1 EventKey (com.kloia.eventapis.common.EventKey)1 EventRecorder (com.kloia.eventapis.common.EventRecorder)1 OperationContext (com.kloia.eventapis.common.OperationContext)1