Introduction
This example takes the very simple flow element described in the simple flow element example, and adds a JavaScript property to use client-side evidence.
Instead of using evidence which is already available, as the simple flow element example did, this example will send JavaScript to the a client to get the evidence.
Download Example
The source code used in this example is available here:
Dependencies
The flow element will need a dependency on the Pipeline core package
FiftyOne.Pipeline.Core
NuGet package. pipeline.core
Maven package from com.51degrees
. To include this, add the following to the <dependencies>
section of the project's pom.xml
:
fiftyone.pipeline.core
NPM package to the package.json, and required it in the source with require("fiftyone.pipeline.core")
. Data
The element data being added to the flow data by this flow element is a star sign. So this should have its own 'getter' in its element data. The additional property containing the JavaScript to get the date of birth is also added using the appropriate type.
In this example, values for star sign and JavaScript are populated. So an interface IStarSign
will extend IElementData
to add a 'getter' for it.
Now the internal implementation of it will implement this 'getter' and add a 'setter' for the flow element to use.
In this example, values for star sign and JavaScript are populated. So an interface StarSignData
will extend ElementData
to add a 'getter' for it.
Now the internal implementation of it will implement this 'getter' and add a 'setter' for the flow element to use.
Note that this concrete implementation of StarSignData
sits in the same package as the flow element, not the StarSignData
interface, as it only needs to be accessible by the flow element.
elementDataDictionary
can be used. JavaScript
The client-side JavaScript for this example will be fairly simple. It will create a popup asking the user to enter their date of birth. It will then store the date of birth in a cookie named 'date-of-birth' and reload the page with the new cookie present.
The JavaScript to do this looks like:
Flow Element
Now the actual flow element needs to be implemented. For this, the flow element's base class can be used which deals with most of the logic. The element properties, evidence keys and the processing are all that need implementing.
FlowElementBase
(and partially implements IFlowElement
). This has the type arguments of IStarSignData
- the interface extending element data which will be added to the flow data, and IElementPropertyMetaData
- as we only need the standard metadata for element properties.
This needs a constructor matching the FlowElementBase
class. So it takes a logger, and an element data factory which will be used to construct an IStarSignData
:
The Init
method in this example will simply initialize a list of star signs with the start and end dates of each star sign and add each to a list of a new class named StarSign
which has the following simple implementation:
Note that the year of the start and end date are both set to 1, as the year should be ignored, but the year 0 cannot be used in a DateTime
.
The new Init
method looks like this:
Now the abstract methods can be implemented to create a functional flow element.
First let's define a class which extends FlowElementBase
(which partially implements FlowElement
). This has the type arguments of StarSignData
- the interface extending element data which will be added to the flow data, and ElementPropertyMetaData
- as we only need the standard metadata for element properties.
This needs a constructor matching the FlowElementBase
class. So it takes a logger, and an element data factory which will be used to construct a StarSignData
:
The init
method in this example will simply initialize a list of star signs with the start and end dates of each star sign and add each to a list of a new class named StarSign
which has the following simple implementation:
Note that the year of the start and end date are both set to 0, as the year should be ignored.
The new init
method looks like this:
Now the abstract methods can be implemented to create a functional flow element.
First let's define a class which extends flowElement
.
This needs a constructor matching the flowElement
class:
Now the abstract methods can be implemented to create a functional flow element.
Builder
Now the flow element needs one final thing, an element builder to construct it. This only needs to provide the flow element with a logger and an element data factory - as this example has no extra configuration options.
Build
. This returns a new flow element which the element builder provides with a logger and an element data factory.
The element data factory is implemented in the element builder class to make use of the same logger factory.
build
. This returns a new flow element which the element builder provides with a logger and an element data factory.
The element data factory is implemented in the element builder class to make use of the same logger factory.
Usage
As this example gathers its evidence using client-side JavaScript, it will need to be run in a web environment. Using a web integration will mean that the flow element can be added, and the client-side JavaScript automatically run.
Then a simple view can be added which includes '51Degrees.core.js' to run the JavaScript, and displays the message which is passed from the controller:
The message is constructed in the controller in the same way as in the previous example:
Then a simple view can be added which includes '51Degrees.core.js' to run the JavaScript, and displays the star sign which is passed from the controller:
The message is constructed in the controller in the same way as in the previous example:
Then a simple view can be added which includes the JavaScript to run, and displays the star sign which is passed from the controller:
The message is constructed and returned: