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;
}
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()));
}
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());
}
}
Aggregations