Search in sources :

Example 21 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class StateInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    if (StateHolder.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State StateHolder properties;
        return new StateInjectionProvider();
    } else if (UnitOfWork.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        if (!(resolution.model() instanceof EntityDescriptor)) {
            throw new InvalidInjectionException("Only EntityComposites can be injected with '@State UnitOfWork'");
        }
        return new UnitOfWorkInjectionProvider();
    } else if (Property.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Property<String> name;
        StateDescriptor descriptor;
        descriptor = ((StatefulCompositeDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
        return new PropertyInjectionProvider(propertyDescriptor);
    } else if (Association.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Association<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getAssociationByName(name);
        return new AssociationInjectionProvider(model);
    } else if (ManyAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State ManyAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getManyAssociationByName(name);
        return new ManyAssociationInjectionProvider(model);
    } else if (NamedAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State NamedAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getNamedAssociationByName(name);
        return new NamedAssociationInjectionProvider(model);
    }
    throw new InjectionProviderException("Injected value has invalid type");
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) NamedAssociation(org.qi4j.api.association.NamedAssociation) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation) AbstractAssociation(org.qi4j.api.association.AbstractAssociation) Association(org.qi4j.api.association.Association) NamedAssociation(org.qi4j.api.association.NamedAssociation) State(org.qi4j.api.injection.scope.State) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) StateDescriptor(org.qi4j.api.composite.StateDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 22 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class EntityModel method newEntityState.

public EntityState newEntityState(EntityStoreUnitOfWork store, EntityReference identity) throws ConstraintViolationException, EntityStoreException {
    try {
        // New EntityState
        EntityState entityState = store.newEntityState(identity, this);
        // Set identity property
        PropertyDescriptor persistentPropertyDescriptor = state().propertyModelFor(IDENTITY_METHOD);
        entityState.setPropertyValue(persistentPropertyDescriptor.qualifiedName(), identity.identity());
        return entityState;
    } catch (EntityAlreadyExistsException e) {
        throw new EntityCompositeAlreadyExistsException(identity);
    } catch (EntityStoreException e) {
        throw new ConstructionException("Could not create new entity in store", e);
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityAlreadyExistsException(org.qi4j.spi.entitystore.EntityAlreadyExistsException) EntityState(org.qi4j.spi.entity.EntityState) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) ConstructionException(org.qi4j.api.common.ConstructionException) EntityCompositeAlreadyExistsException(org.qi4j.api.unitofwork.EntityCompositeAlreadyExistsException)

Example 23 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class EntityStateInstance method checkConstraints.

public void checkConstraints() {
    for (PropertyDescriptor propertyDescriptor : stateModel.properties()) {
        ConstraintsCheck constraints = (ConstraintsCheck) propertyDescriptor;
        Property<Object> property = this.propertyFor(propertyDescriptor.accessor());
        constraints.checkConstraints(property.get());
    }
    for (AssociationDescriptor associationDescriptor : stateModel.associations()) {
        ConstraintsCheck constraints = (ConstraintsCheck) associationDescriptor;
        Association<Object> association = this.associationFor(associationDescriptor.accessor());
        constraints.checkConstraints(association.get());
    }
// TODO Should ManyAssociations be checked too?
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ConstraintsCheck(org.qi4j.runtime.composite.ConstraintsCheck) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 24 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class Qi4jSPITest method validateState.

private void validateState(AssociationStateHolder state, EntityDescriptor entityDescriptor) {
    for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
        Property<?> prop = state.propertyFor(propertyDescriptor.accessor());
        assertThat("Properties could be listed", prop, CoreMatchers.notNullValue());
    }
    AssociationStateDescriptor descriptor = entityDescriptor.state();
    for (AssociationDescriptor associationDescriptor : descriptor.associations()) {
        AbstractAssociation assoc = state.associationFor(associationDescriptor.accessor());
        assertThat("Assocs could be listed", assoc, CoreMatchers.notNullValue());
    }
}
Also used : AbstractAssociation(org.qi4j.api.association.AbstractAssociation) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 25 with PropertyDescriptor

use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.

the class PropertyEqualityTest method givenValuesOfTheSameTypeWhenTestingPropertyDescriptorEqualityExpectEquals.

//
// ------------------------------:: PropertyDescriptor equality tests ::--------------------------------------------
//
@Test
public void givenValuesOfTheSameTypeWhenTestingPropertyDescriptorEqualityExpectEquals() {
    Some some = buildSomeValue(module);
    ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
    PropertyDescriptor someCharPropDesc = someDescriptor.state().findPropertyModelByName("characterProperty");
    Some other = buildSomeValue(module);
    ValueDescriptor otherDescriptor = qi4j.api().valueDescriptorFor(other);
    PropertyDescriptor otherCharPropDesc = otherDescriptor.state().findPropertyModelByName("characterProperty");
    assertThat("PropertyDescriptors equal", someCharPropDesc, equalTo(otherCharPropDesc));
    assertThat("PropertyDescriptors hashcode equal", someCharPropDesc.hashCode(), equalTo(otherCharPropDesc.hashCode()));
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)37 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)15 Map (java.util.Map)12 EntityReference (org.qi4j.api.entity.EntityReference)12 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)11 HashMap (java.util.HashMap)10 QualifiedName (org.qi4j.api.common.QualifiedName)10 LinkedHashMap (java.util.LinkedHashMap)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ValueType (org.qi4j.api.type.ValueType)5 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 JSONArray (org.json.JSONArray)4 Test (org.junit.Test)4 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4