In this lesson, you work directly with EGL source code, starting with an EGL library that you write. A library can contain constants, variables, and functions, any of which can be accessed by the different units of logic in your application. An important characteristic of a library is that changes to a variable are available to any unit of logic that accesses the library. However, the focus in this tutorial is on functions, which you place in a library to avoid having to maintain the same, widely used logic in multiple places.
To handle some commonplace issues, you can use the EGL Model View Controller (MVC) framework, which is provided by the com.ibm.egl.rui project. Although the initials "MVC" typically describe the different components of an enterprise application, the MVC framework in Rich UI concerns only the components of a user interface. Here, the model is a variable or record field, the view is a widget, and the controller is a declaration that oversees the transfer of data between the model and view. That transfer of data is sometimes automatic and is sometimes a response to a function invocation, as shown later.
The drag-and-drop actions of the previous lesson added not only controller declarations, but a form manager, which is a declaration that lets you treat other declarations as components of a single form. A form manager includes a set of form fields, each of which can include a label, a controller, and an error field.


libraries
MortgageLib
library MortgageLib
function formatMoney(amount STRING in) returns(STRING)
len int = strlib.characterLen(amount);
index int = len - 6; // 2 dec places + decimal + 3 chars before separator
while(index > 0)
amount = amount[1 : index] :: "," :: amount[index + 1 : len];
index -= 3;
len += 1;
end
return("$" :: amount);
end
end
As shown, you specify the library name and embed a new function, formatMoney(), which adds commas and a dollar sign to an input string. The assumption here is that the input field contains a money value with two decimal places.
inputRec MortgageCalculationResult
{term = 30, loanAmount=180000, interestRate = 5.2};
Save the file and click the Preview tab.
The interface now shows the default values, including the value that
you specified in the previous lesson, for the term field:
mortService MortgageCalculationService{@dedicatedService};

import services.MortgageCalculationService;
After a few seconds, the error marks are removed even though
you did not save the file.EGL created a stub inputRec_form_Submit function. The intent is for the function to validate all the fields on the form and to “commit” them. The commit is part of the MVC implementation and means that the inputRec record is updated with the values in the widgets.
You will add code to call two other functions. The first function makes the processImage image visible and in this way tells the user that the application is working. The second function calls the service to calculate the mortgage payment.
if(inputRec_form.isValid())
inputRec_form.commit();
showProcessImage();
calculateMortgage();
else
errorLabel.text = "Input form validation failed.";
end

function showProcessImage()
processImage.visible = yes;
end
function hideProcessImage()
processImage.visible = no;
end
function calculateMortgage()
call mortService.amortize(inputRec)
returning to displayResults
onException handleException;
end
function displayResults(retResult MortgageCalculationResult in)
paymentLabel.text = MortgageLib.formatMoney(retResult.monthlyPayment as STRING);
inputRec_form.publish();
hideProcessImage();
end
Given that sequence of events, the form-level publish function is often invoked as it is here: in a callback function that receives data from a service.
Do as follows:
// catch-all exception handler
private function handleException(ae AnyException in)
errorLabel.text = "Error calling service: " + ae.message;
end
Functions with the private modifier
can only be called by the EGL part where the function resides; in
this case, by the embedding handler. The function places text in the errorLabel widget
that you created in the previous lesson.errorLabel.text = "";
In
this way, you clear the errorLabel field before you
invoke the service that does a mortgage calculation. If you do not
add that code, an error such as the temporary loss of service connection
will display an error message, even after the service is invoked successfully.You are now ready to test the calculator.


When the widget loses focus, the tooltip is hidden, and when the widget regains focus, the tooltip is shown.
In the next lesson, you create a pie chart to compare the total principal to the total interest in a given calculation.