Rem    NAME 
Rem      ckcisysf.sql
Rem    DESCRIPTION
Rem      Check-in System Folder and Contents 
Rem
Rem    RETURNS
Rem 	 None
Rem    NOTES
Rem      Called from the RAU during the stage CKCISYSF
Rem	 DO NOT RUN THIS SCRIPT MORE THEN ONCE
Rem    CREATED
Rem	  08/04/1999

DECLARE
   rtn_id 			   number;
   wkarea_irid 		   number; 
   fol_irid 		   number; 
   fol_ivid 		   number; 
   dummy_wrkarea           boolean := FALSE;
   contents                jr_name.objver_list;
   i 				   pls_integer; 
   l_SUPPORTS_DESIGNER     varchar2(1);
   l_SUPPORTS_JDEVEE       varchar2(1); 
 
BEGIN
  dbms_output.enable(1000000);

  select SUPPORTS_JDEVEE
  ,      SUPPORTS_DESIGNER
  into   l_SUPPORTS_JDEVEE
  ,      l_SUPPORTS_DESIGNER
  from   rm$repositories;
  
  -- Get folder irid and ivid
  select irid, ivid 
  into fol_irid, fol_ivid 
  from i$sdd_folders 
  where system_element_flag = 'Y'; 

  -- Restore full access rights for SYSTEM FOLDER to Repository Owner
  dbms_output.put_line('* Restoring full access rights for SYSTEM FOLDER to the Repository Owner');
  jr_acc_rights.upd( fol_irid, user, jr_acc_rights.full_access_rights );

  -- Check-in System Folder and Contents
  dbms_output.put_line('* Checking if Global Shared Workarea exists');

  -- Check Global Shared Workarea exists 
  dbms_output.put_line('* Checking if Global Shared Wokrarea exists' );
  begin
    select irid
    into wkarea_irid 
    from I$sdd_workareas 
    where name = 'GLOBAL SHARED WORKAREA';
  exception when no_data_found then 
    dummy_wrkarea := TRUE;
 
    -- Create a dummy workarea to put the SYSTEM FOLDER in 
    wkarea_irid := jr_workarea.create_workarea( user, 
                                                'SYSTEM WORKAREA', 
                                                'Temporay WorkArea used to create the SYSTEM FOLDER');

    dbms_output.put_line('* Define Spec to include SYSTEM FOLDER');
    jr_workarea.define_spec(wkarea_irid, 'INCLUDE_FOLDER(SYSTEM FOLDER{MAIN;LATEST})' );

    dbms_output.put_line('* Compiling WorkArea');
    jr_workarea.compile (wkarea_irid);

    dummy_wrkarea := TRUE;
  end;

  -- Set workarea context
  jr_context.set_workarea(wkarea_irid);

  -- Check in system folder
  rtn_id := jr_version.check_in(fol_irid, fol_ivid, 'Initial for Versioned Repository', null, null); 
     
  -- Recursively get folder contents and then iterate list and check-in objects
  contents:= jr_name.folder_contents(fol_ivid,TRUE,NULL); 

  i:= contents.first; 
  while i is not null 
  loop 
     rtn_id := jr_version.check_in(contents(i).irid, contents(i).ivid, 'Initial for Versioned Repository', null, null);
     i:= contents.next(i); 
  end loop; 

  jr_context.set_workarea(''); 

  -- Finally Delete the Temporary WorkArea if one was created
  if (dummy_wrkarea) then
    dbms_output.put_line('* Removing WorkArea');
    jr_workarea.delete_workarea(wkarea_irid, user);
  end if;

  commit;

  dbms_output.put_line('* Operation Complete');
                     
EXCEPTION

  WHEN OTHERS THEN 

     dbms_output.put_line('* Exception Raised  ');
     dbms_output.put_line('...ERROR: '||substr(sqlerrm,1,240));
     rollback work;
     RAISE;

END;
/
