Search in sources :

Example 1 with EntityStoreUnitOfWork

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

the class EntityResource method get.

@Override
protected Representation get(Variant variant) throws ResourceException {
    EntityStoreUnitOfWork uow = entityStore.newUnitOfWork(UsecaseBuilder.newUsecase("Get entity"), module, System.currentTimeMillis());
    try {
        EntityState entityState = getEntityState(uow);
        // Check modification date
        Date lastModified = getRequest().getConditions().getModifiedSince();
        if (lastModified != null) {
            if (lastModified.getTime() / 1000 == entityState.lastModified() / 1000) {
                throw new ResourceException(Status.REDIRECTION_NOT_MODIFIED);
            }
        }
        // Generate the right representation according to its media type.
        if (MediaType.APPLICATION_RDF_XML.equals(variant.getMediaType())) {
            return entityHeaders(representRdfXml(entityState), entityState);
        } else if (MediaType.TEXT_HTML.equals(variant.getMediaType())) {
            return entityHeaders(representHtml(entityState), entityState);
        } else if (MediaType.APPLICATION_JSON.equals(variant.getMediaType())) {
            return entityHeaders(representJson(entityState), entityState);
        }
    } catch (ResourceException ex) {
        uow.discard();
        throw ex;
    }
    throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
}
Also used : EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) ResourceException(org.restlet.resource.ResourceException) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) EntityState(org.qi4j.spi.entity.EntityState) Date(java.util.Date)

Example 2 with EntityStoreUnitOfWork

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

the class UnitOfWorkInstance method get.

public <T> T get(EntityReference identity, ModuleUnitOfWork uow, Iterable<ModelModule<EntityModel>> potentialModels, Class<T> mixinType) throws EntityTypeNotFoundException, NoSuchEntityException {
    checkOpen();
    EntityInstance entityInstance = instanceCache.get(identity);
    if (entityInstance == null) {
        // Not yet in cache
        // Check if this is a root UoW, or if no parent UoW knows about this entity
        EntityState entityState = null;
        EntityModel model = null;
        ModuleInstance module = null;
        // Figure out what EntityStore to use
        for (ModelModule<EntityModel> potentialModel : potentialModels) {
            EntityStore store = potentialModel.module().entityStore();
            EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork(store, potentialModel.module());
            try {
                entityState = storeUow.entityStateOf(identity);
            } catch (EntityNotFoundException e) {
                continue;
            }
            // Get the selected model
            model = (EntityModel) entityState.entityDescriptor();
            module = potentialModel.module();
        }
        // Check if model was found
        if (model == null) {
            // Check if state was found
            if (entityState == null) {
                throw new NoSuchEntityException(identity);
            } else {
                throw new EntityTypeNotFoundException(mixinType.getName());
            }
        }
        // Create instance
        entityInstance = new EntityInstance(uow, module, model, entityState);
        instanceCache.put(identity, entityInstance);
    } else {
        // Check if it has been removed
        if (entityInstance.status() == EntityStatus.REMOVED) {
            throw new NoSuchEntityException(identity);
        }
    }
    return entityInstance.proxy();
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ModuleInstance(org.qi4j.runtime.structure.ModuleInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 3 with EntityStoreUnitOfWork

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

the class EntityResource method delete.

@Override
protected Representation delete(Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Remove entity");
    EntityStoreUnitOfWork uow = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    try {
        EntityReference identityRef = EntityReference.parseEntityReference(identity);
        uow.entityStateOf(identityRef).remove();
        uow.applyChanges().commit();
        getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
    } catch (EntityNotFoundException e) {
        uow.discard();
        getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    }
    return new EmptyRepresentation();
}
Also used : EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityReference(org.qi4j.api.entity.EntityReference) Usecase(org.qi4j.api.usecase.Usecase) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 4 with EntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.EntityStoreUnitOfWork 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 5 with EntityStoreUnitOfWork

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

the class UnitOfWorkInstance method getEntityStoreUnitOfWork.

public EntityStoreUnitOfWork getEntityStoreUnitOfWork(EntityStore store, Module module) {
    EntityStoreUnitOfWork uow = storeUnitOfWork.get(store);
    if (uow == null) {
        uow = store.newUnitOfWork(usecase, module, currentTime);
        storeUnitOfWork.put(store, uow);
    }
    return uow;
}
Also used : EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork)

Aggregations

EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)7 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)4 EntityReference (org.qi4j.api.entity.EntityReference)3 EntityState (org.qi4j.spi.entity.EntityState)3 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)2 Usecase (org.qi4j.api.usecase.Usecase)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)2 ResourceException (org.restlet.resource.ResourceException)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)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 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1