31
CenitHub Chapter 3 Translators

CenitHub Presentations | 3- Translator

Embed Size (px)

Citation preview

Page 1: CenitHub Presentations | 3- Translator

CenitHubChapter 3

Translators

Page 2: CenitHub Presentations | 3- Translator

Translators

A translator defines a logic for data manipulation.

Data manipulation can occurs in the following scenarios:

₋ Importing outside data into Cenit

₋ Updating data already stored in Cenit

₋ Converting data already stored in Cenit

₋ Exporting data outside Cenit

Page 3: CenitHub Presentations | 3- Translator

TranslatorsTranslate input data

into data types records

Convert records between data

types

Update data type records

Translate data type records into

outgoing data

Page 4: CenitHub Presentations | 3- Translator

Import Translators

Import translators create data type records from outside data.

The target data type defines the type of the records to be created.

If no target data type is defined then the translator is supposed to be able to import data into any data type.

The logic of an import translator is described in ruby style

Page 5: CenitHub Presentations | 3- Translator

Import Translators: target data type

Import translators create data type records from outside data.

The target data type defines the type of the records to be created.

If no target data type is defined then the translator is supposed to be able to import data into any data type.

The logic of an import translator is described in ruby style

Page 6: CenitHub Presentations | 3- Translator

Import Translators: ruby style

Import translators create data type records from outside data.

The target data type defines the type of the records to be created.

If no target data type is defined then the translator is supposed to be able to import data into any data type.

The logic of an import translator is described in ruby style

Page 7: CenitHub Presentations | 3- Translator

Import Translators: ruby style

The translator transformation is written in a DSL based on the Ruby Programming Language.

The variables data and target_data_type are available and represent the outside data and the target data type respectively.

A simple JSON importer can be defined by the following transformation:

Page 8: CenitHub Presentations | 3- Translator

Import Translators: ruby style

A little more of transformation code allows to parse the outside data and to create multiple records:

Page 9: CenitHub Presentations | 3- Translator

Import Translators: ruby style

There are several methods available on the target data type object that can be invoked to create records. They all have the following pattern:

(create|new)_from_(json|xml|edi)[!]

Example: create_from_xml, new_from_edi

The new methods does not persist the created record an a further invocation of a save method is needed.

The create methods attempt to persist the records, halting on error only if the method name ends with the exclamation symbol.

Page 10: CenitHub Presentations | 3- Translator

Import Translators: ruby style

The following transformation parse a data using Nokogiri and creates one or multiple records from XML document elements:

Page 11: CenitHub Presentations | 3- Translator

Export Translators

Export translators format data type records into data going outside Cenit.

The source data type defines the type of the records to be formatted.

If no source data type is defined then the translator is supposed to be able to format records of any data type.

A MIME type can be optionally defined.

Page 12: CenitHub Presentations | 3- Translator

Export Translators

Export translators format data type records into data going outside Cenit.

The source data type defines the type of the records to be formatted.

If no source data type is defined then the translator is supposed to be able to format records of any data type.

A MIME type can be optionally defined.

Page 13: CenitHub Presentations | 3- Translator

Export Translators

Export translators format data type records into data going outside Cenit.

The source data type defines the type of the records to be formatted.

If no source data type is defined then the translator is supposed to be able to format records of any data type.

A MIME type can be optionally defined.

Page 14: CenitHub Presentations | 3- Translator

Export Translators: ruby style

The translator transformation can be written in ruby style.

Depending on the translator having the option bulk source, the variable source or sources is available representing a single record or an enumeration of records to be formatted.

A simple non bulkable JSON exporter can be defined by the following transformation:

Page 15: CenitHub Presentations | 3- Translator

Export Translators: ruby style

If the translator is bulkable a little more of transformation code is needed to format all the source records into a single JSON data:

Note: the result of the translation execution is the value of the latest evaluated expression.

Page 16: CenitHub Presentations | 3- Translator

Export Translators: ruby style

There are a set of predefined methods available on record objects that can be used for basic formats:

to_(json|xml|edi)

Every formatter method can receive options, for example:

- source.to_json(pretty: true)

- source.to_xml(with_blanks: true)

- source.to_edi(field_separator: ‘+’)

Page 17: CenitHub Presentations | 3- Translator

Export Translators: ruby style

The variable source_data_type is a reference to the translator source data type and for facility a method to_xml_array have been added to records enumeration, so a transformation for bulkable XML exporter can be written as following:

Page 18: CenitHub Presentations | 3- Translator

Export Translators: XSLT style

The logic of an export translator can be described as an XML Stylesheet transformation.

XSLT style translators are no bulkable.

Cenit convert the source record to XML format if necessary and the applies the XSLT transformation.

Page 19: CenitHub Presentations | 3- Translator

Export Translators: XSLT style

The following XSLT transformation changes the value of every attribute with name email by [email protected]:

Page 20: CenitHub Presentations | 3- Translator

Update Translators

Update translators update data type records.

The target data type defines the type of the records to be updated.

If no target data type is defined then the translator is supposed to be able to update records of any data type.

The logic of an import translator is described in ruby style.

Page 21: CenitHub Presentations | 3- Translator

Update Translators

Update translators update data type records.

The target data type defines the type of the records to be updated.

If no target data type is defined then the translator is supposed to be able to update records of any data type.

The logic of an import translator is described in ruby style.

Page 22: CenitHub Presentations | 3- Translator

Update Translators

Update translators update data type records.

The target data type defines the type of the records to be updated.

If no target data type is defined then the translator is supposed to be able to update records of any data type.

The logic of an import translator is described in ruby style.

Page 23: CenitHub Presentations | 3- Translator

Update Translators: ruby style

Update translators are non bulkable so they are applied to a single target record which is available through the variable target:

Page 24: CenitHub Presentations | 3- Translator

Conversion Translators

Conversion translators transform records into others.

The source and target data types defines the records conversion types.

The logic of an import translator can be described in several format: ruby, XSLT and chain.

Page 25: CenitHub Presentations | 3- Translator

Conversion Translators

Conversion translators transform records into others.

The source and target data types defines the records conversion types.

The logic of an import translator can be described in several format: ruby, XSLT and chain.

Page 26: CenitHub Presentations | 3- Translator

Conversion Translators

Conversion translators transform records into others.

The source and target data types defines the records types.

The logic of an import translator can be described in several format: ruby, XSLT and chain.

Page 27: CenitHub Presentations | 3- Translator

Conversion Translators: ruby style

Conversion translators are non bulkable so they convert a record at the time. The source and target records are available through the variables target and source. Converting a costumer from a message would be as simple as:

Page 28: CenitHub Presentations | 3- Translator

Conversion Translators: XSLT style

Even if records are not stored in XML format an XSLT transformation is possible for Cenit by the following steps:

1. Format the source record into XML if necessary.

2. Applies the XSLT transformation to the XML formatted record.

3. Create a target data from the transformed XML document.

Page 29: CenitHub Presentations | 3- Translator

Conversion Translators: chain style

Chain style convert records by concatenating two conversion translator:

⁻ The source exporter: convert the source record to an intermediate data type.

⁻ The target importer: convert the intermediate record to the target data type.

Page 30: CenitHub Presentations | 3- Translator

Conversion Translators: chain style

Chain style convert records by concatenating two conversion translator:

⁻ The source exporter: convert the source record to an intermediate data type.

⁻ The target importer: convert the intermediate record to the target data type.

Page 31: CenitHub Presentations | 3- Translator

CenitHubChapter 3

Translators