Data Adaptor - Sample Code (Autosave with Derby)
The sample Autosave scenario in this example does the following:
- During the investigation of a goal, as the user answers questions the DataAdaptor 'save' function is called to save after each screen
- After completing the investigation of the goal, the DataAdaptor 'save' function is called to save
- An ID is automatically created for the user when he/she starts the Web Determinations Interview
- The DataAdaptor 'save' function saves all the attributes that has an instance value provided by the user
The sample code demonstrates
- How to setup an Event Handler to handle events (for this scenario, to handle Events fired when the user answers a question, and completing a goal)
- Calling the DataAdaptor functions from an Event Handler
- The Event Handler need to have access to the SessionContext (Platform Events) to provide the necessary input arguments for DataAdaptor save() (case ID and InterviewSession)
- Using an Event Handler and Data Adaptor together to implement 'autosaving' functionality during a Web Determinations Interview
The sample code provides functionality for requirements such as the need to be able to autosave based on triggers.
Setup
This sample code needs the following to run:
- DerbyAutoSave plugin
- ExtFrameworkAutoSave (rulebase)
- DERBY SQL database (examples\web-determinations\auto-save\src\create_db.sql
About the DERBY SQL database schema
The DERBY SQL database is created by using create_db.sql which is located in examples\web-determinations\auto-save\src.
AUTOSAVE |
ID |
age |
income |
old |
low_income |
old_low_income |
eligible |
About the Plugins:
- The Event Handler that triggers the Data Adaptor to save is the AutosaveTrigger,
- The AutosaverTrigger handles the OnGetScreenEvent and OnInvestigationEndedEvent (both Events are a Platform event and thus has access to SessionContext - see Events and Event Handlers)
- The AutosaverTrigger handles both Events by implementing the EventHandler interface for both events.
- It needs to handle both Events because:
- The OnGetScreenEvent calls the handler for each event. To prevent the handler from calling the Data Adaptor save() outside of an investigation (for example, Summary screen, Data Review), the handler only calls the Data Adaptor save() for OnGetScreenEvents when a goal is currently being investigated
- With the restriction of a goal needing to be investigated, the Data Adaptor save() is not called when the investigation ends, thus failing to save the user's answers in the last Question screen
- The OnInvestigationEndedEvent is useful for this issue - it calls the handler only when an investigation is finished, and thus saving the user's answers in the last question screen
- The Data Adaptor that handles the save functionality is the AutosaveSaver, and it saves onto the DERBY datasource
- The DataAdaptor and Event Handler plugins are configured to work on a rulebase named 'ExtFrameworkAutoSave'.
To setup this scenario:
-
Create the SQL Database using create_db.sql which can be found in examples\web-determinations\auto-save\src
- Copy the rulebase .zip file (ExtFrameworkAutoSave.zip) from examples\rulebases\compiled to the rulebase folder in Web Determinations (for example, <webroot>\WEB-INF\classes\rulebases)
- Copy and install the DerbyAutoSave.jar file (located in examples\web-determinations\auto-save) into Web Determinations; for more information, refer to Create a Plugin
-
Copy the DERBY libraries to the library folder in Web Determinations (for example, <webroot>\WEB-INF\lib)
-
Ensure that DERBY is run in network server mode
- Run a Web Determinations Interview
Note:
If you need to modify the database connection details or you need class files for your specific database implementation, then you will need to edit the source code, recompile and JAR the DerbyAutoSave.
How the Autosave works
The autosave functionality happens:
- After every investigation question screen
- After an investigation of a goal is completed
When the above events are met, the AutosaveTrigger Event Handler calls the save() method of the AutosaveSaver Data Adaptor.
The AutosaveSaver simply saves all of the attributes into the Autosave table in the DERBY database.
Table AUTOSAVE
ID |
age |
income |
old |
low_income |
old_low_income |
eligible |
VARCHAR(255) |
DOUBLE |
DOUBLE |
INT |
INT |
INT |
INT |
Table 'Autosave' view - start of investigation, to completing an investigation
While going through the Interview, when the user starts an investigation of a goal and the current Case ID is empty - a new Case ID will be created. You can see this in the database; for example:
ID |
age |
income |
old |
low_income |
old_low_income |
eligible |
8821b2f8-76e3-4762-a4a5-af8c10aac521 |
NULL
|
NULL |
NULL |
NULL |
NULL |
NULL |
After the Case ID is generated, as the user answers the questions in the investigation (for example, age=31 below), answers and inferenced data will be added onto the same row item in the database (for example, old = Yes, eligible = Yes).
ID |
age |
income |
old |
low_income |
old_low_income |
eligible |
8821b2f8-76e3-4762-a4a5-af8c10aac521 |
31
|
NULL |
Yes |
NULL |
NULL |
Yes |
Source Code
To view the source code for the DerbyAutoSave sample, refer to examples\web-determinations\auto-save in the Java runtime zip file.