Search in sources :

Example 16 with PropertyDescriptor

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);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueComposite(org.qi4j.api.value.ValueComposite) Composite(org.qi4j.api.composite.Composite) ValueType(org.qi4j.api.type.ValueType) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 17 with PropertyDescriptor

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);
        }
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI)

Example 18 with PropertyDescriptor

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;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Form(org.restlet.data.Form) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) Name(org.qi4j.api.constraint.Name)

Example 19 with PropertyDescriptor

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");
        }
    };
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) WriterRepresentation(org.restlet.representation.WriterRepresentation) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 20 with PropertyDescriptor

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;
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) DefaultTableModel(javax.swing.table.DefaultTableModel) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Aggregations

PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)37 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)15 Map (java.util.Map)12 EntityReference (org.qi4j.api.entity.EntityReference)12 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)11 HashMap (java.util.HashMap)10 QualifiedName (org.qi4j.api.common.QualifiedName)10 LinkedHashMap (java.util.LinkedHashMap)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ValueType (org.qi4j.api.type.ValueType)5 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 JSONArray (org.json.JSONArray)4 Test (org.junit.Test)4 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4