/*****************************************************************************
*
* Filename         - $Workfile:   messages_triggers.sql  $
*
*                     Name              Date      Version     CR Number
* Author           - See revision history below.
* Last Update      - See revision history below.
*
* Project Name     - TAGTRAK(Oracle Database)
*
* Source Code
* Identifier       - 0/3/300/8010/112
*
* Design Spec
* Section          -
*
* Contents         -  Live Schema: triggers for messages table
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 2006
*
*****************************************************************************/

CREATE OR REPLACE TRIGGER ti_messages
BEFORE INSERT ON messages
FOR EACH ROW
BEGIN

    -- Assign Message ID
    SELECT message_id_seq.NEXTVAL INTO :NEW.message_id FROM dual;

    -- Assign the Carrier
    :NEW.carrier := SUBSTR(:NEW.flight_number, 1, 3);

    -- Set Processed State
    :NEW.processed_state := 'U';

    -- Set the number of parts received
    :NEW.parts_received := NVL(:NEW.parts_received, 0);

END;
/

CREATE OR REPLACE TRIGGER td_messages
BEFORE DELETE ON messages
FOR EACH ROW
DECLARE
  l_target site_info.airport_code%TYPE;
BEGIN

    -- Create a trace entry for the deletion
    l_target := SUBSTR(:OLD.version, 3);
    process_message.add_message_trace(:OLD.message_id, NULL, 'MESSAGE_DELETE',
              l_target, NULL, multi_site.get_auto_user(l_target));

END;
/



