--Freemarker Template

<#assign  dimensions = [
  {"key":"8", "name":"Bundesland"}
  ] />
  
 <#assign  dimension_levels_man = [
{"key":"8","apnr":"BW", "druck":"Baden-Württemberg", "astat":"08"},
{"key":"8","apnr":"BY", "druck":"Bayern", "astat":"09"},
{"key":"8","apnr":"BE", "druck":"Berlin", "astat":"11"},
{"key":"8","apnr":"BB", "druck":"Brandenburg", "astat":"12"},
{"key":"8","apnr":"HB", "druck":"Bremen", "astat":"04"},
{"key":"8","apnr":"HH", "druck":"Hamburg", "astat":"02"},
{"key":"8","apnr":"HE", "druck":"Hessen", "astat":"06"},
{"key":"8","apnr":"MV", "druck":"Mecklenburg-Vorpommern", "astat":"13"},
{"key":"8","apnr":"NI", "druck":"Niedersachsen", "astat":"03"},
{"key":"8","apnr":"NW", "druck":"Nordrhein-Westfalen", "astat":"05"},
{"key":"8","apnr":"RP", "druck":"Rheinland-Pfalz", "astat":"07"},
{"key":"8","apnr":"SL", "druck":"Saarland", "astat":"10"},
{"key":"8","apnr":"SN", "druck":"Sachsen", "astat":"14"},
{"key":"8","apnr":"ST", "druck":"Sachsen-Anhalt", "astat":"15"},
{"key":"8","apnr":"SH", "druck":"Schleswig-Holstein", "astat":"01"},
{"key":"8","apnr":"TH", "druck":"Thüringen", "astat":"16"}
 ] />
 

 create temp table tmp_cifx
   (tid serial,
      key smallint not null ,
    hs integer not null,
     apnr varchar(255)  ,
     kurz char(10),
     druck varchar(200),
     lang_1 char(255),
     astat char(10) ,
     parent varchar(255) , 
     uniquename VARCHAR(255) , 
     bund_apnr CHAR(10) , 
     sort1 integer,
     sortc1 char(10),
     d_akt_von date,
     d_akt_bis date,
     sourcesystem_id varchar(255),
     sourcesystem    integer,
lid varchar(255)  ,
parent_lid varchar(255),
     hiskey_id varchar(255),
     struktur_c CHAR(50)
     );
create temp table tmp_hilf(tid integer);
insert into tmp_hilf(tid) select max(tid) from cifx;



<#foreach dimension in dimensions>
 <#foreach dimension_level in dimension_levels_man>
 <#if dimension_level.key==dimension.key >

 
insert into tmp_cifx
   (
    key  ,
    hs,
     apnr  ,
     kurz ,
     druck,
     lang_1 ,
 astat,
     uniquename  , 
     d_akt_von ,
     d_akt_bis ,
     sourcesystem_id ,
     sourcesystem,
lid  ,
--parent_lid,
    hiskey_id ,
     struktur_c
     )
     select 
      ${dimension_level.key}  ,
    0 as hs,
     '${dimension_level.apnr}'  ,
     substring('${dimension_level.druck}' from 1 for 10) as kurz ,
     substring('${dimension_level.druck}' from 1 for 200) as druck,
     substring('${dimension_level.druck}' from 1 for 255) as lang_1 ,
     <#if dimension_level.astat=="">
     NULL::char(10)
     <#else>
     '${dimension_level.astat}'
     </#if>  as astat, 
     '${dimension_level.apnr}' as uniquename  , 
     date_val('01.01.1900') as d_akt_von ,
     date_val('01.01.3000')d_akt_bis ,
     '${dimension_level.apnr}' as sourcesystem_id ,
     15 as sourcesystem,
'${dimension_level.apnr}' as lid  ,
--parent_lid,
NULL::char(10) as    hiskey_id ,
NULL::char(10) as struktur_c
     from  xdummy
     ;
     
  </#if>
 </#foreach>
</#foreach>
 
  --Duplikate raus:
select T.key,T.apnr 
from tmp_cifx T
group by 1,2
having count(*)>1;

 
--Künstliche Schlüssel aus CO:

 
 --vorh. Datensätze ändern:
update cifx set hs=T.hs ,
       apnr=T.apnr ,
       d_akt_von=T.d_akt_von ,
       d_akt_bis=T.d_akt_bis ,
       kurz=T.kurz ,
       druck=T.druck ,
       lang_1=T.lang_1 ,
       parent=T.parent ,
       astat=T.astat ,
       hiskey_id=T.hiskey_id ,
       uniquename=T.uniquename ,
       sort1=T.sort1 ,
       sortc1=T.sortc1 ,
       sourcesystem=T.sourcesystem ,
       sourcesystem_id=T.sourcesystem_id ,
       struktur_c=T.struktur_c ,
       lid=T.lid ,
       parent_lid=T.parent_lid
       from tmp_cifx T
       where T.key=cifx.key
       and T.apnr=cifx.apnr
       and T.sourcesystem=cifx.sourcesystem;

delete from tmp_cifx where (key,apnr)
in (select key,apnr
from cifx);


--neue Datensätze:



insert into cifx
(tid ,
       hs,
       key,
       apnr,
       d_akt_von,
       d_akt_bis,
       kurz,
       druck,
       lang_1,
       parent,
       astat,
       hiskey_id,
       uniquename,
       sort1,
       sortc1,
       sourcesystem,
       sourcesystem_id,
       struktur_c,
       lid,
       parent_lid
       )
       select T.tid+H.tid as tid,
       hs,
       key,
       apnr,
       d_akt_von,
       d_akt_bis,
       kurz,
       druck,
       lang_1,
       parent,
       astat,
       hiskey_id,
       uniquename,
       sort1,
       sortc1,
       sourcesystem,
       sourcesystem_id,
       struktur_c,
       lid,
       parent_lid
       from tmp_cifx T, tmp_hilf H
       where druck is not null
       ;
       
insert into trans_cifx(
      key,
       apnr,
       cifx_tid,
       sourcesystem,
       sourcesystem_id,
       systeminfo_id)
select 
      T.key,
       T.apnr,
       C.tid,
       T.sourcesystem,
       T.sourcesystem_id,
       360 as systeminfo_id
FROM tmp_cifx T, cifx C
where C.key=T.key
and T.apnr=C.apnr;


drop table tmp_cifx;
drop table tmp_hilf;
