Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Traversing Relationships, Conditional Navigation, Query, and Update With JavaScript and Oracle ABCS

DZone's Guide to

Traversing Relationships, Conditional Navigation, Query, and Update With JavaScript and Oracle ABCS

Storing conditional data and utilizing conditional queries can be complex, but using Oracle ABCS with a bit of JavaScript can help solve your problems.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

 Oracle Application Builder Cloud Service (ABCS) lets you do a lot of things in a declarative way, however for more complex validation and conditional logic you might need to resort to some basic coding in JavaScript.

I ran into such a case with an application I was developing and figured out that the code sample from that system will be a good way to illustrate some coding techniques in ABCS.

The application I was working on allows people to register to various events. Each event has a certain capacity, so if there are too many people registered to an event, we want the rest to be added to a wait list. For each record of a person registering, we keep a reference to the event they want to attend. So the logic flow is:

  1. Check how many open spaces are available for the event we are trying to register for.
  2. If there is space in the event, save the new person data, and show a success message.
  3. If there isn't space, update the person "Waitlisted" field to be true, save the data, and show a message saying that the person is on the wait list. 

In the demo video below, I'm showing how to:

  • Define declarative conditional flow of steps based on results from custom code.
  • Traverse relationships between custom object through code.
  • Execute a conditional query and run through the results from a custom object with code.
  • Set the value for a property of a custom object through code.

For reference, here is the complete code from the sample:

require([     
  'operation/js/api/Conditions',     
         'operation/js/api/Operator' ], 
        function (Conditions, Operator) {     
          var lab = Abcs.Entities().findById('Lab');     
          var id = lab.getProperty('id');    
          //condition is to find a lab with the id that is in the page's 
          //drop down box     
          var condition = Conditions.SIMPLE(
            id, Operator.EQUALS, $Person.getValue('ref2Combo_Box'));     
          var operation = Abcs.Operations().read({         
            entity: lab,         
              condition: condition     });     
          //if query returned value we loop over the 
          //rows and check the capacity and registration columns     
          operation.perform().then(function (operationResult) {         
            if (operationResult.isSuccess()) {             
              operationResult.getData().forEach(function (oneRecord) { 

                if ((oneRecord.Capacity - oneRecord.Registered) < 1) { 
                  $Person.setValue('Waitlisted', true);                     
                  reject("error");                 
                } else                 
                {                     
                  resolve("ok");                 
                }             
              });        
            }     
          }).catch(function (operationResult) {         
            if (operationResult.isFailure()) {             
              // Insert code you want to perform if fetching of records failed
              alert('didnt worked');            
              reject("error");        
            }   
          });
        });


More information on the JavaScript APIs used here are in the Oracle ABCS Documentation.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
database ,javascript ,tutorial ,oracle abcs ,conditional flow ,conditional queries

Published at DZone with permission of Shay Shmeltzer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}