Additional Data How To - AEVI-AppFlow/pos-android-sdk GitHub Wiki
Many of the AppFlow data models contain an AdditionalData
object. This object is essentially a bucket for storing any kind of data. This AdditionalData
will be passed around with the primary flow data and can be accessed for read and write by flow services.
Adding data
Simple data types and complex objects can be stored. However, regardless of the object type stored it must be able to be serialised to JSON. Primitive types String, int, and boolean are supported and will be automatically converted. Arrays of the same type of data can also be stored.
Data is added using the addData
method along with a key that they should be stored against.
additionalData.addData("myExtra", "ext"); // store a String
additionalData.addData("myBoolean", true); // store a boolean
additionalData.addData("myInt", 42322); // store an int
additionalData.addData("myLong", 7736663L); // store a long
To get the data stored back at a later point the getValue
method will return it automatically converted back to the original type. e.g.
long myLong = additionalData.getValue("myLong");
If an attempt is made to get the data back as a different type then a null/empty value will be returned e.g.
String str = additionalData.getValue("myLong");
// str above will be null
Helper methods are available to return primitive String, int and boolean getStringValue
, getIntegerValue
and getBooleanValue
. Each of these can also optionally include the passing of a default if the key (of the correct type) is not found.
Complex objects
If a complex object is added to the additional data then when it is passed between flow services it will be serialized to JSON and stored in the data along with a field indicating the object type. Therefore, any object stored must be capable of being serialised. Generally speaking the objects stored should be simple POJO objects. You may also wish to use our own Jsonable
interface to mark the class as able to be converted to/from JSON (for more details about Jsonable
see our json-utils repo ). If this object is to be passed between one or more flow services they must both have access to the class in there classpath in order that it can be de-serialised correctly.
Complex objects are stored in the same way a primitive types and retrieved by adding the class to the getValue
method e.g.
Customer customer = new Customer("123");
additionalData.addData("customer", customer);
// reading the customer object back
Customer customerRet = additionalData.getValue("customer", Customer.class)
If you need to store your object as it own super type and not the actual subclass you can specify the type it is stored as using the addDataWithType
method.
Dog dog = new Dog("rover");
additionalData.addDataWithType("myPet", dog, Animal.class);
Arrays
Arrays can be simply stored and retrieved as above.
additionalData.addData("myArray", "hello", "bye", "hi", "goodbye");
String[] myArrayVals = additionalData.getValue("myArray", String[].class)
Extra finders and utilities
Get all data by type
The getDataOfType
method will scan an entire AdditionalData
object for all instances stored of the type given.
additionalData.addData("one", 1);
additionalData.addData("two", 2);
additionalData.addData("three", "hello");
Map<String, Integer> dataOfType = additionalData.getDataOfType(Integer.class);
The code above will return a Map
containing two integers
Get classname of data
Occasionally it may be useful to know the class type of a data key before attempting to get it. This can be done by using the getValueClassName
simply passing in the key you want to know the class type of, which will be returned as a String.
Cleaning up
Various clear, remove methods are also available to delete and remove keys from the additional data.