Creating Overview Pages
Introduction
Use view entities to show data across multiple associated entities.
Use Case
For this purpose of this use case, the following domain model is used:
In this scenario, you will create an overview page that lists each product in a data grid, including information about its category and supplier. With view entities, you do not have to manage associations when showing the data in a data grid. This means that all fields are filterable and sortable, which allows for higher performance and flexibility.
Create a Data Grid
Create a view entity that combines only the relevant attributes of the entities Product, Supplier, and Category. To do this, follow these steps:
-
Open your domain model and add a new view entity.
-
Name the view entity ProductOverviewVE.
-
Add the following query to the OQL editor:
SELECT p.ProductId as ProductId, p.ProductName as ProductName, p.QuantityPerUnit as QuantityPerUnit, p.Discontinued as Discontinued, s.CompanyName as Supplier, c.CategoryName as Category FROM Shop.Product as p JOIN p/Shop.Product_Supplier/Shop.Supplier as s JOIN p/Shop.Product_Category/Shop.Category as c
-
Click OK. The view entity is added to your domain model.
-
Generate an overview page by right-clicking the view entity > Generate overview pages.
-
Add the new overview page to the navigation.
-
Run your app locally, then click View App. You should see the data grid populated with the information that was previously added.
Alternative to Calculated Attributes
You can use view entities to search and sort items, as well as hold the calculated attribute (or Total Value). This is only calculated once, unlike a non-persistable entity, where it is calculated each time the object is accessed.
For example, the OrderLine entity represents a single entry in an order. It has the following attributes:
UnitPrice
Quantity
Discount
Get Total Value
Suppose you want to get the total value of each order line, which is given by the formula Total = UnitPrice * Quantity * (1 – Discount)
. To do this, follow these steps:
-
Create a view entity and name it OrderLineWithTotalVE.
-
Add the following query to the OQL editor:
SELECT ol.OrderLineId as OrderLineId, ol.Quantity as Quantity, ol.UnitPrice as UnitPrice, ol.Discount as Discount, (ol.Quantity * ol.UnitPrice * (1 - ol.Discount)) as Total FROM Shop.OrderLine ol
-
Generate an overview page by right-clicking the view entity > Generate overview pages.
-
Add the new overview page to the navigation.
-
Run your app locally, then click View App. Use this view entity in a data grid to show the total value.
Join OrderLine and Order Entities
You can calculate the total value of an order by joining the OrderLine
and Order
tables. To do this, follow the steps below:
-
Create a new view entity and name it OrderWithTotalValueVE.
-
Add the following query to the OQL editor:
SELECT o.OrderId as OrderId, o.OrderDate as OrderDate, o.RequiredDate as RequiredDate, o.ShippedDate as ShippedDate, SUM(ol.UnitPrice * ol.Quantity * (1 - ol.Discount)) as TotalOrderValue, SUM(ol.Quantity) as TotalProductCount, COUNT(*) as UniqueProductCount FROM Shop."Order" as o JOIN o/Shop.OrderLine_Order/Shop.OrderLine as ol GROUP BY o.OrderId, o.OrderDate, o.RequiredDate, o.ShippedDate
This results in a view entity that shows the total value of every order.
Shop.”Order”
. This is because Order
is a reserved keyword in OQL. To avoid ambiguity, quotation marks are put around the word.
- Generate an overview page by right-clicking the view entity > Generate overview pages.
- Add the new overview page to the navigation.
- Run your app locally, then click View App. This results in a view entity that shows the total value of every order.
Update Underlying Persistent Entities
On the Product overview page above, there is no button to add or modify a product. This functionality can be added to a view entity to update its corresponding persistable entity object.
-
Create a microflow and name it ACT_UpdateProduct.
-
Add a parameter and in the entity field and select ProductOverviewVE.
-
Add a retrieve activity. In this activity, retrieve a Product object from the database. Configure the activity with the following details:
-
Use the following XPath constraint:
[(ProductId = $ProductOverviewVE/ProductId)]
-
In the Options field, set Range to First
-
-
Add a Change Object activity. Configure the activity by adding the attributes of
Product
to reflect those ofProductOverviewVE
-
Open the ProductOverviewVE_NewEdit page that was generated by ProductOverviewVE.
-
Remove the Product ID, Supplier, and Category fields.
-
Double-click the Save button and in the On click field, select Call a microflow.
-
Under Microflow, click Select > ACT_UpdateProduct.
-
Click OK.
-
Open the ProductOverviewVE_Overview page.
-
Add a button to the grid and link it to the ProductOverviewVE_NewEdit page.
Once these steps are complete, run your app locally and test the functionality.
Update Associated Product Category and Supplier
Add the capability to update a product’s associated category and supplier. To do this, follow these steps:
-
Open your domain model and double-click ProductOverviewVE.
-
Add
CategoryId
andSupplierId
by using the following query in the OQL editor:SELECT p.ProductId as ProductId, p.ProductName as ProductName, p.QuantityPerUnit as QuantityPerUnit, p.Discontinued as Discontinued, s.CompanyName as Supplier, c.CategoryName as Category, s.SupplierId as SupplierId, c.CategoryId as CategoryId FROM Shop.Product as p JOIN p/Shop.Product_Supplier/Shop.Supplier as s JOIN p/Shop.Product_Category/Shop.Category as c
-
Create two more view entities that retrieve ID and names from the Supplier and Category tables and name them SupplierNamesVE and CategoryNamesVE. Add the following queries into the OQL editor. Add one to each entity:
SELECT s.SupplierId as SupplierId, s.CompanyName as SupplierName FROM Shop.Supplier s
SELECT c.CategoryId as CategoryId, c.CategoryName as CategoryName FROM Shop.Category c
-
On the ProductOverviewVE_NewEdit page, add a combo box inside the data view. Include the following information:
- In the Data source field, select Database
- Under Selectable objects, click Edit > Entity (path) > CategoryNamesVE > Select
- Under Caption, select CategoryNames
- Under Value, select CategoryId
- Under Target attribute, select Category
-
Add another combo box and repeat the above steps for the SupplierNameVE entity.
The final data view should look like this:
Update Product Microflow
Once the Product page is complete, update the related microflow. To do this, follow the steps below:
-
Open the ACT_UpdateProduct microflow.
-
Add a Retrieve activity after the existing Retrieve product activity.
-
Retrieve a Category object from the database. Configure the activity using the following details:
-
Use the following XPath constraint:
[CategoryId = $ProductOverviewVE/CategoryId]
-
In the Range field, select First
-
-
Add another Retrieve activity to retrieve the Supplier object. Configure the activity using the following details:
-
Use the following XPath constraint:
[SupplierId = $ProductOverviewVE/SupplierId]
-
In the Range field, select First
-
-
In the existing Change Product activity, click New and add the Category and Supplier associations and set them to their corresponding objects.
When you run your app, you should now be able to update the product’s category and supplier.
Add a New Product
You can use a view entity to add a new product into the existing database. Follow the steps below:
-
Create a new microflow and name it ACT_CreateProduct
-
Add a Create object activity to create a Product (the persistable entity) object. Leave all the attributes blank.
-
Check the Commit checkbox.
-
Place another Retrieve object activity after the previous activity.
-
Retrieve
ProductOverviewVE
that corresponds to the newProduct
object. Configure it with the following details:-
Use the following XPath constraint:
[(ProductId = $NewProduct/ProductId)]
-
In the Range field, select First
-
-
Add a Show page activity and set it to open the Edit Product page. Use
NewProductVE
as the page parameter. -
Open the ProductOverviewVE_Overview page and add a new button named New.
-
In the new button, under On click, select CreateProduct.