lunes, 2 de noviembre de 2015

Ada and HANA playing together

This post was originally posted on Ada and HANA playing together.


Ada is a structured, statically typed, imperative, wide-spectrum and object-oriented programming language, extended from Pascal and other languages.

It was originally developed for the United States Department of Defense. And it was named after Ada Lovelace (1815-1852), who was the first computer programmer.

Ada is a language that simply would not allow you to fail…as it have mechanisms for compile and execution time checks…getting rid of most of the bugs that your application could ever produce…

So…no example or demonstration would be complete if we didn’t hook it up with SAP HANA, right? So…let’s go and do it -;)

First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.

First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.

Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.

Then create a Projection object and select only PRICE and CARRNAME.

On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.


Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.

Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.

Create an empty “.xsapp” file.

Create a file called “.xsaccess” with the following code.

.xsaccess
{
          "exposed" : true,
          "authentication" : [ { "method" : "Basic" } ]
}

Finally create a file called “flights.xsodata” with the following code

flights.xodata
service {
          "Blag/FLIGHTS_BY_CARRIER.calculationview" as "FLIGHTS" keys 
                                                        generate local "Id";
}

Activate your project and launch it on your browser, you should see something like this…


The SAP HANA part is done…so we can move into the Ada part…

Ada comes installed in most Linux-based distros…but the version can be old…so it’s better to download and install a more recent version…so please just do this on a terminal session…

sudo apt-get install gnat
sudo apt-get install libaws-bin libaws3.2.0-dev

To install both Ada and AWS (Nope…not that one…but Ada Web Server)…

Also, we will need to download and install XML/Ada

tar zxvf xmlada-gpl-4.3-src.tgz
cd xmlada-4.3-src/
./configure --prefix=/usr/gnat
PROCESSORS=4 make all
make docs
make install

Then, you will need to update your .bashrc file in order to have the right paths…

PATH=/usr/gnat/lib/gnat:$PATH
export PATH
GPR_PROJECT_PATH=/usr/gnat/lib/gnat:$GPR_PROJECT_PATH
export GPR_PROJECT_PATH

ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/usr/gnat/lib/gnat
export ADA_PROJECT_PATH
ADA_OBJECTS_PATH=$ADA_OBJECTS_PATH:/usr/gnat/lib/gnat
export ADA_OBJECTS_PATH
ADA_INCLUDE_PATH=$ADA_INCLUDE_PATH:/usr/gnat/lib/gnat
export ADA_INCLUDE_PATH

Then create a file called ada_hana.adb on your favorite editor and copy the following code

ada_hana.adb
with Ada.Text_IO; use Ada.Text_IO;
with AWS.Client;
with AWS.Response;

with Input_Sources.Strings;
with Unicode.CES.Utf8;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with Sax.Readers;        use Sax.Readers;
with DOM.Readers;        use DOM.Readers;
with DOM.Core;           use DOM.Core;
with DOM.Core.Documents; use DOM.Core.Documents;
with DOM.Core.Nodes;     use DOM.Core.Nodes;
with DOM.Core.Attrs;     use DOM.Core.Attrs;

procedure Ada_Hana is
 Flights : Unbounded_String;
 Input : Input_Sources.Strings.String_Input;

 Reader : Tree_Reader;
 Doc    : Document;
 C_List   : Node_List;
 P_List   : Node_List;
 C_N      : Node;
 P_N      : Node;

begin
 Flights := AWS.Response.Message_Body (AWS.Client.Get (URL=>"http://YourServer:8000/Flights/flights.xsodata/FLIGHTS", 
                                                              User => "SYSTEM", Pwd => "YourPassword")); 
 
 Input_Sources.Strings.Open (To_String(Flights), Unicode.CES.Utf8.Utf8_Encoding, Input);
 DOM.Readers.Parse (Reader, Input);
 Doc := Get_Tree (Reader); 
 
 C_List := Get_Elements_By_Tag_Name (Doc, "d:CARRNAME");
 P_List := Get_Elements_By_Tag_Name (Doc, "d:PRICE");

   for Index in 1 .. Length (C_List) loop
      C_N := Item (C_List, Index - 1);
      P_N := Item (P_List, Index - 1);
      Put_Line (Node_Value(First_Child(C_N)) & ": " & Node_Value(First_Child(P_N)));
   end loop; 

   Free (C_List);
   Free (P_List);
   Free (Reader);
   
end Ada_Hana;

To compile this application we need to create another file…called compile_ada_hana.gpr

compile_ada_hana.gpr
with "aws";
project Compile_Ada_HANA is
for Main use ("ada_hana.adb");
end Compile_Ada_HANA;

And run it like this…

sudo gprbuild compile_ada_hana.gpr

One the executable is done…we can run it like this…

./ada_hana


If you are wondering…why didn’t I use JSON instead of XML? Well…there’s an easy explanation for that -:) The JSON library is nice…but I had some troubles compiling it and there’s not much examples on how to retrieve the data from a complex JSON…so using XML looked like an easier and better choice  -;)

Greetings,

Blag.
Development Culture.