409 lines
13 KiB
Plaintext
409 lines
13 KiB
Plaintext
CREATE OR REPLACE PACKAGE BODY amfr_timestamp IS
|
|
--
|
|
g_Empty_blob BLOB;
|
|
--
|
|
PROCEDURE Main(p_Args IN VARCHAR2) AS
|
|
LANGUAGE JAVA NAME 'tsdemo.amTimestamp.main(java.lang.String[])';
|
|
---
|
|
-- A M T I M S T A M P
|
|
---
|
|
FUNCTION Amtimestamp(p_File IN BLOB,
|
|
p_Url IN VARCHAR2,
|
|
p_Password IN VARCHAR2,
|
|
p_Keystore IN VARCHAR2) RETURN VARCHAR2 AS
|
|
LANGUAGE JAVA NAME 'tsdemo.amTimestamp.amTimestamp(oracle.sql.BLOB,java.lang.String,java.lang.String,java.lang.String) return String';
|
|
---
|
|
-- G E T H A S H
|
|
---
|
|
FUNCTION Gethash RETURN VARCHAR2 AS
|
|
LANGUAGE JAVA NAME 'tsdemo.amTimestamp.getHash() return String';
|
|
---
|
|
-- G E T R E S P B L O B
|
|
---
|
|
FUNCTION Getrespblob RETURN BLOB AS
|
|
LANGUAGE JAVA NAME 'tsdemo.amTimestamp.getRespBlob() return oracle.sql.BLOB';
|
|
---
|
|
-- S U B M I T R E T R Y
|
|
---
|
|
PROCEDURE submit_retry(p_Identifier IN Document_Timestamps.Doc_Identifier%TYPE,
|
|
p_Doctype IN Document_Timestamps.Doc_Type%TYPE,
|
|
p_Url IN VARCHAR2,
|
|
p_Password IN VARCHAR2,
|
|
p_Keystore IN VARCHAR2) IS
|
|
--
|
|
l_interval VARCHAR2(255) := cout_system_configuration.get_configuration_item('G_TIMESTAMP_INTERVAL');
|
|
l_job INTEGER;
|
|
--
|
|
PRAGMA AUTONOMOUS_TRANSACTION;
|
|
--
|
|
BEGIN
|
|
--
|
|
dbms_output.put_line('In Submit retry');
|
|
--
|
|
IF C_test_run THEN
|
|
--
|
|
NULL;
|
|
--
|
|
ELSE
|
|
--
|
|
dbms_job.submit(job => l_job,
|
|
what => 'amfr_timestamp.retry_timestamp( p_Identifier => '||to_char(p_Identifier)||
|
|
',p_Doctype => '''||p_Doctype||''''||
|
|
',p_Url => '''||p_Url||''''||
|
|
',p_Password => '''||p_Password||''''||
|
|
',p_Keystore => '''||p_Keystore||''');',
|
|
next_date => (SYSDATE + l_interval)
|
|
);
|
|
--
|
|
END IF;
|
|
--
|
|
dbms_output.put_line('Commit In Submit retry');
|
|
--
|
|
COMMIT;
|
|
--
|
|
dbms_output.put_line('Exit Submit retry');
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
--
|
|
dbms_output.put_line('ERROR in submit_retyr: '||SUBSTR(SQLERRM,1,200));
|
|
--
|
|
cout_err.report_and_stop(p_exception_message => 'ERROR in submit_retry: '||SUBSTR(SQLERRM,1,200));
|
|
--
|
|
END ;
|
|
---
|
|
-- F O R M A T J A V A D A T E
|
|
---
|
|
FUNCTION format_java_date(p_value IN VARCHAR2) RETURN VARCHAR2 IS
|
|
---
|
|
-- Java date is return in this format: Wed Mar 28 13:47:48 GMT+01:00 2007
|
|
-- The basic date components must be stripped out and built into an Oracle format
|
|
-- NB The class the date originates form returns a String hence casting has not been done.
|
|
---
|
|
l_month VARCHAR2(10);
|
|
l_year VARCHAR2(10);
|
|
l_date VARCHAR2(10);
|
|
l_time VARCHAR2(10);
|
|
l_value VARCHAR2(255);
|
|
l_return VARCHAR2(255);
|
|
--
|
|
BEGIN
|
|
--
|
|
IF p_value IS NOT NULL THEN
|
|
--
|
|
l_value := TRIM(p_value);
|
|
--
|
|
l_month := SUBSTR(l_value,5,3);
|
|
--
|
|
l_date := SUBSTR(l_value,9,2);
|
|
--
|
|
l_time := SUBSTR(l_value,12,8);
|
|
--
|
|
l_year := SUBSTR(l_value,-4,4);
|
|
--
|
|
l_return := l_date||'-'||l_month||'-'||l_year||' '||l_time;
|
|
dbms_output.put_line('Formated date: '||l_return);
|
|
--
|
|
ELSE
|
|
--
|
|
l_return := NULL;
|
|
--
|
|
END IF;
|
|
--
|
|
RETURN (l_return);
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
cout_err.report_and_stop(p_exception_message => 'ERROR in format_java_date: '||SUBSTR(SQLERRM,1,200));
|
|
END ;
|
|
---
|
|
-- R E T R Y T I M E S T A M P
|
|
---
|
|
PROCEDURE retry_timestamp(p_Identifier IN Document_Timestamps.Doc_Identifier%TYPE,
|
|
p_Doctype IN Document_Timestamps.Doc_Type%TYPE,
|
|
p_Url IN VARCHAR2,
|
|
p_Password IN VARCHAR2,
|
|
p_Keystore IN VARCHAR2) IS
|
|
--
|
|
l_timestamp DATE;
|
|
l_return VARCHAR2(255);
|
|
l_retry_limit NUMBER := cout_system_configuration.get_configuration_item(p_parameter => 'G_RETRY_LIMIT');
|
|
l_retry_count NUMBER;
|
|
l_hashed_File BLOB;
|
|
l_formatted_date VARCHAR2(255);
|
|
l_timestamp_response BLOB;
|
|
--
|
|
CURSOR c_doti IS
|
|
SELECT retry_count,
|
|
hashed_file
|
|
FROM document_timestamps
|
|
WHERE Doc_Identifier = p_Identifier
|
|
AND Doc_Type = p_Doctype;
|
|
--
|
|
BEGIN
|
|
--
|
|
l_timestamp := NULL;
|
|
l_return := NULL;
|
|
l_timestamp_response := g_Empty_blob;
|
|
---
|
|
-- Need to get the retyr count to see if the limit has been reached,
|
|
-- also need to get the BLOB as can't pass as a parameter via DBMS_JOB.
|
|
---
|
|
OPEN c_doti;
|
|
FETCH c_doti INTO l_retry_count,
|
|
l_hashed_file;
|
|
CLOSE c_doti;
|
|
--
|
|
BEGIN
|
|
---
|
|
-- Return the JAVA date as a string so we can format it.
|
|
---
|
|
l_return := Amtimestamp(p_File => l_Hashed_file,
|
|
p_Url => p_Url,
|
|
p_Password => p_Password,
|
|
p_Keystore => p_Keystore);
|
|
--
|
|
dbms_output.put_line('Return Date: '||l_return);
|
|
---
|
|
-- Manipulate the java format into one oracle can use
|
|
---
|
|
l_formatted_date := format_java_date(l_return);
|
|
--
|
|
l_timestamp := TO_DATE(l_formatted_date,C_JAVA_DATA_FMT);
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
cout_err.report_and_go(p_exception_number => SQLCODE
|
|
,p_exception_message => SUBSTR(SQLERRM,1,200)
|
|
,p_error_group => 'APPLICATION'
|
|
,p_severity => 'W'
|
|
,p_source => 'AMFR_TIMESTAMP');
|
|
--
|
|
END ;
|
|
--
|
|
IF l_timestamp IS NOT NULL THEN
|
|
---
|
|
-- As GetRespBlob will cause an error if returns NULL need to ensure the timestamp was received first.
|
|
---
|
|
l_timestamp_response := GetRespBlob;
|
|
--
|
|
UPDATE document_timestamps
|
|
SET doc_timestamp = l_timestamp
|
|
,status = C_Retry
|
|
,retry_count = retry_count + 1
|
|
,timestamp_response = l_timestamp_response
|
|
WHERE doc_identifier = p_identifier
|
|
AND doc_type = p_doctype;
|
|
--
|
|
ELSIF l_timestamp IS NULL AND NVL(l_retry_count,0) < l_retry_limit THEN
|
|
--
|
|
UPDATE document_timestamps
|
|
SET retry_count = retry_count + 1
|
|
WHERE doc_identifier = p_identifier
|
|
AND doc_type = p_doctype;
|
|
--
|
|
submit_retry(p_Identifier => p_Identifier,
|
|
p_Doctype => p_Doctype,
|
|
p_Url => p_URL,
|
|
p_Password => p_Password,
|
|
p_Keystore => p_keystore);
|
|
--
|
|
END IF;
|
|
--
|
|
COMMIT;
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
--
|
|
cout_err.report_and_stop(p_exception_message => 'ERROR in retry_timestamp: '||SUBSTR(SQLERRM,1,200));
|
|
--
|
|
END;
|
|
---
|
|
-- C R E A T E T I M E S T A M P
|
|
---
|
|
PROCEDURE Create_Timestamp(p_doc_identifier IN Document_Timestamps.Doc_Identifier%TYPE,
|
|
p_Doc_Type IN Document_Timestamps.Doc_Type%TYPE,
|
|
p_Doc_Timestamp IN Document_Timestamps.doc_timestamp%TYPE,
|
|
p_Hash IN Document_Timestamps.HASH%TYPE,
|
|
p_Hashed_File IN Document_Timestamps.hashed_file%TYPE,
|
|
p_Status IN Document_Timestamps.status%TYPE,
|
|
p_timestamp_response IN Document_Timestamps.timestamp_response%TYPE) IS
|
|
--
|
|
BEGIN
|
|
--
|
|
INSERT INTO Document_Timestamps
|
|
(doc_identifier,
|
|
Doc_Type,
|
|
Doc_Timestamp,
|
|
HASH,
|
|
Hashed_File,
|
|
Status,
|
|
retry_count,
|
|
timestamp_response
|
|
)
|
|
VALUES
|
|
(p_doc_identifier,
|
|
p_Doc_Type,
|
|
p_Doc_Timestamp,
|
|
p_Hash,
|
|
p_Hashed_File,
|
|
p_Status,
|
|
0,
|
|
p_timestamp_response
|
|
);
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
--
|
|
cout_err.report_and_stop(p_exception_message => 'ERROR in Create_Timestamp: '||SUBSTR(SQLERRM,1,200));
|
|
--
|
|
END;
|
|
---
|
|
-- T I M E S T A M P D O C U M E N T
|
|
---
|
|
FUNCTION Timestamp_Document(p_Identifier IN Document_Timestamps.Doc_Identifier%TYPE,
|
|
p_Doctype IN Document_Timestamps.Doc_Type%TYPE,
|
|
p_File_For_Hash IN BLOB,
|
|
p_Url IN VARCHAR2 DEFAULT g_URL,
|
|
p_Password IN VARCHAR2 DEFAULT g_password,
|
|
p_Keystore IN VARCHAR2 DEFAULT g_keystore) RETURN DATE IS
|
|
--
|
|
l_timestamp DATE;
|
|
l_return VARCHAR2(255);
|
|
l_status VARCHAR2(1);
|
|
l_file_hash VARCHAR2(255);
|
|
l_formatted_date VARCHAR2(255);
|
|
l_timestamp_response BLOB;
|
|
l_success BOOLEAN;
|
|
--
|
|
BEGIN
|
|
--
|
|
dbms_output.put_line('Got into package.');
|
|
--
|
|
l_success := FALSE;
|
|
l_timestamp := NULL;
|
|
l_return := NULL;
|
|
l_status := NULL;
|
|
l_file_hash := NULL;
|
|
l_formatted_date := NULL;
|
|
l_timestamp_response := g_Empty_blob;
|
|
--
|
|
BEGIN
|
|
--
|
|
dbms_output.put_line('Called Timestamp_Document with URL: '||p_URL);
|
|
---
|
|
-- Return the JAVA date as a string so we can format it.
|
|
---
|
|
IF C_test_run THEN
|
|
--
|
|
l_return := SYSDATE;
|
|
l_Status := C_Valid;
|
|
l_timestamp := SYSDATE;
|
|
--
|
|
ELSE
|
|
--
|
|
l_return := Amtimestamp(p_File => p_File_For_Hash,
|
|
p_Url => p_Url,
|
|
p_Password => p_Password,
|
|
p_Keystore => p_Keystore);
|
|
--
|
|
dbms_output.put_line('Return Date: '||l_return);
|
|
---
|
|
-- Manipulate the java format into one oracle can use
|
|
---
|
|
l_formatted_date := format_java_date(l_return);
|
|
--
|
|
l_timestamp := TO_DATE(l_formatted_date,C_JAVA_DATA_FMT);
|
|
--
|
|
l_Status := C_Valid;
|
|
--
|
|
l_success := TRUE;
|
|
--
|
|
END IF;
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
cout_err.report_and_go(p_exception_number => SQLCODE
|
|
,p_exception_message => SUBSTR(SQLERRM,1,200)
|
|
,p_error_group => 'APPLICATION'
|
|
,p_severity => 'W'
|
|
,p_source => 'AMFR_TIMESTAMP');
|
|
--
|
|
l_timestamp := SYSDATE;
|
|
l_status := C_Defaulted;
|
|
--
|
|
dbms_output.put_line('In exception block and submitting retry.');
|
|
--
|
|
submit_retry(p_Identifier => p_Identifier,
|
|
p_Doctype => p_Doctype,
|
|
p_Url => p_URL,
|
|
p_Password => p_Password,
|
|
p_Keystore => p_keystore);
|
|
--
|
|
l_success := FALSE;
|
|
--
|
|
END ;
|
|
--
|
|
dbms_output.put_line('Completed Submit retry');
|
|
--
|
|
IF l_timestamp IS NULL THEN
|
|
--
|
|
dbms_output.put_line('Timestamp IS NULL and submitting retry.');
|
|
--
|
|
l_timestamp := SYSDATE;
|
|
l_status := C_Defaulted;
|
|
--
|
|
submit_retry(p_Identifier => p_Identifier,
|
|
p_Doctype => p_Doctype,
|
|
p_Url => p_URL,
|
|
p_Password => p_Password,
|
|
p_Keystore => p_keystore);
|
|
--
|
|
END IF;
|
|
--
|
|
IF C_test_run THEN
|
|
--
|
|
l_timestamp_response := g_Empty_blob;
|
|
l_file_hash := NULL;
|
|
--
|
|
ELSIF NOT l_success THEN -- Fails for some other reason e.g. connection refused....
|
|
--
|
|
l_timestamp_response := g_Empty_blob;
|
|
l_file_hash := NULL;
|
|
--
|
|
ELSE
|
|
--
|
|
--dbms_output.put_line('Doing BLOBBY things');
|
|
l_timestamp_response := GetRespBlob;
|
|
l_file_hash := Gethash;
|
|
--dbms_output.put_line('Finished doing BLOBBY things');
|
|
--
|
|
END IF;
|
|
--
|
|
dbms_output.put_line('Creating timestamp');
|
|
--
|
|
Create_Timestamp(p_doc_identifier => p_Identifier,
|
|
p_Doc_Type => p_Doctype,
|
|
p_Doc_Timestamp => l_timestamp,
|
|
p_Hash => l_file_hash,
|
|
p_Hashed_File => p_File_For_Hash,
|
|
p_Status => l_status,
|
|
p_timestamp_response => l_timestamp_response);
|
|
--
|
|
dbms_output.put_line('Done the lot!');
|
|
--
|
|
RETURN (l_timestamp);
|
|
--
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
--
|
|
cout_err.report_and_stop(p_exception_message => 'ERROR in Timestamp_Document: '||SUBSTR(SQLERRM,1,200));
|
|
--
|
|
END;
|
|
--
|
|
BEGIN
|
|
-- Initialization
|
|
NULL;
|
|
END amfr_timestamp;
|
|
/
|