Search in sources :

Example 6 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class UnitOfWorkNotificationConcern method createEvent.

@Override
public DomainEventValue createEvent(EntityComposite entity, String name, Object[] args) {
    final UnitOfWork unitOfWork = uowf.currentUnitOfWork();
    DomainEventValue eventValue = next.createEvent(api.dereference(entity), name, args);
    // Add eventValue to list in UoW
    UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
    if (events == null) {
        events = new UnitOfWorkEvents();
        unitOfWork.setMetaInfo(events);
        unitOfWork.addUnitOfWorkCallback(new UnitOfWorkCallback() {

            String user;

            @Override
            public void beforeCompletion() throws UnitOfWorkCompletionException {
                user = currentUser.getCurrentUser();
            }

            @Override
            public void afterCompletion(UnitOfWorkStatus status) {
                if (status.equals(UnitOfWorkStatus.COMPLETED)) {
                    UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
                    ValueBuilder<UnitOfWorkDomainEventsValue> builder = vbf.newValueBuilder(UnitOfWorkDomainEventsValue.class);
                    builder.prototype().user().set(user);
                    builder.prototype().timestamp().set(System.currentTimeMillis());
                    builder.prototype().usecase().set(unitOfWork.usecase().name());
                    builder.prototype().version().set(version);
                    builder.prototype().events().get().addAll(events.getEventValues());
                    try {
                        final UnitOfWorkDomainEventsValue unitOfWorkDomainValue = builder.newInstance();
                        Inputs.iterable(Iterables.iterable(unitOfWorkDomainValue)).transferTo(eventOutput);
                        for (UnitOfWorkEventsVisitor unitOfWorkEventsVisitor : transactionVisitors) {
                            try {
                                unitOfWorkEventsVisitor.visit(unitOfWorkDomainValue);
                            } catch (Exception e) {
                                logger.warn("Could not deliver events", e);
                            }
                        }
                    } catch (IOException e) {
                        logger.error("Could not store events", e);
                    // How do we handle this? This is a major error!
                    }
                }
            }
        });
    }
    events.add(eventValue);
    return eventValue;
}
Also used : ValueBuilder(org.qi4j.api.value.ValueBuilder) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) IOException(java.io.IOException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) UnitOfWorkEventsVisitor(org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor) UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue)

Aggregations

UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)6 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)4 IOException (java.io.IOException)2 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)2 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)2 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 ValueBuilder (org.qi4j.api.value.ValueBuilder)1 ApplicationEvent (org.qi4j.library.eventsourcing.application.api.ApplicationEvent)1 DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)1 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)1 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)1