--
--  Type specific handling for objects.
--
--  This code implements a registry of functions that can be used to perform operations on
--  different types. The Data could be held in the Object_type table but it's in a seperate table for now.
--  I believe that a generic copy/version function is unlikely to work for user defined types unless they are very
--  similar to the types that we tested such a function for.
--
--  Copy and version functions have the same prototype. However, copy returns a new oid whereas
--  version returns a new vseq.
--  The prototype is (oid number, vseq number).
--
prompt Installing default plugins....

----------------------------- CONFIGURATIONS  --------------------------------------------------------------------

insert into i$sdd_version_registry (table_irid, table_ivid, copy_function, new_version_function)
select irid, ivid, 'JR_CONFIGURATION_COPY', 'JR_CONFIGURATION_VERSION'
from   rm_sql_tables
where  table_name='SDD_CONFIGURATIONS';

create or replace function jr_configuration_version ( o_irid            number
                                                    , o_ivid            number
                                                    , copy_dependencies boolean default true
                                                    ) return number as
  new_ivid  number;
  cursor config_cur is 
    select irid, ivid, name, tracking, description
    from   i$sdd_configurations
    where  irid = o_irid
    and    ivid = o_ivid;
  this_config config_cur%rowtype;
begin
  new_ivid := jr_system_util.get_new_ivid;
  
  open config_cur;
  fetch config_cur into this_config;
  
  insert into i$sdd_configurations 
    (irid, ivid, name, tracking, description)
  values 
    (this_config.irid, new_ivid, this_config.name, this_config.tracking, this_config.description);

  close config_cur;
  
  -- No need to update object versions, done in jr_version

  -- Copy all the configuration members
  insert into i$sdd_configuration_members (config_irid, config_ivid, object_irid, object_ivid)
  select config_irid, new_ivid, object_irid, object_ivid
  from   i$sdd_configuration_members
  where  config_irid = o_irid
  and    config_ivid = o_ivid;

  -- Copy any dependencies referencing the configuration
  -- TODO : Dependencies not currently supported to/from configurations
  --        Uncomment line below when they are (this code should work now but will just slow
  --        down versioning as the query for dependencies will fire, but find no rows)
  -- jr_reg_ver.vercopy_dependencies(o_irid, o_ivid, new_ivid, null, copy_dependencies);

  return new_ivid;
end jr_configuration_version;
/

rem TODO: very lame name uniqueness generation here!
create or replace function jr_configuration_copy ( o_irid number
                                                 , o_ivid number
                                                 , copy_dependencies boolean default true
                                                 ) return number AS
  new_irid  number;
  new_ivid  number;
  cursor config_cur is 
    select irid, ivid, name, tracking, description
    from   i$sdd_configurations
    where  irid = o_irid
    and    ivid = o_ivid;
  this_config config_cur%rowtype;
  copy_deps   varchar2(1);
begin
  new_irid := jr_util.get_new_irid;
  new_ivid := jr_system_util.get_new_ivid;

  open config_cur;
  fetch config_cur into this_config;
  
  insert into i$sdd_configurations (irid, ivid, name, tracking, description)
  values 
    (new_irid, new_ivid, this_config.name || '1', this_config.tracking, this_config.description);

  close config_cur;

  if copy_dependencies then
    copy_deps := 'Y';
  else
    copy_deps := 'N';
  end if;

  -- Copy across audit columns from version being copied
  -- Copy obj_notm_when_analyzed only if dependencies have been copied

  update i$sdd_object_versions
  set    (vlabel, notm, obj_notm, date_created, created_by, date_changed, changed_by, obj_notm_when_analyzed)
         = (select old.vlabel
                 , old.notm
                 , old.obj_notm
                 , old.date_created
                 , old.created_by
                 , old.date_changed
                 , old.changed_by
                 , decode( copy_deps
                         , 'Y',  old.obj_notm_when_analyzed
                         , null
                         )
            from   i$sdd_object_versions old
            where  old.ivid = o_ivid
           )
  where  ivid = new_ivid ;

  -- Copy all the configuration members
  insert into i$sdd_configuration_members (config_irid, config_ivid, object_irid, object_ivid)
  select new_irid, new_ivid, object_irid, object_ivid
  from   i$sdd_configuration_members
  where  config_irid=o_irid
  and    config_ivid=o_ivid;

  -- Copy any dependencies referencing the configuration
  -- TODO : Dependencies not currently supported to/from configurations
  --        Uncomment line below when they are (this code should work now but will just slow
  --        down versioning as the query for dependencies will fire, but find no rows)
  -- jr_reg_ver.cpycopy_dependencies(o_irid, o_ivid, new_irid, new_ivid, null, copy_dependencies);

  return new_irid;
end jr_configuration_copy;
/


