Search in sources :

Example 1 with ValueBuilder

use of org.qi4j.api.value.ValueBuilder in project qi4j-sdk by Qi4j.

the class TestData method populate.

static void populate(Module module) throws UnitOfWorkCompletionException {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    try {
        ValueBuilderFactory valueBuilderFactory = module;
        NameableAssert.clear();
        Domain gaming;
        {
            EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder(Domain.class, "Gaming");
            gaming = domainBuilder.instance();
            gaming.name().set("Gaming");
            gaming.description().set("Gaming domain");
            gaming = domainBuilder.newInstance();
            NameableAssert.trace(gaming);
        }
        Domain programming;
        {
            EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder(Domain.class);
            programming = domainBuilder.instance();
            programming.name().set("Programming");
            programming.description().set("Programing domain");
            programming = domainBuilder.newInstance();
            NameableAssert.trace(programming);
        }
        Domain cooking;
        {
            EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder(Domain.class);
            cooking = domainBuilder.instance();
            cooking.name().set("Cooking");
            cooking.description().set("Cooking domain");
            cooking = domainBuilder.newInstance();
            NameableAssert.trace(cooking);
        }
        Domain cars;
        {
            EntityBuilder<Domain> domainBuilder = unitOfWork.newEntityBuilder(Domain.class);
            cars = domainBuilder.instance();
            cars.name().set("Cars");
            cars.description().set("Cars");
            cars = domainBuilder.newInstance();
            NameableAssert.trace(cars);
        }
        City kualaLumpur;
        {
            EntityBuilder<City> cityBuilder = unitOfWork.newEntityBuilder(City.class);
            kualaLumpur = cityBuilder.instance();
            kualaLumpur.name().set("Kuala Lumpur");
            kualaLumpur.country().set("Malaysia");
            kualaLumpur.county().set("Some Jaya");
            kualaLumpur = cityBuilder.newInstance();
            NameableAssert.trace(kualaLumpur);
        }
        City penang;
        {
            EntityBuilder<City> cityBuilder = unitOfWork.newEntityBuilder(City.class);
            penang = cityBuilder.instance();
            penang.name().set("Penang");
            penang.country().set("Malaysia");
            penang.county().set("Some Other Jaya");
            penang = cityBuilder.newInstance();
            NameableAssert.trace(penang);
        }
        Account annsAccount;
        {
            EntityBuilder<Account> accountBuilder = unitOfWork.newEntityBuilder(Account.class);
            annsAccount = accountBuilder.instance();
            annsAccount.number().set("accountOfAnnDoe");
            annsAccount = accountBuilder.newInstance();
        }
        Account jacksAccount;
        {
            EntityBuilder<Account> accountBuilder = unitOfWork.newEntityBuilder(Account.class);
            jacksAccount = accountBuilder.instance();
            jacksAccount.number().set("accountOfJackDoe");
            jacksAccount = accountBuilder.newInstance();
        }
        ValueBuilder<Address> addressBuilder = module.newValueBuilder(Address.class);
        Address address = addressBuilder.prototype();
        address.line1().set("Qi Street 4j");
        address.line2().set("Off main Java Street");
        address.zipcode().set("12345");
        Female annDoe;
        {
            EntityBuilder<FemaleEntity> femaleBuilder = unitOfWork.newEntityBuilder(FemaleEntity.class, "anndoe");
            annDoe = femaleBuilder.instance();
            annDoe.name().set("Ann Doe");
            annDoe.title().set(Person.Title.MRS);
            annDoe.placeOfBirth().set(kualaLumpur);
            annDoe.yearOfBirth().set(1975);
            annDoe.interests().add(0, cooking);
            annDoe.password().set("passwordOfAnnDoe");
            annDoe.mainAccount().set(annsAccount);
            annDoe.accounts().add(0, annsAccount);
            annDoe.accounts().add(0, jacksAccount);
            annDoe.address().set(addressBuilder.newInstance());
            annDoe = femaleBuilder.newInstance();
            NameableAssert.trace(annDoe);
        }
        {
            EntityBuilder<MaleEntity> maleBuilder = unitOfWork.newEntityBuilder(MaleEntity.class);
            Male joeDoe = maleBuilder.instance();
            joeDoe.name().set("Joe Doe");
            joeDoe.title().set(Person.Title.MR);
            joeDoe.placeOfBirth().set(kualaLumpur);
            joeDoe.yearOfBirth().set(1990);
            joeDoe.mother().set(annDoe);
            joeDoe.interests().add(0, programming);
            joeDoe.interests().add(0, gaming);
            joeDoe.email().set("joe@thedoes.net");
            joeDoe.password().set("passwordOfJoeDoe");
            joeDoe = maleBuilder.newInstance();
            address = module.newValueBuilderWithPrototype(address).prototype();
            address.line1().set("Qi Alley 4j");
            joeDoe.address().set(address);
            NameableAssert.trace(joeDoe);
        }
        {
            EntityBuilder<MaleEntity> maleBuilder = unitOfWork.newEntityBuilder(MaleEntity.class);
            Male jackDoe = maleBuilder.instance();
            jackDoe.name().set("Jack Doe");
            jackDoe.title().set(Person.Title.DR);
            jackDoe.placeOfBirth().set(penang);
            jackDoe.yearOfBirth().set(1970);
            jackDoe.interests().add(0, cars);
            jackDoe.wife().set(annDoe);
            jackDoe.password().set("passwordOfJohnDoe");
            jackDoe.mainAccount().set(jacksAccount);
            jackDoe.accounts().add(0, annsAccount);
            jackDoe.accounts().add(0, jacksAccount);
            address = module.newValueBuilderWithPrototype(address).prototype();
            address.line1().set("Qi Avenue 4j");
            jackDoe.address().set(address);
            ValueBuilder<URL> urlBuilder = module.newValueBuilder(URL.class);
            ValueBuilder<Protocol> protocolBuilder = module.newValueBuilder(Protocol.class);
            ValueBuilder<QueryParam> queryParamBuilder = module.newValueBuilder(QueryParam.class);
            Protocol protocol = protocolBuilder.prototype();
            protocol.value().set("http");
            List<QueryParam> queryParams = new ArrayList<QueryParam>();
            QueryParam param = queryParamBuilder.prototype();
            param.name().set("user");
            param.value().set("jackdoe");
            queryParams.add(queryParamBuilder.newInstance());
            queryParamBuilder = module.newValueBuilder(QueryParam.class);
            param = queryParamBuilder.prototype();
            param.name().set("password");
            param.value().set("somepassword");
            queryParams.add(queryParamBuilder.newInstance());
            URL url = urlBuilder.prototype();
            url.protocol().set(protocolBuilder.newInstance());
            url.queryParams().set(queryParams);
            jackDoe.personalWebsite().set(urlBuilder.newInstance());
            jackDoe = maleBuilder.newInstance();
            NameableAssert.trace(jackDoe);
        }
        {
            EntityBuilder<CatEntity> catBuilder = unitOfWork.newEntityBuilder(CatEntity.class);
            Cat felix = catBuilder.instance();
            felix.name().set("Felix");
            felix = catBuilder.newInstance();
            // NameableAssert.trace( felix );
            unitOfWork.complete();
        }
    } finally {
        unitOfWork.discard();
    }
}
Also used : ValueBuilder(org.qi4j.api.value.ValueBuilder) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Account(org.qi4j.test.indexing.model.Account) Address(org.qi4j.test.indexing.model.Address) Male(org.qi4j.test.indexing.model.Male) CatEntity(org.qi4j.test.indexing.model.entities.CatEntity) ValueBuilderFactory(org.qi4j.api.value.ValueBuilderFactory) EntityBuilder(org.qi4j.api.entity.EntityBuilder) City(org.qi4j.test.indexing.model.City) MaleEntity(org.qi4j.test.indexing.model.entities.MaleEntity) URL(org.qi4j.test.indexing.model.URL) QueryParam(org.qi4j.test.indexing.model.QueryParam) FemaleEntity(org.qi4j.test.indexing.model.entities.FemaleEntity) Cat(org.qi4j.test.indexing.model.Cat) Female(org.qi4j.test.indexing.model.Female) ArrayList(java.util.ArrayList) List(java.util.List) Domain(org.qi4j.test.indexing.model.Domain) Protocol(org.qi4j.test.indexing.model.Protocol)

Example 2 with ValueBuilder

use of org.qi4j.api.value.ValueBuilder 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 ValueBuilder

use of org.qi4j.api.value.ValueBuilder 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 ValueBuilder

use of org.qi4j.api.value.ValueBuilder 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)

Example 5 with ValueBuilder

use of org.qi4j.api.value.ValueBuilder 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

ValueBuilder (org.qi4j.api.value.ValueBuilder)5 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)4 Before (org.junit.Before)3 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)3 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)3 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityBuilder (org.qi4j.api.entity.EntityBuilder)1 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 ValueBuilderFactory (org.qi4j.api.value.ValueBuilderFactory)1 DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1 Account (org.qi4j.test.indexing.model.Account)1 Address (org.qi4j.test.indexing.model.Address)1 Cat (org.qi4j.test.indexing.model.Cat)1 City (org.qi4j.test.indexing.model.City)1 Domain (org.qi4j.test.indexing.model.Domain)1