/*
 * 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;
end;
/


/*
 * After Insert trigger on flight_privilege:-
 * Maintain flight_access table according to new entries.
 * Log an audit record.
 */
create or replace trigger tia_flight_priv
after insert on flight_privileges
for each row
begin

	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;
/

create or replace trigger tias_flight_priv
after insert on flight_privileges

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

	f_id       flight_privileges.id%type;
	f_alpha    flight_privileges.airline_alpha_code%type;
	f_agent    flight_privileges.agent_code%type;
	f_fno      flight_privileges.flight_no%type;
	f_fdt      flight_privileges.flight_date%type;
	f_user     flight_privileges.last_user%type;

begin

	for interim_rec in interim_fpriv_records loop

		select
			id,
			airline_alpha_code,
			agent_code,
			flight_no,
			flight_date,
			last_user
		into
			f_id,
			f_alpha,
			f_agent,
			f_fno,
			f_fdt,
			f_user
		from
			flight_privileges
		where
			id = interim_rec.numeric;

		/*
		 * Create appropriate flight_access records according
		 * to the new privileges.
		 */
		grant_flt_access (
			f_id,
			f_alpha,
			f_fno,
			f_fdt,
			f_agent
		);

	end loop;

	delete from
		interim
	where
		id = 2;

end;
/


/*
 * Delete trigger on flight privilege:-
 * Maintain the flight_access table in accordance with
 * deletions here.
 * Log an audit record.
 */

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

	/*
	 * Remove or disassociate any related flight_access records.
	 */
	revoke_flt_access( :old.id );

	/*
	 * 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;
/
