Search in sources :

Example 6 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class ValueToEntityMixin method doConversion.

private <T> EntityBuilder<?> doConversion(Class<T> entityType, String identity, Object value) {
    EntityDescriptor eDesc = module.entityDescriptor(entityType.getName());
    if (eDesc == null) {
        throw new EntityTypeNotFoundException(entityType.getName());
    }
    ValueComposite vComposite = (ValueComposite) value;
    ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
    AssociationStateHolder vState = spi.stateOf(vComposite);
    AssociationStateDescriptor vStateDesc = vDesc.state();
    Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
    if (unqualified == null || !unqualified.value()) {
        return doQualifiedConversion(entityType, identity, vState, vStateDesc);
    }
    return doUnqualifiedConversion(entityType, identity, vState, vStateDesc);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 7 with ValueComposite

use of org.qi4j.api.value.ValueComposite 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 8 with ValueComposite

use of org.qi4j.api.value.ValueComposite 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 9 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class ValueCompositeResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof ValueComposite) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            StringRepresentation representation = new StringRepresentation(valueSerializer.serialize(result), MediaType.APPLICATION_JSON);
            response.setEntity(representation);
            return true;
        } else if (MediaType.TEXT_HTML.equals(type)) {
            Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {

                @Override
                public void write(Writer writer) throws IOException {
                    // Look for type specific template
                    Template template;
                    try {
                        template = cfg.getTemplate("/rest/template/" + result.getClass().getInterfaces()[0].getSimpleName() + ".htm");
                    } catch (Exception e) {
                        // Use default
                        template = cfg.getTemplate("value.htm");
                    }
                    Map<String, Object> context = new HashMap<String, Object>();
                    context.put("request", response.getRequest());
                    context.put("response", response);
                    context.put("result", result);
                    context.put("util", this);
                    try {
                        template.process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }

                public boolean isSequence(Object obj) {
                    return obj instanceof Collection;
                }
            };
            response.setEntity(rep);
            return true;
        }
    }
    return false;
}
Also used : TemplateException(freemarker.template.TemplateException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) ValueComposite(org.qi4j.api.value.ValueComposite) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Example 10 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class PropertyInstance method prepareToBuild.

@SuppressWarnings({ "raw", "unchecked" })
public void prepareToBuild(PropertyModel propertyDescriptor) {
    // Check if state has to be modified
    model = propertyDescriptor.getBuilderInfo();
    if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
        Object value = get();
        if (value != null) {
            ValueInstance.valueInstanceOf((ValueComposite) value).prepareToBuild();
        }
    } else if (propertyDescriptor.valueType() instanceof CollectionType) {
        Object value = get();
        if (value != null) {
            if (value instanceof List) {
                value = new ArrayList((Collection) value);
            } else if (value instanceof Set) {
                value = new LinkedHashSet((Collection) value);
            }
            // Check if items are Values
            CollectionType collection = (CollectionType) propertyDescriptor.valueType();
            if (collection.collectedType() instanceof ValueCompositeType) {
                Collection coll = (Collection) value;
                for (Object instance : coll) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            set((T) value);
        }
    } else if (propertyDescriptor.valueType() instanceof MapType) {
        Object value = get();
        if (value != null) {
            Map map = new LinkedHashMap((Map) value);
            // Check if keys/values are Values
            MapType mapType = (MapType) propertyDescriptor.valueType();
            if (mapType.keyType() instanceof ValueCompositeType) {
                for (Object instance : map.keySet()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            if (mapType.valueType() instanceof ValueCompositeType) {
                for (Object instance : map.values()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            set((T) value);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) LinkedHashMap(java.util.LinkedHashMap) CollectionType(org.qi4j.api.type.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

ValueComposite (org.qi4j.api.value.ValueComposite)14 Collection (java.util.Collection)4 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)4 Map (java.util.Map)3 Set (java.util.Set)3 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)3 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 CollectionType (org.qi4j.api.type.CollectionType)3 MapType (org.qi4j.api.type.MapType)3 ResourceException (org.restlet.resource.ResourceException)3 IOException (java.io.IOException)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 Composite (org.qi4j.api.composite.Composite)2 EntityComposite (org.qi4j.api.entity.EntityComposite)2