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())));
}
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());
}
}
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;
}
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;
}
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()));
}
Aggregations