0

I'm using the Cumulocity Java SDK and I'm trying to get access of the custom fragment in one of the ManagedObject. Something like that:

...
"type": "sap_CustomomerLocation",
"c8y_PropertyType":{
    "Customer.Name":{ 
        "name":"customerName", 
        "sap_field_name":{
            "requestIdentifier":"SAP",
            "adressIdentifier":"customerName" 
        }
    },
    "Customer.Address":{ 
        "name":"customerAddress", 
        "sap_field_name":{
            "requestIdentifier":"SAP",
            "adressIdentifier":"customerAddress" 
        }
    }
}
...

I can't change the format of the fragment. In Java application I fetch the Mo by:

InventoryFilter filter = new InventoryFilter();
filter.byType("sap_CustomomerLocation");

ManagedObjectCollection configuration = platform.getInventoryApi().getManagedObjectsByFilter(filter);
ManagedObjectRepresentation singleConfig = configuration.get().allPages().iterator().next();

Map<String, Object> attrs = singleConfig.getAttrs();

The problem is that I can't get to the "sap_field_name", attrs is a HashMap@Node. In debugger in IntelliJ, when I use "evaluateExpression" something like that is generated:

((HashMap.Node)((HashMap)((HashMap.Node)((HashMap)((HashMap.Node)((HashMap)((HashMap.Node)((HashMap)attrs).entrySet().toArray()[0]).getValue()).entrySet().toArray()[0]).getValue()).entrySet().toArray()[0]).getValue()).entrySet().toArray()[1]).getValue()

So, can you suggest something? Should I somehow cast it to the DTO? But then how to handle keys in the JSON ("Customer.Name", "Customer.Address") that can change because this is a list of fields.

Mariusz Kraj
  • 107
  • 5

2 Answers2

1

I tried to create a new class in namespace c8y.PropertyType and it kinda worked, Cumulocity SDK, or rather Jackson JSON founded that there is a class and tried to map it but the problem remains in terms of this dynamic fields.

After small debugging in the Jackson code, I found out that I would need to create a class in right namespace for all custom fields (unfortunately I don't know the names).

So this is a workaround that I found acceptable:

ManagedObjectRepresentation singleConfig;

Map<String, Object> attrs = singleConfig.getAttrs();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.valueToTree(attrs);

String customFieldName = jsonNode.get("c8y_PropertyType").get("attrs").iterator().next().get("sap_field_name").get("adressIdentifier").asText();
Mariusz Kraj
  • 107
  • 5
0

One way you could approach it is to create a model class for your c8y_PropertyType fragment that matches the content. To map this fragment you would need to create a class PropertyType in the package c8y and put it on the class path.

The JSON parser should then no longer generate this HashMap structure but your class. I am not really sure if the dots in the JSON keys lead to problems here.

If you need examples for such classes check https://bitbucket.org/m2m/cumulocity-clients-java/src/03e47693b1d389308901347d224c13d81250b703/device-capability-model/?at=develop

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23