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;
}
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();
}
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();
}
}
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());
}
}
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();
}
}
Aggregations