--Freemarker Template
<sqlvars>

<sqlvar name="kenn_dim_fgr_exists">
select sp_table_exists('kenn_dim_fgr') from xdummy
</sqlvar>
</sqlvars>

<#assign  dimensions = [
  {"key":"400", "name":"Statustyp (CO)"},
  {"key":"401", "name":"Hörerstatus (CO)"},
  {"key":"90", "name":"Fakultät/Fachbereich"}
  ] />
  
 <#assign  dimension_levels_man = [
  {"key":"400","apnr":"a", "druck":"Studienplatz angenommen", "astat":""},
  {"key":"400","apnr":"B", "druck":"Neueinschreibung", "astat":"2"},
  {"key":"400","apnr":"E", "druck":"Ersteinschreibung", "astat":"1"},
  {"key":"400","apnr":"I", "druck":"gemeldet", "astat":"3"},
  {"key":"400","apnr":"U", "druck":"beurlaubt", "astat":"4"},
  {"key":"400","apnr":"R", "druck":"Rücktritt von der Immatrikulation", "astat":""},
  {"key":"400","apnr":"o", "druck":"Studium offen (noch keine Weitermeldung erfolgt)", "astat":""},
  {"key":"400","apnr":"V", "druck":"Verzicht auf Studienplatz", "astat":""},
  {"key":"400","apnr":"X", "druck":"geschlossen (Abschluss und/oder keine Fortsetzung möglich)", "astat":"5"},
  {"key":"400","apnr":"Z", "druck":"geschlossen", "astat":"5"},
  {"key":"400","apnr":"z", "druck":"wieder einzuschreiben", "astat":""}
 ] />
 
 <#assign  dimension_levels_dynamic = [
  {"key":"401","apnr":"hoererstatus_kb", "druck":"hoererstatus_name", "astat":"", "srctable":"costage_st_hoererstatus", "astat":"", "hs":"", "struktur_c":""},
  {"key":"90","apnr":"fakultaet_key", "druck":"fakultaet_bezeichnung", "srctable":"costage_st_fakultaeten", 
  "astat":"",
  "struktur_c":"",
  "hs":"unikey"}

] />

 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 dimension_level in dimension_levels_dynamic>
 <#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 distinct 
      ${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 ,
<#if dimension_level.struktur_c=="">
     NULL::char(10)
     <#else>
     ${dimension_level.struktur_c}
     </#if>  as struktur_c
     from  ${dimension_level.srctable}
     <#if dimension_level.hs!="">
     where ${dimension_level.hs}=(select hs_nr::char(4) from hochschulinfo)
     </#if>
     ;
   </#if>
 </#foreach>

 </#foreach>

 
<#if kenn_dim_fgr_exists==1>
 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 
 621 as key  ,
    0 as hs,
     '0' || trim(apnr) || '0' as apnr,
     substring(drucktext from 1 for 10) as kurz ,
     drucktext as druck,
     drucktext as lang_1 ,
 apnr as astat,
     '0' || trim(apnr) || '0' as uniquename  , 
    date_val('01.01.1900') as d_akt_von ,
     date_val('01.01.3000')d_akt_bis ,
     apnr as sourcesystem_id  ,
     15 as sourcesystem,
'0' || trim(apnr) || '0' as lid  ,
--parent_lid,
NULL::char(10) as hiskey_id,   
     '' as struktur_c
     from kenn_dim_fgr
          where string_not_null(fg_studierende)!='';
     
</#if>
--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,
       310 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;
