Search in sources :

Example 1 with ConcurrentEntityStateModificationException

use of org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException in project qi4j-sdk by Qi4j.

the class EntityResource method post.

@Override
public Representation post(Representation entityRepresentation, Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Update entity");
    EntityStoreUnitOfWork unitOfWork = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    EntityState entity = getEntityState(unitOfWork);
    Form form = new Form(entityRepresentation);
    try {
        final EntityDescriptor descriptor = entity.entityDescriptor();
        // Parse JSON into properties
        for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
            if (!persistentProperty.isImmutable()) {
                String formValue = form.getFirstValue(persistentProperty.qualifiedName().name(), null);
                if (formValue == null) {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), null);
                } else {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), valueSerialization.deserialize(persistentProperty.valueType(), formValue));
                }
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().associations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            if (newStringAssociation == null || newStringAssociation.equals("")) {
                entity.setAssociationValue(associationType.qualifiedName(), null);
            } else {
                entity.setAssociationValue(associationType.qualifiedName(), EntityReference.parseEntityReference(newStringAssociation));
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            ManyAssociationState manyAssociation = entity.manyAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (EntityReference entityReference : manyAssociation) {
                    manyAssociation.remove(entityReference);
                }
                continue;
            }
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String identity;
            try {
                // Synchronize old and new association
                int index = 0;
                while ((identity = bufferedReader.readLine()) != null) {
                    EntityReference reference = new EntityReference(identity);
                    if (manyAssociation.count() < index && manyAssociation.get(index).equals(reference)) {
                        continue;
                    }
                    try {
                        unitOfWork.entityStateOf(reference);
                        manyAssociation.remove(reference);
                        manyAssociation.add(index++, reference);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                while (manyAssociation.count() > index) {
                    manyAssociation.remove(manyAssociation.get(index));
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    } catch (ValueSerializationException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    } catch (IllegalArgumentException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    try {
        unitOfWork.applyChanges().commit();
    } catch (ConcurrentEntityStateModificationException e) {
        throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);
    } catch (EntityNotFoundException e) {
        throw new ResourceException(Status.CLIENT_ERROR_GONE);
    }
    getResponse().setStatus(Status.SUCCESS_RESET_CONTENT);
    return new EmptyRepresentation();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) EntityState(org.qi4j.spi.entity.EntityState) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityReference(org.qi4j.api.entity.EntityReference) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase)

Example 2 with ConcurrentEntityStateModificationException

use of org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method applyChanges.

private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
    List<StateCommitter> committers = new ArrayList<StateCommitter>();
    for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
        try {
            StateCommitter committer = entityStoreUnitOfWork.applyChanges();
            committers.add(committer);
        } catch (Exception e) {
            // Cancel all previously prepared stores
            for (StateCommitter committer : committers) {
                committer.cancel();
            }
            if (e instanceof ConcurrentEntityStateModificationException) {
                // If we cancelled due to concurrent modification, then create the proper exception for it!
                ConcurrentEntityStateModificationException mee = (ConcurrentEntityStateModificationException) e;
                Collection<EntityReference> modifiedEntityIdentities = mee.modifiedEntities();
                Collection<EntityComposite> modifiedEntities = new ArrayList<EntityComposite>();
                for (EntityReference modifiedEntityIdentity : modifiedEntityIdentities) {
                    Collection<EntityInstance> instances = instanceCache.values();
                    for (EntityInstance instance : instances) {
                        if (instance.identity().equals(modifiedEntityIdentity)) {
                            modifiedEntities.add(instance.<EntityComposite>proxy());
                        }
                    }
                }
                throw new ConcurrentEntityModificationException(modifiedEntities);
            } else {
                throw new UnitOfWorkCompletionException(e);
            }
        }
    }
    return committers;
}
Also used : EntityComposite(org.qi4j.api.entity.EntityComposite) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) ArrayList(java.util.ArrayList) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityReference(org.qi4j.api.entity.EntityReference) Collection(java.util.Collection) StateCommitter(org.qi4j.spi.entitystore.StateCommitter)

Aggregations

EntityReference (org.qi4j.api.entity.EntityReference)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)1 Usecase (org.qi4j.api.usecase.Usecase)1 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)1 EntityInstance (org.qi4j.runtime.entity.EntityInstance)1 EntityState (org.qi4j.spi.entity.EntityState)1 ManyAssociationState (org.qi4j.spi.entity.ManyAssociationState)1