Search in sources :

Example 1 with NoSuchEntityException

use of org.qi4j.api.unitofwork.NoSuchEntityException 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)

Example 2 with NoSuchEntityException

use of org.qi4j.api.unitofwork.NoSuchEntityException 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 3 with NoSuchEntityException

use of org.qi4j.api.unitofwork.NoSuchEntityException in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method whenRemovedEntityThenCannotFindEntity.

@Test
public void whenRemovedEntityThenCannotFindEntity() throws Exception {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    TestEntity newInstance = createEntity(unitOfWork);
    String identity = newInstance.identity().get();
    unitOfWork.complete();
    // Remove entity
    unitOfWork = module.newUnitOfWork();
    TestEntity instance = unitOfWork.get(newInstance);
    unitOfWork.remove(instance);
    unitOfWork.complete();
    // Find entity
    unitOfWork = module.newUnitOfWork();
    try {
        unitOfWork.get(TestEntity.class, identity);
        fail("Should not be able to find entity");
    } catch (NoSuchEntityException e) {
    // Ok!
    } finally {
        unitOfWork.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with NoSuchEntityException

use of org.qi4j.api.unitofwork.NoSuchEntityException in project qi4j-sdk by Qi4j.

the class ModuleUnitOfWork method remove.

@Override
public void remove(Object entity) throws LifecycleException {
    uow.checkOpen();
    EntityComposite entityComposite = (EntityComposite) entity;
    EntityInstance compositeInstance = EntityInstance.entityInstanceOf(entityComposite);
    if (compositeInstance.status() == EntityStatus.NEW) {
        compositeInstance.remove(this);
        uow.remove(compositeInstance.identity());
    } else if (compositeInstance.status() == EntityStatus.LOADED || compositeInstance.status() == EntityStatus.UPDATED) {
        compositeInstance.remove(this);
    } else {
        throw new NoSuchEntityException(compositeInstance.identity());
    }
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityInstance(org.qi4j.runtime.entity.EntityInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 5 with NoSuchEntityException

use of org.qi4j.api.unitofwork.NoSuchEntityException in project qi4j-sdk by Qi4j.

the class RemovalTest method givenEntityCreatedWhenRemovingEntityBeforeCompletingUowThenFindNewEntityShouldNotExist.

@Test
public void givenEntityCreatedWhenRemovingEntityBeforeCompletingUowThenFindNewEntityShouldNotExist() throws Exception {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        EntityBuilder<Abc> builder = uow.newEntityBuilder(Abc.class, "123");
        builder.instance().name().set("Niclas");
        Abc abc = builder.newInstance();
        uow.remove(abc);
        uow.complete();
        uow = module.newUnitOfWork();
        uow.get(Abc.class, "123");
        fail("This '123' entity should not exist.");
    } catch (NoSuchEntityException e) {
    // Expected.
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)10 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)6 Test (org.junit.Test)5 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)3 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 EntityBuilder (org.qi4j.api.entity.EntityBuilder)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1