Section 6: Copying from Source Object
In this section, we will focus on OTCS-commands to convert a source-object to a target-object.
We just learnt to use the
"values:" property in the
"from:" parameter of the copy command. But instead
of the
"values:"property, the
"objectPath:" property has to be used. So, the OTCS-command-block will have
two object-paths, one for the source-object (the
"from:") and the other for the target-object (the
"to:").
For all outputs of the test-cases, please compare and analyze with the source in
"./resources/test-samples/Kronos-passenger-map.xml" file.
Three categories:
The combinations of source and the target object-path can be classified into 3 main categories as explained below-
- Flat paths:
Flat-paths are the object-path combinations of source and target where both the object-paths do not
have collection-tokens in them. Here is an example –
Test case: "${OTC_HOME}/otc-pretest/cpysource_flatpath".
- copy:
id: CP1
from:
objectPath: travelerDetailType.name.surname.value
to:
objectPath: mainPassengerName
When you execute this OTCS-command-block, the output will be as shown here.
- Mixed paths:
Mixed-paths are scenarios where one of the 2 object-paths has collection-tokens in their path while
the other one has a flat-path. Below is an example where only one of them have collection-tokens in
the object-path which is highlighted -
Test case: "${OTC_HOME}/otc-pretest/cpysource_mixedpath".
- copy:
id: CP1
from:
objectPath: travelerDetailType.name.surname.value
to:
objectPath: travelers.traveler[*].anonymousTraveler[*].name.given[*].value
When you execute this OTCS-command-block, the output of the object created in XML format will be as shown
below.
- Collection paths:
Collection-paths are scenarios when both the object-paths have collection tokens in them. Below is an
example where both the object-paths have collection-tokens in their paths -
Test case: "${OTC_HOME}/otc-pretest/cpysource_collection".
- copy:
id: CP1
from:
objectPath: dataLists.passengerList.passenger[*].identityDocuments[*].visas[*].visaType
to:
objectPath: travelers.traveler[*].customObjectsMap[*,*]<K>.name.given[*].value
When you execute this OTCS-command-block, the output of the object created in XML format will be as shown
below.
Type rules:
While creating a target object, there are some rules that you should bear in mind.
- Ideally the leaf-node should be a scalar type such as String, primitive types or wrapper types.
- However, if it is a composite type, then the leaf-nodes of the source should be of the same type or
a sub-type of the target field type.
- If the target's leaf-node field type is any of the primitive or wrapper types, then the type of the
source should be either the same type or a String type that can be parsed without errors.
- If the target's leaf-node field-type is a boolean or Boolean, then the type of the source should be
either the same type or a String type with the value 'true' or 'false'.
Copying to Maps
For copying values from or to Dictionaries (Maps), you need to follow the same steps just as outlined under the
section.
"Copying Literals" under the heading
"Copying literals to Maps:". The only difference is the
use of
"objectPath:" property in the
"from:" parameter in contrast to the
"values:" property
we saw in the earlier example.
Kindly note that OTCS is very flexible allowing you to copy into a dictionary from just anywhere in the source and
vice-versa. Meaning there are
NO RESTRICTIONS such as mandating you to copy only from a dictionary
in the source to another dictionary in the target. Examples given under the section
"Anchors" and the several
test cases under
"${OTC_HOME}/otc-pretest" folder should provide you with enough clarity on constructing
dictionaries.
Copying to Enums and Date fields
Here too the only difference is the use of
"objectPath:" property in the
"from:" parameter instead of
the
"values:" property.