
---------------------------------------------------
prompt Configuration table level triggers
---------------------------------------------------

create or replace trigger sdd_cfg_br
before insert or update on i$sdd_configurations
for each row
begin
  if inserting then
    jr_audit.pre_insert(:new.irid, :new.ivid);
  end if;
  if updating then
    jr_audit.pre_update(:new.notm, :new.date_changed, :new.changed_by);
  end if;
end sdd_cfg_br;
/

create or replace trigger sdd_cfg_ar
after insert or update or delete on i$sdd_configurations
for each row
declare
  type_irid   number;
  table_irid  number;
  curr_wa     number :=jr_context.workarea;
  null_wa     number :=NULL;
  chk_wa_ctxt boolean:=jr_context.is_enabled(jr_context.CHECK_WORKAREA_CONTEXT);
  ins_fol_mem boolean:=jr_context.is_enabled(jr_context.FOLDER_MEMBERS); 
begin
  --Disable workarea ctxt checks, and insert of folder member 
  --(not appropriate for configurations!)
  jr_context.disable(jr_context.CHECK_WORKAREA_CONTEXT);
  jr_context.disable(jr_context.FOLDER_MEMBERS);
  --Ensure null workarea context to prevent config appearing in workarea
  jr_context.set_workarea(null_wa);
  if inserting then
    -- CW 27-Jul-20000
    -- Fix bug 1359789, the following query returns more than one if a type called
    -- 'CONFIGURATION' is registered.
    -- Fix by qualifying the system type called configuration with product = 'SDD'
    -- *and* joining to sdd_configurations explicitly

    select tab.irid
         , et.irid
    into   table_irid
         , type_irid
    from   rm_sql_tables    tab
         , rm_element_types et
         , rm_sql_row_types srt
    where  tab.table_name = 'SDD_CONFIGURATIONS'
    and    et.name        = 'CONFIGURATION'
    and    et.product     = 'SDD'
    and    srt.irid       = et.primary_row_type
    and    tab.irid       = srt.table_mapped
    ;
   
    jr_object_version.ins ( :new.irid
                          , :new.ivid
                          , :new.name
                          , type_irid
                          , table_irid
                          , null       -- no parent ivid column
                          , :new.date_created
                          , :new.created_by
                          , :new.notm
                          );   
  end if; -- inserting

  if updating then
    jr_object_version.upd ( :old.ivid
                          , null       -- no parent ivid column
                          , :new.name
                          , :new.date_changed
                          , :new.changed_by
                          , :new.notm
                          );
  end if; -- updating

  if deleting then
    -- Cannot call jr_object_versions.del as this deletes the config member where this config is a *member*
    -- which does not apply to configurations, they cannot be nested
    
    -- No need to delete configuration members, these are done via cascade delete foreign key

    --Put row into deleted objects, object version corresponding to this configuration 
    --will be deleted by statement level trigger (in order to avoid mutating table errors).
    insert into i$sdd_deleted_objects (object_irid, object_ivid)
    values (:old.irid, :old.ivid);

    -- Delete dependencies
    jr_reg_ver.delete_dependencies(:old.irid, :old.ivid);

  end if; -- deleting

  --Reset values on JR_CONTEXT  
  if chk_wa_ctxt then
   jr_context.enable(jr_context.CHECK_WORKAREA_CONTEXT);
  end if;
  if ins_fol_mem then
   jr_context.enable(jr_context.FOLDER_MEMBERS);
  end if;
  jr_context.set_workarea(curr_wa);                  
exception
 when OTHERS then
  if chk_wa_ctxt then
   jr_context.enable(jr_context.CHECK_WORKAREA_CONTEXT);
  end if;
  if ins_fol_mem then
   jr_context.enable(jr_context.FOLDER_MEMBERS);
  end if;
  jr_context.set_workarea(curr_wa);
  RAISE;
end sdd_cfg_ar;
/

create or replace trigger sdd_cfg_ad
  after delete on i$sdd_configurations
  declare
    cursor del_cur is
      select object_irid, object_ivid
      from   i$sdd_deleted_objects
      where  config_irid is null;
  begin
    for del_rec in del_cur loop
      --Config members are deleted by cascade foriegn key.
      delete i$sdd_object_versions
      where  irid=del_rec.object_irid
      and    ivid=del_rec.object_ivid;

      delete i$sdd_deleted_objects
      where  object_irid=del_rec.object_irid
      and    object_ivid=del_rec.object_ivid;
    end loop;
  end i$sdd_configurations_dval;
/

---------------------------------------------------
prompt Configuration view 'instead-of' triggers
---------------------------------------------------

create or replace trigger sdd_configurations_i
instead of insert on sdd_configurations
for each row
declare
  cfg_irid number;
  cfg_ivid number;
begin
  jr_configuration_def.create_configuration( :new.name
                                           , cfg_irid
                                           , cfg_ivid
                                           , :new.description);
end sdd_configurations_i;
/

create or replace trigger sdd_configurations_u
instead of update on sdd_configurations
for each row
declare
begin
  if :new.name != :old.name then
    jr_configuration_def.rename_configuration( :old.irid
                                             , :old.ivid
                                             , :new.name);
  end if;
  if :new.description != :old.description then
    jr_configuration_def.change_configuration_desc( :old.irid
                                                  , :old.ivid
                                                  , :new.description);
  end if;
end sdd_configurations_u;
/

create or replace trigger sdd_configurations_d
instead of delete on sdd_configurations
for each row
declare
begin
  jr_configuration_def.remove_configuration(:old.irid, :old.ivid);
end sdd_configurations_d;
/


--B1280348 - Remove configuration member table level triggers to improve performance


-- CW 15-Sep-1999 Fix bug 964423 remove the use of config member triggers
-- Configuration members (primary objects) are now validated in the same way as secondary
-- objects, ie. the _inv and _val triggers fire on insert as well as update
-- Validation is not required when building configurations
-- Obsoleting triggers i$sdd_config_members_invalid and i$sdd_config_members_delete
