< Previous | Next >

Lesson 3: Create the mortgage calculation service

Create a dedicated service to calculate monthly payments.

In this lesson, you create an EGL Service part, which is a generatable part. You must place each generatable part in a separate source file, and the name of the part must be the same as the name of the file.

Create a Service part

  1. In the Project Explorer window, right-click MortgageServiceProject, and then click New > Service.
  2. In the New EGL Service Part window, enter the following information:
    1. In the Package field, enter the following name:
      services
    2. In the EGL source file name field, enter the following name:
      MortgageCalculationService
      EGL adds the .egl file extension.
    3. Verify that Create as web (SOAP) service and Create as web (REST) service are cleared, and leave the Implements Interfaces field empty.
      The New EGL Service Part window shows the service name and package.
  3. Click Finish. EGL opens the new Service part in the editor.
  4. Remove the code from the file, leaving only the following lines:
    package services;
    
    service MortgageCalculationService
    
    end
  5. Add the following function before the end statement:
    	function amortize(inputData MortgageCalculationResult inOut)
    		amt MONEY = inputData.loanAmount;
    		// convert to monthly rate
    		rate DECIMAL(10, 8) = (1 + inputData.interestRate / 1200);
    		// convert to months
    		term INT = (inputData.term * 12);
    
    		// calculate monthly payment amount
    		pmt MONEY = (amt * (rate - 1) * Mathlib.pow(rate, term)) /
    				(MathLib.pow(rate, term) - 1);
    		totalInterest MONEY = (pmt * term) - amt;
    		
    		// update result record
    		inputData.monthlyPayment = pmt;
    		inputData.interest = totalInterest;
    	end
    When you paste code from these instructions, the formatting might change. Press Ctrl+Shift+F to reformat the code. You can change the formatting rules by clicking Window > Preferences > EGL > Editor > Formatter.
    Note:
    EGL marks any code errors with a red X in the left margin and a wavy red line under the error. Move your cursor over the X to see an error message.
    Errors are marked with red wavy lines and red Xs in the margin.

    Because you have not yet defined a type named MortgageCalculationResult, EGL cannot create the inputData variable based on that type. When you create this Record type in the next exercise, EGL will remove the error markers from the display.

  6. Save the file by clicking File > Save.

Create a Record part

The amortize() function uses a MortgageCalculationResult record. You can define this record in the same file as the Service.

To create the Record part:

  1. Add the following code after the amortize() function in the MortgageCalculationService.egl file. The Record is a part, so you define it outside the Service part, after the final end statement in the file:
    record MortgageCalculationResult
    	// user input
    	loanAmount MONEY;
    	interestRate DECIMAL(10,8);
    	term INT;
    
    	// calculated fields
    	monthlyPayment MONEY;
    	interest MONEY;
    end
  2. Save the file. EGL should not display error markers in the code. If you see errors in your source file, compare your code to the file contents in Finished code for MortgageCalculationService.egl after Lesson 3. As you work through the tutorial, you might see red Xs next to the project or next to one of the folders below it, yet not see any errors in the file itself. If you encounter this situation, resolve it by clicking Project > Clean. In the Clean window, click Clean projects selected below and then click the appropriate project, such as MortgageServiceProject.
    The MortgageServiceProject is selected from the list.
    Click OK. EGL rebuilds the selected project and the red X is removed from the Project Explorer view.
  3. Close the file by clicking the X next to the file name in the tab at the top of the editor or by clicking File > Close.

Lesson checkpoint

You learned how to complete the following tasks:
  • Create an EGL Service part
  • Create an EGL Record part and add it to the source file for the Service
  • Check for errors in your code

In the next lesson, you create the user interface for the first application portlet.

< Previous | Next >

Feedback