Search in sources :

Example 1 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class EntityStateInstance method associationFor.

@Override
@SuppressWarnings("unchecked")
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);
        association = new AssociationInstance<>(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 : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) NamedAssociation(org.qi4j.api.association.NamedAssociation) 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) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Example 2 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class ManyAssociationInstance method equals.

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    ManyAssociation<?> that = (ManyAssociation) o;
    // Unwrap if needed
    while (that instanceof ManyAssociationWrapper) {
        that = ((ManyAssociationWrapper) that).next();
    }
    // Descriptor equality
    ManyAssociationInstance<?> thatInstance = (ManyAssociationInstance) that;
    AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo();
    if (!associationInfo.equals(thatDescriptor)) {
        return false;
    }
    // State equality
    if (manyAssociationState.count() != thatInstance.manyAssociationState.count()) {
        return false;
    }
    for (EntityReference ref : manyAssociationState) {
        if (!thatInstance.manyAssociationState.contains(ref)) {
            return false;
        }
    }
    return true;
}
Also used : ManyAssociationWrapper(org.qi4j.api.association.ManyAssociationWrapper) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation)

Example 3 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method pause.

public void pause() {
    if (!paused) {
        paused = true;
        getCurrent().pop();
        UnitOfWorkOptions unitOfWorkOptions = metaInfo().get(UnitOfWorkOptions.class);
        if (unitOfWorkOptions == null) {
            unitOfWorkOptions = usecase().metaInfo(UnitOfWorkOptions.class);
        }
        if (unitOfWorkOptions != null) {
            if (unitOfWorkOptions.isPruneOnPause()) {
                List<EntityReference> prunedInstances = null;
                for (EntityInstance entityInstance : instanceCache.values()) {
                    if (entityInstance.status() == EntityStatus.LOADED) {
                        if (prunedInstances == null) {
                            prunedInstances = new ArrayList<>();
                        }
                        prunedInstances.add(entityInstance.identity());
                    }
                }
                if (prunedInstances != null) {
                    for (EntityReference prunedInstance : prunedInstances) {
                        instanceCache.remove(prunedInstance);
                    }
                }
            }
        }
    } else {
        throw new UnitOfWorkException("Unit of work is not active");
    }
}
Also used : UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityReference(org.qi4j.api.entity.EntityReference) EntityInstance(org.qi4j.runtime.entity.EntityInstance) UnitOfWorkOptions(org.qi4j.api.unitofwork.UnitOfWorkOptions)

Example 4 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class EntityBuilderWithStateTest method test.

@Test
public void test() throws UnitOfWorkCompletionException {
    final String associatedIdentity;
    try (UnitOfWork uow = module.newUnitOfWork()) {
        EntityBuilder<SomeEntity> builder = uow.newEntityBuilder(SomeEntity.class);
        builder.instance().prop().set("Associated");
        SomeEntity entity = builder.newInstance();
        associatedIdentity = entity.identity().get();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork()) {
        SomeEntity entity = uow.newEntityBuilderWithState(SomeEntity.class, new Function<PropertyDescriptor, Object>() {

            @Override
            public Object map(PropertyDescriptor descriptor) {
                if ("prop".equals(descriptor.qualifiedName().name())) {
                    return "Foo";
                }
                return null;
            }
        }, new Function<AssociationDescriptor, EntityReference>() {

            @Override
            public EntityReference map(AssociationDescriptor descriptor) {
                if ("ass".equals(descriptor.qualifiedName().name())) {
                    return EntityReference.parseEntityReference(associatedIdentity);
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

            @Override
            public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
                if ("manyAss".equals(descriptor.qualifiedName().name())) {
                    return Arrays.asList(EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

            @Override
            public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
                if ("namedAss".equals(descriptor.qualifiedName().name())) {
                    return Collections.singletonMap("foo", EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }).newInstance();
        assertThat(entity.prop().get(), equalTo("Foo"));
        assertThat(entity.ass().get().identity().get(), equalTo(associatedIdentity));
        assertThat(entity.manyAss().get(0).identity().get(), equalTo(associatedIdentity));
        assertThat(entity.namedAss().get("foo").identity().get(), equalTo(associatedIdentity));
        uow.complete();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class JSONManyAssociationState method iterator.

@Override
public Iterator<EntityReference> iterator() {
    return new Iterator<EntityReference>() {

        private int idx = 0;

        @Override
        public boolean hasNext() {
            return idx < references.length();
        }

        @Override
        public EntityReference next() {
            try {
                EntityReference ref = new EntityReference(references.getString(idx));
                idx++;
                return ref;
            } catch (JSONException e) {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() is not supported on ManyAssociation iterators.");
        }
    };
}
Also used : Iterator(java.util.Iterator) EntityReference(org.qi4j.api.entity.EntityReference) JSONException(org.json.JSONException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

EntityReference (org.qi4j.api.entity.EntityReference)60 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)13 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)12 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)11 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)11 Person (org.qi4j.test.indexing.model.Person)11 HashMap (java.util.HashMap)9 Map (java.util.Map)9 QualifiedName (org.qi4j.api.common.QualifiedName)9 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 Writer (java.io.Writer)8 IOException (java.io.IOException)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 StringWriter (java.io.StringWriter)5 JSONArray (org.json.JSONArray)5