ORDA: Thoroughly compare two entity selections

Automatically translated from English

Since ORDA allows you to work with several entity selections simultaneously, we’re sure you take advantage of this by passing entity selections as a parameter to project methods or to functions of ORDA Data model classes.

To work efficiently with object-oriented programming using as few lines of code as possible, you need robust and optimized functions. That’s why with 4D v19 R3, we are shipping a new function available on the entitySelection object: the selected() function.

Thanks to it, you’ll be able to inspect and compare two entity selections. Let’s see the details!

the new selected() function

Given two entity selections entitySel1 and entitySel2, this function returns the indices of entities of entitySel2 inside entitySel1.

Got it? Here’s an example:

var $invoices; $selection : cs.InvoicesSelection
var $result : Object

$invoices:=ds.Invoices.all()
$selection:=ds.Invoices.query("payment = :1"; "Cash")
$result:=$invoices.selected($selection)
// $result = {ranges:[{start:1,end:1},{start:9,end:12},{start:18,end:18},{start:21,end:25}]}

The selected() function returns an object with a ranges property which is a collection of objects.

Those objects designate a range of entities (start # end) or a single entity (start = end).

The Invoices entities having payment = “Cash” among all invoices are those with indices:

  • 1
  • 9 to 12
  • 18
  • 21 to 25

a concrete use case

Imagine a library with books about different subjects (History, Art, Science, …). Here is the Books table:

On an HTML page, we want to highlight the books about a given subject.

For that, we have a highlight project method called as a 4DACTION with a subject parameter.

highlight project method code:

var $index : Integer
var $subject; $text : Text
var $onSubject : cs.BooksSelection
var $indices; $range : Object
var $titles : Collection
var $i : Integer

ARRAY TEXT($anames; 0)
ARRAY TEXT($avalues; 0

WEB GET VARIABLES($anames; $avalues)

//Get the subject
$index:=Find in array($anames; "subject")
$subject:=$avalues{$index}

//Search books about the given $subject
$onSubject:=ds.Books.query("subject = :1"; $subject)

//Get ranges of books about the subject
$indices:=ds.Books.all().selected($onSubject)

//Build a collection with all the titles
$titles:=ds.Books.all().title

// Loop on the $indices.ranges collection of objects

// $indices.ranges is [{start:0,end:0},{start:2,end:4},{start:7,end:9},{start:11,end:11}]
For each ($range; $indices.ranges)
 For ($i; $range.start; $range.end)
  $titles[$i]:="<b>"+$titles[$i]+"</b>"
 End for
End for each
// Build a text with carriage returns
$text:=$titles.join("<br/>")

WEB SEND TEXT($text)

 

Now let’s run 4DACTION/highlight?subject=History in a browser.

Here is the result:

Now let’s write optimized code with entity selections!

Avatar

• Product Owner •

Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer. Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.