use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class EntitiesResource method representHtml.
private Representation representHtml() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
Representation representation = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer buf) throws IOException {
PrintWriter out = new PrintWriter(buf);
out.println("<html><head><title>All entities</title></head><body><h1>All entities</h1><ul>");
for (EntityReference entity : query) {
out.println("<li><a href=\"" + getRequest().getResourceRef().clone().addSegment(entity.identity() + ".html") + "\">" + entity.identity() + "</a></li>");
}
out.println("</ul></body></html>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(e);
}
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class EntitiesResource method representRdf.
private Representation representRdf() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
WriterRepresentation representation = new WriterRepresentation(MediaType.APPLICATION_RDF_XML) {
@Override
public void write(Writer writer) throws IOException {
PrintWriter out = new PrintWriter(writer);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rdf:RDF\n" + "\txmlns=\"urn:qi4j:\"\n" + "\txmlns:qi4j=\"http://www.qi4j.org/rdf/model/1.0/\"\n" + "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" + "\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">");
for (EntityReference qualifiedIdentity : query) {
out.println("<qi4j:entity rdf:about=\"" + getRequest().getResourceRef().getPath() + "/" + qualifiedIdentity.identity() + ".rdf\"/>");
}
out.println("</rdf:RDF>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
}
use of org.qi4j.api.entity.EntityReference 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();
}
use of org.qi4j.api.entity.EntityReference 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.isEmpty()) {
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
}
}
for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
NamedAssociationState namedAssociation = entity.namedAssociationValueOf(associationType.qualifiedName());
if (newStringAssociation == null) {
// Remove "left-overs"
for (String name : namedAssociation) {
namedAssociation.remove(name);
}
continue;
}
Set<String> names = new HashSet<>();
BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
String name = line;
line = bufferedReader.readLine();
if (line == null) {
break;
}
String identity = line;
EntityReference reference = new EntityReference(identity);
try {
unitOfWork.entityStateOf(reference);
namedAssociation.remove(name);
namedAssociation.put(name, reference);
names.add(name);
} catch (EntityNotFoundException e) {
// Ignore this entity - doesn't exist
}
}
// Remove "left-overs"
for (String assocName : Iterables.toList(namedAssociation)) {
if (!names.contains(assocName)) {
namedAssociation.remove(assocName);
}
}
} catch (IOException e) {
// Ignore
}
}
} catch (ValueSerializationException | 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();
}
use of org.qi4j.api.entity.EntityReference 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;
}
Aggregations