use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class ValueCompositeCxfType method isValueComposite.
@SuppressWarnings("raw")
private boolean isValueComposite(Type type) {
Class clazz = Classes.RAW_CLASS.map(type);
ValueDescriptor descriptor = module.valueDescriptor(clazz.getName());
return descriptor != null;
}
use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class PropertyEqualityTest method givenValuesOfTheSameTypeWhenTestingPropertyDescriptorEqualityExpectEquals.
//
// ------------------------------:: PropertyDescriptor equality tests ::--------------------------------------------
//
@Test
public void givenValuesOfTheSameTypeWhenTestingPropertyDescriptorEqualityExpectEquals() {
Some some = buildSomeValue(module);
ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
PropertyDescriptor someCharPropDesc = someDescriptor.state().findPropertyModelByName("characterProperty");
Some other = buildSomeValue(module);
ValueDescriptor otherDescriptor = qi4j.api().valueDescriptorFor(other);
PropertyDescriptor otherCharPropDesc = otherDescriptor.state().findPropertyModelByName("characterProperty");
assertThat("PropertyDescriptors equal", someCharPropDesc, equalTo(otherCharPropDesc));
assertThat("PropertyDescriptors hashcode equal", someCharPropDesc.hashCode(), equalTo(otherCharPropDesc.hashCode()));
}
use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class PropertyEqualityTest method givenValuesOfDifferentTypesWhenTestingPropertyDescriptorEqualityExpectNotEquals.
@Test
public void givenValuesOfDifferentTypesWhenTestingPropertyDescriptorEqualityExpectNotEquals() {
Some some = buildSomeValue(module);
ValueDescriptor someDescriptor = qi4j.api().valueDescriptorFor(some);
PropertyDescriptor someCharPropDesc = someDescriptor.state().findPropertyModelByName("characterProperty");
Other other = buildOtherValue(module);
ValueDescriptor otherDescriptor = qi4j.api().valueDescriptorFor(other);
PropertyDescriptor otherCharPropDesc = otherDescriptor.state().findPropertyModelByName("characterProperty");
assertThat("PropertyDescriptors not equal", someCharPropDesc, not(equalTo(otherCharPropDesc)));
assertThat("PropertyDescriptors hashcode not equal", someCharPropDesc.hashCode(), not(equalTo(otherCharPropDesc.hashCode())));
}
use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class ValueSerializerAdapter method serializeValueComposite.
private void serializeValueComposite(Object object, OutputType output, boolean includeTypeInfo, boolean rootPass) throws Exception {
CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) object);
ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();
onObjectStart(output);
if (includeTypeInfo && !rootPass) {
onFieldStart(output, "_type");
onValueStart(output);
onValue(output, first(descriptor.valueType().types()).getName());
onValueEnd(output);
onFieldEnd(output);
}
for (PropertyDescriptor persistentProperty : descriptor.valueType().properties()) {
Property<?> property = state.propertyFor(persistentProperty.accessor());
onFieldStart(output, persistentProperty.qualifiedName().name());
onValueStart(output);
doSerialize(property.get(), output, includeTypeInfo, false);
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().associations()) {
Association<?> association = state.associationFor(associationDescriptor.accessor());
Object instance = association.get();
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
if (instance == null) {
onValue(output, null);
} else {
onValue(output, ((Identity) instance).identity().get());
}
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations()) {
ManyAssociation<?> manyAssociation = state.manyAssociationFor(associationDescriptor.accessor());
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
onArrayStart(output);
for (Object instance : manyAssociation) {
onValueStart(output);
onValue(output, ((Identity) instance).identity().get());
onValueEnd(output);
}
onArrayEnd(output);
onValueEnd(output);
onFieldEnd(output);
}
onObjectEnd(output);
}
use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.
the class ValueDescriptorResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof ValueDescriptor) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
JSONObject json = new JSONObject();
ValueDescriptor vd = (ValueDescriptor) result;
try {
for (PropertyDescriptor propertyDescriptor : vd.state().properties()) {
Object o = propertyDescriptor.initialValue(module);
if (o == null) {
json.put(propertyDescriptor.qualifiedName().name(), JSONObject.NULL);
} else {
json.put(propertyDescriptor.qualifiedName().name(), o.toString());
}
}
} catch (JSONException e) {
throw new ResourceException(e);
}
StringRepresentation representation = new StringRepresentation(json.toString(), 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 {
Map<String, Object> context = new HashMap<String, Object>();
context.put("request", response.getRequest());
context.put("response", response);
context.put("result", result);
try {
cfg.getTemplate("form.htm").process(context, writer);
} catch (TemplateException e) {
throw new IOException(e);
}
}
};
response.setEntity(rep);
return true;
}
}
return false;
}
Aggregations