Search in sources :

Example 6 with Identity

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

the class PrivateEntityUnitOfWorkTest method givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit.

@Test
public void givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit() throws Exception {
    System.setProperty("qi4j.compacttrace", "off");
    Energy4Java is = new Energy4Java();
    Application app = is.newApplication(new ApplicationAssembler() {

        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            return applicationFactory.newApplicationAssembly(new Assembler[][][] { { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.objects(PrivateEntityUnitOfWorkTest.class);
                }
            } } }, { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.entities(ProductEntity.class);
                    module.entities(ProductCatalogEntity.class).visibleIn(application);
                    module.values(ProductInfo.class);
                    new EntityTestAssembler().assemble(module);
                }
            } } } });
        }
    });
    app.activate();
    Module module = app.findModule("Layer 1", "Module 1");
    module.injectTo(this);
    UnitOfWork unitOfWork = uowf.newUnitOfWork();
    try {
        unitOfWork.newEntity(ProductEntity.class);
        fail("Should not be able to create product here");
    } catch (EntityTypeNotFoundException e) {
        // Ok
        ProductCatalog catalog = unitOfWork.newEntity(ProductCatalog.class, "1");
        unitOfWork.complete();
    }
    unitOfWork = uowf.newUnitOfWork();
    String id;
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        id = ((Identity) catalog.newProduct()).identity().get();
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
    unitOfWork = module.newUnitOfWork();
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        Product product = catalog.findProduct(id);
        product.price().set(100);
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Module(org.qi4j.api.structure.Module) Identity(org.qi4j.api.entity.Identity) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Aggregations

Identity (org.qi4j.api.entity.Identity)6 Test (org.junit.Test)3 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)3 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 CompositeInstance (org.qi4j.api.composite.CompositeInstance)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 QName (javax.xml.namespace.QName)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 Application (org.qi4j.api.structure.Application)1 Module (org.qi4j.api.structure.Module)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)1 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)1 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)1