CREATE OR REPLACE PACKAGE cout_assert IS
  /**
  -- Package of assertion routines to make it easy to validate assumptions in a declarative fashion.
  -- 
  */

  SUBTYPE g_t_substitution_list IS cout_err.g_t_substitution_list;
  c_empty_substitution_list g_t_substitution_list;
  
   g_default_exception_number NUMBER := -20000;

  /**
  Asserts whether the given p_condition is true
  #param p_condition The condition to be asserted as TRUE
  #param p_message The message to be displayed when the assertion is NOT TRUE
  #param p_raise_exception Requests that an exception be raised if the assertion is NOT TRUE
  #param p_exception The exception to be raised, if requested
  #param p_subsitution_list Table of values to be substituted into the generated exception message
   #param p_helper_call_level If being used by a 'helper' procedure, indicates the call above the curremt call to be reported
  #usage cout_assert.istrue(p_inmo_type IN ('CR', 'CU', 'INMO', 'CA', 'CT'),p_message => 'Invalid inmo_type passed');
  */
  PROCEDURE istrue(p_condition         IN BOOLEAN
                  ,p_message           IN VARCHAR2
                  ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                  ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list
                  ,p_helper_call_level IN NUMBER := 1);
  /**
  Asserts whether the given p_value IS NOT NULL
  #param p_condition The condition to be asserted as TRUE
  #param p_message The message to be displayed when the assertion is NOT TRUE
  #param p_raise_exception Requests that an exception be raised if the assertion is NOT TRUE
  #param p_exception The exception to be raised, if requested
  #param p_subsitution_list Table of values to be substituted into the generated exception message
  #usage cout_assert.isnotnull(p_value => l_inst_id,p_message => 'Inventory statement not found');
  */
  PROCEDURE isnotnull(p_value             IN VARCHAR2
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list);
  /**
  Asserts whether the given p_value IS NOT NULL
  #param p_condition The condition to be asserted as TRUE
  #param p_message The message to be displayed when the assertion is NOT TRUE
  #param p_raise_exception Requests that an exception be raised if the assertion is NOT TRUE
  #param p_exception The exception to be raised, if requested
  #param p_subsitution_list Table of values to be substituted into the generated exception message
  */
  PROCEDURE isnotnull(p_value             IN DATE
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list);
  /**
  Asserts whether the given p_value IS NOT NULL
  #param p_condition The condition to be asserted as TRUE
  #param p_message The message to be displayed when the assertion is NOT TRUE
  #param p_raise_exception Requests that an exception be raised if the assertion is NOT TRUE
  #param p_exception The exception to be raised, if requested
  #param p_subsitution_list Table of values to be substituted into the generated exception message
  */
  PROCEDURE isnotnull(p_value             IN NUMBER
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list);
  /**
  Asserts whether the given p_value IS NOT NULL
  #param p_condition The condition to be asserted as TRUE
  #param p_message The message to be displayed when the assertion is NOT TRUE
  #param p_raise_exception Requests that an exception be raised if the assertion is NOT TRUE
  #param p_exception The exception to be raised, if requested
  #param p_subsitution_list Table of values to be substituted into the generated exception message
  */
  PROCEDURE isnotnull(p_value             IN BOOLEAN
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list);
END cout_assert;
/
CREATE OR REPLACE PACKAGE BODY cout_assert IS

  g_header   CONSTANT VARCHAR2(160) := '$Header: /Isle Of Grain/database/PLSQL/cout_assert.pck 1     7/01/05 12:54 Gilberta $';
  g_revision CONSTANT VARCHAR2(160) := '$Revision: 1 $';
  
  PROCEDURE pl(p_msg VARCHAR2) IS
    l_start NUMBER := 1;
    l_len CONSTANT NUMBER := 255;
  BEGIN
    WHILE l_start <= length(p_msg) LOOP
      dbms_output.put_line(substr(p_msg
                                 ,l_start
                                 ,l_len));
      l_start := l_start + l_len;
    END LOOP;
  END pl;

  PROCEDURE istrue(p_condition         IN BOOLEAN
                  ,p_message           IN VARCHAR2
                  ,p_raise_exception   IN BOOLEAN := TRUE
                  ,p_exception         IN NUMBER := g_default_exception_number
                  ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list
                  ,p_helper_call_level IN NUMBER := 1) IS
  BEGIN
    IF NOT p_condition
       OR p_condition IS NULL THEN
      pl('Assertion Failure!');
      pl(p_message);
      IF p_raise_exception THEN
        cout_err.report_and_stop(p_exception_number  => p_exception
                                ,p_exception_message => 'Assertion Failure:' ||
                                                        p_message
                                ,p_substitution_list => p_substitution_list
                                ,p_helper_call_level => p_helper_call_level);
      ELSE
        cout_err.report_and_go(p_exception_number  => p_exception
                              ,p_exception_message => p_message
                              ,p_substitution_list => p_substitution_list
                              ,p_helper_call_level => p_helper_call_level);
      END IF;
    END IF;
  END istrue;

  PROCEDURE isnotnull(p_value             IN VARCHAR2
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                     ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list) IS
  BEGIN
    istrue(p_value IS NOT NULL
          ,p_message
          ,p_raise_exception
          ,p_exception
          ,p_substitution_list
          ,2);
  END;

  PROCEDURE isnotnull(p_value             IN DATE
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                     ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list) IS
  BEGIN
    istrue(p_value IS NOT NULL
          ,p_message
          ,p_raise_exception
          ,p_exception
          ,p_substitution_list
          ,2);
  END;

  PROCEDURE isnotnull(p_value             IN NUMBER
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                     ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list) IS
  BEGIN
    istrue(p_value IS NOT NULL
          ,p_message
          ,p_raise_exception
          ,p_exception
          ,p_substitution_list
          ,2);
  END;

  PROCEDURE isnotnull(p_value             IN BOOLEAN
                     ,p_message           IN VARCHAR2
                     ,p_raise_exception   IN BOOLEAN := TRUE
                     ,p_exception         IN NUMBER := g_default_exception_number
                     ,p_substitution_list IN g_t_substitution_list := c_empty_substitution_list) IS
  BEGIN
    istrue(p_value IS NOT NULL
          ,p_message
          ,p_raise_exception
          ,p_exception
          ,p_substitution_list
          ,2);
  END;
END cout_assert;
/
