Search in sources :

Example 1 with AssociationModel

use of org.qi4j.runtime.association.AssociationModel in project qi4j-sdk by Qi4j.

the class EntityStateInstance method associationFor.

@Override
public <T> Association<T> associationFor(AccessibleObject accessor) throws IllegalArgumentException {
    Map<AccessibleObject, Object> state = state();
    Association<T> association = (Association<T>) state.get(accessor);
    if (association == null) {
        final AssociationModel associationModel = stateModel.getAssociation(accessor);
        if (associationModel == null) {
            throw new IllegalArgumentException("No such association:" + accessor);
        }
        association = new AssociationInstance<T>(entityState instanceof BuilderEntityState ? associationModel.getBuilderInfo() : associationModel, entityFunction, new Property<EntityReference>() {

            @Override
            public EntityReference get() {
                return entityState.associationValueOf(associationModel.qualifiedName());
            }

            @Override
            public void set(EntityReference newValue) throws IllegalArgumentException, IllegalStateException {
                entityState.setAssociationValue(associationModel.qualifiedName(), newValue);
            }
        });
        state.put(accessor, association);
    }
    return association;
}
Also used : ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) EntityReference(org.qi4j.api.entity.EntityReference) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) BuilderEntityState(org.qi4j.runtime.unitofwork.BuilderEntityState) Property(org.qi4j.api.property.Property) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel) AssociationModel(org.qi4j.runtime.association.AssociationModel)

Example 2 with AssociationModel

use of org.qi4j.runtime.association.AssociationModel in project qi4j-sdk by Qi4j.

the class ValueAssemblyImpl method newAssociationModel.

public AssociationModel newAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for Association references
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the Association itself
    valueConstraintsModel = constraintsFor(annotations, Association.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance associationValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        associationValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    AssociationModel associationModel = new AssociationModel(accessor, valueConstraintsInstance, associationValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Example 3 with AssociationModel

use of org.qi4j.runtime.association.AssociationModel in project qi4j-sdk by Qi4j.

the class EntityAssemblyImpl method newAssociationModel.

public AssociationModel newAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for Association references
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the Association itself
    valueConstraintsModel = constraintsFor(annotations, Association.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance associationValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        associationValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = associationDeclarations.metaInfoFor(accessor);
    AssociationModel associationModel = new AssociationModel(accessor, valueConstraintsInstance, associationValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Example 4 with AssociationModel

use of org.qi4j.runtime.association.AssociationModel in project qi4j-sdk by Qi4j.

the class ValueInstance method prepareToBuild.

/**
 * When a ValueBuilder is about to start, ensure that all state has builder infos, i.e. they are mutable.
 */
public void prepareToBuild() {
    for (PropertyModel propertyDescriptor : descriptor().state().properties()) {
        PropertyInstance<Object> propertyInstance = (PropertyInstance<Object>) state.propertyFor(propertyDescriptor.accessor());
        propertyInstance.prepareToBuild(propertyDescriptor);
    }
    for (AssociationModel associationDescriptor : descriptor().state().associations()) {
        state().associationFor(associationDescriptor.accessor()).setAssociationInfo(associationDescriptor.getBuilderInfo());
    }
    for (ManyAssociationModel associationDescriptor : descriptor().state().manyAssociations()) {
        state().manyAssociationFor(associationDescriptor.accessor()).setAssociationInfo(associationDescriptor.getBuilderInfo());
    }
}
Also used : PropertyModel(org.qi4j.runtime.property.PropertyModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Example 5 with AssociationModel

use of org.qi4j.runtime.association.AssociationModel in project qi4j-sdk by Qi4j.

the class ValueInstance method prepareBuilderState.

/**
 * When a ValueBuilder is finished and is about to instantiate a Value, call this to ensure that the state has correct
 * settings, i.e. is immutable.
 */
public void prepareBuilderState() {
    for (PropertyModel propertyDescriptor : descriptor().state().properties()) {
        PropertyInstance<Object> propertyInstance = (PropertyInstance<Object>) state.propertyFor(propertyDescriptor.accessor());
        propertyInstance.prepareBuilderState(propertyDescriptor);
    }
    for (AssociationModel associationDescriptor : descriptor().state().associations()) {
        state().associationFor(associationDescriptor.accessor()).setAssociationInfo(associationDescriptor);
    }
    for (ManyAssociationModel associationDescriptor : descriptor().state().manyAssociations()) {
        state().manyAssociationFor(associationDescriptor.accessor()).setAssociationInfo(associationDescriptor);
    }
}
Also used : PropertyModel(org.qi4j.runtime.property.PropertyModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Aggregations

AssociationModel (org.qi4j.runtime.association.AssociationModel)5 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)5 Association (org.qi4j.api.association.Association)3 ManyAssociation (org.qi4j.api.association.ManyAssociation)3 Annotation (java.lang.annotation.Annotation)2 Member (java.lang.reflect.Member)2 MetaInfo (org.qi4j.api.common.MetaInfo)2 Optional (org.qi4j.api.common.Optional)2 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)2 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)2 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)2 PropertyModel (org.qi4j.runtime.property.PropertyModel)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 EntityReference (org.qi4j.api.entity.EntityReference)1 Property (org.qi4j.api.property.Property)1 BuilderEntityState (org.qi4j.runtime.unitofwork.BuilderEntityState)1