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;
}
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;
}
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");
}
}
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();
}
}
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.");
}
};
}
Aggregations