CREATE OR REPLACE PACKAGE mip_friendly_messages IS

  -- Author  : HARDYA
  -- Created : 19/02/2008 09:45:09
  -- Purpose : 

  /** Attempt to produce a user friendly message for an Oracle message
  %param p_ora_msg the message to be processed
  %return either a friendly message or p_ora_msg
  */
  FUNCTION get_friendly_message(p_ora_msg IN VARCHAR2) RETURN VARCHAR2;

END mip_friendly_messages;
/
CREATE OR REPLACE PACKAGE BODY mip_friendly_messages IS

  PROCEDURE pl(p_in   VARCHAR2
              ,p_line IN NUMBER DEFAULT NULL) IS
  BEGIN
    NULL;
    /*  $IF mip_debug_constants.debugging OR mip_debug_constants.friendly_messages 
    $THEN*/
    mip_debug.pl(p_unit => $$PLSQL_UNIT
                ,p_line => p_line
                ,p_in   => p_in);
    /*  $END*/
END pl;

  FUNCTION get_constraint_message(p_ora_msg IN VARCHAR2) RETURN VARCHAR2 IS
    c_marker        CONSTANT VARCHAR2(30) := 'constraint (';
    c_marker_length CONSTANT NUMBER := length(c_marker);
    l_msg             constraint_messages.msg%TYPE;
    l_constraint      VARCHAR2(30);
    l_constraint_name VARCHAR2(30);
    l_start_posn      NUMBER;
    l_end_posn        NUMBER;
  BEGIN

    -- get the complete constraint "user.constraint_name"
    l_start_posn := instr(p_ora_msg
                         ,c_marker) + c_marker_length;
    l_end_posn   := instr(p_ora_msg
                         ,')'
                         ,l_start_posn) - 1;
    l_constraint := substr(p_ora_msg
                          ,l_start_posn
                          ,l_end_posn - l_start_posn + 1);
  

    -- get the "constraint_name"
    l_start_posn      := instr(l_constraint
                              ,'.') + 1;
    l_constraint_name := substr(l_constraint
                               ,l_start_posn);

    BEGIN
      SELECT msg
        INTO l_msg
        FROM constraint_messages
       WHERE constraint_name = l_constraint_name;
    EXCEPTION
      WHEN no_data_found THEN
        l_msg := NULL;
    END;
    IF l_msg IS NULL
       AND l_constraint_name LIKE 'SYS!_C%' ESCAPE
     '!'
       OR l_constraint_name LIKE 'AVCON!_%' ESCAPE '!' THEN
      -- this is a system generated check constraint and should not be appearing
      l_msg := 'Application error. Attempting to violate check constraint ' ||
               l_constraint_name ||
               '. Please report this error to Advantica.';
    END IF;
    RETURN l_msg;
  END get_constraint_message;

  FUNCTION get_friendly_message(p_ora_msg IN VARCHAR2) RETURN VARCHAR2 IS
    l_ora_msg VARCHAR2(240) := p_ora_msg;
    l_sqlcode NUMBER;
    l_msg     VARCHAR2(2000);
  BEGIN
  pl('get_friendly_message:entry:'||p_ora_msg);
    l_sqlcode := substr(l_ora_msg
                       ,1
                       ,5);
                       
    IF instr(l_ora_msg
            ,'constraint (') > 0 THEN
      l_msg := nvl(get_constraint_message(l_ora_msg)
                  ,l_ora_msg);
    ELSE
      l_msg := l_ora_msg;
    END IF;
  
  pl('get_friendly_message:exit:'||l_msg);
    return(l_msg);
  END get_friendly_message;

END mip_friendly_messages;
/
