use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class EntityResource method representHtml.
private Representation representHtml(final EntityState entity) {
return new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer writer) throws IOException {
PrintWriter out = new PrintWriter(writer);
out.println("<html><head><title>" + entity.identity() + "</title>" + "<link rel=\"alternate\" type=\"application/rdf+xml\" " + "href=\"" + entity.identity() + ".rdf\"/></head><body>");
out.println("<h1>" + entity.identity() + "</h1>");
out.println("<form method=\"post\" action=\"" + getRequest().getResourceRef().getPath() + "\">\n");
out.println("<fieldset><legend>Properties</legend>\n<table>");
final EntityDescriptor descriptor = entity.entityDescriptor();
for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
Object value = entity.propertyValueOf(persistentProperty.qualifiedName());
out.println("<tr><td>" + "<label for=\"" + persistentProperty.qualifiedName() + "\" >" + persistentProperty.qualifiedName().name() + "</label></td>\n" + "<td><input " + "size=\"80\" " + "type=\"text\" " + (persistentProperty.isImmutable() ? "readonly=\"true\" " : "") + "name=\"" + persistentProperty.qualifiedName() + "\" " + "value=\"" + (value == null ? "" : valueSerialization.serialize(value)) + "\"/></td></tr>");
}
out.println("</table></fieldset>\n");
out.println("<fieldset><legend>Associations</legend>\n<table>");
for (AssociationDescriptor associationType : descriptor.state().associations()) {
Object value = entity.associationValueOf(associationType.qualifiedName());
if (value == null) {
value = "";
}
out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><input " + "type=\"text\" " + "size=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" " + "value=\"" + value + "\"/></td></tr>");
}
out.println("</table></fieldset>\n");
out.println("<fieldset><legend>ManyAssociations</legend>\n<table>");
for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
ManyAssociationState identities = entity.manyAssociationValueOf(associationType.qualifiedName());
String value = "";
for (EntityReference identity : identities) {
value += identity.identity() + "\n";
}
out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
}
out.println("</table></fieldset>\n");
out.println("<fieldset><legend>NamedAssociations</legend>\n<table>");
for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
NamedAssociationState identities = entity.namedAssociationValueOf(associationType.qualifiedName());
String value = "";
for (String name : identities) {
value += name + "\n" + identities.get(name).identity() + "\n";
}
out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
}
out.println("</table></fieldset>\n");
out.println("<input type=\"submit\" value=\"Update\"/></form>\n");
out.println("</body></html>\n");
}
};
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class BuilderEntityState method copyTo.
public void copyTo(EntityState newEntityState) {
for (Map.Entry<QualifiedName, Object> fromPropertyEntry : properties.entrySet()) {
newEntityState.setPropertyValue(fromPropertyEntry.getKey(), fromPropertyEntry.getValue());
}
for (Map.Entry<QualifiedName, EntityReference> fromAssociationEntry : associations.entrySet()) {
newEntityState.setAssociationValue(fromAssociationEntry.getKey(), fromAssociationEntry.getValue());
}
for (Map.Entry<QualifiedName, ManyAssociationState> fromManyAssociationEntry : manyAssociations.entrySet()) {
QualifiedName qName = fromManyAssociationEntry.getKey();
ManyAssociationState fromManyAssoc = fromManyAssociationEntry.getValue();
ManyAssociationState toManyAssoc = newEntityState.manyAssociationValueOf(qName);
for (EntityReference entityReference : fromManyAssoc) {
toManyAssoc.add(0, entityReference);
}
}
for (Map.Entry<QualifiedName, NamedAssociationState> fromNamedAssociationEntry : namedAssociations.entrySet()) {
QualifiedName qName = fromNamedAssociationEntry.getKey();
NamedAssociationState fromNamedAssoc = fromNamedAssociationEntry.getValue();
NamedAssociationState toNamedAssoc = newEntityState.namedAssociationValueOf(qName);
for (String name : fromNamedAssoc) {
toNamedAssoc.put(name, fromNamedAssoc.get(name));
}
}
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class UnitOfWorkInstance method applyChanges.
private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
List<StateCommitter> committers = new ArrayList<>();
for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
try {
StateCommitter committer = entityStoreUnitOfWork.applyChanges();
committers.add(committer);
} catch (Exception e) {
// Cancel all previously prepared stores
for (StateCommitter committer : committers) {
committer.cancel();
}
if (e instanceof ConcurrentEntityStateModificationException) {
// If we cancelled due to concurrent modification, then create the proper exception for it!
ConcurrentEntityStateModificationException mee = (ConcurrentEntityStateModificationException) e;
Collection<EntityReference> modifiedEntityIdentities = mee.modifiedEntities();
Collection<EntityComposite> modifiedEntities = new ArrayList<>();
for (EntityReference modifiedEntityIdentity : modifiedEntityIdentities) {
Collection<EntityInstance> instances = instanceCache.values();
for (EntityInstance instance : instances) {
if (instance.identity().equals(modifiedEntityIdentity)) {
modifiedEntities.add(instance.<EntityComposite>proxy());
}
}
}
throw new ConcurrentEntityModificationException(modifiedEntities);
} else {
throw new UnitOfWorkCompletionException(e);
}
}
}
return committers;
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class JSONEntityState method associationValueOf.
@Override
public EntityReference associationValueOf(QualifiedName stateName) {
try {
Object jsonValue = state.getJSONObject(JSONKeys.ASSOCIATIONS).opt(stateName.name());
if (jsonValue == null) {
return null;
}
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
return value;
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class MapEntityStoreMixin method writeEntityState.
protected void writeEntityState(DefaultEntityState state, Writer writer, String version, long lastModified) throws EntityStoreException {
try {
JSONWriter json = new JSONWriter(writer);
JSONWriter properties = json.object().key(JSONKeys.IDENTITY).value(state.identity().identity()).key(JSONKeys.APPLICATION_VERSION).value(application.version()).key(JSONKeys.TYPE).value(first(state.entityDescriptor().types()).getName()).key(JSONKeys.VERSION).value(version).key(JSONKeys.MODIFIED).value(lastModified).key(JSONKeys.PROPERTIES).object();
EntityDescriptor entityType = state.entityDescriptor();
for (PropertyDescriptor persistentProperty : entityType.state().properties()) {
Object value = state.properties().get(persistentProperty.qualifiedName());
json.key(persistentProperty.qualifiedName().name());
if (value == null || ValueType.isPrimitiveValue(value)) {
json.value(value);
} else {
String serialized = valueSerialization.serialize(value);
if (serialized.startsWith("{")) {
json.value(new JSONObject(serialized));
} else if (serialized.startsWith("[")) {
json.value(new JSONArray(serialized));
} else {
json.value(serialized);
}
}
}
JSONWriter associations = properties.endObject().key(JSONKeys.ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
EntityReference value = stateNameEntityReferenceEntry.getValue();
associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
}
JSONWriter manyAssociations = associations.endObject().key(JSONKeys.MANY_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
for (EntityReference entityReference : stateNameListEntry.getValue()) {
assocs.value(entityReference.identity());
}
assocs.endArray();
}
JSONWriter namedAssociations = manyAssociations.endObject().key(JSONKeys.NAMED_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry : state.namedAssociations().entrySet()) {
JSONWriter assocs = namedAssociations.key(stateNameMapEntry.getKey().name()).object();
for (Map.Entry<String, EntityReference> namedRef : stateNameMapEntry.getValue().entrySet()) {
assocs.key(namedRef.getKey()).value(namedRef.getValue().identity());
}
assocs.endObject();
}
namedAssociations.endObject().endObject();
} catch (JSONException e) {
throw new EntityStoreException("Could not store EntityState", e);
}
}
Aggregations