Search in sources :

Example 6 with EntityBuilder

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

the class AssociationTest method testAssociation.

@Test
public void testAssociation() {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    try {
        Company company = unitOfWork.newEntity(Company.class);
        Assert.assertEquals("Company Name Default", "A Company", company.name().get());
        {
            EntityBuilder<Company> builder = unitOfWork.newEntityBuilder(Company.class);
            final Company companyPrototype = builder.instance();
            companyPrototype.name().set("JayWay");
            company = builder.newInstance();
            Assert.assertEquals("Company Name ", "JayWay", company.name().get());
        }
        company.name().set("Jayway");
        Assert.assertEquals("Company Name ", "Jayway", company.name().get());
        System.out.println("Name is:" + company.name().get());
        EntityBuilder<Person> builder = unitOfWork.newEntityBuilder(Person.class);
        builder.instance().name().set("Rickard");
        Person rickard = builder.newInstance();
        builder = unitOfWork.newEntityBuilder(Person.class);
        builder.instance().name().set("Niclas");
        builder.instance().friend().set(rickard);
        Person niclas = builder.newInstance();
        niclas.members().add(rickard);
        company.employees().add(0, rickard);
        for (Employer employer : rickard.employers()) {
            System.out.println(((Nameable) employer).name());
        }
        Assert.assertEquals(niclas.friend().get(), rickard);
        Assert.assertEquals(niclas.members().get(0), rickard);
    } finally {
        unitOfWork.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) EntityBuilder(org.qi4j.api.entity.EntityBuilder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 7 with EntityBuilder

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

the class LoggingServiceMixin method createLogRecord.

private void createLogRecord(UnitOfWork uow, LogType type, Composite composite, String category, String message, List<Serializable> params) {
    if (composite instanceof ServiceComposite) {
        EntityBuilder<ServiceLogRecord> builder = uow.newEntityBuilder(ServiceLogRecord.class);
        ServiceLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set(((ServiceComposite) composite).identity().get());
        ServiceLogRecord slr = builder.newInstance();
    } else if (composite instanceof EntityComposite) {
        EntityBuilder<EntityLogRecord> builder = uow.newEntityBuilder(EntityLogRecord.class);
        EntityLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set((EntityComposite) composite);
        EntityLogRecord elr = builder.newInstance();
    } else {
        EntityBuilder<CompositeLogRecord> builder = uow.newEntityBuilder(CompositeLogRecord.class);
        CompositeLogRecord state = builder.instance();
        setStandardStuff(type, composite, category, message, state, params);
        state.source().set(composite);
        CompositeLogRecord clr = builder.newInstance();
    }
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) CompositeLogRecord(org.qi4j.logging.log.records.CompositeLogRecord) EntityLogRecord(org.qi4j.logging.log.records.EntityLogRecord) EntityBuilder(org.qi4j.api.entity.EntityBuilder) ServiceLogRecord(org.qi4j.logging.log.records.ServiceLogRecord) ServiceComposite(org.qi4j.api.service.ServiceComposite)

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