Search in sources :

Example 16 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class EntityResource method getEntityState.

private EntityState getEntityState(EntityStoreUnitOfWork unitOfWork) throws ResourceException {
    EntityState entityState;
    try {
        EntityReference entityReference = EntityReference.parseEntityReference(identity);
        entityState = unitOfWork.entityStateOf(entityReference);
    } catch (EntityNotFoundException e) {
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
    }
    return entityState;
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) ResourceException(org.restlet.resource.ResourceException) EntityState(org.qi4j.spi.entity.EntityState) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 17 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class ResourceValidity method checkRequest.

public void checkRequest() throws ResourceException {
    // Check command rules
    Date modificationDate = request.getConditions().getUnmodifiedSince();
    if (modificationDate != null) {
        EntityState state = spi.entityStateOf(entity);
        // Cut off milliseconds
        Date lastModified = new Date((state.lastModified() / 1000) * 1000);
        if (lastModified.after(modificationDate)) {
            throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);
        }
    }
    // Check query rules
    modificationDate = request.getConditions().getModifiedSince();
    if (modificationDate != null) {
        EntityState state = spi.entityStateOf(entity);
        // Cut off milliseconds
        Date lastModified = new Date((state.lastModified() / 1000) * 1000);
        if (!lastModified.after(modificationDate)) {
            throw new ResourceException(Status.REDIRECTION_NOT_MODIFIED);
        }
    }
}
Also used : ResourceException(org.restlet.resource.ResourceException) EntityState(org.qi4j.spi.entity.EntityState) Date(java.util.Date)

Aggregations

EntityState (org.qi4j.spi.entity.EntityState)17 ResourceException (org.restlet.resource.ResourceException)4 Date (java.util.Date)3 EntityReference (org.qi4j.api.entity.EntityReference)3 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)3 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)3 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)3 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)3 PrintWriter (java.io.PrintWriter)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 EntityStatus (org.qi4j.spi.entity.EntityStatus)2 EntityStore (org.qi4j.spi.entitystore.EntityStore)2 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)2 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2