rem {{ $Header: \\ukst76\rcsroot.7_0\model\JIN\GENERATE\RCS\JRIFILE.JPB 40.8 1999/01/06 15:54:34 rmolland Exp rmolland $ rem {{ ------------------------------------------------------------------------ rem File: $Workfile: jrpbfile.sql $ rem Author: $Author: rmolland $ rem Date: $Date: 1999/01/06 15:54:34 $ rem Version: $Revision: 40.8 $ rem Status: $State: Exp $ rem Locked_by: $Locker: rmolland $ rem Project: The Oracle Common Repository rem Description: File access service rem Notes: rem RealAuth: Jon Wetherbee rem IncepDate: December 4, 1997 rem Copyright: (c) Oracle Corporation 1997. All Rights Reserved. rem Tagline: -- Oracle Repository: Managing *all* your data -- rem --------------------------------------------------------------------------- rem Log of Changes from Source Control System rem --------------------------------------------------------------------------- rem }} ------------------------------------------------------------------------ rem prompt Package Body: jr_file create or replace package body jr_file is --------------------------------------------------------------------------- -- jr_file.new_file() -- create a new file (within the context of a parent folder, if specified) -- through the CDAPI -- Thin wrapper for compatibility with previous code, returns irid. --------------------------------------------------------------------------- function new_file(filename varchar2, context_folder number default null) return number is file_irid number; file_ivid number; b_loc BLOB; begin jr_file.new_file(filename, context_folder, file_irid, file_ivid); return file_irid; end; --------------------------------------------------------------------------- -- jr_file.new_file() -- create a new file (within the context of a parent folder, if specified) -- through the CDAPI (thin wrapper onto CDAPI) -- Folder is created within the context of a (specified) folder. A context -- folder *must* be specified... -- IMPORTANT: NO exception handling! Caller must catch and deal with exceptions -- -- Sets the file type based on the file_registry setting --------------------------------------------------------------------------- procedure new_file(filename in varchar2, context_folder in number, file_irid out number, file_ivid out number, file_kind out varchar2, blob_loc out BLOB, file_platform in varchar2 default null) is file cioprimary_access_file.data; repos_ctxt_folder number; BEGIN --cache the current repository context folder repos_ctxt_folder := jr_context.working_folder; -- set the new file's context folder cdapi.set_context_appsys(null,null,context_folder); -- disable uniqueness checking for the creation of this file cioapp_con_interface.disable_unique_check := true; -- Get the insert values file.v.NAME := filename; file.i.NAME := true; -- create the file... let caller deal with any exceptions cioprimary_access_file.ins(null,file); file_irid := file.v.IRID; file_ivid := file.v.IVID; -- now set the file kind, determined by the file registry file_kind := get_file_registry_LOB_kind(filename, file_platform); -- prepare the file for writing prepare_file_for_write(file_irid,file_ivid,file_kind,blob_loc); -- re-enable uniqueness checking cioapp_con_interface.disable_unique_check := false; -- reset the repos ctxt folder cdapi.set_context_appsys(null,null,repos_ctxt_folder); EXCEPTION when others then --ensure uniqueness checking re-enabled cioapp_con_interface.disable_unique_check := false; RAISE; end new_file; --------------------------------------------------------------------------- -- jr_file.new_file() -- create a new file (within the context of a parent folder, if specified) -- through the CDAPI (thin wrapper onto CDAPI) -- Folder is created within the context of a (specified) folder. A context -- folder *must* be specified... -- IMPORTANT: NO exception handling! Caller must catch and deal with exceptions -- -- Sets the file type based on the file_registry setting --------------------------------------------------------------------------- procedure new_file(filename in varchar2, context_folder in number, file_irid out number, file_ivid out number, file_platform in varchar2 default null) is file_kind varchar2(2); blob_loc blob; BEGIN new_file(filename,context_folder,file_irid,file_ivid, file_kind,blob_loc,file_platform); end new_file; ------------------------------------------------------------------------- -- Delete file -- Through CDAPI -- IMPORTANT: NO exception handling! Caller must catch and deal with -- exceptions ------------------------------------------------------------------------- procedure delete_file(file_irid in number) is BEGIN -- delete the file ciofolder.del(file_irid); END; --------------------------------------------------------------------------------- -- Procedure which creates a new file and registers it with the specified folder. -- Returns file_irid and file_ivid of file (new or exisiting) -- RJM 26/8/98 --------------------------------------------------------------------------------- procedure register_file (file_name in varchar2, file_irid out number, file_ivid out number, folder_ref in number, file_status out varchar2) is begin --see if the file exists in this folder already select f.irid, f.ivid into file_irid, file_ivid from sdd_files f, sdd_folder_members m where f.irid = m.member_object and m.folder_reference = folder_ref and f.name = file_name; --if the object exists, find out its current state and return as a char IF jr_version.is_checked_in(file_irid, file_ivid) THEN file_status := 'I'; ELSE file_status := 'O'; END IF; dbms_output.put_line(file_name||' IRID:IVID:STATUS = '||file_irid||':'||file_ivid||':'||file_status); exception when no_data_found then --create new file and bind to context jr_file.new_file(file_name, folder_ref, file_irid, file_ivid); when others then raise; file_status := 'N'; dbms_output.put_line(file_name||' IRID:IVID:STATUS = '||file_irid||':'||file_ivid||':'||file_status); end; --------------------------------------------------------------------------------- -- jr_file.prepare_file_for_upload() -- Gets relevant upload details for a repository file and ensures the -- file is ready for upload, checking that the file is checked out and has a -- a valid lob locator. -- -- Returns the lob length, file status, the file kind ((B)LOB or (C)LOB), -- the os_file size, the file CRC and the file os_timestamp. -- RJM 22/12/98 --------------------------------------------------------------------------------- procedure prepare_file_for_upload (f_irid in number, f_ivid in number, f_kind out varchar2, f_status out varchar2, f_size out number, f_crc out number, f_timestamp out date, f_blob out BLOB, platform in varchar2 default null) is begin select file_size, crc, os_timestamp into f_size, f_crc, f_timestamp from i$sdd_files where irid = f_irid and ivid = f_ivid; prepare_for_stream_upload(f_irid, f_ivid, f_kind, f_status, f_blob, platform); end; --------------------------------------------------------------------------------- -- jr_file.prepare_for_stream_upload() -- Procedure which prepares a file (lob) for upload from a memory stream. -- Performs the bare minimum checks to ensure the file is valid for upload. -- I.e. Makes sure the file is checked out and has a valid lob locator. -- -- If the file kind is not set, this function determines it (from the file registry) -- and prepare_file_for_write sets file kind -- -- Returns the lob length, file status and the file kind ((B)LOB or (C)LOB) -- RJM 22/12/98 --------------------------------------------------------------------------------- procedure prepare_for_stream_upload (f_irid in number, f_ivid in number, f_kind out varchar2, f_status out varchar2, f_blob out BLOB, platform in varchar2 default null) is f_name varchar2(256); begin -- Get the state and type of this file. select f.name, f.kind, o.state into f_name, f_kind, f_status from i$sdd_files f, i$sdd_object_versions o where f.irid = f_irid and f.ivid = f_ivid and f.irid = o.irid and f.ivid = o.ivid; -- prepare the file for writing, as long as file is not CI if f_status != 'I' then -- if kind is not set, use file registry to determine kind (default is blob) if f_kind is null or f_kind = kind_unkown then -- if platform = WIN32 then -- WIN32 has no filename case sensitivity, but solaris does -- f_name := upper(f_name); -- end if; f_kind := get_file_registry_LOB_kind(f_name, platform); end if; prepare_file_for_write (f_irid,f_ivid,f_kind,f_blob); end if; exception when no_data_found then raise NO_DATA_FOUND; when others then raise; end; --------------------------------------------------------------------------------- -- jr_file.prepare_file_for_write() -- Procedure which prepares a file (lob) for writing -- The lob_locator is set to empty_lob() -- RJM 22/12/98 --------------------------------------------------------------------------------- procedure prepare_file_for_write (f_irid in number, f_ivid in number, f_kind in varchar2, blob_loc out BLOB) is begin select contents_blob into blob_loc from i$sdd_files where irid = f_irid and ivid = f_ivid for update nowait; update i$sdd_files set contents_blob = empty_blob(), kind = f_kind where irid = f_irid and ivid = f_ivid returning contents_blob into blob_loc; end; --------------------------------------------------------------------------------- -- jr_file.get_file_for_upload() -- Gets details about the repository file in the specified folder and prepare -- file for upload. -- If the file does not exist it is created. -- RJM 22/12/98 --------------------------------------------------------------------------------- procedure get_file_for_upload (file_name in varchar2, folder_ref in number, f_kind out varchar2, f_irid out number, f_ivid out number, f_status out varchar2, f_size out number, f_crc out number, f_timestamp out date, f_blob out BLOB, platform in varchar2 default null) is dummy varchar2(2); begin --see if the file exists in this folder already select f.irid, f.ivid into f_irid, f_ivid from sdd_files f, sdd_folder_members m where f.irid = m.member_object and m.folder_reference = folder_ref and f.name = file_name; --get details and prepare for upload prepare_file_for_upload(f_irid, f_ivid, f_kind, f_status, f_size, f_crc, f_timestamp, f_blob); exception when no_data_found then --create new file and add to folder jr_file.new_file(file_name, folder_ref, f_irid, f_ivid, platform); --file kind is set in a sub function of prepare_file_for_upload... --get details and prepare for upload prepare_file_for_upload(f_irid, f_ivid, f_kind, f_status, f_size, f_crc, f_timestamp, f_blob, platform); when others then raise; end; --------------------------------------------------------------------------------- -- jr_file.count_files_for_download() -- Counts the number of files (recursively) that will be downloaded from the -- root_ref folder --------------------------------------------------------------------------------- function count_files_for_download(root_ref number) return number is begin return num_files_for_download(root_ref, '%'); end; --------------------------------------------------------------------------------- -- jr_file.count_files_for_download() -- Counts the number of files (recursively) that will be downloaded from the -- root_ref folder --------------------------------------------------------------------------------- function num_files_for_download(root_ref number, strFilter varchar2) return number is cursor sub_folders is select f.irid from sdd_folders f, sdd_folder_members m where f.irid = m.member_object and m.folder_reference = root_ref; file_count number; begin select count(f.irid) into file_count from sdd_files f, sdd_folder_members m where f.irid = m.member_object and m.folder_reference = root_ref and f.name like strFilter; --dbms_output.put_line('File count = '||file_count); for folder in sub_folders loop file_count := file_count + num_files_for_download(folder.irid, strFilter); end loop; return file_count; end; --------------------------------------------------------------------------------- -- -- jr_file.get_file_registry_LOB_kind() -- get the lob kind for the specified file name from the file registry -- 'C' = clob, 'B' = blob -- NOTE: If file_name pattern is not found in the registry, then the default -- return value is 'B' (blob), as this is the safe way to store data. -- NOTE: Search is case sensitive -- NOTE: Both null and % are wildcards -- --------------------------------------------------------------------------------- function get_file_registry_LOB_kind(file_name varchar2, file_platform varchar2) return varchar2 is cursor case_insens_file_type is select rule, platform from i$sdd_file_registry where nls_upper(file_name) like nls_upper(pattern) and rule in (rule_blob,rule_clob,rule_deflate,rule_inflate) order by seq; cursor case_sens_file_type is select rule, platform from i$sdd_file_registry where file_name like pattern and rule in (rule_blob,rule_clob,rule_deflate,rule_inflate) order by seq; file_kind varchar2(2); bCompression boolean; bTypeNotSet boolean; bCompNotSet boolean; case_sens_flg varchar2(1); begin file_kind := kind_blob; --default bCompression := false; bTypeNotSet := true; bCompNotSet := true; case_sens_flg := jr_policy.get_policy('CASE_SENSITIVE_FILENAMES'); if case_sens_flg is null or case_sens_flg = 'N' then for file_reg in case_insens_file_type loop if file_reg.platform is null or file_reg.platform = file_platform then if bTypeNotSet and file_reg.rule = rule_blob then dbms_output.put_line('Its a blob rule = '||kind_blob); bTypeNotSet := false; end if; if bTypeNotSet and file_reg.rule = rule_clob then dbms_output.put_line('Its a clob rule = '||kind_clob); file_kind := kind_clob; bTypeNotSet := false; end if; if bCompNotSet and file_reg.rule = rule_inflate then dbms_output.put_line('Inflate file...'); bCompNotSet := false; end if; if bCompNotSet and file_reg.rule = rule_deflate then dbms_output.put_line('Deflate file...'); bCompression := true; bCompNotSet := false; end if; end if; if not bTypeNotSet and not bCompNotSet then exit; end if; end loop; else for file_reg in case_sens_file_type loop if file_reg.platform is null or file_reg.platform = file_platform then if bTypeNotSet and file_reg.rule = rule_blob then dbms_output.put_line('Its a blob rule = '||kind_blob); bTypeNotSet := false; end if; if bTypeNotSet and file_reg.rule = rule_clob then dbms_output.put_line('Its a clob rule = '||kind_clob); file_kind := kind_clob; bTypeNotSet := false; end if; if bCompNotSet and file_reg.rule = rule_inflate then dbms_output.put_line('Inflate file...'); bCompNotSet := false; end if; if bCompNotSet and file_reg.rule = rule_deflate then dbms_output.put_line('Deflate file...'); bCompression := true; bCompNotSet := false; end if; end if; if not bTypeNotSet and not bCompNotSet then exit; end if; end loop; end if; if bCompression then -- compressed file kind = 'X' for compressed CLOB, and 'Y' for compressed BLOB if file_kind = 'C' then file_kind := 'X'; end if; if file_kind = 'B' then file_kind := 'Y'; end if; end if; -- default is a blob return file_kind; end; --------------------------------------------------------------------------------- -- jr_file.GetEmptyLobLoc() -- RJM 22/12/98 --------------------------------------------------------------------------------- procedure get_empty_lob_loc(irid in number, ivid in number, name in varchar2, kind out varchar2, blob_loc out BLOB, platform in varchar2 default null) is begin kind := get_file_registry_LOB_kind(name, platform); prepare_file_for_write(irid,ivid,kind,blob_loc); end; --------------------------------------------------------------------------------- -- jr_file.test() --------------------------------------------------------------------------------- procedure test (file_name varchar2, folder_name varchar2, platform varchar2 default null) is fol_irid number; fol_ivid number; f_irid number; f_ivid number; f_kind varchar2(2); status varchar2(2); f_status varchar2(2); f_size number; f_crc number; f_timestamp date; f_blob BLOB; s_sec number; f_sec number; begin s_sec := 60*to_number(to_char(SYSDATE, 'MI')); s_sec := s_sec+to_number(to_char(SYSDATE, 'SS')); --create the folder as a root folder... jr_folder.REGISTER_FOLDER (folder_name, fol_irid, fol_ivid, null, status); --create a new file get_file_for_upload(file_name, fol_irid, f_kind, f_irid, f_ivid, f_status, f_size, f_crc, f_timestamp, f_blob, platform); --select kind into f_kind from sdd_files where irid = f_irid and ivid = f_ivid; dbms_output.put_line(file_name||' f_irid = '||f_irid); dbms_output.put_line(file_name||' f_ivid = '||f_ivid); dbms_output.put_line(file_name||' f_kind = '||f_kind); dbms_output.put_line(file_name||' f_status = '||f_status); dbms_output.put_line(file_name||' f_size = '||f_size); dbms_output.put_line(file_name||' f_crc = '||f_crc); dbms_output.put_line(file_name||' f_timestamp = '||f_timestamp); f_sec := 60*to_number(to_char(SYSDATE, 'MI')); f_sec := f_sec+to_number(to_char(SYSDATE, 'SS')); dbms_output.put_line('Time taken to create file and folder = '||f_sec||' seconds'); end; --------------------------------------------------------------------------------- -- jr_file.test() --------------------------------------------------------------------------------- procedure test2 (file_name varchar2, folder_name varchar2, platform varchar2 default null) is fol_irid number; fol_ivid number; f_irid number; f_ivid number; f_kind varchar2(2); status varchar2(2); f_blob BLOB; s_sec number; f_sec number; begin s_sec := 60*to_number(to_char(SYSDATE, 'MI')); s_sec := s_sec+to_number(to_char(SYSDATE, 'SS')); --create the folder as a root folder... jr_folder.REGISTER_FOLDER (folder_name, fol_irid, fol_ivid, null, status); --create a new file new_file(file_name, fol_irid, f_irid, f_ivid, f_kind, f_blob, platform); dbms_output.put_line(file_name||' f_irid = '||f_irid); dbms_output.put_line(file_name||' f_ivid = '||f_ivid); dbms_output.put_line(file_name||' f_kind = '||f_kind); f_sec := 60*to_number(to_char(SYSDATE, 'MI')); f_sec := f_sec+to_number(to_char(SYSDATE, 'SS')); dbms_output.put_line('Time taken to create file and folder = '||f_sec||' seconds'); end; end jr_file; / rem rem ---------------------------------------------------------------------------- rem }} End of file $Workfile: jrpbfile.sql $ $$Header_is_done rem ---------------------------------------------------------------------------x