Search in sources :

Example 1 with EntityBuilder

use of org.qi4j.api.entity.EntityBuilder in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification.

@Test
public void givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification() throws UnitOfWorkCompletionException {
    TestEntity testEntity;
    {
        UnitOfWork unitOfWork = module.newUnitOfWork();
        EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder(TestEntity.class);
        testEntity = builder.newInstance();
        unitOfWork.complete();
    }
    UnitOfWork unitOfWork1;
    TestEntity testEntity1;
    String version;
    {
        // Start working with Entity in one UoW
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        version = spi.entityStateOf(testEntity1).version();
        if (version.isEmpty()) {
            unitOfWork1.discard();
            // Store doesn't track versions - no point in testing it
            return;
        }
        testEntity1.name().set("A");
        testEntity1.unsetName().set("A");
    }
    {
        // Start working with same Entity in another UoW, and complete it
        UnitOfWork unitOfWork = module.newUnitOfWork();
        TestEntity testEntity2 = unitOfWork.get(testEntity);
        assertThat("version is correct", spi.entityStateOf(testEntity1).version(), equalTo(version));
        testEntity2.name().set("B");
        unitOfWork.complete();
    }
    {
        // Try to complete first UnitOfWork
        try {
            unitOfWork1.complete();
            fail("Should have thrown concurrent modification exception");
        } catch (ConcurrentEntityModificationException e) {
            unitOfWork1.discard();
        }
    }
    {
        // Check values
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        assertThat("property name has not been set", testEntity1.name().get(), equalTo("B"));
        assertThat("version is incorrect", spi.entityStateOf(testEntity1).version(), not(equalTo(version)));
        unitOfWork1.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) EntityBuilder(org.qi4j.api.entity.EntityBuilder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with EntityBuilder

use of org.qi4j.api.entity.EntityBuilder in project qi4j-sdk by Qi4j.

the class TestData method populate.

static void populate(Module module) throws UnitOfWorkCompletionException {
    try (UnitOfWork unitOfWork = module.newUnitOfWork()) {
        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, "accountOfAnnDoe");
            annsAccount = accountBuilder.instance();
            annsAccount.number().set("accountOfAnnDoe");
            annsAccount = accountBuilder.newInstance();
        }
        Account jacksAccount;
        {
            EntityBuilder<Account> accountBuilder = unitOfWork.newEntityBuilder(Account.class, "accountOfJackDoe");
            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().put("anns", annsAccount);
            annDoe.accounts().put("jacks", 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);
            joeDoe.bigInteger().set(new BigInteger("23232323232323232323232323"));
            joeDoe.bigDecimal().set(new BigDecimal("23.4276931348623157e+309"));
            joeDoe.dateValue().set(new DateTime("2020-03-04T13:24:35", UTC).toDate());
            joeDoe.dateTimeValue().set(new DateTime("2020-03-04T13:24:35", UTC));
            joeDoe.localDateTimeValue().set(new LocalDateTime("2020-03-04T13:23:00"));
            joeDoe.localDateValue().set(new LocalDate("2020-03-04"));
            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().put("anns", annsAccount);
            jackDoe.accounts().put("jacks", jacksAccount);
            address = module.newValueBuilderWithPrototype(address).prototype();
            address.line1().set("Qi Avenue 4j");
            jackDoe.address().set(address);
            jackDoe.bigInteger().set(new BigInteger("42424242424242424242424242"));
            jackDoe.bigDecimal().set(new BigDecimal("42.2376931348623157e+309"));
            jackDoe.dateValue().set(new DateTime("2010-03-04T13:24:35", UTC).toDate());
            jackDoe.dateTimeValue().set(new DateTime("2010-03-04T13:24:35", UTC));
            jackDoe.localDateTimeValue().set(new LocalDateTime("2010-03-04T13:23:00"));
            jackDoe.localDateValue().set(new LocalDate("2010-03-04"));
            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<>(2);
            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");
            catBuilder.newInstance();
        }
        unitOfWork.complete();
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) 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) EntityBuilder(org.qi4j.api.entity.EntityBuilder) City(org.qi4j.test.indexing.model.City) MaleEntity(org.qi4j.test.indexing.model.entities.MaleEntity) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) 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) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) Domain(org.qi4j.test.indexing.model.Domain) Protocol(org.qi4j.test.indexing.model.Protocol)

Example 3 with EntityBuilder

use of org.qi4j.api.entity.EntityBuilder in project qi4j-sdk by Qi4j.

the class TraceServiceMixin method createTraceRecord.

private void createTraceRecord(UnitOfWork uow, Class compositeType, Composite object, Method method, Object[] args, long entryTime, long durationNano, Throwable exception) {
    if (object instanceof EntityComposite) {
        EntityComposite entity = (EntityComposite) object;
        String identity = entity.identity().get();
        EntityComposite source = (EntityComposite) uow.get((Class<?>) first(Qi4j.FUNCTION_DESCRIPTOR_FOR.map(entity).types()), identity);
        EntityBuilder<EntityTraceRecordEntity> builder = uow.newEntityBuilder(EntityTraceRecordEntity.class);
        EntityTraceRecordEntity state = builder.instance();
        setStandardStuff(compositeType, method, args, entryTime, durationNano, state, exception);
        state.source().set(source);
        // Record is created.
        EntityTraceRecordEntity etr = builder.newInstance();
    } else if (object instanceof ServiceComposite) {
        ServiceComposite service = (ServiceComposite) object;
        EntityBuilder<ServiceTraceRecordEntity> builder = uow.newEntityBuilder(ServiceTraceRecordEntity.class);
        ServiceTraceRecordEntity state = builder.instance();
        setStandardStuff(compositeType, method, args, entryTime, durationNano, state, exception);
        state.source().set(service.toString());
        // Record is created.
        ServiceTraceRecordEntity str = builder.newInstance();
    } else {
        EntityBuilder<CompositeTraceRecordEntity> builder = uow.newEntityBuilder(CompositeTraceRecordEntity.class);
        CompositeTraceRecordEntity state = builder.instance();
        state.source().set(object);
        setStandardStuff(compositeType, method, args, entryTime, durationNano, state, exception);
        // Record is created.
        CompositeTraceRecordEntity ctr = builder.newInstance();
    }
}
Also used : EntityTraceRecordEntity(org.qi4j.logging.trace.records.EntityTraceRecordEntity) EntityComposite(org.qi4j.api.entity.EntityComposite) EntityBuilder(org.qi4j.api.entity.EntityBuilder) ServiceComposite(org.qi4j.api.service.ServiceComposite) CompositeTraceRecordEntity(org.qi4j.logging.trace.records.CompositeTraceRecordEntity) ServiceTraceRecordEntity(org.qi4j.logging.trace.records.ServiceTraceRecordEntity)

Example 4 with EntityBuilder

use of org.qi4j.api.entity.EntityBuilder in project qi4j-sdk by Qi4j.

the class DebuggingServiceMixin method createDebugRecord.

private void createDebugRecord(UnitOfWork uow, Composite composite, String message, List<Serializable> params) {
    if (composite instanceof ServiceComposite) {
        EntityBuilder<ServiceDebugRecordEntity> builder = uow.newEntityBuilder(ServiceDebugRecordEntity.class);
        ServiceDebugRecordEntity state = builder.instance();
        setStandardStuff(composite, message, state, params);
        state.source().set(((ServiceComposite) composite).identity().get());
        ServiceDebugRecordEntity slr = builder.newInstance();
    } else if (composite instanceof EntityComposite) {
        EntityBuilder<EntityDebugRecordEntity> builder = uow.newEntityBuilder(EntityDebugRecordEntity.class);
        EntityDebugRecordEntity state = builder.instance();
        setStandardStuff(composite, message, state, params);
        state.source().set((EntityComposite) composite);
        EntityDebugRecordEntity elr = builder.newInstance();
    } else {
        EntityBuilder<CompositeDebugRecordEntity> builder = uow.newEntityBuilder(CompositeDebugRecordEntity.class);
        CompositeDebugRecordEntity state = builder.instance();
        setStandardStuff(composite, message, state, params);
        state.source().set(composite);
        CompositeDebugRecordEntity clr = builder.newInstance();
    }
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityBuilder(org.qi4j.api.entity.EntityBuilder) EntityDebugRecordEntity(org.qi4j.logging.debug.records.EntityDebugRecordEntity) ServiceComposite(org.qi4j.api.service.ServiceComposite) ServiceDebugRecordEntity(org.qi4j.logging.debug.records.ServiceDebugRecordEntity) CompositeDebugRecordEntity(org.qi4j.logging.debug.records.CompositeDebugRecordEntity)

Example 5 with EntityBuilder

use of org.qi4j.api.entity.EntityBuilder in project qi4j-sdk by Qi4j.

the class AbstractDataSourceServiceImporterMixin method getConfiguration.

private DataSourceConfigurationValue getConfiguration(String identity) throws InstantiationException {
    DataSourceConfigurationValue config = configs.get(identity);
    if (config == null) {
        UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("Create DataSource pool configuration"));
        try {
            DataSourceConfiguration configEntity = uow.get(DataSourceConfiguration.class, identity);
            config = entityToValue.convert(DataSourceConfigurationValue.class, configEntity);
        } catch (NoSuchEntityException e) {
            EntityBuilder<DataSourceConfiguration> configBuilder = uow.newEntityBuilder(DataSourceConfiguration.class, identity);
            // Check for defaults
            String s = identity + ".properties";
            InputStream asStream = DataSourceConfiguration.class.getClassLoader().getResourceAsStream(s);
            if (asStream != null) {
                try {
                    PropertyMapper.map(asStream, configBuilder.instance());
                } catch (IOException e1) {
                    uow.discard();
                    InstantiationException exception = new InstantiationException("Could not read underlying Properties file.");
                    exception.initCause(e1);
                    throw exception;
                }
            }
            DataSourceConfiguration configEntity = configBuilder.newInstance();
            config = entityToValue.convert(DataSourceConfigurationValue.class, configEntity);
            // save
            try {
                uow.complete();
            } catch (UnitOfWorkCompletionException e2) {
                InstantiationException exception = new InstantiationException("Could not save configuration in JavaPreferences.");
                exception.initCause(e2);
                throw exception;
            }
        }
        configs.put(identity, config);
    }
    return config;
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) InputStream(java.io.InputStream) EntityBuilder(org.qi4j.api.entity.EntityBuilder) IOException(java.io.IOException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Aggregations

EntityBuilder (org.qi4j.api.entity.EntityBuilder)7 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)4 EntityComposite (org.qi4j.api.entity.EntityComposite)3 ServiceComposite (org.qi4j.api.service.ServiceComposite)3 Test (org.junit.Test)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 LocalDateTime (org.joda.time.LocalDateTime)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 ValueBuilder (org.qi4j.api.value.ValueBuilder)1 CompositeDebugRecordEntity (org.qi4j.logging.debug.records.CompositeDebugRecordEntity)1