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