use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serializeValueComposite.
private void serializeValueComposite(Resource subject, URI predicate, ValueComposite value, ValueType valueType, Graph graph, String baseUri, boolean includeNonQueryable) {
final ValueFactory valueFactory = graph.getValueFactory();
BNode collection = valueFactory.createBNode();
graph.add(subject, predicate, collection);
for (PropertyDescriptor persistentProperty : ((ValueCompositeType) valueType).properties()) {
Object propertyValue = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((Composite) value).state().propertyFor(persistentProperty.accessor()).get();
if (propertyValue == null) {
// Skip null values
continue;
}
ValueType type = persistentProperty.valueType();
if (type instanceof ValueCompositeType) {
URI pred = valueFactory.createURI(baseUri, persistentProperty.qualifiedName().name());
serializeValueComposite(collection, pred, (ValueComposite) propertyValue, type, graph, baseUri + persistentProperty.qualifiedName().name() + "/", includeNonQueryable);
} else {
serializeProperty(persistentProperty, propertyValue, collection, graph, includeNonQueryable);
}
}
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class EntityTypeSerializer method serializePropertyTypes.
private void serializePropertyTypes(final EntityDescriptor entityDescriptor, final Graph graph, final URI entityTypeUri) {
ValueFactory values = graph.getValueFactory();
// Properties
for (PropertyDescriptor persistentProperty : entityDescriptor.state().properties()) {
URI propertyURI = values.createURI(persistentProperty.qualifiedName().toURI());
graph.add(propertyURI, Rdfs.DOMAIN, entityTypeUri);
graph.add(propertyURI, Rdfs.TYPE, Rdfs.PROPERTY);
// TODO Support more types
URI type = dataTypes.get(persistentProperty.valueType().mainType().getName());
if (type != null) {
graph.add(propertyURI, Rdfs.RANGE, type);
}
}
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class ContextResource method formForMethod.
private Form formForMethod(Method interactionMethod) {
Form form = new Form();
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
Representation representation = Request.getCurrent().getEntity();
if (representation != null && !EmptyRepresentation.class.isInstance(representation)) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
Class<?> valueType = interactionMethod.getParameterTypes()[0];
if (ValueComposite.class.isAssignableFrom(valueType)) {
ValueDescriptor valueDescriptor = module.valueDescriptor(valueType.getName());
for (PropertyDescriptor propertyDescriptor : valueDescriptor.state().properties()) {
String value = getValue(propertyDescriptor.qualifiedName().name(), queryAsForm, entityAsForm);
if (value == null) {
Object initialValue = propertyDescriptor.initialValue(module);
if (initialValue != null) {
value = initialValue.toString();
}
}
form.add(propertyDescriptor.qualifiedName().name(), value);
}
} else if (valueType.isInterface() && interactionMethod.getParameterTypes().length == 1) {
// Single entity as input
form.add("entity", getValue("entity", queryAsForm, entityAsForm));
} else {
// Construct form out of individual parameters instead
for (Annotation[] annotations : interactionMethod.getParameterAnnotations()) {
Name name = (Name) first(filter(isType(Name.class), iterable(annotations)));
form.add(name.value(), getValue(name.value(), queryAsForm, entityAsForm));
}
}
return form;
}
use of org.qi4j.api.property.PropertyDescriptor 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.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class PropertiesPanel method createData.
/**
* Create table table or properties using the supplied query
*
* @param query the Query
*
* @return TableModel
*/
protected TableModel createData(Query query) {
DefaultTableModel model = new DefaultTableModel();
for (Object qObj : query) {
AssociationStateHolder state = qi4jspi.stateOf((EntityComposite) qObj);
EntityDescriptor descriptor = qi4jspi.entityDescriptorFor((EntityComposite) qObj);
// genereate column, first time only
if (model.getColumnCount() < 1) {
for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
model.addColumn(persistentPropertyDescriptor.qualifiedName().name());
}
}
Object[] rowData = new Object[model.getColumnCount()];
int i = 0;
for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
rowData[i++] = state.propertyFor(persistentPropertyDescriptor.accessor());
}
model.addRow(rowData);
}
return model;
}
Aggregations