Search in sources :

Example 6 with UnitOfWorkDomainEventsValue

use of org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue 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)

Example 7 with UnitOfWorkDomainEventsValue

use of org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue in project qi4j-sdk by Qi4j.

the class DomainEventTest method testDomainEvent.

@Test
public void testDomainEvent() throws UnitOfWorkCompletionException, IOException {
    // Set principal for the UoW
    Principal administratorPrincipal = new Principal() {

        public String getName() {
            return "administrator";
        }
    };
    // Perform UoW with usecase defined
    UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Change description"));
    uow.setMetaInfo(administratorPrincipal);
    TestEntity entity = uow.newEntity(TestEntity.class);
    entity.changedDescription("New description");
    uow.complete();
    // Print events
    EventSource source = (EventSource) module.findService(EventSource.class).get();
    source.events(0, Long.MAX_VALUE).transferTo(Transforms.map(new Function<UnitOfWorkDomainEventsValue, String>() {

        public String map(UnitOfWorkDomainEventsValue unitOfWorkDomainEventsValue) {
            return unitOfWorkDomainEventsValue.toString();
        }
    }, Outputs.systemOut()));
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) EventSource(org.qi4j.library.eventsourcing.domain.source.EventSource) Principal(java.security.Principal) CurrentUserUoWPrincipal(org.qi4j.library.eventsourcing.domain.factory.CurrentUserUoWPrincipal) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 8 with UnitOfWorkDomainEventsValue

use of org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue in project qi4j-sdk by Qi4j.

the class EventRouterTest method testData.

@Before
public void testData() throws ActivationException, AssemblyException {
    SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.values(UnitOfWorkDomainEventsValue.class, DomainEventValue.class);
        }
    };
    list = new ArrayList<UnitOfWorkDomainEventsValue>();
    {
        ValueBuilder<UnitOfWorkDomainEventsValue> builder = assembler.module().newValueBuilder(UnitOfWorkDomainEventsValue.class);
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test1"));
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test2"));
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test3"));
        builder.prototype().version().set("1.0");
        builder.prototype().timestamp().set(System.currentTimeMillis());
        builder.prototype().usecase().set("Test");
        list.add(builder.newInstance());
    }
    {
        ValueBuilder<UnitOfWorkDomainEventsValue> builder = assembler.module().newValueBuilder(UnitOfWorkDomainEventsValue.class);
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test4"));
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test5"));
        builder.prototype().events().get().add(newDomainEvent(assembler, "Test6"));
        builder.prototype().version().set("1.0");
        builder.prototype().timestamp().set(System.currentTimeMillis());
        builder.prototype().usecase().set("Test2");
        list.add(builder.newInstance());
    }
}
Also used : ValueBuilder(org.qi4j.api.value.ValueBuilder) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) Before(org.junit.Before)

Aggregations

UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)8 ValueBuilder (org.qi4j.api.value.ValueBuilder)4 Before (org.junit.Before)3 Test (org.junit.Test)3 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)3 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)3 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)3 IOException (java.io.IOException)2 Principal (java.security.Principal)2 ArrayList (java.util.ArrayList)2 Function (org.qi4j.functional.Function)2 DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)2 CurrentUserUoWPrincipal (org.qi4j.library.eventsourcing.domain.factory.CurrentUserUoWPrincipal)2 EventSource (org.qi4j.library.eventsourcing.domain.source.EventSource)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 Writer (java.io.Writer)1 Date (java.util.Date)1 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1