/*****************************************************************************
*
* Filename         - $Workfile:   system_config_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 system_config table
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1997
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   system_config_triggers.sql  $  $Revision:   1.4  $
*
* 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/system_config_triggers.svl  $
* 
*    Rev 1.4   21 Jun 2001 15:04:40   tagtrak
* OR:
* CR:
* Re-baseline version 4.0.0 to include patches.
* 
*    Rev 1.3   02 Apr 1998 19:46:30   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.2   26 Feb 1998 11:42:52   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
* 
*    Rev 1.1   15 Jan 1998 13:39:14   tagtrak
* OR: OR11101, TN13, TN15
* CR:
* REV:
* Phase 2C to Phase 3 upgrade plus code review actions and resolving some technical issues.
* 
* 
*    Rev 1.0   22 Oct 1997 16:38:42   tagtrak
* OR:
* CR:
* REV:
* Initial check-in
*
****************************************************************************/

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

create or replace trigger tu_system_cfg
before update on system_cfg
for each row
begin

	/*
	 * Before an Update on the system_cfg table
	 * - Check the process_name, subunit_name, dcsid and unit_name have
	 *   not changed.  If they have changed, then provide an error.
	 */

	if (:old.process_name != :new.process_name) then
		raise_application_error(-20018, 'non modifiable column cannot be modified.');
	end if;
	
	if (:old.subunit_name != :new.subunit_name) then
		raise_application_error(-20018, 'non modifiable column cannot be modified.');
	end if;

	if (:old.dcsid != :new.dcsid) then
		raise_application_error(-20018, 'non modifiable column cannot be modified.');
	end if;
	
	if (:old.unit_name != :new.unit_name) then
		raise_application_error(-20018, 'non modifiable column cannot be modified.');
	end if;
end;
/

create or replace trigger td_system_cfg
before delete on system_cfg
for each row

declare
	numrows number;

begin

	/*
	 * Before a delete on the system_cfg table
	 * - Make sure that no dependants exist
	 */
 
  /*  cannot delete parent "system_cfg" if children still exist in "flight"  */
  select
		count(*)
  into
		numrows
  from
		flight
  where
		process_name = :old.process_name and
		unit_name = :old.unit_name and
		dcsid = :old.dcsid and
		subunit_name = :old.subunit_name;

  if (numrows > 0) then
     raise_application_error(-20016, 'children still exist in child table. cannot delete parent.');
  end if;
 
  /*  cannot delete parent "system_cfg" if children still exist in "bag"  */
  select
		count(*)
  into
		numrows
  from
		bag
  where
		process_name = :old.process_name and
		unit_name = :old.unit_name and
		dcsid = :old.dcsid and
		subunit_name = :old.subunit_name;

  if (numrows > 0) then
     raise_application_error(-20016, 'children still exist in child table. cannot delete parent.');
  end if;
 
end;
/

create or replace trigger tu2_system_cfg
after update on system_cfg
for each row

declare
	addr char(30);
	mesg varchar2(100);
	rv number;

begin

	/*
	 * After each Update of the system_cfg table :
	 * - Clear the mail if the process_name is DCS and the bpm_status is N
	 * - Clear the mail if the dcs_status's are not the same
	 */
	
	if (:old.process_name = 'DCS') then

		/* clear mail for DCS */
		if (:old.bpm_status != :new.bpm_status) then

			if (:new.bpm_status = 'N') then
				addr := rtrim(:old.process_name) || :old.unit_name;
				mesg := :old.dcsid || ',' || :old.subunit_name || ',%';
	
				delete from
					mail_indiv
				where
					msg_address = addr and
					msg_type = 'BPM' and
					msg_text like mesg;

			end if;

		end if;

		/* mail if DCS goes UP or DOWN */
		if (:old.dcs_status != :new.dcs_status) then

			addr := rtrim(:old.process_name) || :old.unit_name;
			mesg := :old.process_name || ',' || :old.unit_name || ','
				|| :old.dcsid || ',' || :old.subunit_name;

			delete from
				mail_indiv
			where
				msg_address = addr and
				msg_type = 'DCSOFL' or
				msg_type = 'DCSONL';

			if (:new.dcs_status = 'D') then
				mail_pack.store_mail (addr, 'DCSOFL', mesg, 1, 'I', rv);
			elsif (:new.dcs_status = 'U') then
				mail_pack.store_mail (addr, 'DCSONL', mesg, 1, 'I', rv);
			end if;

		end if;

	end if;
		
end;
/

create or replace trigger td2_system_cfg
after delete on system_cfg
for each row

declare
	addr char(30);
	mesg varchar2(100);

begin

	/*
	 * After each delete on the system_cfg table :
	 * - Clear mail if the old process_name is DCS and the old
	 *   process_name is Y.
	 */
	
	if (:old.process_name = 'DCS') then

		/* clear mail for DCS */
		if (:old.bpm_status = 'Y') then

			addr := rtrim (:old.process_name) || :old.unit_name;
			mesg := :old.dcsid || ',' || :old.subunit_name || ',%';
	
			delete from
				mail_indiv
			where
				msg_address = addr and
				msg_text like mesg;

		end if;

	end if;
		
end;
/
/*
 * This is replaced by Primary Key:-
 * create or replace trigger ti_system_cfg
 */
