Search in sources :

Example 11 with ValueDescriptor

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

the class ValueEqualityTest method givenValuesOfCommonTypesWhenTestingValueDescriptorEqualityExpectNotEquals.

@Test
public void givenValuesOfCommonTypesWhenTestingValueDescriptorEqualityExpectNotEquals() {
    Some some = buildSomeValue(module);
    ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
    PrimitivesValue primitive = buildPrimitivesValue(module);
    ValueDescriptor primitiveDescriptor = qi4j.api().valueDescriptorFor(primitive);
    assertThat("ValueDescriptors not equal", someDescriptor, not(equalTo(primitiveDescriptor)));
    assertThat("ValueDescriptors hashcode not equal", someDescriptor.hashCode(), not(equalTo(primitiveDescriptor.hashCode())));
}
Also used : AnotherSome(org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome) Some(org.qi4j.runtime.property.PropertyEqualityTest.Some) PrimitivesValue(org.qi4j.runtime.property.PropertyEqualityTest.PrimitivesValue) PropertyEqualityTest.buildPrimitivesValue(org.qi4j.runtime.property.PropertyEqualityTest.buildPrimitivesValue) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 12 with ValueDescriptor

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

the class PDFWriter method writeTypeGeneralPage.

private void writeTypeGeneralPage(Object objectDesciptor) {
    setFont(header5Font, header5FontSize);
    writeString("General: ", headerLineSpace);
    setFont(normalFont, normalFontSize);
    if (objectDesciptor instanceof ServiceDetailDescriptor) {
        ServiceDescriptor descriptor = ((ServiceDetailDescriptor) objectDesciptor).descriptor();
        writeString("- identity: " + descriptor.identity());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
        writeString("- startup: " + ((ServiceDetailDescriptor) objectDesciptor).descriptor().isInstantiateOnStartup());
    } else if (objectDesciptor instanceof EntityDetailDescriptor) {
        EntityDescriptor descriptor = ((EntityDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof ValueDetailDescriptor) {
        ValueDescriptor descriptor = ((ValueDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof ObjectDetailDescriptor) {
        ObjectDescriptor descriptor = ((ObjectDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof CompositeDetailDescriptor) {
        CompositeDescriptor descriptor = ((CompositeDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ServiceDescriptor(org.qi4j.api.service.ServiceDescriptor) ImportedServiceDescriptor(org.qi4j.api.service.ImportedServiceDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) ObjectDescriptor(org.qi4j.api.object.ObjectDescriptor)

Example 13 with ValueDescriptor

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

use of org.qi4j.api.value.ValueDescriptor 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 = null;
    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
        int idx = 0;
        for (Annotation[] annotations : interactionMethod.getParameterAnnotations()) {
            Name name = (Name) first(filter(isType(Name.class), iterable(annotations)));
            String value = getValue(name.value(), queryAsForm, entityAsForm);
            String paramName;
            if (name != null) {
                paramName = name.value();
            } else {
                paramName = "param" + idx;
            }
            form.add(paramName, value);
            idx++;
        }
    }
    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 15 with ValueDescriptor

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

the class PropertyEqualityTest method givenValuesOfCommonTypesWhenTestingPropertyDescriptorEqualityExpectEquals.

@Test
public void givenValuesOfCommonTypesWhenTestingPropertyDescriptorEqualityExpectEquals() {
    Some some = buildSomeValue(module);
    ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
    PropertyDescriptor someCharPropDesc = someDescriptor.state().findPropertyModelByName("characterProperty");
    PrimitivesValue primitive = buildPrimitivesValue(module);
    ValueDescriptor primitiveDescriptor = qi4j.api().valueDescriptorFor(primitive);
    PropertyDescriptor primitiveCharPropDesc = primitiveDescriptor.state().findPropertyModelByName("characterProperty");
    assertThat("PropertyDescriptors equal", someCharPropDesc, equalTo(primitiveCharPropDesc));
    assertThat("PropertyDescriptors hashcode equal", someCharPropDesc.hashCode(), equalTo(primitiveCharPropDesc.hashCode()));
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) 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