use of org.qi4j.api.unitofwork.EntityTypeNotFoundException in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doConversion.
private <T> EntityBuilder<?> doConversion(Class<T> entityType, String identity, Object value) {
EntityDescriptor eDesc = module.entityDescriptor(entityType.getName());
if (eDesc == null) {
throw new EntityTypeNotFoundException(entityType.getName());
}
ValueComposite vComposite = (ValueComposite) value;
ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
AssociationStateHolder vState = spi.stateOf(vComposite);
AssociationStateDescriptor vStateDesc = vDesc.state();
Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
if (unqualified == null || !unqualified.value()) {
return doQualifiedConversion(entityType, identity, vState, vStateDesc);
}
return doUnqualifiedConversion(entityType, identity, vState, vStateDesc);
}
use of org.qi4j.api.unitofwork.EntityTypeNotFoundException in project qi4j-sdk by Qi4j.
the class MapEntityStoreMixin method readEntityState.
protected EntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
try {
Module module = unitOfWork.module();
JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
EntityStatus status = EntityStatus.LOADED;
String version = jsonObject.getString("version");
long modified = jsonObject.getLong("modified");
String identity = jsonObject.getString("identity");
// Check if version is correct
String currentAppVersion = jsonObject.optString(MapEntityStore.JSONKeys.application_version.name(), "0.0");
if (!currentAppVersion.equals(application.version())) {
if (migration != null) {
migration.migrate(jsonObject, application.version(), this);
} else {
// Do nothing - set version to be correct
jsonObject.put(MapEntityStore.JSONKeys.application_version.name(), application.version());
}
LoggerFactory.getLogger(MapEntityStoreMixin.class).debug("Updated version nr on " + identity + " from " + currentAppVersion + " to " + application.version());
// State changed
status = EntityStatus.UPDATED;
}
String type = jsonObject.getString("type");
EntityDescriptor entityDescriptor = module.entityDescriptor(type);
if (entityDescriptor == null) {
throw new EntityTypeNotFoundException(type);
}
Map<QualifiedName, Object> properties = new HashMap<QualifiedName, Object>();
JSONObject props = jsonObject.getJSONObject("properties");
for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
Object jsonValue;
try {
jsonValue = props.get(propertyDescriptor.qualifiedName().name());
} catch (JSONException e) {
// Value not found, default it
Object initialValue = propertyDescriptor.initialValue(module);
properties.put(propertyDescriptor.qualifiedName(), initialValue);
status = EntityStatus.UPDATED;
continue;
}
if (JSONObject.NULL.equals(jsonValue)) {
properties.put(propertyDescriptor.qualifiedName(), null);
} else {
Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
properties.put(propertyDescriptor.qualifiedName(), value);
}
}
Map<QualifiedName, EntityReference> associations = new HashMap<QualifiedName, EntityReference>();
JSONObject assocs = jsonObject.getJSONObject("associations");
for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
try {
Object jsonValue = assocs.get(associationType.qualifiedName().name());
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
associations.put(associationType.qualifiedName(), value);
} catch (JSONException e) {
// Association not found, default it to null
associations.put(associationType.qualifiedName(), null);
status = EntityStatus.UPDATED;
}
}
JSONObject manyAssocs = jsonObject.getJSONObject("manyassociations");
Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<QualifiedName, List<EntityReference>>();
for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
List<EntityReference> references = new ArrayList<EntityReference>();
try {
JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
for (int i = 0; i < jsonValues.length(); i++) {
Object jsonValue = jsonValues.getString(i);
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
references.add(value);
}
manyAssociations.put(manyAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// ManyAssociation not found, default to empty one
manyAssociations.put(manyAssociationType.qualifiedName(), references);
}
}
return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.unitofwork.EntityTypeNotFoundException 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.EntityTypeNotFoundException 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;
}
use of org.qi4j.api.unitofwork.EntityTypeNotFoundException 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);
}
Aggregations