Search in sources :

Example 6 with EntityState

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

the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.

@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        // There is no Query capability available for Libraries, since that sits in Extensions.
        // Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
        // to the test sitting in qi4j-libraries source repository.
        //            QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
        //            Query<DebugRecord> query = builder.newQuery( uow );
        //            assertEquals( 0, query.count() );
        Some service = (Some) module.findService(Some.class).get();
        String message = service.doSomething("World!", 10);
        assertEquals(message, "Hello!");
        EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
        final String[] result = new String[1];
        es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {

            public EntityState map(EntityState entityState) {
                if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
                    result[0] = entityState.identity().identity();
                }
                return entityState;
            }
        }, Outputs.<EntityState>noop()));
        ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
        String mess = debugEntry.message().get();
        System.out.println(mess);
        assertEquals("some message.", mess);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
        e.printStackTrace();
        uow.discard();
    } catch (UnitOfWorkCompletionException e) {
        e.printStackTrace();
        uow.discard();
    } finally {
        if (uow.isOpen()) {
            uow.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityState(org.qi4j.spi.entity.EntityState) ServiceDebugRecordEntity(org.qi4j.logging.debug.records.ServiceDebugRecordEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 7 with EntityState

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

the class EntitySerializerTest method testEntitySerializer.

@Test
public void testEntitySerializer() throws RDFHandlerException {
    EntityReference entityReference = new EntityReference("test2");
    EntityState entityState = entityStore.newUnitOfWork(UsecaseBuilder.newUsecase("Test"), module, System.currentTimeMillis()).entityStateOf(entityReference);
    Iterable<Statement> graph = serializer.serialize(entityState);
    String[] prefixes = new String[] { "rdf", "dc", " vc" };
    String[] namespaces = new String[] { Rdfs.RDF, DcRdf.NAMESPACE, "http://www.w3.org/2001/vcard-rdf/3.0#" };
    new RdfXmlSerializer().serialize(graph, new PrintWriter(System.out), prefixes, namespaces);
}
Also used : Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) EntityReference(org.qi4j.api.entity.EntityReference) EntityState(org.qi4j.spi.entity.EntityState) PrintWriter(java.io.PrintWriter) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 8 with EntityState

use of org.qi4j.spi.entity.EntityState 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) EntityState(org.qi4j.spi.entity.EntityState) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) Date(java.util.Date)

Example 9 with EntityState

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

the class ResourceValidity method updateResponse.

public void updateResponse(Response response) {
    if (entity != null) {
        EntityState state = spi.entityStateOf(entity);
        Date lastModified = new Date(state.lastModified());
        Tag tag = new Tag(state.identity().identity() + "/" + state.version());
        response.getEntity().setModificationDate(lastModified);
        response.getEntity().setTag(tag);
    }
}
Also used : EntityState(org.qi4j.spi.entity.EntityState) Tag(org.restlet.data.Tag) Date(java.util.Date)

Example 10 with EntityState

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

the class EntityModel method newEntityState.

public EntityState newEntityState(EntityStoreUnitOfWork store, EntityReference identity) throws ConstraintViolationException, EntityStoreException {
    try {
        // New EntityState
        EntityState entityState = store.newEntityState(identity, this);
        // Set identity property
        PropertyDescriptor persistentPropertyDescriptor = state().propertyModelFor(IDENTITY_METHOD);
        entityState.setPropertyValue(persistentPropertyDescriptor.qualifiedName(), identity.identity());
        return entityState;
    } catch (EntityAlreadyExistsException e) {
        throw new EntityCompositeAlreadyExistsException(identity);
    } catch (EntityStoreException e) {
        throw new ConstructionException("Could not create new entity in store", e);
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityAlreadyExistsException(org.qi4j.spi.entitystore.EntityAlreadyExistsException) EntityState(org.qi4j.spi.entity.EntityState) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) ConstructionException(org.qi4j.api.common.ConstructionException) EntityCompositeAlreadyExistsException(org.qi4j.api.unitofwork.EntityCompositeAlreadyExistsException)

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