Search in sources :

Example 1 with UnitOfWorkDomainEventsValue

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

the class UnitOfWorkRouterTest method testRouterException.

@Test(expected = IOException.class)
public void testRouterException() throws IOException {
    final List<String> matched = new ArrayList<String>();
    UnitOfWorkRouter<IOException> router = new UnitOfWorkRouter<IOException>();
    router.route(Events.withUsecases("Test2"), new Receiver<UnitOfWorkDomainEventsValue, IOException>() {

        @Override
        public void receive(UnitOfWorkDomainEventsValue item) throws IOException {
            throw new IOException("Failed");
        }
    });
    EventRouter<IOException> eventRouter = new EventRouter<IOException>();
    eventRouter.defaultReceiver(new Receiver<DomainEventValue, IOException>() {

        @Override
        public void receive(DomainEventValue item) throws IOException {
            System.out.println(item);
        }
    });
    router.defaultReceiver(eventRouter);
    Inputs.iterable(list).transferTo(router);
    Assert.assertThat(matched.toString(), CoreMatchers.equalTo("[Test]"));
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue) Test(org.junit.Test)

Example 2 with UnitOfWorkDomainEventsValue

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

the class UnitOfWorkRouterTest 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)

Example 3 with UnitOfWorkDomainEventsValue

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

the class EventsTest 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)

Example 4 with UnitOfWorkDomainEventsValue

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

the class JdbmEventStoreServiceTest method testDomainEvent.

@Test
public void testDomainEvent() throws UnitOfWorkCompletionException, IOException {
    UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Create entity"));
    TestEntity entity = uow.newEntity(TestEntity.class);
    uow.complete();
    int count = 10;
    for (int i = 0; i < count; i++) {
        uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Change description"));
        uow.setMetaInfo(new Principal() {

            public String getName() {
                return "administrator";
            }
        });
        entity = uow.get(entity);
        entity.changeDescription("New description");
        uow.complete();
    }
    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 5 with UnitOfWorkDomainEventsValue

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

the class DomainEventSourceResource method handle.

@Override
public void handle(Request request, Response response) {
    long eventCount = source.count();
    long pageSize = 10;
    long startEvent = -1;
    long endEvent = -1;
    long limit = pageSize;
    final List<UnitOfWorkDomainEventsValue> eventsValues = new ArrayList<UnitOfWorkDomainEventsValue>();
    final Feed feed = new Feed();
    feed.setBaseReference(request.getResourceRef().getParentRef());
    List<Link> links = feed.getLinks();
    String remainingPart = request.getResourceRef().getRemainingPart();
    if (remainingPart.equals("/")) {
        // Current set - always contains the last "pageSize" events
        startEvent = Math.max(0, eventCount - pageSize - 1);
        feed.setTitle(new Text("Current set"));
        if (startEvent > 0) {
            long previousStart = Math.max(0, startEvent - pageSize);
            long previousEnd = startEvent - 1;
            Link link = new Link(new Reference(previousStart + "," + previousEnd), new Relation("previous"), MediaType.APPLICATION_ATOM);
            link.setTitle("Previous page");
            links.add(link);
        }
    } else {
        // Archive
        String[] indices = remainingPart.substring(1).split(",");
        if (indices.length == 1) {
            // Working set
            startEvent = Long.parseLong(indices[0]);
            endEvent = startEvent + pageSize - 1;
            limit = pageSize;
            feed.setTitle(new Text("Working set"));
        } else if (indices.length == 2) {
            feed.setTitle(new Text("Archive page"));
            startEvent = Long.parseLong(indices[0]);
            endEvent = Long.parseLong(indices[1]);
            limit = 1 + endEvent - startEvent;
        } else
            throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
        if (startEvent > 0) {
            long previousStart = Math.max(0, startEvent - pageSize);
            long previousEnd = startEvent - 1;
            Link link = new Link(new Reference(previousStart + "," + previousEnd), new Relation("previous"), MediaType.APPLICATION_ATOM);
            link.setTitle("Previous page");
            links.add(link);
        }
        long nextStart = endEvent + 1;
        long nextEnd = nextStart + pageSize - 1;
        if (nextStart < eventCount)
            if (nextEnd >= eventCount) {
                Link next = new Link(new Reference(nextStart + ""), new Relation("next"), MediaType.APPLICATION_ATOM);
                next.setTitle("Working set");
                links.add(next);
            } else {
                Link next = new Link(new Reference(nextStart + "," + nextEnd), new Relation("next"), MediaType.APPLICATION_ATOM);
                next.setTitle("Next page");
                links.add(next);
            }
    }
    try {
        source.events(startEvent, limit).transferTo(Outputs.collection(eventsValues));
    } catch (Throwable throwable) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, throwable);
    }
    Link last = new Link(new Reference("0," + (pageSize - 1)), new Relation("last"), MediaType.APPLICATION_ATOM);
    last.setTitle("Last archive page");
    links.add(last);
    Link first = new Link(new Reference("."), new Relation("first"), MediaType.APPLICATION_ATOM);
    first.setTitle("Current set");
    links.add(first);
    /*
        if (previousPage != -1)
        {
            Link link = new Link( new Reference( ""+previousPage ), new Relation( "prev-archive" ), MediaType.APPLICATION_ATOM );
            link.setTitle( "Previous archive page" );
            links.add( link );
        }
        if (nextPage != -1)
        {
            Link link = new Link( new Reference( "" + nextPage ), new Relation( "next-archive" ), MediaType.APPLICATION_ATOM );
            link.setTitle( "Next archive page" );
            links.add( link );
        }
        else if (startEvent != workingSetOffset)
        {
            Link next = new Link( new Reference( "" ), new Relation( "next" ), MediaType.APPLICATION_ATOM );
            next.setTitle( "Next page" );
            links.add( next );
        }
*/
    Date lastModified = null;
    for (UnitOfWorkDomainEventsValue eventsValue : eventsValues) {
        Entry entry = new Entry();
        entry.setTitle(new Text(eventsValue.usecase().get() + "(" + eventsValue.user().get() + ")"));
        entry.setPublished(new Date(eventsValue.timestamp().get()));
        entry.setModificationDate(lastModified = new Date(eventsValue.timestamp().get()));
        entry.setId(Long.toString(startEvent + 1));
        startEvent++;
        Content content = new Content();
        content.setInlineContent(new StringRepresentation(eventsValue.toString(), MediaType.APPLICATION_JSON));
        entry.setContent(content);
        feed.getEntries().add(entry);
    }
    feed.setModificationDate(lastModified);
    MediaType mediaType = request.getClientInfo().getPreferredMediaType(Iterables.toList(iterable(MediaType.TEXT_HTML, MediaType.APPLICATION_ATOM)));
    if (MediaType.APPLICATION_ATOM.equals(mediaType)) {
        WriterRepresentation representation = new WriterRepresentation(MediaType.APPLICATION_ATOM) {

            @Override
            public void write(final Writer writer) throws IOException {
                feed.write(writer);
            }
        };
        representation.setCharacterSet(CharacterSet.UTF_8);
        response.setEntity(representation);
    } else {
        WriterRepresentation representation = new WriterRepresentation(MediaType.TEXT_HTML) {

            @Override
            public void write(Writer writer) throws IOException {
                writer.append("<html><head><title>Events</title></head><body>");
                for (Link link : feed.getLinks()) {
                    writer.append("<a href=\"").append(link.getHref().getPath()).append("\">");
                    writer.append(link.getTitle());
                    writer.append("</a><br/>");
                }
                writer.append("<ol>");
                for (Entry entry : feed.getEntries()) {
                    writer.append("<li>").append(entry.getTitle().toString()).append("</li>");
                }
                writer.append("</ol></body>");
            }
        };
        representation.setCharacterSet(CharacterSet.UTF_8);
        response.setEntity(representation);
    }
/*
        } else
        {
            throw new ResourceException( Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE );
        }
*/
}
Also used : Reference(org.restlet.data.Reference) ArrayList(java.util.ArrayList) Date(java.util.Date) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) ResourceException(org.restlet.resource.ResourceException) Writer(java.io.Writer)

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