--Freemarker Template
<#include "SQL_lingua_franca"/>
<#include "SuperX_general"/>


<#assign  etl_jobs = [
  {"uniquename":"sx_select_mask", "name":"Maske entladen", "systeminfo_id":270 ,"logfile":""},
  {"uniquename":"sx_insert_mask", "name":"Maske hochladen", "systeminfo_id":270 ,"logfile":""},
  {"uniquename":"kern_konstanten_update", "name":"Konstanten aktualisieren", "systeminfo_id":270 ,"logfile":""},
  {"uniquename":"sichten_unload", "name":"Sichten entladen", "systeminfo_id":270 ,"logfile":""},
  {"uniquename":"sos_gewichtung_unload", "name":"Gewichtungen entladen", "systeminfo_id":270 ,"logfile":""}
 
 
  ] />
  
<#assign  etl_job_params = [
  {"etl_job":"sx_select_mask","param_name":"TID", "name":"Maskennr.", "param_default":""},
  {"etl_job":"sx_select_mask","param_name":"PATH_TO_OUTPUTFILE", "name":"Ausgabedatei", "param_default":"$SUPERX_DIR/db/masken/$TID.xml"},
  {"etl_job":"sx_select_mask","param_name":"FORMAT", "name":"Ausgabeformat", "param_default":"XML"},

  {"etl_job":"sx_insert_mask","param_name":"TID", "name":"Maskennr.", "param_default":""},
  {"etl_job":"sx_insert_mask","param_name":"PATH_TO_INPUTFILE", "name":"Eingabepfad", "param_default":"$SUPERX_DIR/db/masken/$TID.xml"},
  {"etl_job":"sx_insert_mask","param_name":"FORMAT", "name":"Format", "param_default":"XML"},
  
  {"etl_job":"kern_konstanten_update","param_name":"SUPERX_DIR", "name":"Superx-Pfad zu WEB-INF/conf/edustore", "param_default":"$WEBAPP/WEB-INF/conf/edustore"},
  {"etl_job":"kern_konstanten_update","param_name":"PATH_TO_INPUTFILE", "name":"Eingabepfad", "param_default":"$SUPERX_DIR/db/install/schluesseltabellen/kern_feste_konstanten_fuellen.sql"},
 
 {"etl_job":"sichten_unload","param_name":"SYSTEMINFO_ID", "name":"Komponente", "param_default":""},
  {"etl_job":"sichten_unload","param_name":"PATH_TO_OUTPUTFILE", "name":"Ausgabedatei", "param_default":"$SUPERX_DIR/db/masken/sichten_$SYSTEMINFO_ID.xml"},
  {"etl_job":"sichten_unload","param_name":"FORMAT", "name":"Ausgabeformat", "param_default":"XML"},

  {"etl_job":"sos_gewichtung_unload","param_name":"SYSTEMINFO_ID", "name":"Komponente", "param_default":""},
  {"etl_job":"sos_gewichtung_unload","param_name":"PATH_TO_OUTPUTFILE", "name":"Ausgabedatei", "param_default":"$SUPERX_DIR/db/db/module/sos/schluesseltabellen/sichten_$SYSTEMINFO_ID.xml"},
  {"etl_job":"sos_gewichtung_unload","param_name":"FORMAT", "name":"Ausgabeformat", "param_default":"XML"}

  ] />

<#assign  etl_steps = [
  {"etl_job":"sx_select_mask", "uniquename":"unload_masken_stammdaten", "name":"Masken-Daten entladen", "type":"MSG"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_maskeninfo", "name":"Maskeninfo entladen", "type":"UNLOAD", "parent":"unload_masken_stammdaten"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_felderinfo", "name":"felderinfo entladen", "type":"UNLOAD", "parent":"unload_masken_stammdaten"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_masken_felder_bez", "name":"masken_felder_bez entladen", "type":"UNLOAD", "parent":"unload_masken_stammdaten"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_sachgeb_maske_bez", "name":"sachgeb_maske_bez entladen", "type":"UNLOAD", "parent":"unload_masken_stammdaten"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_maske_system_bez", "name":"maske_system_bez entladen", "type":"UNLOAD", "parent":"unload_masken_stammdaten"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_themenbaum", "name":"themenbaum entladen", "type":"UNLOAD"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_sx_mask_style", "name":"sx_mask_style entladen", "type":"UNLOAD"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_sx_stylesheets", "name":"sx_stylesheets entladen", "type":"UNLOAD"},
  {"etl_job":"sx_select_mask", "uniquename":"unload_stylesheet_field", "name":"stylesheet_field entladen", "type":"UNLOAD"},

  {"etl_job":"sx_insert_mask", "uniquename":"delete_maskeninfo", "name":"Maskeninfo löschen", "type":"DOQUERY" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_maskeninfo", "name":"Maskeninfo hochladen", "type":"LOAD" },
   
  {"etl_job":"sx_insert_mask", "uniquename":"delete_felderinfo", "name":"felderinfo löschen", "type":"DOQUERY" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_felderinfo", "name":"felderinfo hochladen", "type":"LOAD" },

  
  {"etl_job":"sx_insert_mask", "uniquename":"delete_masken_felder_bez", "name":"masken_felder_bez löschen", "type":"DOQUERY" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_masken_felder_bez", "name":"masken_felder_bez hochladen", "type":"LOAD" },

  
  {"etl_job":"sx_insert_mask", "uniquename":"delete_sachgeb_maske_bez", "name":"sachgeb_maske_bez löschen", "type":"DOQUERY" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_sachgeb_maske_bez", "name":"sachgeb_maske_bez hochladen", "type":"LOAD" },

  {"etl_job":"sx_insert_mask", "uniquename":"delete_maske_system_bez", "name":"maske_system_bez löschen", "type":"DOQUERY" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_maske_system_bez", "name":"maske_system_bez hochladen", "type":"LOAD" },
  {"etl_job":"sx_insert_mask", "uniquename":"create_tmp_etl_themenbaum", "name":"Tabelle tmp_etl_themenbaum erzeugen", "type":"DOSQL" },
  {"etl_job":"sx_insert_mask", "uniquename":"create_tmp_etl_stylesheets", "name":"Tabelle tmp_etl_stylesheets erzeugen", "type":"DOSQL" },
  
  {"etl_job":"sx_insert_mask", "uniquename":"upload_tmp_etl_themenbaum", "name":"tmp_etl_themenbaum hochladen", "type":"LOAD" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_tmp_etl_stylesheets", "name":"tmp_etl_stylesheets hochladen", "type":"LOAD" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_tmp_etl_mask_style", "name":"tmp_etl_mask_style hochladen", "type":"LOAD" },
  {"etl_job":"sx_insert_mask", "uniquename":"upload_tmp_etl_stylesheet_field", "name":"tmp_etl_stylesheet_field hochladen", "type":"LOAD" },

  {"etl_job":"sx_insert_mask", "uniquename":"themenbaum_fuellen", "name":"themenbaum_fuellen", "type":"DOSQL" },
  {"etl_job":"sx_insert_mask", "uniquename":"sx_stylesheets_fuellen", "name":"sx_stylesheets_fuellen", "type":"DOSQL" },

  {"etl_job":"kern_konstanten_update", "uniquename":"kern_konstanten_update", "name":"Kern Konstanten aktualisieren", "type":"DOSQL" },

  {"etl_job":"sichten_unload", "uniquename":"unload_sichten_ges", "name":"Sichten entladen", "type":"MSG"},
  {"etl_job":"sichten_unload", "uniquename":"unload_sichtentab", "name":"Sichten entladen", "type":"UNLOAD", "parent":"unload_sichten_ges"},
  {"etl_job":"sichten_unload", "uniquename":"unload_sichten_sachgeb", "name":"Sachgebiete von Sichtarten entladen", "type":"UNLOAD", "parent":"unload_sichten_ges"},
  

  {"etl_job":"sos_gewichtung_unload", "uniquename":"unload_sos_gewichtung_ges", "name":"Gewichtungen entladen", "type":"MSG"},
  {"etl_job":"sos_gewichtung_unload", "uniquename":"unload_sos_gewichtung", "name":"Gewichtungen entladen", "type":"UNLOAD", "parent":"unload_sos_gewichtung_ges"}


  ] />
  
<#assign  etl_step_properties = [
  {"etl_step":"unload_masken_stammdaten","prop_name":"msg", "prop_value":"Entlade Stammdaten Maske $TID" },
  {"etl_step":"unload_maskeninfo","prop_name":"select_stmt", "prop_value":"select tid,name,select_stmt,xil_proplist,chart_xtitel,chart_ytitel,erlaeuterung,cleanup_stmt,default_file,frontend,breite,hoehe,ampel,hilfe,hinweis from maskeninfo where tid=$TID" },
  {"etl_step":"unload_felderinfo","prop_name":"select_stmt", "prop_value":"select tid ,name,nummer,x,y,buttonbreite,feldbreite,zeilenanzahl,typ,laenge,obligatorisch,art,relation,attribut,defaultwert from felderinfo where tid in (select felderinfo_id from masken_felder_bez where maskeninfo_id = $TID) order by tid" },
  {"etl_step":"unload_masken_felder_bez","prop_name":"select_stmt", "prop_value":"select maskeninfo_id,felderinfo_id from masken_felder_bez where maskeninfo_id=$TID order by 1,2" },
  {"etl_step":"unload_sachgeb_maske_bez","prop_name":"select_stmt", "prop_value":"select sachgebiete_id,maskeninfo_id from sachgeb_maske_bez where maskeninfo_id=$TID order by 1,2" },
  {"etl_step":"unload_maske_system_bez","prop_name":"select_stmt", "prop_value":"select maskeninfo_id,systeminfo_id from maske_system_bez where maskeninfo_id=$TID order by 1,2" },
  {"etl_step":"unload_themenbaum","prop_name":"select_stmt", "prop_value":"select name,maskeninfo_id,parent,(select name from themenbaum where tid=T.parent) as parent_name,gueltig_seit,gueltig_bis,erlaeuterung,sort,css_class from themenbaum T where maskeninfo_id=$TID order by 2,1" },
  {"etl_step":"unload_sx_mask_style","prop_name":"select_stmt", "prop_value":"select maskeninfo_id,stylesheet_id,ord,(select filename from sx_stylesheets where tid=S.stylesheet_id) as stylesheet_filename from sx_mask_style S where maskeninfo_id=$TID order by 1,2,3" },
  {"etl_step":"unload_sx_stylesheets","prop_name":"select_stmt", "prop_value":"select S.tid,S.filename,S.caption,S.description,S.relation,S.useragent,S.contenttype,S.is_generic,S.toolbar_icon_filepath,S.usage_resultset_data,S.stylesheet_type,S.jr_datasource from sx_stylesheets S, sx_mask_style M where S.tid=M.stylesheet_id and M.maskeninfo_id=$TID order by 1,2,3" },
  {"etl_step":"unload_stylesheet_field","prop_name":"select_stmt", "prop_value":"select F.tid,F.stylesheet_id,S.filename as stylesheet_filename,F.tablename,F.fieldname FROM stylesheet_field F, sx_stylesheets S, sx_mask_style M where F.stylesheet_id=S.tid and S.tid=M.stylesheet_id and M.maskeninfo_id=$TID order by 1,2,3,4,5" },

  {"etl_step":"delete_maskeninfo","prop_name":"select_stmt", "prop_value":"delete  from maskeninfo where tid=$TID" },
  {"etl_step":"upload_maskeninfo","prop_name":"target_table", "prop_value":"maskeninfo" },
  {"etl_step":"upload_maskeninfo","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_maskeninfo","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_maskeninfo\"]/rs/row" },
  {"etl_step":"upload_maskeninfo","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"delete_felderinfo","prop_name":"select_stmt", "prop_value":"delete from felderinfo where tid in  (select felderinfo_id from masken_felder_bez where maskeninfo_id =$TID)" },
  {"etl_step":"upload_felderinfo","prop_name":"target_table", "prop_value":"felderinfo" },
  {"etl_step":"upload_felderinfo","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_felderinfo","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_felderinfo\"]/rs/row" },
  {"etl_step":"upload_felderinfo","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"delete_masken_felder_bez","prop_name":"select_stmt", "prop_value":"delete from masken_felder_bez where maskeninfo_id =$TID" },
  {"etl_step":"upload_masken_felder_bez","prop_name":"target_table", "prop_value":"masken_felder_bez" },
  {"etl_step":"upload_masken_felder_bez","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_masken_felder_bez","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_masken_felder_bez\"]/rs/row" },
  {"etl_step":"upload_masken_felder_bez","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"delete_sachgeb_maske_bez","prop_name":"select_stmt", "prop_value":"delete from sachgeb_maske_bez where maskeninfo_id =$TID" },
  {"etl_step":"upload_sachgeb_maske_bez","prop_name":"target_table", "prop_value":"sachgeb_maske_bez" },
  {"etl_step":"upload_sachgeb_maske_bez","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_sachgeb_maske_bez","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_sachgeb_maske_bez\"]/rs/row" },
  {"etl_step":"upload_sachgeb_maske_bez","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"delete_maske_system_bez","prop_name":"select_stmt", "prop_value":"delete from maske_system_bez where maskeninfo_id =$TID" },
  {"etl_step":"upload_maske_system_bez","prop_name":"target_table", "prop_value":"maske_system_bez" },
  {"etl_step":"upload_maske_system_bez","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_maske_system_bez","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_maske_system_bez\"]/rs/row" },
  {"etl_step":"upload_maske_system_bez","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },
  {"etl_step":"create_tmp_etl_themenbaum","prop_name":"PATH_TO_INPUTFILE", "prop_value":"$SUPERX_DIR/db/module/etl/schluesseltabellen/create_load_etl_themenbaum.sql" },
  {"etl_step":"create_tmp_etl_stylesheets","prop_name":"PATH_TO_INPUTFILE", "prop_value":"$SUPERX_DIR/db/module/etl/schluesseltabellen/create_load_etl_stylesheets.sql" },
  
  {"etl_step":"upload_tmp_etl_themenbaum","prop_name":"target_table", "prop_value":"tmp_etl_themenbaum" },
  {"etl_step":"upload_tmp_etl_themenbaum","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_tmp_etl_themenbaum","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_themenbaum\"]/rs/row" },
  {"etl_step":"upload_tmp_etl_themenbaum","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"upload_tmp_etl_stylesheets","prop_name":"target_table", "prop_value":"tmp_etl_stylesheets" },
  {"etl_step":"upload_tmp_etl_stylesheets","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_tmp_etl_stylesheets","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_sx_stylesheets\"]/rs/row" },
  {"etl_step":"upload_tmp_etl_stylesheets","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },
 
  {"etl_step":"upload_tmp_etl_mask_style","prop_name":"target_table", "prop_value":"tmp_etl_mask_style" },
  {"etl_step":"upload_tmp_etl_mask_style","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_tmp_etl_mask_style","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_sx_mask_style\"]/rs/row" },
  {"etl_step":"upload_tmp_etl_mask_style","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"upload_tmp_etl_stylesheet_field","prop_name":"target_table", "prop_value":"tmp_etl_stylesheet_field" },
  {"etl_step":"upload_tmp_etl_stylesheet_field","prop_name":"format", "prop_value":"xml" },
  {"etl_step":"upload_tmp_etl_stylesheet_field","prop_name":"search_path", "prop_value":"/etlAction/unload [@name=\"unload_stylesheet_field\"]/rs/row" },
  {"etl_step":"upload_tmp_etl_stylesheet_field","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },

  {"etl_step":"themenbaum_fuellen","prop_name":"PATH_TO_INPUTFILE", "prop_value":"$SUPERX_DIR/db/module/etl/schluesseltabellen/themenbaum_fuellen.sql" },
  {"etl_step":"sx_stylesheets_fuellen","prop_name":"PATH_TO_INPUTFILE", "prop_value":"$SUPERX_DIR/db/module/etl/schluesseltabellen/sx_stylesheets_fuellen.sql" },
  
  {"etl_step":"kern_konstanten_update","prop_name":"path_to_inputfile", "prop_value":"$PATH_TO_INPUTFILE" },
  
  {"etl_step":"unload_sichten_ges","prop_name":"msg", "prop_value":"Entlade Sichten Komponente  $SYSTEMINFO_ID" },
  {"etl_step":"unload_sichtentab","prop_name":"select_stmt", "prop_value":"SELECT S.systeminfoid,S.art,S.type,S.name_intern,S.name,S.sortnr,S.quelle,S.standbutton FROM sichten S where S.systeminfoid= $SYSTEMINFO_ID and position(''<#'' in quelle) =0 order by art,sortnr ,name;" },
  {"etl_step":"unload_sichten_sachgeb","prop_name":"select_stmt", "prop_value":"SELECT A.sichtart,A.sachgebiete_id FROM sachgeb_sichtarten A where A.sichtart in (select distinct S.art from sichten S where S.systeminfoid= $SYSTEMINFO_ID) order by 1,2;" },

  {"etl_step":"unload_sos_gewichtung_ges","prop_name":"msg", "prop_value":"Entlade Gewichtungen Komponente  $SYSTEMINFO_ID" },
  {"etl_step":"unload_sos_gewichtung","prop_name":"select_stmt", "prop_value":" SELECT art, ch35_ang_abschluss, fach_nr, kz_fach, text, faktor FROM sos_gewichtung  where ch35_ang_abschluss in (''B5'',''B6'',''B7'',''B8'',''M3'',''M4'',''M7'',''M8'') and art=''vzae'' order by 1,2,3,4,5  ;" }


  
  ] />
<#assign testfaelle  = [
  {"testcase":"test_sx_select_mask","assertion":1, "sql":"select count(*) from etl_job where uniquename='sx_select_mask'" },
  {"testcase":"test_sx_insert_mask","assertion":1, "sql":"select count(*) from etl_job where uniquename='sx_insert_mask'" },
  {"testcase":"test_sx_insert_mask_params","assertion":3, "sql":"select count(*) from etl_job_param P, etl_job J where J.tid=P.etl_job_id and J.uniquename='sx_insert_mask'" },
  {"testcase":"test_sx_insert_mask_steps","assertion":18, "sql":"select count(*) from etl_step S, etl_step_relation R, etl_job J where J.tid=R.job_id and S.tid=R.step_id and J.uniquename='sx_insert_mask'" },
  {"testcase":"test_sx_select_mask_steps","assertion":10, "sql":"select count(*) from etl_step S, etl_step_relation R, etl_job J where J.tid=R.job_id and S.tid=R.step_id and J.uniquename='sx_select_mask'" }

 ] />
 
create temp table tmp_etl_step(
tid INTEGER, 
uniquename VARCHAR(255) , 
caption VARCHAR(255) , 
systeminfo_id INTEGER  not null, 
step_type INTEGER, 
step_type_uniquename VARCHAR(255), 
sortnr SMALLINT  not null, 
force_continue SMALLINT,
etl_job_id INTEGER , 
parent_step_id INTEGER , 
parent_step_uniquename varchar(255),
parent_job_uniquename varchar(255),
logfile  varchar(255),
custom_step smallint,
already_exists smallint
) 
;

create temp table tmp_etl_job(
	tid INTEGER, 
uniquename VARCHAR(255) , 
caption VARCHAR(255) , 
systeminfo_id INTEGER  not null,
logfile  varchar(255),
already_exists smallint,
custom_job smallint
) 
;

create temp table tmp_etl_job_param(
	tid SERIAL  not null, 
etl_job_id INTEGER , 
uniquename VARCHAR(255)  not null, 
name VARCHAR(255) , 
param_default VARCHAR(255) 
) 
;


create temp table tmp_etl_step_property(
	tid SERIAL  not null, 
etl_step_id INTEGER  not null, 
prop_name VARCHAR(255) , 
prop_value text

) 
;

create temp table tmp_etl_step_relation(
	tid SERIAL  not null, 
step_id INTEGER  not null, 
parent_step_id INTEGER , 
job_id INTEGER  not null, 
force_continue SMALLINT  default 1 , 
step_active SMALLINT  default 1, 
sortnr SMALLINT  default 1,
custom_step  SMALLINT  default 1

) 
;

--jobs with custom_job=0 get deleted in HISinOne-BI
--therefore set to 2:
update etl_job set custom_job=2
where custom_job=0
and uniquename in ('sx_select_mask','sx_insert_mask',
'kern_konstanten_update',
'sichten_unload',
'sos_gewichtung_unload');


update etl_step set custom_step=2
where custom_step=0
and tid in (select R.step_id from etl_step_relation R, etl_job J
where R.job_id=J.tid
and J.uniquename in ('sx_select_mask','sx_insert_mask',
'kern_konstanten_update',
'sichten_unload',
'sos_gewichtung_unload'));

update etl_step_relation set custom_step=2
where custom_step=0
and job_id in (select J.tid
from etl_job J
where J.uniquename in ('sx_select_mask','sx_insert_mask',
'kern_konstanten_update',
'sichten_unload',
'sos_gewichtung_unload'));

<#foreach etl_job in etl_jobs>

truncate table tmp_etl_job;
truncate table tmp_etl_step;
truncate table tmp_etl_job_param;
truncate table tmp_etl_step_property;
truncate table tmp_etl_step_relation;

--tids der jobs dürfen sich nicht ändern, daher 
--
-- 1. vorh. Jobs updaten
-- 2. neue Jobs einfügen
-- 3. alte Jobs löschen

insert into tmp_etl_job(uniquename,caption,systeminfo_id,logfile,already_exists,custom_job)
values ('${etl_job.uniquename}',
'${etl_job.name}',
${etl_job.systeminfo_id},
'${etl_job.logfile}',0,2);

<#if SQLdialect='Postgres'>
--Postgres Dialekt:

update tmp_etl_job set tid=J.tid,
already_exists=1,
caption=J.caption,
logfile=J.logfile,
custom_job=J.custom_job
from  etl_job J where J.uniquename=tmp_etl_job.uniquename
and J.systeminfo_id=tmp_etl_job.systeminfo_id
;

<#else>

--Informix Dialekt:
update tmp_etl_job set (tid,
already_exists,
caption,
logfile,
custom_job) 
       = ((select 
       tid,
1 as already_exists,
caption,
logfile,
custom_job
from  etl_job J where J.uniquename=tmp_etl_job.uniquename
and J.systeminfo_id=tmp_etl_job.systeminfo_id))
where 0 <(select count(*)
from  etl_job J where J.uniquename=tmp_etl_job.uniquename
and J.systeminfo_id=tmp_etl_job.systeminfo_id)

;

</#if>



--TODO Informix

--neue jobs:
insert into etl_job(uniquename,caption,systeminfo_id,custom_job)
select uniquename,caption,systeminfo_id,custom_job
from tmp_etl_job
where already_exists=0;
--tid von neuen Jobs ermitteln:
update tmp_etl_job set tid=(select J.tid
from  etl_job J where J.uniquename=tmp_etl_job.uniquename
and J.systeminfo_id=tmp_etl_job.systeminfo_id)
where already_exists=0
;

--TODO
--obsolete Jobs: sollen  bei Deinstallation des Moduls entfernt werden


--Parameter:
<#foreach etl_job_param in etl_job_params>
<#if etl_job_param.etl_job==etl_job.uniquename>

insert into tmp_etl_job_param(
etl_job_id  , 
uniquename, 
name , 
param_default)
select J.tid,
'${etl_job_param.param_name}',
'${etl_job_param.name}',
'${etl_job_param.param_default}'
from tmp_etl_job J
;
</#if>
</#foreach>



--ETL-Schritte 
<#assign sortnr=0 />
<#foreach etl_step in etl_steps>
<#if etl_step.etl_job==etl_job.uniquename>

<#assign sortnr=sortnr+1 />

insert into tmp_etl_step(
uniquename , 
caption  , 
systeminfo_id  , 
step_type_uniquename,
sortnr, 
force_continue,
etl_job_id , 
parent_step_uniquename,
parent_job_uniquename,
logfile,
custom_step,
already_exists
)
select '${etl_step.uniquename}',
'${etl_step.name}',
${etl_job.systeminfo_id},
'${etl_step.type}' as step_type_uniquename,
${sortnr}*10 as sortnr,
0 as force_continue,
J.tid as etl_job_id,
<#if etl_step.parent?exists &&  etl_step.parent !="" >
'${etl_step.parent}',
<#else>
'' as parent_step_uniquename,
</#if>
J.uniquename,
'${etl_job.logfile}' as logfile,
2,
0
from etl_job J
where J.uniquename='${etl_job.uniquename}'
and J.systeminfo_id=${etl_job.systeminfo_id};




</#if> --Ende steps eines job
</#foreach>

--erst job-params einfügen:

delete from etl_job_param
where etl_job_id in (
 SELECT distinct 
       etl_job_id
FROM tmp_etl_job_param )
 ;

insert into etl_job_param
(
       etl_job_id,
       uniquename,
       name,
       param_default
       )
 SELECT 
       etl_job_id,
       uniquename,
       name,
       param_default
FROM tmp_etl_job_param 
 ;

--nun steps einfügen:


update tmp_etl_step set step_type=(select T.tid from etl_step_type T
where T.uniquename=tmp_etl_step.step_type_uniquename);

--select * from tmp_etl_step
--where step_type is null;--_uniquename from tmp_etl_step;


--vorhandene Steps erkennen:
<#if SQLdialect='Postgres'>
--Postgres Dialekt:

update tmp_etl_step set tid=S.tid,
already_exists=1,
caption=S.caption,
logfile=S.logfile,
custom_step=S.custom_step
from  etl_step S where S.uniquename=tmp_etl_step.uniquename
and S.systeminfo_id=tmp_etl_step.systeminfo_id
;
<#else>

--Informix Dialekt:
update tmp_etl_step set (tid,
already_exists,
caption,
logfile,
custom_step) 
       = ((select 
       tid,
1 as already_exists,
caption,
logfile,
custom_step
from  etl_step S where S.uniquename=tmp_etl_step.uniquename
and S.systeminfo_id=tmp_etl_step.systeminfo_id))
where 0 <(select count(*)
from  etl_step S where S.uniquename=tmp_etl_step.uniquename
and S.systeminfo_id=tmp_etl_step.systeminfo_id)
;



</#if>


--neue Steps einfügen:
insert into etl_step(
       uniquename,
       caption,
       systeminfo_id,
       step_type_id,
       logfile,
       custom_step)
select 
       uniquename,
       caption,
       systeminfo_id,
       step_type,
       logfile,
       custom_step
FROM tmp_etl_step 
where already_exists=0
;

--tid von neuen steps ermitteln:
update tmp_etl_step set tid=(select S.tid
from  etl_step S where S.uniquename=tmp_etl_step.uniquename
and S.systeminfo_id=tmp_etl_step.systeminfo_id)
where already_exists=0
;

--parent ermitteln:
update tmp_etl_step set parent_step_id=(select S.tid
from  etl_step S where S.uniquename=tmp_etl_step.parent_step_uniquename
and S.systeminfo_id=tmp_etl_step.systeminfo_id)
;


delete from etl_step_property
where etl_step_id in (select T.tid
FROM tmp_etl_step T )
 ;
delete from etl_step_relation
where job_id in (select J.tid
FROM  tmp_etl_job J)
and custom_step=2
 ;
 

--jetzt step-params:

<#foreach etl_step_property in etl_step_properties>
insert into tmp_etl_step_property(
  etl_step_id,
       prop_name)
select 
  T.tid as etl_step_id,
  '${etl_step_property.prop_name}'
 
FROM tmp_etl_step T 
where uniquename ='${etl_step_property.etl_step}'
;

</#foreach>

--einfügen  in echte Tabelle:
insert into etl_step_property(
  etl_step_id,
       prop_name)
select 
  T.etl_step_id,
  T.prop_name
FROM  tmp_etl_step_property T 
;

--jetzt step-relation:
insert into tmp_etl_step_relation(
 step_id,
       parent_step_id,
       job_id,
       force_continue,
       step_active,
       sortnr,
       custom_step
)
select 
  tid,
       parent_step_id,
       etl_job_id,
       0 as force_continue,
       1 as step_active,
       sortnr,
       2
FROM tmp_etl_step 
 ;
 
insert into etl_step_relation(
 step_id,
       parent_step_id,
       job_id,
       force_continue,
       step_active,
       sortnr,
       custom_step
)
select 
 step_id,
       parent_step_id,
       job_id,
       force_continue,
       step_active,
       sortnr,
       custom_step
FROM tmp_etl_step_relation 
 ;
</#foreach> --Ende job


drop table tmp_etl_step;
drop table tmp_etl_job;
drop table tmp_etl_job_param;
drop table tmp_etl_step_property;
drop table tmp_etl_step_relation;


<#foreach testfall in testfaelle>
select 'testfall ${testfall.testcase}: ERFOLG'
from xdummy
where ${testfall.assertion}=(${testfall.sql})
;
select 'testfall ${testfall.testcase}: FEHLER bei ${testfall.testcase}'
from xdummy
where ${testfall.assertion}!=(${testfall.sql})
;


</#foreach> --Ende Testfälle


<#foreach etl_step_property in etl_step_properties>

update etl_step_property set prop_value='${etl_step_property.prop_value}' where prop_name='${etl_step_property.prop_name}' and etl_step_id=(select S.tid from etl_step S where S.uniquename='${etl_step_property.etl_step}');

</#foreach>



