/*****************************************************************************
*
* Filename         - $Workfile:   flight_priv_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         - Triggers for flight_privileges table
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1998
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   flight_priv_triggers.sql  $  $Revision:   1.2  $
*
* Revision History
*
* Note that user tagtrak is many users. For these users the user
* name is entered as the last line in the change description.
*
* $Log:   /pvcsarch/db_ora/db/source/live/triggers/flight_priv_triggers.svl  $
* 
*    Rev 1.2   06 Jun 2000 09:56:32   tagtrak
*  
* 
*    Rev 1.1.1.1   10 Jun 1999 14:51:54   tagtrak
* OR:
* CR: 14043
* REV:
* Changed Archive and Backup so that the database
* can be accessed during the eraseLive function.
* 
* Neil Bishop.
* 
*    Rev 1.1   02 Apr 1998 19:46:10   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.0   26 Feb 1998 11:43:50   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
*
****************************************************************************/

-- File renamed from flight_priv_triggers.sql - Dave Banks - FALCP/CA/1771 - 8th March 2006

/*
 * Before Update trigger on flight_privilege:-
 * Prevent updates to this table.
 */
create or replace trigger tu_flight_priv
before update on flight_privileges
for each row
begin

	/*
	 * Any attempt to update a record in this table will result
	 * in an application error.
	 */
	raise_application_error( -20034, 'Cannot modify flight_privilege record' );

end;
/



/*
 * Before Insert trigger on flight_privilege:-
 * Assign next sequential ID.
 */
create or replace trigger tib_flight_priv
before insert on flight_privileges
for each row
begin
	select
		flight_priv_id.nextval
	into
		:new.id
	from
		dual;

	/*
	 * MWS client app can send blanks instead of NULLs, so
	 * we must replace these with proper NULL values.
	 */
	if ( rtrim(:new.flight_no) is null ) then
		:new.flight_no := NULL;
	end if;

	if ( rtrim(:new.flight_date) is null ) then
		:new.flight_date := NULL;
	end if;

end;
/


/*
 * After Insert trigger on flight_privilege:-
 * This trigger will place the newly created record ID in an
 * interim table to be used in the creation of access
 * records in the after statement trigger tias_flight_priv.
 * This mechanism avoids a mutating/constraining table error.
 * Also Log an audit record.
 */
create or replace trigger tia_flight_priv
after insert on flight_privileges
for each row
begin

	/*
	 * Save the new ID temporarily.
	 */
	insert into interim
		(id, numeric)
	values
		(2,:new.id);

	/*
	 * Maintain audit trail.
	 */
	add_log_rec( 'FLIGHT_PRIVILEGES',
		'insert: ID: '||:new.id||'. Airline: '||rtrim(:new.airline_alpha_code)||
		'. Flight No: '||rtrim(nvl(:new.flight_no,'blank'))||
		'. Flight Date: '||rtrim(nvl(:new.flight_date,'blank'))||
		'. Agent: '||rtrim(:new.agent_code)||'. Internal User: '||USER,
		:new.last_user );

end;
/

/*
 * After insert statement trigger on flight_privileges.
 * Maintain flight_access/stats_access tables according to new entries.
 * This is done via the interim table to avoid mutating/constraining
 * table errors.
 */
create or replace trigger tias_flight_priv
after insert on flight_privileges

declare
	cursor interim_priv_records is
		select
			numeric
		from
			interim
		where
			id = 2;

begin


	for interim_rec in interim_priv_records loop

		/*
		 * Create appropriate flight_access and stats_access
		 * records according to the new privileges.
		 */
		priv_id_to_flt_access( interim_rec.numeric );

	end loop;

	/*
	 * Remove interim records.
	 */
	delete from
		interim
	where
		id = 2;

end;
/


/*
 * Delete trigger on flight privilege:-
 * Log an audit record.
 */

create or replace trigger td_flight_priv
before delete on flight_privileges
for each row
begin

	/*
	 * Maintain audit trail. User ID is unknown in this context.
	 */
	add_log_rec( 'FLIGHT_PRIVILEGES',
		'delete: ID: '||:old.id||'. Airline: '||rtrim(:old.airline_alpha_code)||
		'. Flight No: '||rtrim(nvl(:old.flight_no,'blank'))||
		'. Flight Date: '||rtrim(nvl(:old.flight_date,'blank'))||
		'. Agent: '||rtrim(:old.agent_code)||'. Last User: '||:old.last_user||
		'. Internal User: '||USER,
		NULL );

end;
/
