Components and migration documentation

1 Introduction

On a table we can have different components or operations that fire server side code. This code is mainly written in Javascript and will receive arguments (normally the row being processed).

1.1 Types of table server code

Server side code are grouped in 4 categories

  1. Table transaction events, fired during operations on a single row (transaction DML before and after events).
  2. Table form events, fired during operations on a single row (before field, after field events).
  3. Table buttons, fired to execute a button action on one or multiple rows.
  4. Table form widgets, fired on a single row to compute an output to be displayed.
  5. Table pdf print (page perfect layout using FOP).

Type Coding Argument Multiple rows (*) Return Type
Table transaction events JS / SQL (data)   void No return is expected
Table form events JS (data)   void No return is expected
Table button actions JS (data, field)   Object can be a text, html or ResultSet to be displayed as a result of the operation
Form widgets JS (data)   Object Can be a text, html, svg, graphviz to be displayed in the box
Table pdf print JS (data)   PDF object The PDF object to be rendered

(*) Data can be single row (a JSON objecy) or multiple rows (a JSON array of JSON objects) depending if the selection refers to a single or multiple rows.

1.2 Table transaction events

During an event, current row data passed as a JSON map to teh event function. The even can made changes on the tuple that will affect the next operation (only if they have meaning). No return is expected from the function.

Event JS SQL Data can be changed? Description
before_insert       change will affect insert data
before_update       change will affect update data
before_delete       any change has no effect
after_insert       any change has no effect
after_update       any change has no effect
after_delete       any change has no effect
Copy
<script>
  data.total = data.quantity * data.price;
</script>

1.3 Table form events

A field can have code programmed to be executed before and after the user focus the field.

  • During table editing the client can fire a before and after event to handle this code.
  • The content of the row beeing edited is passed as an argument to the function.
  • The javascript code may modify the data fields. Their changes will be seen back in the form.

Event JS SQL Data can be changed? Description
before_field       change will be seen in form
after_field       change will be seen in form
Copy
<script>
  data.total = data.quantity * data.price;
</script>

1.4 Table button actions

A button action is a piece of code that can be fired on a single row or a collection of rows. Thus the action can receive a JSON with the row or an array of JSON rows.

A button can have fields. If so, before executing the action a form with the fields is presented to the user. On accept a JSON with field names and values are pased as argument filed in combination with the row or selected rows as data argument.

The button function may return an object to the client or nothing. The object can be:

  • Text, to display a plain message
  • Html, to display a rich text message
  • ResultSet, to display a table with data

Copy
<script>

  // Access a field input and access a tuple data
  if (field.discount > data.total * .5) {
  
  }
   
</script>

1.5 Form widgets & decoration

TO DO

This section is incomplete and will be concluded as soon as possible.

2 Examples

For this document we will use two simple tables.

2.1 Example tables

Copy
-- ***********************************
-- swing_table_sample
-- ***********************************
CREATE TABLE swing_table_sample (
	column_id  serial   not null ,
	column_name  varchar (90)   not null ,
	column_status  smallint  default 1    not null ,
	date_updated  datetime year TO second   not null 
);
ALTER TABLE swing_table_sample LOCK MODE (ROW);

CREATE UNIQUE INDEX p_swing_table_sample ON swing_table_sample(column_id);
ALTER TABLE swing_table_sample ADD CONSTRAINT 
PRIMARY KEY (column_id) CONSTRAINT p_swing_table_sample;

-- ***********************************
-- swing_table_sample_log
-- **********************************
CREATE TABLE swing_table_sample_log (
	log_id  serial   not null ,
	column_id  INTEGER   not null ,
	user_code  varchar (20)  default USER    not null ,
	log_event  varchar (10)   not null ,
	log_date  datetime year TO second  default current year TO second    not null 
);
ALTER TABLE swing_table_sample_log LOCK MODE (ROW);

CREATE UNIQUE INDEX p_swing_table_sample_log ON swing_table_sample_log(log_id);
ALTER TABLE swing_table_sample_log ADD CONSTRAINT 
 PRIMARY KEY (log_id)  CONSTRAINT p_swing_table_sample_log;

CREATE  INDEX f_swing_table_sample_log1 ON swing_table_sample_log(column_id);
ALTER TABLE swing_table_sample_log ADD CONSTRAINT 
FOREIGN KEY (column_id) REFERENCES swing_table_sample  (column_id)
ON DELETE CASCADE CONSTRAINT f_swing_table_sample_log1;

-- ***********************************
-- load data
-- **********************************
INSERT INTO swing_table_sample (column_id,column_name,column_status,date_updated) VALUES (1,'AAA',1,TO_DATE ('2022-04-14 12:52:19', '%Y-%m-%d %H:%M:%S'));
INSERT INTO swing_table_sample (column_id,column_name,column_status,date_updated) VALUES (2,'BBB',1,TO_DATE ('2022-04-14 12:30:03', '%Y-%m-%d %H:%M:%S'));

3 Table tansaction events

In the classic version transaction events are stored in the table wic_jrep_trxmgr. In the next version, table events belong to wic_obj_table_events

This table wic_jrep_trxmgr in classic version supports conditional events using an expression defined in (trx_expr). This is not longer supported as the expression can be defined in the javascript event code.

3.1 Master vs Next

Master vs Next
Master VNext
Tables wic_jrep_trxmgr wic_obj_table_event
Language Coding
SQL
Copy
INSERT INTO swing_table_sample_log VALUES (0,${column_id},$USER,'UPD_SQL',CURRENT);
Copy
let column_id = data.column_id;
let user_code = Ax.ext.user.getCode();
Ax.db.execute(`INSERT INTO swing_table_sample_log VALUES (0,column_id,user_code,'UPD_JS',CURRENT)`;);
XSQLScript
Copy
<call>
    <args>
        <arg>${column_id}</arg>
    </args>
<xsql-script name="add_swing_table_sample_log">
    <args>
        <arg name="p_column_id"/>
    </args>
    <body>
        <insert table='swing_table_sample_log'>
            <column name='column_id'><p_column_id/></column>
            <column name='user_code'><system.user.getCode /></column>
            <column name='log_event'>UPD_XSQL</column>
            <column name='log_date'><date.current/></column>
        </insert>
    </body>
</xsql-script>
</call>
Must be implemented as Javascript:
Copy
let column_id = data.column_id;
let user_code = Ax.ext.user.getCode();

Ax.db.insert('swing_table_sample_log',{
    column_id : column_id,
    user_code : user_code,
    log_event : 'UPD_JS',
    log_date  : new Date()
});
Javascript
Copy
<script>
    let column_id = Ax.context.data.column_id;
    let user_code = Ax.ext.user.getCode();
    
    Ax.db.insert('swing_table_sample_log',{
        column_id : column_id,
        user_code : user_code,
        log_event : 'UPD_JS',
        log_date  : new Date()
    });
</script>
Copy
let column_id = data.column_id;
let user_code = Ax.ext.user.getCode();
    
Ax.db.insert('swing_table_sample_log',{
    column_id : column_id,
    user_code : user_code,
    log_event : 'UPD_JS',
    log_date  : new Date()
});