Search in sources :

Example 1 with EntityStore

use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method get.

public <T> T get(EntityReference identity, ModuleUnitOfWork uow, Iterable<ModelModule<EntityModel>> potentialModels, Class<T> mixinType) throws EntityTypeNotFoundException, NoSuchEntityException {
    checkOpen();
    EntityInstance entityInstance = instanceCache.get(identity);
    if (entityInstance == null) {
        // Not yet in cache
        // Check if this is a root UoW, or if no parent UoW knows about this entity
        EntityState entityState = null;
        EntityModel model = null;
        ModuleInstance module = null;
        // Figure out what EntityStore to use
        for (ModelModule<EntityModel> potentialModel : potentialModels) {
            EntityStore store = potentialModel.module().entityStore();
            EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork(store, potentialModel.module());
            try {
                entityState = storeUow.entityStateOf(identity);
            } catch (EntityNotFoundException e) {
                continue;
            }
            // Get the selected model
            model = (EntityModel) entityState.entityDescriptor();
            module = potentialModel.module();
        }
        // Check if model was found
        if (model == null) {
            // Check if state was found
            if (entityState == null) {
                throw new NoSuchEntityException(identity);
            } else {
                throw new EntityTypeNotFoundException(mixinType.getName());
            }
        }
        // Create instance
        entityInstance = new EntityInstance(uow, module, model, entityState);
        instanceCache.put(identity, entityInstance);
    } else {
        // Check if it has been removed
        if (entityInstance.status() == EntityStatus.REMOVED) {
            throw new NoSuchEntityException(identity);
        }
    }
    return entityInstance.proxy();
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ModuleInstance(org.qi4j.runtime.structure.ModuleInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 2 with EntityStore

use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.

the class ModuleUnitOfWork method newEntityBuilder.

@Override
public <T> EntityBuilder<T> newEntityBuilder(Class<T> type, String identity) throws EntityTypeNotFoundException {
    ModelModule<EntityModel> model = moduleInstance.typeLookup().lookupEntityModel(type);
    if (model == null) {
        throw new EntityTypeNotFoundException(type.getName());
    }
    EntityStore entityStore = model.module().entityStore();
    // Generate id if necessary
    if (identity == null) {
        IdentityGenerator idGen = model.module().identityGenerator();
        if (idGen == null) {
            throw new NoSuchServiceException(IdentityGenerator.class.getName(), model.module().name());
        }
        identity = idGen.generate(first(model.model().types()));
    }
    EntityBuilder<T> builder;
    builder = new EntityBuilderInstance<T>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity);
    return builder;
}
Also used : NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityStore(org.qi4j.spi.entitystore.EntityStore) IdentityGenerator(org.qi4j.api.entity.IdentityGenerator)

Example 3 with EntityStore

use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.

the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.

@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        // There is no Query capability available for Libraries, since that sits in Extensions.
        // Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
        // to the test sitting in qi4j-libraries source repository.
        // QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
        // Query<DebugRecord> query = builder.newQuery( uow );
        // assertEquals( 0, query.count() );
        Some service = (Some) module.findService(Some.class).get();
        String message = service.doSomething("World!", 10);
        assertEquals(message, "Hello!");
        EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
        final String[] result = new String[1];
        es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {

            public EntityState map(EntityState entityState) {
                if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
                    result[0] = entityState.identity().identity();
                }
                return entityState;
            }
        }, Outputs.<EntityState>noop()));
        ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
        String mess = debugEntry.message().get();
        System.out.println(mess);
        assertEquals("some message.", mess);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
        e.printStackTrace();
        uow.discard();
    } catch (UnitOfWorkCompletionException e) {
        e.printStackTrace();
        uow.discard();
    } finally {
        if (uow.isOpen()) {
            uow.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityState(org.qi4j.spi.entity.EntityState) ServiceDebugRecordEntity(org.qi4j.logging.debug.records.ServiceDebugRecordEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with EntityStore

use of org.qi4j.spi.entitystore.EntityStore in project qi4j-sdk by Qi4j.

the class ModuleUnitOfWork method newEntityBuilderWithState.

@Override
public <T> EntityBuilder<T> newEntityBuilderWithState(Class<T> type, String identity, Function<PropertyDescriptor, Object> propertyFunction, Function<AssociationDescriptor, EntityReference> associationFunction, Function<AssociationDescriptor, Iterable<EntityReference>> manyAssociationFunction, Function<AssociationDescriptor, Map<String, EntityReference>> namedAssociationFunction) throws EntityTypeNotFoundException {
    NullArgumentException.validateNotNull("propertyFunction", propertyFunction);
    NullArgumentException.validateNotNull("associationFunction", associationFunction);
    NullArgumentException.validateNotNull("manyAssociationFunction", manyAssociationFunction);
    NullArgumentException.validateNotNull("namedAssociationFunction", namedAssociationFunction);
    ModelModule<EntityModel> model = moduleInstance.typeLookup().lookupEntityModel(type);
    if (model == null) {
        throw new EntityTypeNotFoundException(type.getName());
    }
    EntityStore entityStore = model.module().entityStore();
    StateResolver stateResolver = new FunctionStateResolver(propertyFunction, associationFunction, manyAssociationFunction, namedAssociationFunction);
    if (identity == null) {
        // Use identity from StateResolver if available
        PropertyModel identityModel = model.model().state().findPropertyModelByQualifiedName(IDENTITY_STATE_NAME);
        identity = (String) stateResolver.getPropertyState(identityModel);
        if (identity == null) {
            // Generate identity
            IdentityGenerator idGen = model.module().identityGenerator();
            if (idGen == null) {
                throw new NoSuchServiceException(IdentityGenerator.class.getName(), model.module().name());
            }
            identity = idGen.generate(first(model.model().types()));
        }
    }
    return new EntityBuilderInstance<>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity, stateResolver);
}
Also used : NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityModel(org.qi4j.runtime.entity.EntityModel) PropertyModel(org.qi4j.runtime.property.PropertyModel) EntityStore(org.qi4j.spi.entitystore.EntityStore) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver) IdentityGenerator(org.qi4j.api.entity.IdentityGenerator) EntityBuilderInstance(org.qi4j.runtime.unitofwork.EntityBuilderInstance) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver)

Aggregations

EntityStore (org.qi4j.spi.entitystore.EntityStore)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)3 EntityModel (org.qi4j.runtime.entity.EntityModel)3 IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)2 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)2 EntityState (org.qi4j.spi.entity.EntityState)2 Test (org.junit.Test)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 Function (org.qi4j.functional.Function)1 ServiceDebugRecordEntity (org.qi4j.logging.debug.records.ServiceDebugRecordEntity)1 FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)1 StateResolver (org.qi4j.runtime.composite.StateResolver)1 EntityInstance (org.qi4j.runtime.entity.EntityInstance)1 PropertyModel (org.qi4j.runtime.property.PropertyModel)1 ModuleInstance (org.qi4j.runtime.structure.ModuleInstance)1 EntityBuilderInstance (org.qi4j.runtime.unitofwork.EntityBuilderInstance)1 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)1