Search in sources :

Example 1 with EntityTypeNotFoundException

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);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 2 with EntityTypeNotFoundException

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);
    }
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) EntityStatus(org.qi4j.spi.entity.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONTokener(org.json.JSONTokener) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Module(org.qi4j.api.structure.Module)

Example 3 with EntityTypeNotFoundException

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();
}
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 4 with EntityTypeNotFoundException

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;
}
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 5 with EntityTypeNotFoundException

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

EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)10 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)5 Module (org.qi4j.api.structure.Module)5 EntityStatus (org.qi4j.spi.entity.EntityStatus)4 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 JSONTokener (org.json.JSONTokener)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 EntityReference (org.qi4j.api.entity.EntityReference)3 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)3 JSONArray (org.json.JSONArray)2 IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)2 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)2 EntityModel (org.qi4j.runtime.entity.EntityModel)2