Search in sources :

Example 6 with AssociationStateHolder

use of org.qi4j.api.association.AssociationStateHolder in project qi4j-sdk by Qi4j.

the class PropertiesPanel method createData.

/**
     * Create table table or properties using the supplied query
     *
     * @param query the Query
     *
     * @return TableModel
     */
protected TableModel createData(Query query) {
    DefaultTableModel model = new DefaultTableModel();
    for (Object qObj : query) {
        AssociationStateHolder state = qi4jspi.stateOf((EntityComposite) qObj);
        EntityDescriptor descriptor = qi4jspi.entityDescriptorFor((EntityComposite) qObj);
        // genereate column, first time only
        if (model.getColumnCount() < 1) {
            for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
                model.addColumn(persistentPropertyDescriptor.qualifiedName().name());
            }
        }
        Object[] rowData = new Object[model.getColumnCount()];
        int i = 0;
        for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
            rowData[i++] = state.propertyFor(persistentPropertyDescriptor.accessor());
        }
        model.addRow(rowData);
    }
    return model;
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) DefaultTableModel(javax.swing.table.DefaultTableModel) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Example 7 with AssociationStateHolder

use of org.qi4j.api.association.AssociationStateHolder in project qi4j-sdk by Qi4j.

the class AssociationFunction method map.

@Override
public Association<T> map(Composite entity) {
    try {
        Object target = entity;
        if (traversedAssociation != null) {
            Association<?> association = traversedAssociation.map(entity);
            if (association == null) {
                return null;
            }
            target = association.get();
        } else if (traversedManyAssociation != null) {
            throw new IllegalArgumentException("Cannot evaluate a ManyAssociation");
        } else if (traversedNamedAssociation != null) {
            throw new IllegalArgumentException("Cannot evaluate a NamedAssociation");
        }
        if (target == null) {
            return null;
        }
        CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
        return ((AssociationStateHolder) handler.state()).associationFor(accessor);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : CompositeInstance(org.qi4j.api.composite.CompositeInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Example 8 with AssociationStateHolder

use of org.qi4j.api.association.AssociationStateHolder in project qi4j-sdk by Qi4j.

the class NamedAssociationFunction method map.

@Override
public NamedAssociation<T> map(Composite entity) {
    try {
        Object target = entity;
        if (traversedAssociation != null) {
            target = traversedAssociation.map(entity).get();
        }
        if (traversedManyAssociation != null) {
            throw new IllegalArgumentException("Cannot traverse ManyAssociations");
        }
        if (traversedNamedAssociation != null) {
            throw new IllegalArgumentException("Cannot traverse NamedAssociations");
        }
        CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
        return ((AssociationStateHolder) handler.state()).namedAssociationFor(accessor);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : CompositeInstance(org.qi4j.api.composite.CompositeInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Example 9 with AssociationStateHolder

use of org.qi4j.api.association.AssociationStateHolder in project qi4j-sdk by Qi4j.

the class Qi4jSPITest method givenEntityWhenGettingStateThenGetCorrectState.

@Test
public void givenEntityWhenGettingStateThenGetCorrectState() throws Exception {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    TestEntity testEntity;
    try {
        EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder(TestEntity.class);
        testEntity = builder.newInstance();
        AssociationStateHolder state = spi.stateOf(testEntity);
        validateState(state, spi.entityDescriptorFor(testEntity));
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
    UnitOfWork uow = module.newUnitOfWork();
    try {
        testEntity = uow.get(testEntity);
        validateState(spi.stateOf(testEntity), spi.entityDescriptorFor(testEntity));
        uow.complete();
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 10 with AssociationStateHolder

use of org.qi4j.api.association.AssociationStateHolder in project qi4j-sdk by Qi4j.

the class ValueEqualityTest method givenValuesOfSameTypesAndDifferentStateWhenTestingValueStateEqualityExpectNotEquals.

@Test
public void givenValuesOfSameTypesAndDifferentStateWhenTestingValueStateEqualityExpectNotEquals() {
    Some some = buildSomeValue(module);
    AssociationStateHolder someState = qi4j.spi().stateOf((ValueComposite) some);
    Some some2 = buildSomeValueWithDifferentState(module);
    AssociationStateHolder some2State = qi4j.spi().stateOf((ValueComposite) some2);
    assertThat("ValueStates not equal", someState, not(equalTo(some2State)));
    assertThat("ValueStates hashcode not equal", someState.hashCode(), not(equalTo(some2State.hashCode())));
}
Also used : AnotherSome(org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome) Some(org.qi4j.runtime.property.PropertyEqualityTest.Some) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)13 Test (org.junit.Test)5 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5 CompositeInstance (org.qi4j.api.composite.CompositeInstance)4 AnotherSome (org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome)4 Some (org.qi4j.runtime.property.PropertyEqualityTest.Some)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)3 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 Identity (org.qi4j.api.entity.Identity)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 DefaultTableModel (javax.swing.table.DefaultTableModel)1 QName (javax.xml.namespace.QName)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1