--MARS-770

set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;

column owner format a15
column object_name format a30
column spoolname new_value spoolname 

select 'install_package_MarsGeneralRelease_MARS770_packages_2_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'


prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;

--set echo on
--=============================================================================================================================
-- start install --MARS-770 payload --CT_MRDS.WORKFLOW_MANAGER package
--=============================================================================================================================

CREATE OR REPLACE EDITIONABLE PACKAGE "CT_MRDS"."WORKFLOW_MANAGER" 
IS

   FUNCTION INIT_WORKFLOW(pServiceName IN VARCHAR2, pWorkflowRunId IN VARCHAR2, pWorkflowName in VARCHAR2)
   RETURN NUMBER;

   PROCEDURE FINALISE_WORKFLOW(pWorkflowHistoryKey IN NUMBER, pWorkflowStatus IN VARCHAR2);
   PROCEDURE FINALISE_WORKFLOW(pWorkflowHistoryKey IN NUMBER, pServiceName IN VARCHAR2, pWorkflowStatus IN VARCHAR2);

   FUNCTION INIT_TASK(pTaskRunId IN VARCHAR2, pTaskName in VARCHAR2, pWorkflowHistoryKey IN NUMBER)
   RETURN NUMBER;

   PROCEDURE FINALISE_TASK(pTaskHistoryKey IN NUMBER, pTaskStatus IN VARCHAR2);

   PROCEDURE STORE_TASK_SOURCE_INFO(pTaskHistoryKey IN NUMBER, pSourceName IN VARCHAR2, pNumRows IN NUMBER);

   PROCEDURE STORE_TASK_TARGET_INFO(pTaskHistoryKey IN NUMBER, pSourceName IN VARCHAR2,
                                    pNumRowsApplied IN NUMBER, pNumRowsRejected IN NUMBER);

   vpRunningStatus CT_MRDS.A_WORKFLOW_HISTORY.WORKFLOW_SUCCESSFUL%TYPE := 'Running';

   --
   -- Set and get information on workflow level
   --
   PROCEDURE SET_WORKFLOW_PROPERTY(
       pWorkflowHistoryKey IN NUMBER
      ,pServiceName        IN VARCHAR2
      ,pProperty           IN VARCHAR2
      ,pValue              IN VARCHAR2
   );
   
   FUNCTION GET_WORKFLOW_PROPERTY(
       pWorkflowHistoryKey IN NUMBER
      ,pServiceName        IN VARCHAR2
      ,pProperty           IN VARCHAR2)
   RETURN VARCHAR2;

END WORKFLOW_MANAGER;
/

CREATE OR REPLACE EDITIONABLE PACKAGE BODY "CT_MRDS"."WORKFLOW_MANAGER" 
IS

   FUNCTION INIT_WORKFLOW(pServiceName IN VARCHAR2, pWorkflowRunId IN VARCHAR2, pWorkflowName in VARCHAR2)
   RETURN NUMBER
   IS
      vWorkflowHistoryKey NUMBER;
   BEGIN
      vWorkflowHistoryKey := A_WORKFLOW_HISTORY_KEY_SEQ.NEXTVAL;
      INSERT INTO CT_MRDS.A_WORKFLOW_HISTORY (SERVICE_NAME, A_WORKFLOW_HISTORY_KEY, ORCHESTRATION_RUN_ID,
                                              WORKFLOW_NAME, WORKFLOW_START, WORKFLOW_SUCCESSFUL)
                                      VALUES (pServiceName, vWorkflowHistoryKey, pWorkflowRunId,
                                              pWorkflowName, SYSTIMESTAMP, vpRunningStatus);

      return vWorkflowHistoryKey;

   END INIT_WORKFLOW;


   --
   -- Overload without service name for backward compatability, to be cleaned up later
   --
   PROCEDURE FINALISE_WORKFLOW(pWorkflowHistoryKey IN NUMBER, pWorkflowStatus IN VARCHAR2)
   IS
   BEGIN
      FINALISE_WORKFLOW(pWorkflowHistoryKey, NULL, pWorkflowStatus);
   END;

   PROCEDURE FINALISE_WORKFLOW(pWorkflowHistoryKey IN NUMBER, pServiceName IN VARCHAR2, pWorkflowStatus IN VARCHAR2)
   IS
   BEGIN
      UPDATE CT_MRDS.A_WORKFLOW_HISTORY SET WORKFLOW_SUCCESSFUL = pWorkflowStatus,
                                            WORKFLOW_END = SYSTIMESTAMP
                                  WHERE A_WORKFLOW_HISTORY_KEY = pWorkflowHistoryKey
                                    AND SERVICE_NAME = NVL(pServiceName,SERVICE_NAME);
   END FINALISE_WORKFLOW;


   FUNCTION INIT_TASK(pTaskRunId IN VARCHAR2, pTaskName in VARCHAR2, pWorkflowHistoryKey IN NUMBER)
   RETURN NUMBER
   IS
      vTaskHistoryKey NUMBER;
   BEGIN
      vTaskHistoryKey := A_TASK_HISTORY_KEY_SEQ.NEXTVAL;
      INSERT INTO CT_MRDS.A_TASK_HISTORY (A_TASK_HISTORY_KEY, TASK_RUN_ID, A_WORKFLOW_HISTORY_KEY,
                                              TASK_NAME, TASK_START, TASK_SUCCESSFUL, SERVICE_NAME)
                                      VALUES (vTaskHistoryKey, pTaskRunId, pWorkflowHistoryKey,
                                              pTaskName, SYSTIMESTAMP, vpRunningStatus, 'ODS');

      return vTaskHistoryKey;

   END INIT_TASK;


   PROCEDURE FINALISE_TASK(pTaskHistoryKey IN NUMBER, pTaskStatus IN VARCHAR2)
   IS
   BEGIN
      UPDATE CT_MRDS.A_TASK_HISTORY SET TASK_SUCCESSFUL = pTaskStatus,
                                            TASK_END = SYSTIMESTAMP
                                  WHERE A_TASK_HISTORY_KEY = pTaskHistoryKey;
   END FINALISE_TASK;

   PROCEDURE STORE_TASK_SOURCE_INFO(pTaskHistoryKey IN NUMBER, pSourceName IN VARCHAR2, pNumRows IN NUMBER)
   IS
   BEGIN
      INSERT INTO CT_MRDS.A_TASK_HISTORY_SOURCE (A_TASK_HISTORY_SOURCE_KEY, A_TASK_HISTORY_KEY,
                                                 SOURCE_NAME, ROW_COUNT)
                                         VALUES (A_TASK_HISTORY_SOURCE_KEY_SEQ.NEXTVAL, pTaskHistoryKey,
                                                 pSourceName, pNumRows);
      COMMIT;
   END;

   PROCEDURE STORE_TASK_TARGET_INFO(pTaskHistoryKey IN NUMBER, pSourceName IN VARCHAR2,
                                    pNumRowsApplied IN NUMBER, pNumRowsRejected IN NUMBER)
   IS
   BEGIN
      INSERT INTO CT_MRDS.A_TASK_HISTORY_TARGET (A_TASK_HISTORY_TARGET_KEY, A_TASK_HISTORY_KEY,
                                                 TARGET_NAME, ROW_COUNT_APPLIED, ROW_COUNT_REJECTED)
                                         VALUES (A_TASK_HISTORY_TARGET_KEY_SEQ.NEXTVAL, pTaskHistoryKey,
                                                 pSourceName, pNumRowsApplied, pNumRowsRejected);
      COMMIT;
   END;

   PROCEDURE SET_WORKFLOW_PROPERTY(
       pWorkflowHistoryKey IN NUMBER
      ,pServiceName        IN VARCHAR2
      ,pProperty           IN VARCHAR2
      ,pValue              IN VARCHAR2
   ) IS
   BEGIN
      INSERT INTO CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY (A_WORKFLOW_HISTORY_KEY, SERVICE_NAME, PROPERTY, VALUE)
                                               VALUES (pWorkflowHistoryKey, pServiceName, pProperty, pValue);
   END;

   FUNCTION GET_WORKFLOW_PROPERTY(
       pWorkflowHistoryKey IN NUMBER
      ,pServiceName        IN VARCHAR2
      ,pProperty           IN VARCHAR2
   ) RETURN VARCHAR2
   IS
      vValue CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY.VALUE%TYPE;
   BEGIN
      SELECT VALUE
        INTO vValue
        FROM CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY
       WHERE A_WORKFLOW_HISTORY_KEY = pWorkflowHistoryKey
         AND SERVICE_NAME = pServiceName
         AND PROPERTY = pProperty;

      RETURN vValue;
   END;

END WORKFLOW_MANAGER;