Those of you who have attended the SAP BC400 training course, which is the introduction to ABAP development, will start by writing a program that acts as a simple calculator.

It is based on ABAP PARAMETERS for inputs, ABAP WRITE statements for outputs , and is designed to show the basic constructs of most programming languages, introducing the concepts of modularisation, conditional program flows, error handling etc.

In the quiet end of year period, I decided to see if I could do something similar with more modern concepts.

The front end had to be UI5 based, and so the backend logic needed to be based on OData.

I already had a small SEGW project that I used for generating random Fiori Launchpad tile statuses, so I added a new entity to that project. The entity has three key properties, two integers for the operands and a character string for the operator, and a property for the result and any error message.

The GitHub repo can be found at https://github.com/michaelnicholls/ux100.git.

The logic for GET_ENTITYSET is to loop through a list of valid operators, and execute my CALC method with that operator and 2 random operands.

The logic for GET_ENTITY is to simply execute the CALC method.

The UI5 app was built by hand, as there was no obvious template. It consists of two <Input>s for the operands and a <RadioButtonGroup> for the operator. The contents of this group are derived from a property which is returned by the OData call, which contains a list of valid operators.

My second attempt for the OData service was to create a CDS view which accepts the three parameters and returns the result base on a nested CASE statement.

The view is based on a select statement from the T000 table, as this was the only table I could think of which I knew would always have at least one value.

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'entity'
@OData.publish: true
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
    serviceQuality: #X,
    sizeCategory: #S,
    dataClass: #MIXED
}
define view entity ZCALC  with parameters
    op1 :int4,
    op2 :int4,
    op  :char1
  as select from t000
{
//  key $parameters.op1 as operand1,
//  key $parameters.op2 as operand2,
//  key $parameters.op  as operator,
    key '' as x,
      case $parameters.op
          when '+' then
        $parameters.op1 + $parameters.op2
          when '-' then
        $parameters.op1 - $parameters.op2
          when '*' then
        $parameters.op1 * $parameters.op2
          when '/' then
          case  $parameters.op2 when  0 then 0
//
// handle division by 0
//
          else division($parameters.op1, $parameters.op2, 2 )
          end
           when '%' then
          case  $parameters.op2 when  0 then 0
//
// handle percentage of 0
//


          else div(100 * $parameters.op1, $parameters.op2 )
          end
        else
          0
        end           as res,

      case $parameters.op
        when '+' then
      ''
        when '-' then
      ''
        when '*' then
      ''
        when '/' then
        case $parameters.op2 when 0 then 'No division by 0'
        else
      ''
      end
        when '%' then
        case $parameters.op2 when 0 then 'No percentage of 0'
        else
      ''
      end
      else
        'Bad operator'
      end             as error

}
where
  mandt = '000'

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x