Grinder
Grinder

Reputation: 151

Displaying multiple values in one Olingo property

Sorry if this has been asked already. I haven't found the answer. I have an oData v4 API that builds an Olingo entity collection from a json string that it gets from an internal web service in my company. I can also get json for individual records so I can implement both readEntityCollection and also readEntity. Thus, I take the json string and build an entity that I send to Olingo for serialization in Java. So, I don't have access to the relational database in the back end. I can only work with the JSON string that this 3rd party system returns to me. The JSON string I get is something like this. Notice that the order_items are also in JSON.

{
    "id": 4703,
    "order_number": "123",
    "order_items": "[{"item_no":"1334","item_name":"Widget 1"},{"item_no":"1334","item_name":"Widget 2"}]"
}

It's simple enough to just display the order_items as-is as a string property, which is one big JSON string. But I was hoping that I could pass the order_items json array string to a pre-defined OData property. Because if I add the $format=xml option, it will still show a json string for order_items even if everything else is in XML. So my question is, can I convert a json array (from a string) and pass it to a pre-defined preoperty. (i.e. CsdlProperty) I hope I am making sense.

Thanks

Upvotes: 1

Views: 932

Answers (1)

Grinder
Grinder

Reputation: 151

Ok, I found what I needed. What I really needed to do was to create a collection property for "order_items" that can hold multiple complex types. In other words, a collection of complex types, with each complex type having multiple complex values.

1. I created a new collection type, in getEntityType(), to hold my complex types:

final List<CsdlProperty> result = new ArrayList<CsdlProperty>();
CsdlProperty property = new CsdlProperty() // Complex types
.setName("order_items")
.setType(CT_ORDER_ITEMS.getFullQualifiedNameAsString())
.setNullable(true)
.setCollection(true);
result.add(property);

2. In my readEntity() implementation, I created a complex collection property and populated it with values:

List<ComplexValue> complexCollection = new ArrayList<ComplexValue>();
ComplexValue complexValue = new ComplexValue();
complexValue.getValue().add(new Property(null, "item_no", ValueType.PRIMITIVE, itemNo));
complexValue.getValue().add(new Property(null, "item_name", ValueType.PRIMITIVE, itemName));
...etc
complexCollection.add(complexValue);
Property orderItems = new Property();
orderItems = new Property(null, "order_items", ValueType.COLLECTION_COMPLEX, complexCollection);
...now serialize and send response

Olingo took care of the rest. Thanks to those who answered. If anyone needs further help with this, just send me a message.

Upvotes: 2

Related Questions