ORDA: work with objects and collections

Automatically translated from English

In this latest addition to the ORDA series, we’ll look at how the new ORDA concepts can interact with existing objects and collections. In fact, it’s possible to turn entities and entity selections into objects and collections, as well as build entities and entity selections from objects and collections. By doing so, you can easily integrate ORDA code within your existing code. You can take full advantage of ORDA without needing to rewrite your code!

Today you use SELECTION TO ARRAY and ARRAY TO SELECTION to work in a disconnected way from the server, but you have to manage as many arrays as fields. And for a current record, there is no way to extract it in any form. With ORDA, you can easily export entities and entity selections from the database to work on them separately. And that’s not all, you can import previously created objects and collections into the database.  All with a single and easy-to-use method!

This is very useful to work efficiently in client server mode, to communicate with other applications, or to import data using batch processes.

ORDA: working with objects and collections

EXPORTING ENTITY AND ENTITY SELECTIONs TO OBJECTs AND COLLECTIONs

The following examples are based on this structure. Let’s get our hands dirty!

EXPORT TO OBJECTs

An entity can be extracted as an object using the toObject() method. Here, we extract the $pupil entity as an object. We can choose which properties to extract.

Then, we can apply the JSON Stringify command to convert the value of this object into a JSON string and send it through the network, for example. C_OBJECT($pupil;$pupilObject)
C_TEXT($pupilString)
//Get the first pupil whose last name is "Dunaway"
$pupil:=ds.Pupil.query("lastName=:1";"Dunaway").first()
If ($pupil#Null)
// Turn the entity to an object
 $pupilObject:=$pupil.toObject("firstName, lastName, email")
 $pupilString:=JSON Stringify($pupilObject) // Stringify the object
End if

The $pupilObject object will be:

{
 "firstName": "Cecil",
 "lastName": "Dunaway",
 "email": "Cecil.Dunaway@echo"
}

EXPORT TO COLLECTIONs

An entity selection can be exported to a collection using the toCollection() method. Here, we export the $pupils entity selection (which contains 2 entities) as a collection. We decide to extract only the student’s lastName and firstName properties, along with all the properties of the school related entity.

Then, we can apply the JSON Stringify command to convert the values of this collection into a JSON string and send it through the network, for example.

C_OBJECT($pupils)
C_COLLECTION($pupilsCollection)
C_TEXT($pupilsString
// Get the pupils whose first name starts with "L" 
$pupils:=ds.Pupil.query("firstName=:1";"L@")
//Turn the entity selection to a collection of objects
$pupilsCollection:=$pupils.toCollection("lastName,firstName,school.*")
$pupilsString:=JSON Stringify($pupilsCollection)

The $pupilsCollection collection will be:

[
  {
   "firstName": "Lauren",
   "lastName": "Bouchard",
   "school": {
   "ID": 45,
   "name": "Arts schools",
   "state": "New Jersey" }
  },
  {
   "firstName": "Laurie",
   "lastName": "Valenti",
   "school": {
   "ID": 46,
   "name": "Mathematics school",
   "state": "New Jersey"}
  }
]

More options for toObject() and toCollection() methods are available, check out the documentation for more details!

IMPORTING ENTITy AND ENTITY SELECTIONs FROM OBJECTs AND COLLECTIONs

import from Objects

An entity can be built from an object with the fromObject() method. Here, we create a new $pupil entity from a given object:

C_OBJECT($pupil;$pupilObject)
$pupilObject:=New object("firstName";"Mary";"lastName";"Smith";"email";"Mary.Smith@whisky")
//Create a new empty entity
$pupil:=ds.Pupil.new()
//Fill entity
$pupil.fromObject(
$pupilObject)

import from collections

An entity selection can be built from a collection with the fromCollection() method. Here, we build a $pupils entity selection from a given collection.

Note that this mechanism can be used for creating new entities or updating existing ones. It simply depends on the primary key’s presence in the objects.

C_OBJECT($pupils)
C_TEXT($txtPupils)
C_COLLECTION($pupilsCollection)
$txtPupils:=Document to text(Get 4D folder(Current resources folder)+"pupils_data.json")
//Get a collection of objects
$pupilsCollection:=JSON Parse($txtPupils)
$pupils:=ds.Pupil.fromCollection($pupilsCollection)

and here is the $pupilsCollection collection:

[
 {             // The primary key of the entity is not given. A new entity will be created
 "firstName": "Mark",
 "lastName": "Mac Arthur",
 "email": "Mark.MacArthur@yankee",
 "school": {
 "__KEY": "45"
 }
},
 {          // The primary key of the entity is given. This entity will be updated (if it exists)
 "__KEY": 3436, 
 "firstName": "Victor",
 "lastName": "Valenti",
 "email": "Victor.Valenti@NEWSCHOOL",
 "school": {
 "__KEY": "45"
 }
}
 ]

 

• 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.