JavaListFunction

Table of Contents

logo-large

Tutorial Actifsource Tutorial – Java List Functions
Required Time - 20 Minutes
Prerequisites - Actifsource Tutorial – Installing Actifsource
- Actifsource Tutorial – Simple Service
- Actifsource Tutorial – Complex Service
Goal - Learn about JavaListFunctions
- Create your own JavaListFunction to sort resources
Topics covered - JavaListFunction
Notation ↪ To do
ⓘ Information
Bold: Terms from actifsource or other technologies and tools
Bold underlined: actifsource Resources
Monospaced: User input
Italics: Important terms in current situation
Disclaimer The authors do not accept any liability arising out of the application or use of any information or equipment described herein. The information contained within this document is by its very nature incomplete. Therefore the authors accept no responsibility for the precise accuracy of the documentation contained herein. It should be used rather as a guide and starting point.
Contact Actifsource AG
Täfernstrasse 37
5405 Baden-Dättwil
Switzerland
www.actifsource.com
Trademark Actifsource is a registered trademark of Actifsource AG in Switzerland, the EU, USA, and China. other names appearing on the site may be trademarks of their respective owners.

Overview

  • Let’s create a simple model for a club with its members with a resource which represents the date of birth

  • Let’s write a JavaListFunction which can sort members by their date of birth and use it in a template

    Templateview of ClubOverview Templateview of ClubOverview

  • Create some html output for the club members sorted by the date of birth (month and day)

    Generated sorted HTML output Generated sorted HTML output

Part I Preparation

Metamodel of ch.actifsource.tutorial.javalistfunction Metamodel of ch.actifsource.tutorial.javalistfunction

  • Create a simple design consist of a club, containing members having a date of birth

  • Club is a NamedResource

  • Club owns 1..N Member

  • Member is a Resource

  • Member contains the following Attributes of type StringLiteral:

    • firstName
    • lastName
  • Member owns 1*1 DateOfBirth

  • DateOfBirth is a Resource

  • DateOfBirth contains the following Attributes of type IntegerLiteral:

    • Year
    • Month
    • Day
Info

The attributes for the individual classes can be displayed in the Class Diagram Editor by selecting the Show Attributes option the context menu (right mouse button on the class).

Show Attributes in context menu Show Attributes in context menu

Nameaspect of Member Nameaspect of Member

  • Create your own name aspect for Member
Info

Note that this step is not necessary for sorting

Nameaspect of DateOfBirth Nameaspect of DateOfBirth

  • Create your own name aspect for DateOfBirth
Info

Note that this step is not necessary for sorting

Part II Creating an HTML Template

Templateview of ClubOverview Templateview of ClubOverview

  • Create a simple html template to display all club members and their birthdays

Generated HTML output Generated HTML output

  • The generated result should look like shown above

Part III Creating a JavaListFunction

New created FunctionSpace New created FunctionSpace

  • Create a new FunctionSpace called MyFunctions
  • Create a new FunctionContext for the typeRef Member

Inserted new JavaListFunction Inserted new JavaListFunction

  • Create a new JavaListFunction called sortByDateOfBirth
  • Select GenericContextListType as returnType
  • Please note that the GenericContextListType automatically converts to the same type as used as the function target in the selector
  • You will love this feature if your type (typeRef) has sub classes
  • Imagine to create a sortByName function for NamedResource
  • Calling A.sortByName for A as a sub class of NamedResource will return A as the type of result and not NamedResource even sortByName has been defined for NamedResource

Generated JavaListFunction skeleton Generated JavaListFunction skeleton

  • After saving the FunctionSpace MyFunctions the folder src-gen will contain a Java file named the same as your FunctionSpace
  • Find the generated function body for the JavaListFunction sortByDateOfBirth
  • Let’s write some Java code to sort our club members by their date of birth

First code fragment First code fragment

  • Since the memberList passed by Actifsource is marked final we have to copy the list in order to modify\

    ArrayList<T> memberListSorted = new ArrayList<>(memberList);
  • Return the copied list to get rid of compile errors\

    return memberListSorted;

Importsection Importsection

  • Please make sure to place the import statement for ArrayList in the ProtectedRegion
  • Otherwise the your import will be overwritten from the generator

Completed code fragment Completed code fragment

  • Using the static function Collections.sort() from the Java collection framework allows us to pass a so called Comparator
  • Let’s implement the anonymous class of type Comparator<T> as shown above
  • Comparator.compare shall return 0 if objects are equal, -1 if o1 is a predecessor of o2, or +1 if o1 is a successor of o2
  • Since the variables o1 and o2 are fully typed we can directly access the properties of Member using the selectXYZ() functions
  • The code shown above is sorting dates regarding month and day only
  • Try to implement a sort function which shows the oldest member first

Part IV Putting it all together

Templateview of ClubOverview 2 Templateview of ClubOverview 2

  • Use the sortByDateOfBirth function to sort any selected list of members by their date of birth

Generated sorted HTML output Generated sorted HTML output

  • As a result your members are sorted by month and day

actifsource-point-large