miércoles, 18 de abril de 2012

Automata with GeneXus Ev1 on Linux and Windows, and I


Today I will begin a series of four publications in which I will try to give a glimpse into how to program an automata in Genexus Ev1, periodically run it on Linux or Windows and send logs and/or resulting reports by mail.

This technique we used in production at one company where I worked as a developer to implement a data integrity verification process.

In this first issue will discuss the creation of the automaton with GeneXus Evolution 1 and the Java generator for Windows, but in fact could be programmed with any language that can throw a PDF with the name and directory you specify.


In the second issue we will deal with running it periodically under Linux using nothing but standard GNU applications that come with most distributions.

In the third post will do the same but under Windows, using some third party applications to achieve the same effect as inLinux.

In the fourth and final issue I will modify the script for Linux to use the same applications that we will use in Windows, as an exercise in adaptation from one platform to another.


Requirements of the automata

We need to create a program without a user interface that receives parameters from the command line, create files in a directory passed as parameter, as its output goes to standard output text to the OS.

Access to databases or other procedures will depend in each case we want to implement this technique, so they are optional as requirement.


Hands On

The main procedure

As a first step we create a KB on GeneXus. For the purposes of this project we choose Java Environment in Prototyping Environment and chose Win as target, the rest of the details aren't relevant but to follow the example would be advisable to name chequeodesatendido to the KB.


Now create a procedure called chequeodesatendido, this will be the main procedure of our automata. Change in its properties  Main program  to True and Call protocol to Command line.



Add some code:

Rules
parm(in:&date_ini, in:&date_end, in:&filename);

Source

if &date_ini.IsEmpty() or &date_end.IsEmpty() or &filename.IsEmpty() 
  msg("Faltan parametros.")
  msg("Se debe proveer fecha inicial, fecha final y nombre del archivo de salida")
endif

msg("date ini: " + &date_ini.ToFormattedString() + 
  " | date_end: " + &date_end.ToFormattedString() + 
  " | filename: " + &filename
)

reporte.call(&date_ini, &date_end, &filename)

As can be seen the code is quite simple, just as an example.Msg is output to the console, which under Linux can be redirected to a file easily under Windows can not find yet how to do the same.

This is where we run or call the code that performs some verification, correction, closing process, etc.. on our data.




Report

Create a procedure called report and modify its properties. By coincidence the properties matches the above procedure, change  Main program  to True and Call  protocol  to Command line.
Rules
parm(in:&date_ini, in:&date_end, in:&filename);
output_file(&filename, 'PDF');

Source
print printBlock1
return

Layout

For our example create just a band called printBlock1 and add the variables that received as parameter. In production this is where the report to be sent as a result should be generated.



Now define chequeodesatendido as the Startup object and create the project.


Deploy it

The easiest way to package the files needed to run our project is doing a "deploy", so you run the Deployment Wizard



On the first screen our two procedures appears in the list of Available mains, pass them to the right under Mains to deploy.



We turn to the second screen and don't touch anything there, just hit Next to reach the third screen. Once there we check the checkbox Transfer 
location  files then we enter a directory where the wizard will place the files, finally click Finish.



Now opens the Genexus Web Start Deployment window, change VM: to Sun, specify a name in Application name:, I used again chequeodesatendido, now click on Build Archives.



Now, if all went well, we should have all the files needed to run our program in the directory you specified in the third window of the Genexus Deployment Wizard. We should have a Shared folder and another with the name you specified in the Application name: in the GeneXus Web Start Deployment. Create a folder named reportes, which is where we'll ask our automata to send its reports.


Last actions

From here, in theory, we are able to test our automata, but there are still a couple of details that the wizard didn't cover, no idea why. For some reason the wizard doesn't copy the package iText.jar that is necessary to generate the report, so we must copy it manually from our KB. Copy it from the folder JavaModel on the KB to Shared on our deployment. 

Under Windows 7, I gess it's should be the same under Vista, at the first run the progam attempts to copy the file winjutil.dll to the bin folder of the JRE, but will fail due to permissions. There are two ways to solve this problem, the first is run once our project as Administrator, the other is to copy the file from the KB JavaModel our JRE's bin folder.



Testing the automata

Create a file names  named test.cmd and add the following code:


@echo off

rem reemplazamos los backslach "/" de la fecha por el signo menos "-"
for /f "tokens=1-3 delims=/" %%a in ("%date%") do set FECHA=%%a-%%b-%%c
set ARCHIVO_SALIDA=cr_%FECHA%
set DIRECTORIO_SALIDA=%CD%\reportes
set GXCLASSPATH="shared/.;shared/gxclassp.jar;shared/iText.jar;chequeodesatendido/chequeodesatendido_GXWS.jar"

java -cp %GXCLASSPATH% achequeodesatendido "%FECHA%" "%FECHA%" "%DIRECTORIO_SALIDA%\%ARCHIVO_SALIDA%"


Execute test.cmd, after that whe should have a PDF file in the reportes folder.

That's all for the first issue, the second part in a couple of days.

1 comentario:

Unknown dijo...

Una consulta como generas el automata en Genexus pero C#, necesito enviar correos de alerta todos los dias, te agradezco anticipadamente

Atte

Walter Alburqueque Carreño
Consultor SBA SA