Search in sources :

Example 1 with PropertyModel

use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.

the class ModuleInstance method newTransientBuilder.

// Implementation of TransientBuilderFactory
@Override
public <T> TransientBuilder<T> newTransientBuilder(Class<T> mixinType) throws NoSuchTransientException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<TransientModel> modelModule = typeLookup.lookupTransientModel(mixinType);
    if (modelModule == null) {
        throw new NoSuchTransientException(mixinType.getName(), name());
    }
    Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
    for (PropertyModel propertyModel : modelModule.model().state().properties()) {
        Property<?> property = new PropertyInstance<Object>(propertyModel.getBuilderInfo(), propertyModel.initialValue(modelModule.module()));
        properties.put(propertyModel.accessor(), property);
    }
    TransientStateInstance state = new TransientStateInstance(properties);
    return new TransientBuilderInstance<T>(modelModule, state, UsesInstance.EMPTY_USES);
}
Also used : NoSuchTransientException(org.qi4j.api.composite.NoSuchTransientException) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) TransientModel(org.qi4j.runtime.composite.TransientModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) TransientBuilderInstance(org.qi4j.runtime.composite.TransientBuilderInstance) AccessibleObject(java.lang.reflect.AccessibleObject) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) Property(org.qi4j.api.property.Property)

Example 2 with PropertyModel

use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method newPropertyModel.

protected PropertyModel newPropertyModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericPropertyInfo.propertyTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    Object initialValue = stateDeclarations.initialValueOf(accessor);
    boolean useDefaults = metaInfo.get(UseDefaults.class) != null || stateDeclarations.useDefaults(accessor);
    boolean immutable = this.immutable || metaInfo.get(Immutable.class) != null;
    PropertyModel propertyModel = new PropertyModel(accessor, immutable, useDefaults, valueConstraintsInstance, metaInfo, initialValue);
    return propertyModel;
}
Also used : Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) PropertyModel(org.qi4j.runtime.property.PropertyModel) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) AccessibleObject(java.lang.reflect.AccessibleObject) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Annotation(java.lang.annotation.Annotation)

Example 3 with PropertyModel

use of org.qi4j.runtime.property.PropertyModel 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)

Example 4 with PropertyModel

use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.

the class ServiceModel method newInstance.

public ServiceInstance newInstance(final ModuleInstance module) {
    Object[] mixins = mixinsModel.newMixinHolder();
    Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
    for (PropertyModel propertyModel : stateModel.properties()) {
        Object initialValue = propertyModel.initialValue(module);
        if (propertyModel.accessor().equals(identityMethod)) {
            initialValue = identity;
        }
        Property property = new PropertyInstance<Object>(propertyModel, initialValue);
        properties.put(propertyModel.accessor(), property);
    }
    TransientStateInstance state = new TransientStateInstance(properties);
    ServiceInstance compositeInstance = new ServiceInstance(this, module, mixins, state);
    // Instantiate all mixins
    int i = 0;
    UsesInstance uses = UsesInstance.EMPTY_USES.use(this);
    InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
    for (MixinModel mixinModel : mixinsModel.mixinModels()) {
        mixins[i++] = mixinModel.newInstance(injectionContext);
    }
    return compositeInstance;
}
Also used : UsesInstance(org.qi4j.runtime.composite.UsesInstance) MixinModel(org.qi4j.runtime.composite.MixinModel) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) InjectionContext(org.qi4j.runtime.injection.InjectionContext) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) Property(org.qi4j.api.property.Property)

Example 5 with PropertyModel

use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.

the class EntityStateInstance method propertyFor.

@Override
public <T> Property<T> propertyFor(AccessibleObject accessor) throws IllegalArgumentException {
    Map<AccessibleObject, Object> state = state();
    Property<T> property = (Property<T>) state.get(accessor);
    if (property == null) {
        PropertyModel entityPropertyModel = stateModel.propertyModelFor(accessor);
        if (entityPropertyModel == null) {
            throw new IllegalArgumentException("No such property:" + accessor);
        }
        property = new EntityPropertyInstance<T>(entityState instanceof BuilderEntityState ? entityPropertyModel.getBuilderInfo() : entityPropertyModel, entityState);
        state.put(accessor, property);
    }
    return property;
}
Also used : PropertyModel(org.qi4j.runtime.property.PropertyModel) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) BuilderEntityState(org.qi4j.runtime.unitofwork.BuilderEntityState) Property(org.qi4j.api.property.Property)

Aggregations

PropertyModel (org.qi4j.runtime.property.PropertyModel)9 AccessibleObject (java.lang.reflect.AccessibleObject)6 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)4 Annotation (java.lang.annotation.Annotation)3 MetaInfo (org.qi4j.api.common.MetaInfo)3 Optional (org.qi4j.api.common.Optional)3 Property (org.qi4j.api.property.Property)3 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)3 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)3 HashMap (java.util.HashMap)2 AssociationModel (org.qi4j.runtime.association.AssociationModel)2 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)2 TransientStateInstance (org.qi4j.runtime.composite.TransientStateInstance)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NoSuchTransientException (org.qi4j.api.composite.NoSuchTransientException)1 IdentityGenerator (org.qi4j.api.entity.IdentityGenerator)1 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)1 MixinModel (org.qi4j.runtime.composite.MixinModel)1