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