Section |
---|
Column |
---|
width | 240px |
---|
On this page:
Looking for a section on this page? Hover your cursor over the Table of Contents icon to the right to quickly navigate this page.
Introduction
The database connector maps between HTTP requests issued from forms to SQL queries executed against your database. The connector enables you to use the database of your choice to save and load your form data. You can create a new database or you can automatically generate forms that connect to your existing database tables to save and load data. The database connector contains working sample databases with some test data. The BIRT (ClassicModels) and myStore querySets are preloaded with in-memory Derby databases. We will use the in-memory databases to successfully complete this tutorial.
...
Download the Database Connector Tutorial project. This project contains all of the example forms used in this tutorial. This download is a project zip file that you can uploaded upload into your user account. See Project Home Page documentation for more information on uploading this project zip file.
...
Cloud customers should verify that the Database Connector is accessible from the frevvo servers. Cloud customers should replace <localhost:8082> with <app.frevvo.com> in the Business Rule and Document Action URL examples below.
Define a Queryset
The Typically, the first step in connecting your form/workflow to your database is to define a queryset and give it a name - BIRT in the configuration.xml file. Since we are using the built-in configuration.xml file, you do not have to do this. Here is an example of the BIRT queryset and the allCustomers query that is described below.
...
The database connector can simultaneously connect to multiple databases; each must be defined in its own queryset. The queryset consists of a resource definition and any number of named queries. The resource definition is configured in the dbconnector.properties file. You will need to modify it as required: change localhost to the name of the machine running MySQL, specify the MySQL driver, change the database user name and password.
Next you would define a simple query named 'allCustomers' in the configuration.xml file. Queries can contain four SQL statements (create, retrieve, update, delete). In this example, our query contains just one SQL statement to get the list of customers. Here is an example of the BIRT queryset and the allCustomers query that is described above. Learn more about this process in the Defining SQL Queries documentation.
Code Block |
---|
<dbconnector>
<queryset name="BIRT">
<query name="allCustomers" autocreate="true">
<retrieve>
<statement>
SELECT "customerNumber","customerName" from "Customers" order by "customerName"
</statement>
</retrieve>
</query>
</queryset>
</dbconnector> |
Mapping HTTP requests to SQL queries
The database connector can simultaneously connect to multiple databases; each must be defined in its own queryset. The queryset consists of a resource definition and any number of named queries. The resource definition is shown above for MySQL. It is configured in the dbconnector.properties file. You will need to modify it as required: change localhost to the name of the machine running MySQL, specify the MySQL driver, change the database user name and password.
Next we define a simple query named 'allCustomers' in the configuration.xml file. Queries can contain four SQL statements (create, retrieve, update, delete). In this example, our query contains just one SQL statement to get the list of customers.
Mapping HTTP requests to SQL queries
The database connector maps between HTTP requests issued from and SQL queries. It is important to understand how HTTP URLs map to SQL queries in the database connector. The above query returns a resultset that contains data as shown below:
forms work with XML or JSON data. You can test the above query using the maps between HTTP requests issued from and SQL queries. It is important to understand how HTTP URLs map to SQL queries in the database connector. The above query returns a resultSet that contains data as shown below:
forms work with XML or JSON data. You can test the above query using the URL: http://localhost:8082/database/BIRT/allCustomers. This should return an XML document with customerNumber and customerName for each customer as shown below. The DB connector has converted the SQL data into a format that is usable by forms.
Let's take a closer look at this URL.
- The 'localhost:8082' points to the database connector inside the tomcat servlet container on localhost and listening on port 8082.
- 'database' refers to the context path for the database connector web application running inside Tomcat.
- 'BIRT' refers to the name of the queryset defined above.
- 'allCustomers' is the name of the query.
- Since we are issuing an HTTP GET from the browser, this maps to the <retrieve> SQL.
- _mediaType=xml tells the connector to return an XML document. This is the default; if you leave out a _mediaType, the connector will return XML. When the HTTP GET is issued, the connector executes the <retrieve> SQL for the 'allCustomers' query in the 'BIRT' queryset using the resource definition to connect to the appropriate database. The data in the resultset is converted to XML and returned.
The connector is also capable of returning JSON data. Try the URL:http://localhost:80818082/database/BIRT/allCustomers. This should return an XML document with customerNumber and customerName for each customer as shown below. The DB connector has converted the SQL data into a format that is usable by forms.
Let's take a closer look at this URL.
- The 'localhost:8082' points to the database connector inside the tomcat servlet container on localhost and listening on port 8082.
- 'database' refers to the context path for the database connector web application running inside Tomcat.
- 'BIRT' refers to the name of the queryset defined above.
- 'allCustomers' is the name of the query.
- Since we are issuing an HTTP GET from the browser, this maps to the <retrieve> SQL.
- _mediaType=xml tells the connector to return an XML document. This is the default; if you leave out a _mediaType, the connector will return XML. When the HTTP GET is issued, the connector executes the <retrieve> SQL for the 'allCustomers' query in the 'BIRT' queryset using the resource definition to connect to the appropriate database. The data in the resultset is converted to XML and returned.
The connector is also capable of returning JSON data. Try the URL: http://localhost:8082/database/BIRT/allCustomers?_mediaType=json the connector will return the same data as JSON. forms can use JSON data in business rules for dynamic behavior.
We will use these features in the examples below.
Note |
---|
It is very helpful to test your queries as shown above by entering the query URL directly into your web browser and verify that the data returned to your browser as a web page is as you expect. Note however that browsers often cache web pages. If you edit your configuration.xml SQL query and reload/refresh the URL in your browser you may NOT get the updated results due to your browser's caching. Avoid this caching issue by always opening a new browser tab to retest an updated query. |
Generating xml schema
If you prefer, you can generate the schema for the entire BIRT queryset before you begin. This eliminates the need to create a schema one by one for each query as specified in the instructions. The queryset schema can be uploaded to your project one time and then you can add the relevant schema elements to your forms/workflows as you need them. Browse this URL to get the schema, save the schema as an .xsd file in your file system then upload the schema to Live Forms.
Code Block |
---|
http://<server>:<port>/database/BIRT/schema |
Example 1: View customer list
Our first example is to create a form with a dropdown control that is dynamically populated with the above list of customers from the database. When the user selects a customer by name, the value of the dropdown control should be set to the customer number.
The steps are as follows:
Define a query
...
?_mediaType=json the connector will return the same data as JSON. forms can use JSON data in business rules for dynamic behavior.
We will use these features in the examples below.
Note |
---|
It is very helpful to test your queries as shown above by entering the query URL directly into your web browser and verify that the data returned to your browser as a web page is as you expect. Note however that browsers often cache web pages. If you edit your configuration.xml SQL query and reload/refresh the URL in your browser you may NOT get the updated results due to your browser's caching. Avoid this caching issue by always opening a new browser tab to retest an updated query. |
Generating xml schema
If you prefer, you can generate the schema for the entire BIRT queryset before you begin. This eliminates the need to create a schema one by one for each query as specified in the instructions. The queryset schema can be uploaded to your project one time and then you can add the relevant schema elements to your forms/workflows as you need them. Browse this URL to get the schema, save the schema as an .xsd file in your file system then upload the schema to Live Forms.
Code Block |
---|
http://<server>:<port>/database/BIRT/schema |
Example 1: View customer list
Tip |
---|
The example below shows you how to add dynamic dropdown options using a business rule to connect to your database and define the options. However, the Dynamic Options - No Coding Needed feature allows you to do this without a business rule, but simply by selecting Web Service from the Options Src property of your dropdown control and defining your query url and options there. Please see the documentation or watch this 5 minute video to use this recommended method. |
Our first example is to create a form with a dropdown control that is dynamically populated with the above list of customers from the database. When the user selects a customer by name, the value of the dropdown control should be set to the customer number.
The steps are as follows:
Define a query
We will use the query 'allCustomers' in the configuration.xml file.
Code Block |
---|
<query name="allCustomers" autocreate="true"> <retrieve> <retrieve> <statement> <statement> SELECT "customerNumber","customerName" from "Customers" order by "customerName" </statement> </retrieve> </query> |
Create a form
- Create a new form. Accept the defaults by clicking the Finish button. The Form Designer will appear.
- Drag a Dropdown control from the Palette and drop it into the form.
- In the properties pane on the left, change the Label as desired.
- In the properties pane, change the Name to sc.
...
- if (form.load) - this implies that the rule will execute when the form first loads.
eval ('x=' + http.get('http://localhost:8082/database/BIRT/allCustomers')) - this causes an HTTP GET to be issued to the indicated URL. When invoked in the context of a rule, the media type is automatically set to JSON. As we saw above, the database connector will return the list of customers as a JSON object.
Info For the Standalone bundle with Cloud, replace localhost:8082 with "app.frevvo.com" in the URL.UR
- The rest of the rule is just JavaScript. We create an array of options in the format value=label (e.g. 242=Alpha Cognac) from the data returned in the JSON.
- Finally, we set the options of the dropdown to the array of options using the name we chose earlier (sc).
...
This step generates controls that are bound to the createOrderDetail data source by . You can render the controls as you wish but will always generate an XML document conforming to the XML schema above. Here's what our form looks like:
Create/Update business rules
...
- Click + to create a new rule and the Edit button to open the rule.
- Set the Name to 'Order Line Item Added'.
- In the Rule text area: copy and paste the following:
Code Block |
---|
if (OrderLines.itemAdded) { var ix = OrderLines.itemIndex; pc[ix].options = pc[0].options; oln[ix].value = 0; olonum[ix].value = 0; } |
...
- Click on the Form Properties icon in the toolbar at the top of the form.
- Select the Document Actions tab and the Send Data sub-tab.
- Select Manually set document URIs.
- You should see the Document name 'createOrder', which is the data source we are interested in.
- Set the Write URL to: http://localhost:8082/database/BIRT/createOrderDetail
- Leave the Read method empty.
- Set the Write method to POST.
- Click the Submit button.
When the form is submitted, will issue an HTTP POST request to the above URL sending the createOrderDetail XML document in the payload. The database connector will map this to the <create> operation of the createOrderDetail query that we defined above, will use the data in the XML document to resolve the query. Since there are multiple line items, the query will be executed once for each Order line item thereby inserting the multiple line items for an Order.
...
- Click the Test button.
- Select a customer from the drop down.
- Enter at least one line item.
- Submit the form.
- Verify that the order was successfully inserted.
- Use the form from Example 3 by clicking the Test button.
- Select the customer.
- You should see your order. Select the order.
- You should see Line Item details appear below.
Example 6: Update existing records
...
- Click on the Form Properties icon in the toolbar at the top of the form.
- Select the Document Actions tab and the Send Data sub-tab.
- Select Manually set document URIs.
- You should see the Document name 'customerCreditLimit', which is the data source we are interested in.
- Set the Read and Write URLs to: http://localhost:8082/database/BIRT/customerCreditLimit?cnum={sc}
- Set the Read method to GET.
- Set the Write method to PUT.
- Click the Finish buttonSubmit.
This step indicates to that the 'customerCreditLimit' document is linked to the above URL via the READ method GET and the WRITE METHOD PUT. will issue an HTTP GET request to the above URL and if an XML document conforming to the schema above is returned, will automatically initialize controls in the form using that document. The URL itself is a dynamic URL and has a parameter indicated by {sc}. When the value of the control named 'sc' changes (which will happen when the user selects a customer), will automatically issue an HTTP GET to the above URL using the new customer number and will dynamically update the values of the credit Limit and cnum controls without refreshing the form.
When the form is submitted, will issue an HTTP PUT request to the above URL sending the customerCreditLimit XML document in the payload. The database connector will map this to the <update> operation of the customerCreditLimit query that we defined above, will use the data in the XML document to resolve the query (replace {creditLimit} and {cnum} in the query with the actual values from the XML document) and perform the UPDATE.
...