Using inheritance and polymorphism with attributes - Cloud - 7.3

Talend Studio User Guide

Version
Cloud
7.3
Language
English
Product
Talend Big Data
Talend Big Data Platform
Talend Cloud
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Design and Development
Last publication date
2024-02-13
Available in...

Data Fabric

MDM Platform

Consider as an example that your data model could contain a Person entity. This business entity has several attributes including Address. This attribute has one main reusable type, AddressType. Talend Studio enables you to create inheritance reusable types that return a collection of new elements while inheriting the attributes from the AddressType in order to return one address format specific to Europe and another specific to the USA. So in this example, the Europe and US address types both extend the generic address type.

Before you begin

You have already created the data model and business entities in the data model. For further information, see Creating a data model and Creating business entities in a data model respectively.

About this task

To create inheritance and polymorphism for attributes, do the following:

Procedure

  1. Expand the business entity you created and the group type in succession and right-click the default Primary Key record then select Edit Element from the contextual menu.

    A dialog box pops up.

  2. Change the name to Id and then click OK to close the dialog box.
  3. Right-click Id, select Add Element (after) from the contextual menu, and then select Add string Element.

    Add Firstname, Lastname, DOB, PostalCode, Resolution and Address as attributes of the Person entity as outlined in Adding attributes to the business entity.

    Your data model should look like the one shown below.

  4. Right-click Address and select from the list Change to a complex type.

    A dialog box pops up.

    The complex type enables you to define a complete structure, as you need for an address, and not only a single element.

  5. Enter a name for the complex type and then select the Sequence option in order to list the address elements in the defined order.

    Click OK to validate your changes and close the dialog box.

    The complex type is listed under the Address attribute and also in the Data Model Types panel.

  6. You now need to create the elements in your address:
    Expand AddressType and rename the by-default subelement to Line1.
  7. Right-click Line1, select Add Element (after) from the contextual menu, and then select Add string Element. Add two other elements to the address: Line2 and City.

    The elements in the address types are now created below the Address attribute and also in the Data Model Types panel.

    Now you need to create the inheritance type(s) for the US and EU addresses. The EUAddressType extends the main AddressType by adding a postal code and a country, whereas theUSAddressType extends the main AddressType by adding a zip code and a state.

  8. Right-click in the Data Model Types panel and select Create a Complex Type.

    A dialog box pops up.

  9. Enter a name for this inheritance type, EUAddressType in this example.
    From the complex type list, select the main type from which you want to inherit existing elements, AddressType in this example.
  10. Click OK to validate your changes and close the dialog box.

    The inheritance type is created in the Data Model Types panel with a by-default subelement element.

    This inheritance type derives the three elements in the main AddressType. You can now add elements specific to this type in order to have a complete address format for Europe.

  11. Change subelement to PostalCode and then add a Country element.

    Follow the same steps in the Data Model Types panel to create another inheritance type that you call USAddressType that inherits the main AddressType. Add a ZipCode element and a State element in order to complete the address format for the USA.

  12. Save your data model.

What to do next

You have achieved inheritance here at the attribute level by extending an existing type (AddressType) to add specific elements to complete the US and EU addresses while inheriting the attributes from the main address type.

Polymorphism is also achieved when, at runtime, the actual address instance of a person can be an EUAddressType or a USAddressType. When authorized business users browse master data records through Talend MDM Web UI, the concrete address type for the open record (person) is automatically selected. Business users will also have in an open record a list for the Address attribute. From this list they can choose any of the complex address types you defined in Talend Studio.

By default, the list displays the code of the types, but Talend Studio also lets you define labels for the complex types so that they can have labels in different languages and become more understandable as displayed in the list.

You can define labels either in the Data Model Types panel or in the source XML schema of the data model.

Note: You can set the order in which you want to display the extended types within the raw XML schema editor by adding the annotation manually. Doing so via the graphical user interface is not currently supported.

To define labels for complex types in the Data Model Types panel, proceed as follows:

  1. Open the data model where you have defined the complex types.

  2. Right-click the complex type for which you want to set labels and then select Set the labels.
  3. The Set the Labels dialog box pops up.

    From the list to the left, select a language and then enter a label corresponding to the selected language in the field to the right.

  4. Click the [+] button in the upper right corner to add the defined label to the Language/Label list.

  5. Click OK to validate your changes and close the dialog box.

    The labels will be displayed under the Annotations node of the complex type.

When authorized business users browse master data records in the updated data model through Talend MDM Web UI, the list for the Address attribute shows the complex type labels you defined in the annotations.

To define labels for the complex types in the source XML schema of the data model, proceed as follows:

  1. Open the data model where you have defined the complex types.

  2. Click Schema Source at the bottom of the editor to open the source XML schema of the data model.

  3. Browse to the schema corresponding to the extended complex type to which you want to add a label and which you want to display in a certain order for the business user.

  4. Inside the root element of the complex type, use the X_Label_<ISO> annotation to add a label for the extended type and the X_Order_Value annotation to define the order of the extended type, as follows:

    ...
    <xsd:complexType name="EUAddressType">
     <xsd:annotation>
      <xsd:appinfo source="X_Label_EN">EU address format</xsd:appinfo>
        <xsd:appinfo source="X_Label_FR">format d'adresse UE</xsd:appinfo>
        <xsd:appinfo source="X_Order_Value">2</xsd:appinfo>
     </xsd:annotation>
      <xsd:complexContent>
        <xsd:extension base="AddressType">
          <xsd:sequence maxOccurs="1" minOccurs="1">
            <xsd:element maxOccurs="1" minOccurs="1" name="PostalCode" type="xsd:string"/>
            <xsd:element maxOccurs="1" minOccurs="1" name="Country" type="xsd:string"/>
          </xsd:sequence>
        </xsd:extension>
     </xsd:complexContent>
    </xsd:complexType>
    <xsd:complexType name="USAddressType">
    <xsd:annotation>
       <xsd:appinfo source="X_Label_EN">US address format</xsd:appinfo>
       <xsd:appinfo source="X_Label_FR">format d'adresse US</xsd:appinfo>
       <xsd:appinfo source="X_Order_Value">1</xsd:appinfo>
    </xsd:annotation>
    <xsd:complexContent>
      <xsd:extension base="AddressType">
        <xsd:sequence maxOccurs="1" minOccurs="1">
        <xsd:element maxOccurs="1" minOccurs="1" name="ZipCode"   type="xsd:string"/>
        <xsd:element maxOccurs="1" minOccurs="1" name="State" type="xsd:string"/>
       </xsd:sequence>
      </xsd:extension>
     </xsd:complexContent>
    </xsd:complexType>
    ...

    In the above schema, you want to name the EUAddressType as EU address format and you want it to be the second in the list of extended types. You want also to name the USAddressType as US address format and you want it to be the first in the list of extended types.

  5. Save your modifications in the model editor.

When authorized business users browse master data records in the updated data model through Talend MDM Web UI, the list for the Address attribute shows the complex type labels you defined in the annotations and the extended types are displayed in the order you defined as well.