Search in sources :

Example 1 with StateResolver

use of org.qi4j.runtime.composite.StateResolver in project qi4j-sdk by Qi4j.

the class ModuleInstance method newValueBuilderWithState.

@Override
public <T> ValueBuilder<T> newValueBuilderWithState(Class<T> mixinType, Function<PropertyDescriptor, Object> propertyFunction, Function<AssociationDescriptor, EntityReference> associationFunction, Function<AssociationDescriptor, Iterable<EntityReference>> manyAssociationFunction, Function<AssociationDescriptor, Map<String, EntityReference>> namedAssociationFunction) {
    NullArgumentException.validateNotNull("propertyFunction", propertyFunction);
    NullArgumentException.validateNotNull("associationFunction", associationFunction);
    NullArgumentException.validateNotNull("manyAssociationFunction", manyAssociationFunction);
    NullArgumentException.validateNotNull("namedAssociationFunction", namedAssociationFunction);
    ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel(mixinType);
    if (compositeModelModule == null) {
        throw new NoSuchValueException(mixinType.getName(), name());
    }
    StateResolver stateResolver = new FunctionStateResolver(propertyFunction, associationFunction, manyAssociationFunction, namedAssociationFunction);
    return new ValueBuilderWithState<>(compositeModelModule, this, stateResolver);
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) ValueBuilderWithState(org.qi4j.runtime.value.ValueBuilderWithState) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver)

Example 2 with StateResolver

use of org.qi4j.runtime.composite.StateResolver in project qi4j-sdk by Qi4j.

the class ModuleInstance method newValueBuilder.

@Override
public <T> ValueBuilder<T> newValueBuilder(Class<T> mixinType) throws NoSuchValueException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel(mixinType);
    if (compositeModelModule == null) {
        throw new NoSuchValueException(mixinType.getName(), name());
    }
    StateResolver stateResolver = new InitialStateResolver(compositeModelModule.module());
    return new ValueBuilderInstance<>(compositeModelModule, this, stateResolver);
}
Also used : ValueBuilderInstance(org.qi4j.runtime.value.ValueBuilderInstance) NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver)

Example 3 with StateResolver

use of org.qi4j.runtime.composite.StateResolver 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

FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)3 StateResolver (org.qi4j.runtime.composite.StateResolver)3 NoSuchValueException (org.qi4j.api.value.NoSuchValueException)2 ValueModel (org.qi4j.runtime.value.ValueModel)2 IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)1 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 EntityModel (org.qi4j.runtime.entity.EntityModel)1 PropertyModel (org.qi4j.runtime.property.PropertyModel)1 EntityBuilderInstance (org.qi4j.runtime.unitofwork.EntityBuilderInstance)1 ValueBuilderInstance (org.qi4j.runtime.value.ValueBuilderInstance)1 ValueBuilderWithState (org.qi4j.runtime.value.ValueBuilderWithState)1 EntityStore (org.qi4j.spi.entitystore.EntityStore)1