cancel
Showing results for 
Search instead for 
Did you mean: 

Deep Expansion (with $expand or another alternative)

iperez-sofos
Participant

Greetings, SAP community.

I am working on a project with SAP CAP and SAP HANA in the SAP BTP environment (temporarily, trial account).

I have some entities similar to the following:

entity Regions : SAP.CodeList {
    key code        : String(5); //> ISO 3166-1 alpha-2 codes (or alpha-3)
        country     : SAP_Country not null;
        superRegion : Association to Regions;
        subRegions  : Composition of many Regions on subRegions.superRegion = $self;
}
entity ORG : SAP.CodeList {
  key ID      : String(36);
      parent  : Association to ORG;
      country : SAP_Country;
      sons    : Composition of many ORG on sons.parent = $self;
}

As can be seen, entities have compositions with themselves of one-to-many cardinality. For example, a region can have multiple subregions. Each subregion (being a region) can also have several subregions, and so on.

The situation is the following:
For each of these entities, I need a service (a GET operation) that returns the "whole tree" (deep expansion). This is, for example in the case of regions, that the service returns the data for the region, all its subregions, the subregions of those subregions, and so on down to the "lowest" level (subregions that do not have other nested subregions ).

My specific questions are several:

How can I address this requirement?

What is the best way to solve it?

In principle, I need something like the $expand operator from OData services, but that works by "expanding" all the necessary levels (an unknown number of levels).

As far as I know, the $expand operator works with only one level of expansion (or a number of levels explicitly stated in the query), am I correct? Is there a way to do what I need just with the $expand operator?

I think that to solve this requirement I must implement a Custom Handler, however, I would like to know if it is possible, in such a custom handler, to use/invoke the generic handler. Is it possible?

The previous idea is inspired by Object-Oriented Programming, in which (in certain languages), in the "specialization" of a method in a subclass, the implementation of the same method belonging to the parent class can be invoked.

Thank you very much in advance for any help and/or suggestions regarding one or more of my questions.

mariya_yordanova
Explorer
0 Kudos

Hi Isaac,

do you have a Java or Node.js application ?

Best Regards

Maria

iperez-sofos
Participant
0 Kudos

Hello Maria Yordanova.

My application uses NodeJS (an important detail that I missed adding at the beginning).

Thanks for asking.

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hi Isaac,

the only way right now to achieve this is by writing a custom handler, as you already noticed. You can call the generic handlers from a custom handler like this:

srv.on('READ', 'EntityName', (req, next) => {
  // TODO do some custom logic
next() // -> calls generic handler for that entity })
But you have to make sure, that everything is conform according to spec, else it might happen that the generic handlers fail, if you modify the query. Why do you want to run the generic handlers after that? You could as well just build your custom query, execute it and return the result.

Best

Samuel