@Override public <T extends Aggregate<T>> LoadedEvents find(String aggregateType, String entityId, Optional<AggregateCrudFindOptions> findOptions) { try { return target.find(aggregateType, entityId, findOptions).get(timeoutOptions.getTimeout(), timeoutOptions.getTimeUnit()); } catch (Throwable e) { Throwable unwrapped = CompletableFutureUtil.unwrap(e); if (unwrapped instanceof RuntimeException) throw (RuntimeException)unwrapped; else throw new RuntimeException(unwrapped); } }
@Override public <T extends Aggregate<T>> LoadedEvents find(String aggregateType, String entityId, Optional<AggregateCrudFindOptions> findOptions) { try { return target.find(aggregateType, entityId, findOptions).get(timeoutOptions.getTimeout(), timeoutOptions.getTimeUnit()); } catch (Throwable e) { Throwable unwrapped = CompletableFutureUtil.unwrap(e); if (unwrapped instanceof RuntimeException) throw (RuntimeException)unwrapped; else throw new RuntimeException(unwrapped); } }
@Override public <T extends Aggregate<T>> CompletableFuture<EntityWithMetadata<T>> find(Class<T> clasz, String entityId, Optional<FindOptions> findOptions) { CompletableFuture<LoadedEvents> outcome = aggregateCrud.find(clasz.getName(), entityId, AggregateCrudMapping.toAggregateCrudFindOptions(findOptions)); CompletableFuture<LoadedEvents> tappedOutcome; if (activityLogger.isDebugEnabled()) tappedOutcome = CompletableFutureUtil.tap(outcome, (result, throwable) -> { if (throwable == null) activityLogger.debug("Loaded entity: {} {} {}", clasz.getName(), entityId, result.getEvents()); else { if (throwable instanceof EventuateException) activityLogger.trace(String.format("Find entity failed: %s %s %s", clasz.getName(), entityId, throwable.getClass().getName())); else activityLogger.trace(String.format("Find entity failed: %s %s", clasz.getName(), entityId), throwable); } }); else tappedOutcome = outcome; return tappedOutcome.thenApply(le -> { List<EventWithMetadata> eventsWithIds = eventSchemaMetadataManager.upcastEvents(clasz, le.getEvents()).stream().map(AggregateCrudMapping::toEventWithMetadata).collect(Collectors.toList()); List<Event> events = eventsWithIds.stream().map(EventWithMetadata::getEvent).collect(Collectors.toList()); return new EntityWithMetadata<T>( new EntityIdAndVersion(entityId, le.getEvents().isEmpty() ? le.getSnapshot().get().getEntityVersion() : le.getEvents().get(le.getEvents().size() - 1).getId()), le.getSnapshot().map(SerializedSnapshotWithVersion::getEntityVersion), eventsWithIds, le.getSnapshot().map(ss -> Aggregates.applyEventsToMutableAggregate((T) snapshotManager.recreateFromSnapshot(clasz, AggregateCrudMapping.toSnapshot(ss.getSerializedSnapshot()), missingApplyEventMethodStrategy), events, missingApplyEventMethodStrategy)) .orElseGet(() -> Aggregates.recreateAggregate(clasz, events, missingApplyEventMethodStrategy))); }); }
@Override public <T extends Aggregate<T>> CompletableFuture<EntityWithMetadata<T>> find(Class<T> clasz, String entityId, Optional<FindOptions> findOptions) { CompletableFuture<LoadedEvents> outcome = aggregateCrud.find(clasz.getName(), entityId, AggregateCrudMapping.toAggregateCrudFindOptions(findOptions)); CompletableFuture<LoadedEvents> tappedOutcome; if (activityLogger.isDebugEnabled()) tappedOutcome = CompletableFutureUtil.tap(outcome, (result, throwable) -> { if (throwable == null) activityLogger.debug("Loaded entity: {} {} {}", clasz.getName(), entityId, result.getEvents()); else { if (throwable instanceof EventuateException) activityLogger.trace(String.format("Find entity failed: %s %s %s", clasz.getName(), entityId, throwable.getClass().getName())); else activityLogger.trace(String.format("Find entity failed: %s %s", clasz.getName(), entityId), throwable); } }); else tappedOutcome = outcome; return tappedOutcome.thenApply(le -> { List<EventWithMetadata> eventsWithIds = eventSchemaMetadataManager.upcastEvents(clasz, le.getEvents()).stream().map(AggregateCrudMapping::toEventWithMetadata).collect(Collectors.toList()); List<Event> events = eventsWithIds.stream().map(EventWithMetadata::getEvent).collect(Collectors.toList()); return new EntityWithMetadata<T>( new EntityIdAndVersion(entityId, le.getEvents().isEmpty() ? le.getSnapshot().get().getEntityVersion() : le.getEvents().get(le.getEvents().size() - 1).getId()), le.getSnapshot().map(SerializedSnapshotWithVersion::getEntityVersion), eventsWithIds, le.getSnapshot().map(ss -> Aggregates.applyEventsToMutableAggregate((T) snapshotManager.recreateFromSnapshot(clasz, AggregateCrudMapping.toSnapshot(ss.getSerializedSnapshot()), missingApplyEventMethodStrategy), events, missingApplyEventMethodStrategy)) .orElseGet(() -> Aggregates.recreateAggregate(clasz, events, missingApplyEventMethodStrategy))); }); }