You will use MDriven Designer to trace and correct model errors, give an association class stable navigation names, and generate an OpenDocument report from a CarTransferOwnershipDocument.
This chapter continues from Training:Bootcamp:Chapter 9. Complete that chapter first: this chapter uses its Car ownership transaction model, including the CarTransferOwnershipDocument association class.
What you will build
By the end of this chapter, you will have:
- Practised locating model errors from the Model Check and Report Errors Tool.
- Renamed the navigation roles for the HistoricOwnership association class and updated affected OCL expressions.
- Created ProperCarView, a read-oriented Car ViewModel with a grid of ownership-transfer documents.
- Created an OpenDocument Text (ODT) template and generated a report whose file name includes the car registration number.
Validate and correct model errors
MDriven Designer validates expressions as you edit and when you save. Use deliberate, temporary errors to learn how to find the model element that contains an invalid expression.
Test an invalid state entry action
- Navigate to the Car state diagram.
- Select the entry action of the InOwnshipTransaction state.
- In its expression, temporarily change
CartransferOwnershipDocumentstoCartransferOwnershipDocument. - Observe the editor error. For example, Designer reports that
CartransferOwnershipDocumentis not a member ofCar. - Save the model.
- In the Model Check and Report Errors Tool, select the red error indicator and follow the error link to the expression.
- Restore the correct member name,
CartransferOwnershipDocuments, and save again.
The reported error disappears when the expression again refers to a member that exists on Car.
Test an invalid class-action expression
- Select Car and open its auto diagram.
- Find the class actions, shown with a yellow background.
- Open
99999AutoForms---Car_Trigger_CloseSale. - In Enable Expression, temporarily change
self.CloseSaletoself.CloseSales. - Save the model and use the red error indicator in the Model Check and Report Errors Tool to navigate to the problem.
- Restore
self.CloseSaleand save.
This exercise demonstrates that model checking covers expressions in both state-machine actions and class actions.
Name the HistoricOwnership link roles
A LinkRoleName is the navigation name exposed for an association class at an association end. Changing it changes the name that expressions use to reach that link role.
- Locate the HistoricOwnership link class.
- Select each of its two associations.
- At both association ends, change LinkRoleName from
<Default>toHistoricOwnerships. - Save the model and inspect the reported errors.
- Open each reported expression and replace references to
HistoricOwnershipwithHistoricOwnershipswhere the expression navigates through the renamed role. - Save again and confirm that no errors remain.
For example, an expression that previously navigated through HistoricOwnership must use HistoricOwnerships after the role rename. Do not change class names or unrelated identifiers when correcting these errors.
Create a read-oriented Car ViewModel
A ViewModel defines the data and actions presented in the generated application UI. Create a dedicated view rather than retaining an action that has no ViewModel to display.
- Remove the ShowCar action that does not have a ViewModel to show.
- Create a new ViewModel named ProperCarView.
- Set its Class to Car.
- Select Requires Root.
- Add a class action that opens ProperCarView.
Add the Car fields
Add the following columns to ProperCarView:
- RegistrationNumber
- State
- BrandOfCar
- CarOwner
Set State, BrandOfCar, and CarOwner to static so that this view does not edit them.
Add the transfer-document grid
- Add the multi-link for CarTransferOwnershipDocuments as a grid.
- Add Seller and Buyer columns to the grid.
- Remove the default AsString column.
- Start the web prototype.
- Search for cars and open ProperCarView for a Car that has CarTransferOwnershipDocuments.
Verify that the Car details are shown and that each ownership-transfer document shows its seller and buyer in the grid.
Add the ProduceDocument action
- Add a class action named ProduceDocument.
- Set its ExecuteExpression to:
self.opendocumentreportshow(CarTransferOwnershipDocument.ViewModels.TheTemplateForCarTransferOwnershipDocumentReport)
- Save the model.
At this point, expect an error: the ViewModel TheTemplateForCarTransferOwnershipDocumentReport does not yet exist. Create it in the next section, then verify that this action resolves its ViewModel reference.
Define the report-template ViewModel
- Create a ViewModel named TheTemplateForCarTransferOwnershipDocumentReport.
- Set its Class to CarTransferOwnershipDocument.
- Select Requires Root.
- In the ViewModel tree, right-click the menu and add the columns required by the report.
- Confirm that the ViewModel includes TemplateUrl and ReportFileName columns.
- Return to ProduceDocument and verify that its expression now finds TheTemplateForCarTransferOwnershipDocumentReport.
- In TheTemplateForCarTransferOwnershipDocumentReport, select UsePlacingHints to hide the UI part of this ViewModel.
The report ViewModel supplies data to the document generator; it is not intended to be a user-facing form.
Create and serve the ODT template
The template must be in the model's AssetsTK Content folder so that the running application can retrieve it by URL.
Create the AssetsTK folder structure
- In MDriven Designer, choose FileMenu and then ShowCurrentModelDirectory.
- Check the model file name. This training model uses
Start.modlr. - In the model directory, create a folder named
Start_AssetsTK. - Inside
Start_AssetsTK, create a folder namedContent. - In a word processor, create a new document containing the text
this will be my template. - Save it in ODT format as
Start_AssetsTK/Content/ThisWillBeMyTemplate.odt. - Close the word processor before MDriven Designer accesses the file. An open document can lock the file.
Verify that Turnkey serves the template
- Start the web prototype and note its URL, which begins with
http://localhost:. - In a browser, open
http://localhost:<port>/content/ThisWillBeMyTemplate.odt, replacing<port>with the port used by your prototype. - Confirm that the template is accessible.
- In TheTemplateForCarTransferOwnershipDocumentReport, set TemplateUrl to the string value:
http://localhost:<port>/content/ThisWillBeMyTemplate.odt
The AssetsTK folder name is based on the model file name without .modlr. For example, a model named MyStart.modlr requires MyStart_AssetsTK. Avoid additional dots and special characters in the model file name. If the template URL cannot be downloaded, first check both this folder name and the prototype port.
Open the AssetsTK folder with Turnkey Live Editor
You can use Turnkey Live Editor to open the correct AssetsTK folder rather than locating it manually.
- In the MDriven Designer top menu, select Turnkey Live Editor.
- In the window that opens, select the purple AssetsTK Sync tab on the right.
- Find the folder at the far left, such as
Start_AssetsTKor<model name>_AssetsTK. - Right-click the folder and select Open.
- Create the Content folder and ODT file there, then repeat the template URL test.
Set the generated report name
Set the ReportFileName expression in the template ViewModel to:
'TheSalesReportForCar'+self.Car.RegistrationNumber+'.odt'
Run ProduceDocument and verify that the downloaded document name includes the selected Car's RegistrationNumber, for example TheSalesReportForCarABC123.odt.
Add report data and template tags
OpenDocument report generation expands percent-delimited tags in the ODT template using columns from the template ViewModel.
Inspect available data
- Open
ThisWillBeMyTemplate.odt. - Add
%meta%to the document. - Save and close the document.
- Save the model so that the changed asset is uploaded.
- Run ProduceDocument again.
- Open the generated document and inspect the expanded properties produced by
%meta%.
Add the required ViewModel columns
In TheTemplateForCarTransferOwnershipDocumentReport, add these columns:
| Column | Value or expression |
|---|---|
| Buyer | Buyer |
| Seller | Seller |
| RegistrationNumber | RegistrationNumber |
| Brand | self.Car.Brand.Name
|
- Run ProduceDocument again.
- Copy the expanded ViewModel data names from the generated document into the original ODT template.
- Replace the template text with a sentence that uses the tags:
The buyer named %Buyer% has purchased the car %RegistrationNumber% of type %Brand% from %Seller%
- Turn off spellcheck in the document so the percent-delimited tags are not altered.
- Save and close the ODT document.
- Save the model.
- Run ProduceDocument once more.
Verify that the generated document replaces %Buyer%, %RegistrationNumber%, %Brand%, and %Seller% with values from the selected ownership-transfer document.
Completion check
Before continuing, confirm all of the following:
- The Model Check and Report Errors Tool reports no errors from the intentional-error exercises or the HistoricOwnership role rename.
- ProperCarView shows a Car and its CarTransferOwnershipDocuments grid.
- ProduceDocument opens or downloads an ODT report.
- The report file name contains the car registration number.
- The report text contains expanded buyer, seller, registration number, and brand values.
The completed training model is available as ModelAfterChapter10.zip.
Next chapter
Continue with Training:Bootcamp:Chapter 11.
