Search in sources :

Example 21 with ValueDescriptor

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

the class ResourceTemplateResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
    if (type != null) {
        // Try to find template for this specific resource
        StringBuilder templateBuilder = (StringBuilder) response.getRequest().getAttributes().get("template");
        String templateName = templateBuilder.toString();
        if (result instanceof ValueDescriptor) {
            templateName += "_form";
        }
        final String extension = metadataService.getExtension(type);
        templateName += "." + extension;
        // Have we failed on this one before, then don't try again
        if (skip.contains(templateName)) {
            return false;
        }
        try {
            final Template template = cfg.getTemplate(templateName);
            Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {

                @Override
                public void write(Writer writer) throws IOException {
                    Map<String, Object> context = new HashMap<String, Object>();
                    context.put("request", response.getRequest());
                    context.put("response", response);
                    context.put("result", result);
                    try {
                        template.process(context, writer);
                    } catch (TemplateException e) {
                        throw new IOException(e);
                    }
                }
            };
            response.setEntity(rep);
            return true;
        } catch (Exception e) {
            skip.add(templateName);
        // Ignore
        }
    }
    return false;
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Writer(java.io.Writer)

Example 22 with ValueDescriptor

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

the class ValueEqualityTest method givenValuesOfTheSameTypeWhenTestingValueDescriptorEqualityExpectEquals.

// 
// -------------------------------:: ValueDescriptor equality tests ::----------------------------------------------
// 
@Test
public void givenValuesOfTheSameTypeWhenTestingValueDescriptorEqualityExpectEquals() {
    Some some = buildSomeValue(module);
    ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
    Some other = buildSomeValue(module);
    ValueDescriptor otherDescriptor = qi4j.api().valueDescriptorFor(other);
    assertThat("ValueDescriptors equal", someDescriptor, equalTo(otherDescriptor));
    assertThat("ValueDescriptors hashcode equal", someDescriptor.hashCode(), equalTo(otherDescriptor.hashCode()));
}
Also used : AnotherSome(org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome) Some(org.qi4j.runtime.property.PropertyEqualityTest.Some) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

ValueDescriptor (org.qi4j.api.value.ValueDescriptor)22 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)10 Test (org.junit.Test)6 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)6 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)4 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)4 ValueComposite (org.qi4j.api.value.ValueComposite)4 IOException (java.io.IOException)3 Writer (java.io.Writer)3 Identity (org.qi4j.api.entity.Identity)3 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)3 AnotherSome (org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome)3 Some (org.qi4j.runtime.property.PropertyEqualityTest.Some)3 TemplateException (freemarker.template.TemplateException)2 HashMap (java.util.HashMap)2 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 QualifiedName (org.qi4j.api.common.QualifiedName)2 CompositeDescriptor (org.qi4j.api.composite.CompositeDescriptor)2 CompositeInstance (org.qi4j.api.composite.CompositeInstance)2