Search in sources :

Example 1 with StateHolder

use of org.qi4j.api.property.StateHolder in project qi4j-sdk by Qi4j.

the class ValueCompositeRequestWriter method writeRequest.

@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException {
    if (requestObject instanceof ValueComposite) {
        // Value as parameter
        final ValueComposite valueObject = (ValueComposite) requestObject;
        if (request.getMethod().equals(Method.GET)) {
            StateHolder holder = spi.stateOf(valueObject);
            final ValueDescriptor descriptor = spi.valueDescriptorFor(valueObject);
            final Reference ref = request.getResourceRef();
            ref.setQuery(null);
            try {
                for (PropertyDescriptor propertyDescriptor : descriptor.state().properties()) {
                    Object value = holder.propertyFor(propertyDescriptor.accessor()).get();
                    String param;
                    if (value == null) {
                        param = null;
                    } else {
                        param = valueSerializer.serialize(value);
                    }
                    ref.addQueryParameter(propertyDescriptor.qualifiedName().name(), param);
                }
            } catch (ValueSerializationException e) {
                throw new ResourceException(e);
            }
        } else {
            request.setEntity(new WriterRepresentation(MediaType.APPLICATION_JSON) {

                @Override
                public void write(Writer writer) throws IOException {
                    setCharacterSet(CharacterSet.UTF_8);
                    valueSerializer.serialize(valueObject, new WriterOutputStream(writer));
                }
            });
        }
        return true;
    }
    return false;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Reference(org.restlet.data.Reference) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) IOException(java.io.IOException) WriterOutputStream(org.restlet.engine.io.WriterOutputStream) ValueComposite(org.qi4j.api.value.ValueComposite) WriterRepresentation(org.restlet.representation.WriterRepresentation) StateHolder(org.qi4j.api.property.StateHolder) ResourceException(org.restlet.resource.ResourceException) RequestWriter(org.qi4j.library.rest.client.spi.RequestWriter) Writer(java.io.Writer)

Example 2 with StateHolder

use of org.qi4j.api.property.StateHolder in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method storePropertiesOfVC.

private // 
Integer storePropertiesOfVC(// 
Map<QualifiedName, PreparedStatement> qNameInsertPSs, // 
PreparedStatement insertAllQNamesPS, // 
Integer propertyPK, // 
Long entityPK, // 
Object property) throws SQLException {
    ValueDescriptor vDesc = this._qi4SPI.valueDescriptorFor((ValueComposite) property);
    StateHolder state = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) property).state();
    Integer originalPropertyPK = propertyPK;
    ++propertyPK;
    for (PropertyDescriptor pDesc : vDesc.state().properties()) {
        propertyPK = // 
        this.insertProperty(// 
        qNameInsertPSs, // 
        insertAllQNamesPS, // 
        propertyPK, // 
        entityPK, // 
        pDesc.qualifiedName(), // 
        state.propertyFor(pDesc.accessor()).get(), // 
        originalPropertyPK);
    }
    return propertyPK;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) StateHolder(org.qi4j.api.property.StateHolder) ValueComposite(org.qi4j.api.value.ValueComposite)

Aggregations

PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 StateHolder (org.qi4j.api.property.StateHolder)2 ValueComposite (org.qi4j.api.value.ValueComposite)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)1 RequestWriter (org.qi4j.library.rest.client.spi.RequestWriter)1 Reference (org.restlet.data.Reference)1 WriterOutputStream (org.restlet.engine.io.WriterOutputStream)1 WriterRepresentation (org.restlet.representation.WriterRepresentation)1 ResourceException (org.restlet.resource.ResourceException)1