--freemarker template
<#if SQLdialect='Postgres'>
select setval('sichten_tid_seq',(select max(tid) from sichten));
</#if>
<#assign systeminfoid="1000" />
<#assign  sichten = [
  {"name_intern":"xcube_dim_alter", 
  "name":"Alter (gruppiert in 5er-Schritten)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''alter'' order by 2",
  "art":"XCUBE-Alter-Sicht"},
  {"name_intern":"xcube_dim_wohnort", 
  "name":"Wohnort nach Bundesländern",
  "quelle":"<<SQL>> select druck ,apnr, parent from xcube_wohnort_staat order by 1",
  "art":"XCUBE-Wohnort-Sicht"},
  {"name_intern":"xcube_dim_hzbnote", 
  "name":"HZB-Note (gruppiert)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''hzbnote'' order by 2",
  "art":"XCUBE-HZB-Note-Sicht"},
  {"name_intern":"xcube_dim_hoererstatus", 
  "name":"Hörerstatus (intern)",
  "quelle":"<<SQL>> select druck ,apnr, parent from xcube_hoererstatus order by 1",
  "art":"XCUBE-Hörerstatus-Sicht"},
  {"name_intern":"xcube_dim_note", 
  "name":"Note (gruppiert)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''note'' order by 2",
  "art":"XCUBE-Note-Sicht"},
  {"name_intern":"xcube_dim_alter1", 
  "name":"Alter (starke Aufteilung)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''alter1'' order by 2",
  "art":"XCUBE-Alter-Sicht"},
  {"name_intern":"xcube_dim_alter2", 
  "name":"Alter (spez. für Studierende/Prüf.)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''alter2'' order by 2",
  "art":"XCUBE-Alter-Sicht"},
  {"name_intern":"xcube_dim_alter3", 
  "name":"Alter (einzeln)",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''alter3'' order by 2",
  "art":"XCUBE-Alter-Sicht"},
  {"name_intern":"xcube_dim_hzb_zul", 
  "name":"Hochschulzugangsber",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''hzb_zul'' order by 2",
  "art":"XCUBE-HZB-ZUL-Sicht"},
  {"name_intern":"xcube_dim_zul_erg", 
  "name":"Zulassungsergebnis",
  "quelle":"<<SQL>> select name,apnr,parent,strukturstr from xcube_dims where id=''zul_erg'' order by 2",
  "art":"XCUBE-ZUL-Erg-Sicht"}
  ] />
  
  
--Ab hier braucht man nichts ändern:

CREATE temp TABLE tmp_sichten (
    parent character(255),
    systeminfoid integer DEFAULT 0 NOT NULL,
    art character(40),
    type integer ,
    name_intern character(200),
    name character(200),
    beschreibung character(255),
    sortnr integer DEFAULT 0 NOT NULL,
    quelle character(255),
    alt_hier_id character(150),
    treecfgtable character(255),
    treecfgid character(150),
    label smallint DEFAULT 0 NOT NULL,
    user_rechte smallint DEFAULT 1 NOT NULL,
    rechtequelle character(255),
    sesamkey character(100),
    standbutton smallint DEFAULT 0 NOT NULL,
    attribut1 character(255),
    attribut2 character(255),
    attribut3 integer,
    attribut4 integer,
    xmlmaxentries integer,
    gueltig_seit date ,
    gueltig_bis date,
    aktiv smallint DEFAULT 1
);
<#assign sortnr=0 />
<#foreach sicht in sichten>
<#assign sortnr=sortnr+1 />
--  Sicht ${sicht.name}

INSERT INTO tmp_sichten(
  systeminfoid  , 
  art  , 
  type  , 
  name_intern  , 
  name  , 
  sortnr  ,
  quelle  ,
  label  , 
  user_rechte  ,
  standbutton  , 
    gueltig_seit  ,
   gueltig_bis  , 
   aktiv
   ) select 
  ${systeminfoid}  ,
  '${sicht.art}'  ,
  10  ,
  '${sicht.name_intern}'  , 
  '${sicht.name}'  ,
  ${sortnr}  , 
  '${sicht.quelle}', 
  0  , 
  0  ,  
  0  , 
  date_val('01.01.1900')  , 
  date_val('01.01.3000') , 
  1 from xdummy;
</#foreach>   

--Zur Sicherheit alle Quellen und Standbutton von Sichten 
update sichten set quelle=(select T.quelle from tmp_sichten T
  where T.name_intern=sichten.name_intern)
  where name_intern in (select T2.name_intern from tmp_sichten T2)
  ;
update sichten set standbutton=(select T.standbutton from tmp_sichten T
  where T.name_intern=sichten.name_intern)
  where name_intern in (select T2.name_intern from tmp_sichten T2)
  ;

delete from tmp_sichten where name_intern in (select name_intern from sichten);


  INSERT INTO sichten(
     systeminfoid  , art  , type  , name_intern  , name  , 
    sortnr  , quelle  , label  , 
    user_rechte  ,  standbutton  , 
     xmlmaxentries  , 
    gueltig_seit  , gueltig_bis  , aktiv)
    select systeminfoid  , art  , type  , name_intern  , name  , 
    sortnr  , quelle  , label  , 
    user_rechte  ,  standbutton  , 
     xmlmaxentries  , 
    gueltig_seit  , gueltig_bis  , aktiv
    from tmp_sichten S;


--Rechte für Sichten geben
select 'Rechte für Sichten geben' from xdummy;
delete from sachgeb_sichtarten where sichtart in 
(select distinct art from tmp_sichten);

insert into sachgeb_sichtarten
(
   sachgebiete_id ,
   sichtart 
)
select distinct ${systeminfoid},art from tmp_sichten;


drop table tmp_sichten;

