--------------------------------------------------------
--  File created - Thursday-August-01-2013   
--------------------------------------------------------
--------------------------------------------------------
--  DDL for Function GET_DATE
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "GET_DATE" RETURN DATE IS

/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
BEGIN
	 
	RETURN TO_DATE( TO_CHAR( SYSDATE,'DD-MON-YYYY' ),'DD-MON-YYYY' );
		
END GET_DATE;

/

/
--------------------------------------------------------
--  DDL for Function GET_DATETIME
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "GET_DATETIME" RETURN DATE IS

/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
BEGIN
	 
	RETURN TO_DATE( TO_CHAR( SYSDATE,'DD-MON-YYYY HH24:MI:SS' ),'DD-MON-YYYY HH24:MI:SS' );
		
END GET_DATETIME;

/

/
--------------------------------------------------------
--  DDL for Function GET_SYSTEM_VALUE
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "GET_SYSTEM_VALUE" ( sItemName IN SYSTEM_CONFIGURATION.ITEM_NAME%TYPE ) RETURN SYSTEM_CONFIGURATION.ITEM_VALUE%TYPE IS

/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
ItemValue SYSTEM_CONFIGURATION.ITEM_VALUE%TYPE;
 
BEGIN
	-- Get system value
	SELECT sc.ITEM_VALUE INTO ItemValue
	  FROM SYSTEM_CONFIGURATION sc
	 WHERE sc.ITEM_NAME = sItemName;
	 
	RETURN ItemValue;
		
END GET_SYSTEM_VALUE;

/

/
--------------------------------------------------------
--  DDL for Function IN_LIST_NUMBER
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "IN_LIST_NUMBER" ( sInList IN VARCHAR2 ) RETURN DEPMGR_NUMBER_TAB_t IS

/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */

    sync_rtags		   DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
	pos				   NUMBER;
	in_list			   VARCHAR2(4000) := sInList || ',';

BEGIN

	IF NOT sInList IS NULL
	THEN
		LOOP
	        EXIT WHEN in_list IS NULL;
	        pos := INSTR ( in_list, ',' );
	        sync_rtags.extend;
	        sync_rtags(sync_rtags.count) := LTRIM ( RTRIM ( SUBSTR ( in_list, 1, pos-1 ) ) );
	        in_list := SUBSTR ( in_list, pos+1 );
		END LOOP;
	END IF;

	RETURN sync_rtags;
END IN_LIST_NUMBER;

/

/
--------------------------------------------------------
--  DDL for Function PRINT_SBOM_PACKAGES
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "PRINT_SBOM_PACKAGES" (v_dm_branch_name VARCHAR2, v_sbom_version VARCHAR2)
  RETURN NUMBER IS
   
  cur ref_cursor;
  SqlCmd VARCHAR2(4000);
BEGIN

  SqlCmd := ' 
  CREATE GLOBAL TEMPORARY TABLE sbom_packages
   ON COMMIT PRESERVE ROWS 
   AS SELECT DISTINCT pv.pv_id
      FROM deployment_manager.boms b
    INNER JOIN deployment_manager.bom_contents bc ON b.bom_id = bc.bom_id
    INNER JOIN deployment_manager.branches br ON b.branch_id = br.branch_id
    INNER JOIN deployment_manager.operating_systems os ON bc.node_id = os.node_id
    INNER JOIN deployment_manager.os_contents osc ON os.os_id = osc.os_id
    INNER JOIN release_manager.package_versions pv ON osc.prod_id = pv.pv_id
    WHERE br.branch_name = v_dm_branch_name
        AND b.bom_version||''.''||b.bom_lifecycle = v_sbom_version;
        ';
  EXECUTE SqlCmd;


  FOR cur IN SELECT DISTINCT pv_id FROM sbom_packages LOOP

    INSERT INTO sbom_packages (    
    SELECT DISTINCT PV.pv_id
      FROM release_content RC
      INNER JOIN package_versions PV ON PV.pv_id = RC.pv_id
      INNER JOIN packages P ON P.pkg_id = PV.pkg_id
      WHERE PV.pv_id IN ( SELECT DISTINCT dpv_id
                            FROM package_dependencies
                          START WITH pv_id = cur.pv_id
                        CONNECT BY pv_id = PRIOR dpv_id )
                               );
  END LOOP;

  FOR cur IN SELECT DISTINCT pv_id FROM sbom_packages LOOP
    Dbms_Output.put_line(cur.pv_id);
  END LOOP;

  RETURN 0;
END;

/

/
--------------------------------------------------------
--  DDL for Function STRREPLACE
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION "STRREPLACE" (str varchar2, from_str varchar2, to_str varchar2)
  return varchar2
AS
  str_temp varchar2(4000);
  str_pos  number := instr(str, from_str);
BEGIN
  str_temp := str;
  while ( str_pos > 0 ) loop
    str_temp := substr(str_temp, 0, str_pos-1) || to_str ||
                substr(str_temp, str_pos + length(from_str));
    str_pos  := instr(str_temp, from_str);
  end loop;
  return str_temp;
END;

/

/
--------------------------------------------------------
--  DDL for Package PK_BASE_ENV
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_BASE_ENV" IS
			
PROCEDURE Add_Base_Env ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 nBosId IN BASE_ENV.BOS_ID%TYPE,
						 nBomId IN BASE_ENV.BOM_ID%TYPE,
						 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						 nUserId IN NUMBER );
						 
PROCEDURE Update_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
							sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 	nBosId IN BASE_ENV.BOS_ID%TYPE,
							nOsId IN OS_BASE_ENV.OS_ID%TYPE,
							nUserId IN NUMBER );		

FUNCTION Get_Base_Env_Id ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						   nBomId IN BASE_ENV.BOM_ID%TYPE ) RETURN BASE_ENV.BASE_ENV_ID%TYPE;
							
PROCEDURE Destroy_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
 		  				   	 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						 	 nUserId IN NUMBER);

PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
						nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						nUserId IN NUMBER );

PROCEDURE Add_Product_Wrap ( nPkgId IN NUMBER,
		  				   	 sProdVersion IN VARCHAR2,
							 nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
							 nUserId IN NUMBER,
							 sComments IN VARCHAR2  );

PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							nUserId IN NUMBER );

PROCEDURE Update_Product ( nOldProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						   nSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN BASE_ENV_CONTENTS.BASE_ENV_COMMENTS%TYPE,
						   sSunOSElectronicName IN PACKAGES.SUNOS_ELECTRONIC_NAME%TYPE,
						   sWinElectronicName IN PACKAGES.WIN_ELECTRONIC_NAME%TYPE,
						   sNotXML IN BASE_ENV_CONTENTS.NOT_XML%TYPE,
						   sPatchElectronicName IN PACKAGE_VERSIONS.PATCH_ELECTRONIC_NAME%TYPE,
						   nUserId IN NUMBER,
						   outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE );

PROCEDURE Shift_Product_SeqNum ( nProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
		  				 	     nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						 		 nToSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE );
								 
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						  nUserId IN NUMBER );

								
END pk_Base_Env;

/

/
--------------------------------------------------------
--  DDL for Package PK_BASE_ENV_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_BASE_ENV_TEST" IS
			
PROCEDURE Add_Base_Env ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 nBosId IN BASE_ENV.BOS_ID%TYPE,
						 nBomId IN BASE_ENV.BOM_ID%TYPE,
						 nOsId IN OS_BASE_ENV.OS_ID%TYPE );
						 
PROCEDURE Update_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
							sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 	nBosId IN BASE_ENV.BOS_ID%TYPE );		

FUNCTION Get_Base_Env_Id ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						   nBomId IN BASE_ENV.BOM_ID%TYPE ) RETURN BASE_ENV.BASE_ENV_ID%TYPE;
							
PROCEDURE Destroy_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE );

PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
						nUserId IN NUMBER );

PROCEDURE Add_Product_Wrap ( nPkgId IN NUMBER,
		  				   	 sProdVersion IN VARCHAR2,
							 nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							 nUserId IN NUMBER,
							 sComments IN VARCHAR2  );

PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							nUserId IN NUMBER );

PROCEDURE Update_Product ( nOldProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						   nSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN BASE_ENV_CONTENTS.BASE_ENV_COMMENTS%TYPE,
						   sElectronicName IN PACKAGES.ELECTRONIC_NAME%TYPE );

PROCEDURE Shift_Product_SeqNum ( nProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
		  				 	     nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						 		 nToSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE );
								 
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						  nUserId IN NUMBER );

								
END pk_Base_Env_test;

/

/
--------------------------------------------------------
--  DDL for Package PK_BOM
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_BOM" IS

PROCEDURE Add_Bom ( sBomName IN BOM_NAMES.BOM_NAME%TYPE DEFAULT NULL,
		  		  	sBomVersion IN BOMS.BOM_VERSION%TYPE,
		  		  	sBomLifeCycle IN BOMS.BOM_LIFECYCLE%TYPE,		
					sBomComments IN BOMS.BOM_COMMENTS%TYPE DEFAULT NULL,
					nToBranchId IN BOMS.BRANCH_ID%TYPE,
					nFromBomId IN BOMS.BOM_ID%TYPE DEFAULT NULL,
					nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					nUserId IN BOM_STATE.USER_ID%TYPE );
					
PROCEDURE Update_Bom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER );		
					   
PROCEDURE Update_ProdBom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER );							   			
					
FUNCTION New_Bom_Name ( sBomName IN BOM_NAMES.BOM_NAME%TYPE ) RETURN BOM_NAMES.BOM_NAME_ID%TYPE;	
FUNCTION New_Bom_Name ( nFromBomId IN BOMS.BOM_NAME_ID%TYPE ) RETURN BOMS.BOM_NAME_ID%TYPE;

PROCEDURE Accept_Reject ( nBomId IN BOMS.BOM_ID%TYPE,
						  cIsRejected IN BOMS.IS_REJECTED%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE,
						  nStateId IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE  );

PROCEDURE Set_Bom_Official ( nBomId IN BOMS.BOM_ID%TYPE,
							 cIsOfficial IN BOMS.IS_READONLY%TYPE,
							 nStateId IN BOM_STATE.STATE_ID%TYPE,
							 nUserId IN NUMBER );		

PROCEDURE Update_Nodes_Version ( nBomId IN BOMS.BOM_ID%TYPE,
							 	 nUserId IN NUMBER );							 		

PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 );

PROCEDURE Destroy_BaseEnv ( nBaseEndIdCollector IN DEPMGR_NUMBER_TAB_t );

PROCEDURE Destroy_Hardware ( nHardwareNameIdCollector IN DEPMGR_NUMBER_TAB_t );			

PROCEDURE Get_Next_Version (nFromBomId IN BOMS.BOM_ID%TYPE,
							nToBranchId IN BOMS.BRANCH_ID%TYPE,
							sNextVersion OUT BOMS.BOM_VERSION%TYPE,
							sNextLifeCycle OUT BOMS.BOM_LIFECYCLE%TYPE,
							nParentBomId IN OUT BOMS.PARENT_BOM_ID%TYPE,
							nBomNameId IN BOMS.BOM_NAME_ID%TYPE );	
							
PROCEDURE Release_To ( nBomId IN BOM_STATE.BOM_ID%TYPE,
					   nUserId IN BOM_STATE.USER_ID%TYPE,
					   nToState IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
					   sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL );	
					   
PROCEDURE Accept_Reject_Comments ( nBomId IN BOMS.BOM_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE );					   					
							
PROCEDURE Log_Bom_Trail ( nBomId IN BOM_TRAIL.BOM_ID%TYPE,
						  sAction IN BOM_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN BOM_TRAIL.USER_ID%TYPE,
						  sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL  );	

PROCEDURE Log_Node_Trail ( nNodeId IN NODE_TRAIL.NODE_ID%TYPE,
						   sAction IN NODE_TRAIL.TRAIL_ACTION%TYPE,
						   nUserId IN NODE_TRAIL.USER_ID%TYPE  );	
						   
PROCEDURE Log_Os_Trail ( nOsId IN OS_TRAIL.OS_ID%TYPE,
						  sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN OS_TRAIL.USER_ID%TYPE  );						   
						   
						   					  					  						  				  				 

END pk_Bom;

/

/
--------------------------------------------------------
--  DDL for Package PK_BOM_RPT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_BOM_RPT" AS
	   TYPE TOC_Type IS REF CURSOR RETURN TableOfContents%ROWTYPE;
END PK_BOM_RPT;



/

/
--------------------------------------------------------
--  DDL for Package PK_BOM_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_BOM_TEST" IS

PROCEDURE Add_Bom ( sBomName IN BOM_NAMES.BOM_NAME%TYPE DEFAULT NULL,
		  		  	sBomVersion IN BOMS.BOM_VERSION%TYPE,
		  		  	sBomLifeCycle IN BOMS.BOM_LIFECYCLE%TYPE,		
					sBomComments IN BOMS.BOM_COMMENTS%TYPE DEFAULT NULL,
					nToBranchId IN BOMS.BRANCH_ID%TYPE,
					nFromBomId IN BOMS.BOM_ID%TYPE DEFAULT NULL,
					nUserId IN BOM_STATE.USER_ID%TYPE );
					
PROCEDURE Update_Bom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER );					
					
FUNCTION New_Bom_Name ( sBomName IN BOM_NAMES.BOM_NAME%TYPE ) RETURN BOM_NAMES.BOM_NAME_ID%TYPE;	
FUNCTION New_Bom_Name ( nFromBomId IN BOMS.BOM_NAME_ID%TYPE ) RETURN BOMS.BOM_NAME_ID%TYPE;

PROCEDURE Accept_Reject ( nBomId IN BOMS.BOM_ID%TYPE,
						  cIsRejected IN BOMS.IS_REJECTED%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE,
						  nStateId IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE  );

PROCEDURE Set_Bom_Official ( nBomId IN BOMS.BOM_ID%TYPE,
							 cIsOfficial IN BOMS.IS_READONLY%TYPE,
							 nStateId IN BOM_STATE.STATE_ID%TYPE,
							 nUserId IN NUMBER );		

PROCEDURE Update_Nodes_Version ( nBomId IN BOMS.BOM_ID%TYPE,
							 	 nUserId IN NUMBER );							 		

PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 );

PROCEDURE Destroy_BaseEnv ( nBaseEndIdCollector IN DEPMGR_NUMBER_TAB_t );

PROCEDURE Destroy_Hardware ( nHardwareNameIdCollector IN DEPMGR_NUMBER_TAB_t );			

PROCEDURE Get_Next_Version (nFromBomId IN BOMS.BOM_ID%TYPE,
							nToBranchId IN BOMS.BRANCH_ID%TYPE,
							sNextVersion OUT BOMS.BOM_VERSION%TYPE,
							sNextLifeCycle OUT BOMS.BOM_LIFECYCLE%TYPE,
							nParentBomId IN OUT BOMS.PARENT_BOM_ID%TYPE,
							nBomNameId IN BOMS.BOM_NAME_ID%TYPE );	
							
PROCEDURE Release_To ( nBomId IN BOM_STATE.BOM_ID%TYPE,
					   nUserId IN BOM_STATE.USER_ID%TYPE,
					   nToState IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
					   sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL );	
					   
PROCEDURE Accept_Reject_Comments ( nBomId IN BOMS.BOM_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE );					   					
							
PROCEDURE Log_Bom_Trail ( nBomId IN BOM_TRAIL.BOM_ID%TYPE,
						  sAction IN BOM_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN BOM_TRAIL.USER_ID%TYPE,
						  sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL  );	

PROCEDURE Log_Node_Trail ( nNodeId IN NODE_TRAIL.NODE_ID%TYPE,
						   sAction IN NODE_TRAIL.TRAIL_ACTION%TYPE,
						   nUserId IN NODE_TRAIL.USER_ID%TYPE  );	
						   
PROCEDURE Log_Os_Trail ( nOsId IN OS_TRAIL.OS_ID%TYPE,
						  sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN OS_TRAIL.USER_ID%TYPE  );						   
						   
						   					  					  						  				  				 

END pk_Bom_Test;

/

/
--------------------------------------------------------
--  DDL for Package PK_NETWORK_NODE
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_NETWORK_NODE" IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
PROCEDURE Add_Node ( sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
					 nBomId IN BOM_CONTENTS.BOM_ID%TYPE,
					 nUserId IN NUMBER,
					 sNotXML IN NETWORK_NODES.NOT_XML%TYPE );
					 
PROCEDURE Update_Node ( nNodeId IN NETWORK_NODES.NODE_ID%TYPE,
						sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     	nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 	sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
						nUserId IN NUMBER,
						nLifeCycle IN NUMBER,
						sNotXML IN NETWORK_NODES.NOT_XML%TYPE );					 

PROCEDURE Remove_Node ( sNodeIdList IN VARCHAR2,
					  	nBomId IN BOM_CONTENTS.BOM_ID%TYPE );	
						
PROCEDURE Destroy_Node ( nNodeIdCollection IN DEPMGR_NUMBER_TAB_t, nUserId IN NUMBER );	

PROCEDURE Destroy_Node ( nNodeIdList IN VARCHAR2, nUserId IN NUMBER );			

PROCEDURE Sort_Nodes ( sNodeIdOrder IN VARCHAR2,
		  			   nBomId IN BOMS.BOM_ID%TYPE,
					   nUserId IN NUMBER );						 

END pk_Network_Node;

/

/
--------------------------------------------------------
--  DDL for Package PK_NETWORK_NODE_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_NETWORK_NODE_TEST" IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
PROCEDURE Add_Node ( sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
					 nBomId IN BOM_CONTENTS.BOM_ID%TYPE,
					 nUserId IN NUMBER );
					 
PROCEDURE Update_Node ( nNodeId IN NETWORK_NODES.NODE_ID%TYPE,
						sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     	nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 	sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE );					 

PROCEDURE Remove_Node ( sNodeIdList IN VARCHAR2,
					  	nBomId IN BOM_CONTENTS.BOM_ID%TYPE );	
						
PROCEDURE Destroy_Node ( nNodeIdCollection IN DEPMGR_NUMBER_TAB_t );	

PROCEDURE Destroy_Node ( nNodeIdList IN VARCHAR2 );			

PROCEDURE Sort_Nodes ( sNodeIdOrder IN VARCHAR2,
		  			   nBomId IN BOMS.BOM_ID%TYPE,
					   nUserId IN NUMBER );						 

END pk_Network_Node_test;

/

/
--------------------------------------------------------
--  DDL for Package PK_OPERATING_SYSTEM
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_OPERATING_SYSTEM" 
IS
   PROCEDURE add_os (
      sosname         IN   operating_systems.os_name%TYPE,
      soscomments     IN   operating_systems.os_comments%TYPE,
      snodespecfile   IN   operating_systems.node_spec_file%TYPE,
      snotxml         IN   operating_systems.not_xml%TYPE,
      nnodeid         IN   operating_systems.node_id%TYPE,
      nbaseenvname    IN   base_env.base_env_name%TYPE,
      nbosid          IN   base_env.bos_id%TYPE DEFAULT NULL,
      nuserid         IN   NUMBER
   );

   PROCEDURE update_os (
      nosid           IN   operating_systems.os_id%TYPE,
      sosname         IN   operating_systems.os_name%TYPE,
      soscomments     IN   operating_systems.os_comments%TYPE,
      nbaseenvname    IN   base_env.base_env_name%TYPE,
      snodespecfile   IN   operating_systems.node_spec_file%TYPE,
      snotxml         IN   operating_systems.not_xml%TYPE,
      nuserid         IN   NUMBER
   );

   PROCEDURE add_product_wrap (
      npkgid         IN   NUMBER,
      sprodversion   IN   VARCHAR2,
      nosid          IN   os_contents.os_id%TYPE,
      nuserid        IN   NUMBER,
      scomments      IN   VARCHAR2 DEFAULT NULL
   );

   PROCEDURE set_base_conf (
      nosid        IN   os_base_env.os_id%TYPE,
      nbaseenvid   IN   os_base_env.base_env_id%TYPE
   );

   PROCEDURE shift_product_seqnum (
      nprodid     IN   os_contents.prod_id%TYPE,
      nosid       IN   os_contents.os_id%TYPE,
      ntoseqnum   IN   os_contents.seq_num%TYPE
   );

   PROCEDURE shift_bulk_products_seqnum (
      sprodidlist   IN   VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      cdirection    IN   CHAR
   );

   PROCEDURE destroy_os (
      nosidcollection   IN   depmgr_number_tab_t,
      nuserid           IN   NUMBER
   );

   PROCEDURE destroy_os (nosidlist IN VARCHAR2, nuserid IN NUMBER);

   PROCEDURE add_package_process (
      nprocidlist   IN   VARCHAR2,
	  npvid 		IN 	 processes_config.prod_id%TYPE,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   );
   
   PROCEDURE add_packages_process (
	  npvidlist		IN 	 VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   );   
END pk_operating_system;

/

/
--------------------------------------------------------
--  DDL for Package PK_PRODUCT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_PRODUCT" IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
     
PROCEDURE New_Product ( sProdName IN PACKAGES.PKG_NAME%TYPE,
						sProdVersion IN PACKAGE_VERSIONS.PKG_VERSION%TYPE,
						nUserId IN NUMBER,
						outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE );

PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );
						
PROCEDURE Hide_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );	

PROCEDURE Unhide_Product ( sProdIdList IN NUMBER,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );												
						
PROCEDURE Update_Process (nProcId IN PROCESSES_CONFIG.PROC_ID%TYPE,
		  				  nProdId IN PROCESSES_CONFIG.PROD_ID%TYPE,
		  				  nIsInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,		  
		  				  nOsId IN PROCESSES_CONFIG.OS_ID%TYPE,		  
						  nCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
						  nPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
						  sPkgHealthTag IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,
						  nProduct IN PROCESSES_CONFIG.PROD_ID%TYPE );						
						
PROCEDURE Update_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nOsId IN OS_CONTENTS.OS_ID%TYPE,
						   nSeqNum IN OS_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN OS_CONTENTS.PRODUCT_COMMENTS%TYPE,
						   sSunOSElectronicName IN PACKAGES.SUNOS_ELECTRONIC_NAME%TYPE,
						   sWinElectronicName IN PACKAGES.WIN_ELECTRONIC_NAME%TYPE,
						   sIsNodeSpec IN OS_CONTENTS.IS_NODE_SPEC%TYPE,
						   sNotXML IN OS_CONTENTS.NOT_XML%TYPE,
						   sPatchElectronicName IN PACKAGE_VERSIONS.PATCH_ELECTRONIC_NAME%TYPE,
   						   nUserId IN NUMBER,
						   outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE );						 

PROCEDURE Replace_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  				  	nNewProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  			  	    nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER );

PROCEDURE Remove_Process ( 	sProcName IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,		
		  				   	sProdIdList IN VARCHAR2,
		  			  	  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
							sCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
							sPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
							sInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,
							nUserId IN NUMBER );							
													
PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER ); 
							
PROCEDURE Add_Products_NodeSpec ( sProdIdList IN VARCHAR2,
		  			  	  		  nOsId IN OS_CONTENTS.OS_ID%TYPE,
								  nUserId IN NUMBER ); 														

PROCEDURE Accept_Reject ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,
						  nBomId IN BOMS.BOM_ID%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
						  nOsId IN NUMBER );
						  
PROCEDURE Accept_Reject_List ( nProdIdList IN VARCHAR2,
							   nBomId IN BOMS.BOM_ID%TYPE,
						  	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
							   nOsId IN NUMBER );
							   
PROCEDURE Accept_Reject_Comments ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE );				
								   
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nOsId IN OS_CONTENTS.OS_ID%TYPE,
						  nUserId IN NUMBER );	
						  
PROCEDURE Remove_Obsolete_Patches ( nOsId IN OS_CONTENTS.OS_ID%TYPE,
		  						  	nUserId IN NUMBER );
						  
						  
FUNCTION GET_OS_ID ( PvId IN NUMBER ) RETURN NUMBER;	

FUNCTION GET_NODE_ID ( OsId IN NUMBER ) RETURN NUMBER;

PROCEDURE Add_Product_Note ( nProdId IN NUMBER,
		  				   	 sNotes IN VARCHAR2,
							 nUserId IN NUMBER );

PROCEDURE Edit_Product_Note ( nProdId IN NUMBER,
		  				   	 sNotes IN VARCHAR2,
							 nUserId IN NUMBER );							 
							 
PROCEDURE Log_Product_Trail ( nProdId IN PRODUCT_TRAIL.PROD_ID%TYPE,
						  	  sAction IN PRODUCT_TRAIL.TRAIL_ACTION%TYPE,
						  	  nUserId IN PRODUCT_TRAIL.USER_ID%TYPE  );								 
						  			  								   		
END pk_Product;

/

/
--------------------------------------------------------
--  DDL for Package PK_PRODUCTION
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_PRODUCTION" AS
/******************************************************************************
   NAME:       PK_PRODUCTION
   PURPOSE:

   REVISIONS:
   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
   1.0        26/07/2006             1. Created this package.
******************************************************************************/

  PROCEDURE Release_Authorisation_Tester ( nPvId IN NUMBER,
  										   nBomId IN NUMBER,
										   nTesterId IN NUMBER,
										   sTesterComments IN VARCHAR2);
										   
  PROCEDURE Release_Authorisation_Manager ( nPvId IN NUMBER,
  										   	nBomId IN NUMBER,
										   	nManagerId IN NUMBER,
										   	sManagerComments IN VARCHAR2);		
											
  PROCEDURE Log_Pkg_Trail ( nPkgId IN OS_TRAIL.OS_ID%TYPE,
						  	sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  	nUserId IN OS_TRAIL.USER_ID%TYPE  );
							
  PROCEDURE Reject_Product ( nPvId IN NUMBER,
							 nTesterId IN NUMBER,
							 sTesterComments IN VARCHAR2  );	
							 
  PROCEDURE Add_Production_Bom ( nBomId IN NUMBER, 
  							   	 nBranchId IN NUMBER, 
								 nStateId IN NUMBER, 
								 nProjId IN NUMBER ); 							 																									   
							
END PK_PRODUCTION;

/

/
--------------------------------------------------------
--  DDL for Package PK_PRODUCT_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_PRODUCT_TEST" IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
     
PROCEDURE New_Product ( sProdName IN PACKAGES.PKG_NAME%TYPE,
						sProdVersion IN PACKAGE_VERSIONS.PKG_VERSION%TYPE,
						nUserId IN NUMBER,
						outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE );

PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );
						
PROCEDURE Hide_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );	

PROCEDURE Unhide_Product ( sProdIdList IN NUMBER,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER );												
						
PROCEDURE Update_Process (nProcId IN PROCESSES_CONFIG.PROC_ID%TYPE,
		  				  nProdId IN PROCESSES_CONFIG.PROD_ID%TYPE,
		  				  nIsInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,		  
		  				  nOsId IN PROCESSES_CONFIG.OS_ID%TYPE,		  
						  nCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
						  nPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
						  sPkgHealthTag IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,
						  nProduct IN PROCESSES_CONFIG.PROD_ID%TYPE );						
						
PROCEDURE Update_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nOsId IN OS_CONTENTS.OS_ID%TYPE,
						   nSeqNum IN OS_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN OS_CONTENTS.PRODUCT_COMMENTS%TYPE,
						   sSunOSElectronicName IN PACKAGES.SUNOS_ELECTRONIC_NAME%TYPE,
						   sWinElectronicName IN PACKAGES.WIN_ELECTRONIC_NAME%TYPE,
						   sIsNodeSpec IN OS_CONTENTS.IS_NODE_SPEC%TYPE,
						   sNotXML IN OS_CONTENTS.NOT_XML%TYPE,
						   sPatchElectronicName IN PACKAGE_VERSIONS.PATCH_ELECTRONIC_NAME%TYPE,
						   nUserId IN NUMBER,
						   outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE );						 

PROCEDURE Replace_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  				  	nNewProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  			  	    nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER );

PROCEDURE Remove_Process ( 	sProcName IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,		
		  				   	sProdIdList IN VARCHAR2,
		  			  	  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
							sCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
							sPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
							sInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,
							nUserId IN NUMBER );							
													
PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER ); 
							
PROCEDURE Add_Products_NodeSpec ( sProdIdList IN VARCHAR2,
		  			  	  		  nOsId IN OS_CONTENTS.OS_ID%TYPE,
								  nUserId IN NUMBER ); 														

PROCEDURE Accept_Reject ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,
						  nBomId IN BOMS.BOM_ID%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
						  nOsId IN NUMBER );
						  
PROCEDURE Accept_Reject_List ( nProdIdList IN VARCHAR2,
							   nBomId IN BOMS.BOM_ID%TYPE,
						  	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
							   nOsId IN NUMBER );
							   
PROCEDURE Accept_Reject_Comments ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE );				
								   
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nOsId IN OS_CONTENTS.OS_ID%TYPE,
						  nUserId IN NUMBER );	
						  
PROCEDURE Remove_Obsolete_Patches ( nOsId IN OS_CONTENTS.OS_ID%TYPE,
		  						  	nUserId IN NUMBER );
						  
						  
FUNCTION GET_OS_ID ( PvId IN NUMBER ) RETURN NUMBER;	

FUNCTION GET_NODE_ID ( OsId IN NUMBER ) RETURN NUMBER;
					  								   			   
			 

END pk_Product_Test;

/

/
--------------------------------------------------------
--  DDL for Package PK_PROJECT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_PROJECT" IS
			

PROCEDURE Add_Project ( sProjName IN DM_PROJECTS.PROJ_NAME%TYPE,
		  			  	nRMProjFk IN DM_PROJECTS.RM_PROJECTS_FK%TYPE );
						
PROCEDURE Update_Project ( nProjId IN DM_PROJECTS.PROJ_ID%TYPE,
		  				   sProjName IN DM_PROJECTS.PROJ_NAME%TYPE,
						   nRMProjFk IN DM_PROJECTS.RM_PROJECTS_FK%TYPE );		
						   
PROCEDURE ShowHide_Project ( nProjId DM_PROJECTS.PROJ_ID%TYPE );						   				
						
PROCEDURE Remove_Project ( nProjId IN DM_PROJECTS.PROJ_ID%TYPE );						

PROCEDURE Add_Branch ( sBranchName IN BRANCHES.BRANCH_NAME%TYPE,
		  			   nProjId IN BRANCHES.PROJ_ID%TYPE,
					   nRMVTreeId IN BRANCHES.RM_VTREE_FK%TYPE,
                       sBranchComments IN BRANCHES.BRANCH_COMMENTS%TYPE );
					   
PROCEDURE Update_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE,
		  				  sBranchName IN BRANCHES.BRANCH_NAME%TYPE,
					   	  nRMVTreeId IN BRANCHES.RM_VTREE_FK%TYPE,
                          sBranchComments IN BRANCHES.BRANCH_COMMENTS%TYPE );					   
					   
PROCEDURE Remove_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE );	

PROCEDURE ShowHide_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE );

PROCEDURE ShowHide_Download ( nProjId DM_PROJECTS.PROJ_ID%TYPE );	

PROCEDURE ShowHide_StateDownload ( nStateId STATES.STATE_ID%TYPE );	


			 

END pk_Project;

/

/
--------------------------------------------------------
--  DDL for Package PK_REPORT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_REPORT" IS

/*
------------------------------
||  Last Modified:  S.Vukovic
||  Modified Date:  8/Mar/2005
||  Spec Version:   1.0
------------------------------
*/

	TYPE typeCur IS REF CURSOR;

    /*================================================================================================*/
	
	FUNCTION BOM_PRODUCTS_BY_NODE_BY_OS ( BomId NUMBER ) RETURN typeCur;

	/*================================================================================================*/
				
							
END PK_REPORT;

/

/
--------------------------------------------------------
--  DDL for Package PK_REQUIREMENTS
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_REQUIREMENTS" IS
				  						  				  				 
PROCEDURE Add_Bom ( nFromBomId IN BOMS.BOM_ID%TYPE,
					nToBranchId IN BOMS.BRANCH_ID%TYPE );	
					
PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 );					
									
END pk_Requirements;

/

/
--------------------------------------------------------
--  DDL for Package PK_STATE
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_STATE" IS
			

PROCEDURE Add_State ( sStateName IN STATES.STATE_NAME%TYPE,
		  			  nStateTypeEnum IN STATES.STATE_TYPE_ENUM%TYPE,
		  			  nProjId IN STATES.PROJ_ID%TYPE );

PROCEDURE Update_State ( nStateId IN STATES.STATE_ID%TYPE,
		  			   	 sStateName IN STATES.STATE_NAME%TYPE,
						 nStateSeq IN STATES.STATE_SEQ%TYPE,
						 nStateTypeEnum IN STATES.STATE_TYPE_ENUM%TYPE );
					  
PROCEDURE Remove_State ( nStateId IN STATES.STATE_ID%TYPE );	

PROCEDURE Toggle_Is_Displayed ( nStateId IN BOM_STATE.STATE_ID%TYPE,
								nBomId IN BOM_STATE.BOM_ID%TYPE,
								cIsDisplay IN BOM_STATE.IS_DISPLAYED%TYPE );
								
PROCEDURE Auto_Hide ( nStateId IN BOM_STATE.STATE_ID%TYPE,
					  nInsertedBomId IN BOM_STATE.BOM_ID%TYPE,
					  nEventValue IN SYSTEM_CONFIGURATION.ITEM_VALUE%TYPE );								
								
END pk_State;

/

/
--------------------------------------------------------
--  DDL for Package PK_UTILS
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE "PK_UTILS" IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
PROCEDURE Clone_BOM ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  			  nToBomId IN BOMS.BOM_ID%TYPE,
					  nUserId IN NUMBER );
					  
PROCEDURE Copy_BOM_Contents ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  					  nToBomId IN BOMS.BOM_ID%TYPE );	

PROCEDURE Copy_Network_Node ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  					  nToBomId IN BOMS.BOM_ID%TYPE,
							  nFromNodeId IN NUMBER DEFAULT 0  );	

PROCEDURE Paste_OS_Contents;							  
							  
PROCEDURE Paste_Node ( sNodeIdList IN VARCHAR2,
					   nFromBomId IN OPERATING_SYSTEMS.NODE_ID%TYPE,
					   nToBomId IN OPERATING_SYSTEMS.NODE_ID%TYPE );
							  
PROCEDURE Paste_Os ( sOsIdList IN VARCHAR2,
					 nFromNodeId IN OPERATING_SYSTEMS.NODE_ID%TYPE,
					 nToNodeId IN OPERATING_SYSTEMS.NODE_ID%TYPE );

PROCEDURE Paste_Products ( sProdIdList IN VARCHAR2,
						   nFromOsId IN OS_CONTENTS.OS_ID%TYPE, 
						   nToOsId IN OS_CONTENTS.OS_ID%TYPE );
						   
PROCEDURE Paste_Products_To_Base_Env ( sProdIdList IN VARCHAR2,
									   nFromOsId IN OS_CONTENTS.OS_ID%TYPE, 
									   nToBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE );						   

PROCEDURE Clean_Old_Id_Column;	

PROCEDURE Log_Rejection_Trail ( nEnumEntityType IN REJECTION_TRAIL.ENUM_ENTITY_TYPE%TYPE,
								nEntityId IN REJECTION_TRAIL.ENTITY_ID%TYPE,
								cIsRejected IN REJECTION_TRAIL.IS_REJECTED%TYPE,
								nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
								sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL );

PROCEDURE Log_Action ( nUserId IN ACTION_TRAIL.USER_ID%TYPE, 
					   nEvent IN ACTION_TRAIL.ACTION_EVENT%TYPE,
					   sMethod IN ACTION_TRAIL.METHOD_CALL%TYPE DEFAULT NULL,
					   sActionScript IN ACTION_TRAIL.ACTION_SCRIPT%TYPE DEFAULT NULL,
					   sDescription IN ACTION_TRAIL.ACTION_DESCRIPTION%TYPE DEFAULT NULL  );					  						  				  				 

END pk_Utils;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_BASE_ENV
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_BASE_ENV" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
   
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Base_Env ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 nBosId IN BASE_ENV.BOS_ID%TYPE,
						 nBomId IN BASE_ENV.BOM_ID%TYPE,
						 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						 nUserId IN NUMBER ) IS
						 
nBaseEnvId BASE_ENV.BASE_ENV_ID%TYPE;	
NodeName VARCHAR2(4000);					 
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId;
	
	-- Get base_env_id
	SELECT SEQ_BASE_ENV_ID.NEXTVAL INTO nBaseEnvId FROM DUAL;
	
	-- Insert new base env name
	INSERT INTO BASE_ENV ( BASE_ENV_ID, BOS_ID, BASE_ENV_NAME, BOM_ID )
	VALUES ( nBaseEnvId, nBosId, sBaseEnvName, nBomId );
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Os_Trail ( nOsId, 'Added Base Configuration: '||sBaseEnvName||' into Node: '||NodeName, nUserId );	
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Base Configuration Name "'|| sBaseEnvName ||'" is Already Used in this BOM.');
		
END	Add_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
							sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 	nBosId IN BASE_ENV.BOS_ID%TYPE,
							nOsId IN OS_BASE_ENV.OS_ID%TYPE,
							nUserId IN NUMBER ) IS
							
NodeName VARCHAR2(4000);							
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId= '|| nBaseEnvId  );
		
	END IF;
	/*-------------------------------------------------------*/
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId;
	
	-- Update Base Env
	UPDATE BASE_ENV SET
		BASE_ENV_NAME = sBaseEnvName,
		BOS_ID = nBosId
	WHERE BASE_ENV_ID = nBaseEnvId;
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Os_Trail ( nOsId, 'Updated Base Configuration: '||sBaseEnvName||' on Node: '||NodeName, nUserId );	

	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Base Configuration Name "'|| sBaseEnvName ||'" is Already Used in this BOM.');
		
END	Update_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION Get_Base_Env_Id ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						   nBomId IN BASE_ENV.BOM_ID%TYPE ) RETURN BASE_ENV.BASE_ENV_ID%TYPE IS

ReturnValue BASE_ENV.BASE_ENV_ID%TYPE;	

CURSOR curBaseEnv IS
	SELECT be.BASE_ENV_ID
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_NAME = sBaseEnvName
	   AND be.BOM_ID = nBomId;
recBaseEnv curBaseEnv%ROWTYPE;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get bom_name_id
	OPEN curBaseEnv;
	FETCH curBaseEnv INTO recBaseEnv;
	
	IF curBaseEnv%FOUND THEN
		ReturnValue := recBaseEnv.BASE_ENV_ID;
	ELSE
		ReturnValue := NULL;
	END IF;
	
	CLOSE curBaseEnv;
	
	RETURN ReturnValue;
END	Get_Base_Env_Id;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE, 
		  				   	 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						 	 nUserId IN NUMBER ) IS

BaseEnvName VARCHAR2(100);		
NodeName VARCHAR2(4000);					 
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId= '|| nBaseEnvId  );
		
	END IF;
	/*-------------------------------------------------------*/
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId;	
	
	SELECT BASE_ENV_NAME INTO BaseEnvName
	FROM BASE_ENV
	WHERE BASE_ENV_ID = nBaseEnvId;
	
	-- Destroy Base Env Contents
	DELETE
      FROM BASE_ENV_CONTENTS
     WHERE BASE_ENV_ID = nBaseEnvId;

	-- Remove Base Env From BOM
	DELETE 
	  FROM OS_BASE_ENV obe
	 WHERE obe.BASE_ENV_ID = nBaseEnvId;
	
	-- Destroy Base Env
	DELETE 
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_ID = nBaseEnvId;		
	 
	/* Log BOM_TRAIL */
	pk_Bom.Log_Os_Trail ( nOsId, 'Destroyed Base Configuration: '||BaseEnvName||' on Node: '||NodeName, nUserId );		 		 
						 	
END	Destroy_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
						nOsId IN OS_BASE_ENV.OS_ID%TYPE,
						nUserId IN NUMBER ) IS
						 
NextSeqNum NUMBER;
BaseEnvName VARCHAR2(100);
PkgName VARCHAR2(100);
PkgVersion VARCHAR2(100);
NodeName VARCHAR2(4000); 

CURSOR prod_cur IS
	SELECT pv.pv_id AS PROD_ID,
		   pv.PV_DESCRIPTION
	  FROM PACKAGE_VERSIONS pv
	 WHERE pv.pv_id IN (
	 	   			   	SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
						);
prod_rec prod_cur%ROWTYPE;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	SELECT BASE_ENV_NAME INTO BaseEnvName 
	FROM BASE_ENV
	WHERE BASE_ENV_ID = nBaseEnvId;
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId;
	
	
	-- Get seq_num
	SELECT MAX( bec.SEQ_NUM ) INTO NextSeqNum
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = nBaseEnvId;
	
	-- Set NextSeqNum to 0 if no existing products are found
	IF NextSeqNum IS NULL THEN
	   NextSeqNum := 1;
	ELSE
	   NextSeqNum := NextSeqNum + 1;
	END IF;
	
	
	-- Insert into OS Contents
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		INSERT INTO BASE_ENV_CONTENTS ( BASE_ENV_ID, SEQ_NUM, PROD_ID, BASE_ENV_COMMENTS )
		VALUES (
				nBaseEnvId, 
				NextSeqNum, 
				prod_rec.PROD_ID,
				prod_rec.PV_DESCRIPTION
				);
				
		SELECT PKG.PKG_NAME INTO PkgName
		FROM PACKAGES PKG, PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = prod_rec.PROD_ID
		AND PKG.PKG_ID = PV.PKG_ID;
		
		SELECT PKG_VERSION INTO PkgVersion
		FROM PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = prod_rec.PROD_ID;				
		
		-- Log Action --
		pk_Bom.Log_Os_Trail ( nOsId,  
							  'Added Product: '||PkgName||' (Version '||PkgVersion||') to Base Configuration: '||BaseEnvName||' with Install Order: '||NextSeqNum||' on Node: '||NodeName,
							  nUserId );
		
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
	END LOOP;
	
	
	CLOSE prod_cur;
	
	
END	Add_Product; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							nUserId IN NUMBER ) IS
		
PkgName VARCHAR2(100);
PkgVersion VARCHAR2(100);		
NextSeqNum NUMBER := 1;
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
							
CURSOR prod_cur IS
	SELECT bec.PROD_ID, bec.SEQ_NUM
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = nBaseEnvId
	 ORDER BY bec.SEQ_NUM;
prod_rec prod_cur%ROWTYPE;	
  
 							
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF sProdIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Products.');
		
	END IF;
	/*-------------------------------------------------------*/


	
	-- Remove Products
	DELETE 
	FROM BASE_ENV_CONTENTS
	WHERE BASE_ENV_ID = nBaseEnvId
	  AND PROD_ID IN ( 
	  	  		  	    SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 );
	
	
	-- Update Seq Numbers
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		UPDATE BASE_ENV_CONTENTS SET
		SEQ_NUM = NextSeqNum
		WHERE BASE_ENV_ID = nBaseEnvId
		  AND PROD_ID = prod_rec.PROD_ID;
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
	END LOOP;
	
	CLOSE prod_cur;
	
	nIdCollector := IN_LIST_NUMBER ( sProdIdList );
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
		SELECT PKG.PKG_NAME INTO PkgName
		FROM PACKAGES PKG, PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nIdCollector(i)
		AND PKG.PKG_ID = PV.PKG_ID;
		
		SELECT PKG_VERSION INTO PkgVersion
		FROM PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nIdCollector(i);	
		
		-- Log Action --
		pk_Bom.Log_Os_Trail ( nBaseEnvId,  
							  'Deleted Product: '||PkgName||' (Version: '||PkgVersion||') From Base Configuration from Node',
							  nUserId );					  
	END LOOP;	
	
				 					 
	  
END Remove_Products; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product_Wrap ( nPkgId IN NUMBER,
		  				   	 sProdVersion IN VARCHAR2,
							 nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							 nOsId IN OS_BASE_ENV.OS_ID%TYPE,
							 nUserId IN NUMBER,
							 sComments IN VARCHAR2 ) IS
						 
ProdId PACKAGE_VERSIONS.PV_ID%TYPE;
ProdName PACKAGES.PKG_NAME%TYPE;

CURSOR prod_cur IS
	SELECT bec.PROD_ID
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.PROD_ID = ProdId
	   AND bec.BASE_ENV_ID = nBaseEnvId;
prod_rec prod_cur%ROWTYPE;
				  						 
BEGIN
	/*--------------- Business Rules Here -------------------*/                             
	IF (NOT nPkgId IS NULL) AND (sProdVersion IS NULL) AND (NOT nBaseEnvId IS NULL) AND (NOT nUserId IS NULL) THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Please provide Product Version.('|| sProdVersion ||')' );

	ELSIF (nPkgId IS NULL) OR (sProdVersion IS NULL) OR (nBaseEnvId IS NULL) OR (nUserId IS NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nPkgId= '|| nPkgId ||', sProdVersion='|| sProdVersion ||', nBaseEnvId='|| nBaseEnvId ||', nUserId='|| nUserId );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get Product Name --
	SELECT PKG_NAME INTO ProdName FROM PACKAGES WHERE PKG_ID = nPkgId;
	
	
	-- Create Product Version in Release Manager if Required, and Get Prod_id --
	Seed_Package_Names_Versions ( ProdName, sProdVersion, nUserId, ProdId );
	
	
	-- Add Product To Base Env --
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	IF prod_cur%NOTFOUND THEN
	   pk_Base_Env.Add_Product ( ProdId, nBaseEnvId, nOsId, nUserId );
	   
	   -- Update Comments
	   IF (NOT sComments IS NULL) THEN
	   		UPDATE BASE_ENV_CONTENTS bec SET
				bec.BASE_ENV_COMMENTS = sComments
			 WHERE bec.BASE_ENV_ID = nBaseEnvId
			   AND bec.PROD_ID = ProdId;
			    
	   END IF;
	END IF;
	
	CLOSE prod_cur;
	
	
END	Add_Product_Wrap;    
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Product ( nOldProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						   nSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN BASE_ENV_CONTENTS.BASE_ENV_COMMENTS%TYPE,
						   sSunOSElectronicName IN PACKAGES.SUNOS_ELECTRONIC_NAME%TYPE,
						   sWinElectronicName IN PACKAGES.WIN_ELECTRONIC_NAME%TYPE,
						   sNotXML IN BASE_ENV_CONTENTS.NOT_XML%TYPE,
						   sPatchElectronicName IN PACKAGE_VERSIONS.PATCH_ELECTRONIC_NAME%TYPE,
						   nUserId IN NUMBER,
						   outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE ) IS
						   
nNewProdId BASE_ENV_CONTENTS.PROD_ID%TYPE;		

sProdName PACKAGES.PKG_NAME%TYPE;					  
						   
CURSOR curProd IS
 	SELECT pv.PV_ID
	  FROM PACKAGE_VERSIONS pv
	 WHERE pv.pkg_id IN ( SELECT pv.pkg_id FROM PACKAGE_VERSIONS pv WHERE pv.PV_ID = nOldProdId )
	   AND pv.PKG_VERSION = sNewProdIdVersion;
recProd curProd%ROWTYPE;						   
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nOldProdId IS NULL) OR (sNewProdIdVersion IS NULL) OR (nBaseEnvId IS NULL) OR (nSeqNum IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOldProdId= '|| nOldProdId  ||', sNewProdIdVersion= '|| sNewProdIdVersion ||', nBaseEnvId= '|| nBaseEnvId ||', nSeqNum= '|| nSeqNum);
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get new prod_id
	OPEN curProd;
	FETCH curProd INTO recProd;
	
	-- Get the Package Name
	SELECT pkg.pkg_name into sProdName
	FROM PACKAGES pkg, PACKAGE_VERSIONS pv
	WHERE pkg.pkg_id = pv.pkg_id 
	AND pv.pv_id = nOldProdId;		
	
	IF curProd%FOUND THEN
		nNewProdId := recProd.PV_ID;
	ELSE
		-- Call Release Manager to add new product	
		Seed_Package_Names_Versions ( sProdName, sNewProdIdVersion, nUserId, outProdId );
		
		nNewProdId := outProdId;
		/*RAISE_APPLICATION_ERROR (-20000, 'Version '|| sNewProdIdVersion ||' does not exist.');*/
	END IF;
	
	CLOSE curProd;
	
	
	-- Update Product Details
	UPDATE BASE_ENV_CONTENTS SET
		PROD_ID = nNewProdId,
		BASE_ENV_COMMENTS = sComments,
		NOT_XML = sNotXML
	 WHERE BASE_ENV_ID = nBaseEnvId
	   AND PROD_ID = nOldProdId;
	   
	
	UPDATE PACKAGES SET
		SUNOS_ELECTRONIC_NAME = sSunOSElectronicName,
		WIN_ELECTRONIC_NAME = sWinElectronicName
	WHERE  PKG_ID = 
	(
	 SELECT PKG_ID
	 FROM PACKAGE_VERSIONS 
	 WHERE PACKAGE_VERSIONS.PV_ID = nNewProdId
	);   
	
	
	UPDATE PACKAGE_VERSIONS SET 
		 PATCH_ELECTRONIC_NAME = sPatchElectronicName
	WHERE PV_ID = nNewProdId;   		   
	
	
	
	-- Update seq_num
	Shift_Product_SeqNum ( nNewProdId, nBaseEnvId, nSeqNum );
	
END	Update_Product; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Shift_Product_SeqNum ( nProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
		  				 	     nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						 		 nToSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE ) IS
						 
currSeqNum NUMBER;
FromSeqNum NUMBER;
						 
BEGIN
	 -- Get Current seq_num
	 SELECT bec.SEQ_NUM INTO currSeqNum
	   FROM BASE_ENV_CONTENTS bec
	  WHERE bec.BASE_ENV_ID = nBaseEnvId
	    AND bec.PROD_ID = nProdId;
	
		
	 IF currSeqNum > nToSeqNum
	 THEN
	 	
		FromSeqNum := nToSeqNum;
		 
			-- Shift others Up
		UPDATE BASE_ENV_CONTENTS bec SET
			   bec.SEQ_NUM = bec.SEQ_NUM + 1
		 WHERE bec.BASE_ENV_ID = nBaseEnvId 
		   AND bec.PROD_ID != nProdId
		   AND bec.SEQ_NUM BETWEEN  FromSeqNum  AND  currSeqNum - 1;
		   
		 
	 ELSIF currSeqNum < nToSeqNum
	 THEN
	 	 
		FromSeqNum := currSeqNum + 1;  
		
		 -- Shift others Down
		UPDATE BASE_ENV_CONTENTS bec SET
		   	   bec.SEQ_NUM = bec.SEQ_NUM - 1
		 WHERE bec.BASE_ENV_ID = nBaseEnvId 
		   AND bec.PROD_ID != nProdId
		   AND bec.SEQ_NUM BETWEEN  FromSeqNum  AND  nToSeqNum;
		 
	 END IF;
	
	 
	 -- Move Product to new seq_num
	 UPDATE BASE_ENV_CONTENTS bec SET
		    bec.SEQ_NUM = nToSeqNum
	  WHERE bec.BASE_ENV_ID = nBaseEnvId 
	    AND bec.PROD_ID = nProdId;
		
		

END Shift_Product_SeqNum;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						  nUserId IN NUMBER ) IS
		
	nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();						 
	SeqNum NUMBER;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (sProdIdOrder IS NULL)  OR (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId='|| nBaseEnvId  ||' sProdIdOrder='|| sProdIdOrder  );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( sProdIdOrder );
	
	-- Set start sequence number
	SeqNum := 1;
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
	
		-- Update new sequence number
		UPDATE BASE_ENV_CONTENTS osc SET
			osc.SEQ_NUM = SeqNum
		 WHERE osc.BASE_ENV_ID = nBaseEnvId
		   AND osc.PROD_ID = nIdCollector(i);
		   
		SeqNum := SeqNum + 1;
			   
	END LOOP;
	
	
END	Sort_Products;
/*--------------------------------------------------------------------------------------------------*/


END pk_Base_Env;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_BASE_ENV_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_BASE_ENV_TEST" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
   
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Base_Env ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 nBosId IN BASE_ENV.BOS_ID%TYPE,
						 nBomId IN BASE_ENV.BOM_ID%TYPE,
						 nOsId IN OS_BASE_ENV.OS_ID%TYPE ) IS
						 
nBaseEnvId BASE_ENV.BASE_ENV_ID%TYPE;						 
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get base_env_id
	SELECT SEQ_BASE_ENV_ID.NEXTVAL INTO nBaseEnvId FROM DUAL;
	
	-- Insert new base env name
	INSERT INTO BASE_ENV ( BASE_ENV_ID, BOS_ID, BASE_ENV_NAME, BOM_ID )
	VALUES ( nBaseEnvId, nBosId, sBaseEnvName, nBomId );
	

	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Base Configuration Name "'|| sBaseEnvName ||'" is Already Used in this BOM.');
		
END	Add_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
							sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						 	nBosId IN BASE_ENV.BOS_ID%TYPE ) IS
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId= '|| nBaseEnvId  );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Update Base Env
	UPDATE BASE_ENV SET
		BASE_ENV_NAME = sBaseEnvName,
		BOS_ID = nBosId
	WHERE BASE_ENV_ID = nBaseEnvId;
	

	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Base Configuration Name "'|| sBaseEnvName ||'" is Already Used in this BOM.');
		
END	Update_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION Get_Base_Env_Id ( sBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
						   nBomId IN BASE_ENV.BOM_ID%TYPE ) RETURN BASE_ENV.BASE_ENV_ID%TYPE IS

ReturnValue BASE_ENV.BASE_ENV_ID%TYPE;	

CURSOR curBaseEnv IS
	SELECT be.BASE_ENV_ID
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_NAME = sBaseEnvName
	   AND be.BOM_ID = nBomId;
recBaseEnv curBaseEnv%ROWTYPE;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get bom_name_id
	OPEN curBaseEnv;
	FETCH curBaseEnv INTO recBaseEnv;
	
	IF curBaseEnv%FOUND THEN
		ReturnValue := recBaseEnv.BASE_ENV_ID;
	ELSE
		ReturnValue := NULL;
	END IF;
	
	CLOSE curBaseEnv;
	
	RETURN ReturnValue;
END	Get_Base_Env_Id;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Base_Env ( nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE ) IS

						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId= '|| nBaseEnvId  );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Destroy Base Env Contents
	DELETE
      FROM BASE_ENV_CONTENTS
     WHERE BASE_ENV_ID = nBaseEnvId;

	-- Remove Base Env From BOM
	DELETE 
	  FROM OS_BASE_ENV obe
	 WHERE obe.BASE_ENV_ID = nBaseEnvId;
	
	-- Destroy Base Env
	DELETE 
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_ID = nBaseEnvId;				 
						 	
END	Destroy_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE,
						nUserId IN NUMBER ) IS
						 
NextSeqNum NUMBER;

CURSOR prod_cur IS
	SELECT pv.pv_id AS PROD_ID,
		   pv.PV_DESCRIPTION
	  FROM PACKAGE_VERSIONS pv
	 WHERE pv.pv_id IN (
	 	   			   	SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
						);
prod_rec prod_cur%ROWTYPE;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get seq_num
	SELECT MAX( bec.SEQ_NUM ) INTO NextSeqNum
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = nBaseEnvId;
	
	-- Set NextSeqNum to 0 if no existing products are found
	IF NextSeqNum IS NULL THEN
	   NextSeqNum := 1;
	ELSE
	   NextSeqNum := NextSeqNum + 1;
	END IF;
	
	
	-- Insert into OS Contents
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		INSERT INTO BASE_ENV_CONTENTS ( BASE_ENV_ID, SEQ_NUM, PROD_ID, BASE_ENV_COMMENTS )
		VALUES (
				nBaseEnvId, 
				NextSeqNum, 
				prod_rec.PROD_ID,
				prod_rec.PV_DESCRIPTION
				);
		
		-- Log Action --
		pk_Utils.Log_Action ( nUserId, 
							  1, 
							  'pk_Base_Env.Add_Product with BASE_ENV_ID='|| nBaseEnvId ||', SEQ_NUM='|| NextSeqNum ||', PROD_ID='|| prod_rec.PROD_ID,
							  NULL, NULL );
		
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
	END LOOP;
	
	
	CLOSE prod_cur;
	
	
END	Add_Product; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							nUserId IN NUMBER ) IS
		
NextSeqNum NUMBER := 1;
							
CURSOR prod_cur IS
	SELECT bec.PROD_ID, bec.SEQ_NUM
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = nBaseEnvId
	 ORDER BY bec.SEQ_NUM;
prod_rec prod_cur%ROWTYPE;	  
 							
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF sProdIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Products.');
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Remove Products
	DELETE 
	FROM BASE_ENV_CONTENTS
	WHERE BASE_ENV_ID = nBaseEnvId
	  AND PROD_ID IN ( 
	  	  		  	    SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 );
	
	
	-- Update Seq Numbers
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		UPDATE BASE_ENV_CONTENTS SET
		SEQ_NUM = NextSeqNum
		WHERE BASE_ENV_ID = nBaseEnvId
		  AND PROD_ID = prod_rec.PROD_ID;
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
	END LOOP;
	
	CLOSE prod_cur;
	
				 					 
	  
END Remove_Products; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product_Wrap ( nPkgId IN NUMBER,
		  				   	 sProdVersion IN VARCHAR2,
							 nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
							 nUserId IN NUMBER,
							 sComments IN VARCHAR2 ) IS
						 
ProdId PACKAGE_VERSIONS.PV_ID%TYPE;
ProdName PACKAGES.PKG_NAME%TYPE;

CURSOR prod_cur IS
	SELECT bec.PROD_ID
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.PROD_ID = ProdId
	   AND bec.BASE_ENV_ID = nBaseEnvId;
prod_rec prod_cur%ROWTYPE;
				  						 
BEGIN
	/*--------------- Business Rules Here -------------------*/                             
	IF (NOT nPkgId IS NULL) AND (sProdVersion IS NULL) AND (NOT nBaseEnvId IS NULL) AND (NOT nUserId IS NULL) THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Please provide Product Version.('|| sProdVersion ||')' );

	ELSIF (nPkgId IS NULL) OR (sProdVersion IS NULL) OR (nBaseEnvId IS NULL) OR (nUserId IS NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nPkgId= '|| nPkgId ||', sProdVersion='|| sProdVersion ||', nBaseEnvId='|| nBaseEnvId ||', nUserId='|| nUserId );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get Product Name --
	SELECT PKG_NAME INTO ProdName FROM PACKAGES WHERE PKG_ID = nPkgId;
	
	
	-- Create Product Version in Release Manager if Required, and Get Prod_id --
	Seed_Package_Names_Versions ( ProdName, sProdVersion, nUserId, ProdId );
	
	
	-- Add Product To Base Env --
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	IF prod_cur%NOTFOUND THEN
	   pk_Base_Env.Add_Product ( ProdId, nBaseEnvId, nUserId );
	   
	   -- Update Comments
	   IF (NOT sComments IS NULL) THEN
	   		UPDATE BASE_ENV_CONTENTS bec SET
				bec.BASE_ENV_COMMENTS = sComments
			 WHERE bec.BASE_ENV_ID = nBaseEnvId
			   AND bec.PROD_ID = ProdId;
			    
	   END IF;
	END IF;
	
	CLOSE prod_cur;
	
	
END	Add_Product_Wrap;    
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Product ( nOldProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						   nSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN BASE_ENV_CONTENTS.BASE_ENV_COMMENTS%TYPE,
						   sElectronicName IN PACKAGES.ELECTRONIC_NAME%TYPE ) IS
						   
nNewProdId BASE_ENV_CONTENTS.PROD_ID%TYPE;						  
						   
CURSOR curProd IS
 	SELECT pv.PV_ID
	  FROM PACKAGE_VERSIONS pv
	 WHERE pv.pkg_id IN ( SELECT pv.pkg_id FROM PACKAGE_VERSIONS pv WHERE pv.PV_ID = nOldProdId )
	   AND pv.PKG_VERSION = sNewProdIdVersion;
recProd curProd%ROWTYPE;						   
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nOldProdId IS NULL) OR (sNewProdIdVersion IS NULL) OR (nBaseEnvId IS NULL) OR (nSeqNum IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOldProdId= '|| nOldProdId  ||', sNewProdIdVersion= '|| sNewProdIdVersion ||', nBaseEnvId= '|| nBaseEnvId ||', nSeqNum= '|| nSeqNum);
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get new prod_id
	OPEN curProd;
	FETCH curProd INTO recProd;
	
	IF curProd%FOUND THEN
		nNewProdId := recProd.PV_ID;
	ELSE
		RAISE_APPLICATION_ERROR (-20000, 'Version '|| sNewProdIdVersion ||' does not exist.');
	END IF;
	
	CLOSE curProd;
	
	
	-- Update Product Details
	UPDATE BASE_ENV_CONTENTS SET
		PROD_ID = nNewProdId,
		BASE_ENV_COMMENTS = sComments
	 WHERE BASE_ENV_ID = nBaseEnvId
	   AND PROD_ID = nOldProdId;
	   
	
	UPDATE PACKAGES SET
		ELECTRONIC_NAME = sElectronicName
	WHERE  PKG_ID = 
	(
	 SELECT PKG_ID
	 FROM PACKAGE_VERSIONS 
	 WHERE PACKAGE_VERSIONS.PV_ID = nNewProdId
	);   
	
	
	
	-- Update seq_num
	Shift_Product_SeqNum ( nNewProdId, nBaseEnvId, nSeqNum );
	
END	Update_Product; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Shift_Product_SeqNum ( nProdId IN BASE_ENV_CONTENTS.PROD_ID%TYPE,
		  				 	     nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						 		 nToSeqNum IN BASE_ENV_CONTENTS.SEQ_NUM%TYPE ) IS
						 
currSeqNum NUMBER;
FromSeqNum NUMBER;
						 
BEGIN
	 -- Get Current seq_num
	 SELECT bec.SEQ_NUM INTO currSeqNum
	   FROM BASE_ENV_CONTENTS bec
	  WHERE bec.BASE_ENV_ID = nBaseEnvId
	    AND bec.PROD_ID = nProdId;
	
		
	 IF currSeqNum > nToSeqNum
	 THEN
	 	
		FromSeqNum := nToSeqNum;
		 
			-- Shift others Up
		UPDATE BASE_ENV_CONTENTS bec SET
			   bec.SEQ_NUM = bec.SEQ_NUM + 1
		 WHERE bec.BASE_ENV_ID = nBaseEnvId 
		   AND bec.PROD_ID != nProdId
		   AND bec.SEQ_NUM BETWEEN  FromSeqNum  AND  currSeqNum - 1;
		   
		 
	 ELSIF currSeqNum < nToSeqNum
	 THEN
	 	 
		FromSeqNum := currSeqNum + 1;  
		
		 -- Shift others Down
		UPDATE BASE_ENV_CONTENTS bec SET
		   	   bec.SEQ_NUM = bec.SEQ_NUM - 1
		 WHERE bec.BASE_ENV_ID = nBaseEnvId 
		   AND bec.PROD_ID != nProdId
		   AND bec.SEQ_NUM BETWEEN  FromSeqNum  AND  nToSeqNum;
		 
	 END IF;
	
	 
	 -- Move Product to new seq_num
	 UPDATE BASE_ENV_CONTENTS bec SET
		    bec.SEQ_NUM = nToSeqNum
	  WHERE bec.BASE_ENV_ID = nBaseEnvId 
	    AND bec.PROD_ID = nProdId;
		
		

END Shift_Product_SeqNum;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nBaseEnvId IN BASE_ENV_CONTENTS.BASE_ENV_ID%TYPE,
						  nUserId IN NUMBER ) IS
		
	nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();						 
	SeqNum NUMBER;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (sProdIdOrder IS NULL)  OR (nBaseEnvId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBaseEnvId='|| nBaseEnvId  ||' sProdIdOrder='|| sProdIdOrder  );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( sProdIdOrder );
	
	-- Set start sequence number
	SeqNum := 1;
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
	
		-- Update new sequence number
		UPDATE BASE_ENV_CONTENTS osc SET
			osc.SEQ_NUM = SeqNum
		 WHERE osc.BASE_ENV_ID = nBaseEnvId
		   AND osc.PROD_ID = nIdCollector(i);
		   
		SeqNum := SeqNum + 1;
			   
	END LOOP;
	
	
END	Sort_Products;
/*--------------------------------------------------------------------------------------------------*/


END pk_Base_Env_test;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_BOM
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_BOM" 
IS
/* ---------------------------------------------------------------------------
    Version: 2.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Bom ( sBomName IN BOM_NAMES.BOM_NAME%TYPE DEFAULT NULL,
		  		  	sBomVersion IN BOMS.BOM_VERSION%TYPE,
		  		  	sBomLifeCycle IN BOMS.BOM_LIFECYCLE%TYPE,		
					sBomComments IN BOMS.BOM_COMMENTS%TYPE DEFAULT NULL,
					nToBranchId IN BOMS.BRANCH_ID%TYPE,
					nFromBomId IN BOMS.BOM_ID%TYPE DEFAULT NULL,
					nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					nUserId IN BOM_STATE.USER_ID%TYPE ) IS

BomId BOMS.BOM_ID%TYPE;
ParentBomId BOMS.PARENT_BOM_ID%TYPE;
BomNameId BOM_NAMES.BOM_NAME_ID%TYPE;
BomVersion BOMS.BOM_VERSION%TYPE;
BomLifeCycle BOMS.BOM_LIFECYCLE%TYPE;
ProjId BRANCHES.PROJ_ID%TYPE;

CURSOR curBom IS 
		SELECT bo.BOM_ID
		  FROM BOMS bo,
		  	   BRANCHES br
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID = ProjId
		   AND bo.BOM_NAME_ID = BomNameId
		   AND bo.BOM_VERSION = BomVersion
		   AND bo.BOM_LIFECYCLE = BomLifeCycle;
recBom curBom%ROWTYPE;

DUPLICATE_BOM EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nToBranchId IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nToBranchId= '|| nToBranchId ||', nUserId='|| nUserId );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get bom_id
	SELECT SEQ_BOM_ID.NEXTVAL INTO BomId FROM DUAL;
	
	
	-- Get bom_name_id
	IF nFromBomId IS NULL THEN
		BomNameId := New_Bom_Name ( sBomName );
        ParentBomId := BomId;
	ELSE
		BomNameId := New_Bom_Name ( nFromBomId );
        ParentBomId := nFromBomId;
	END IF;		

	


	-- Get Bom Version and Lifecycle
	--Get_Next_Version ( nFromBomId, nToBranchId, BomVersion, BomLifeCycle, ParentBomId, BomNameId );

	
	
	-- Get proj_id
	SELECT br.PROJ_ID INTO ProjId
	  FROM BRANCHES br
	 WHERE br.BRANCH_ID = nToBranchId;
	
	
	
	-- Check for duplicate BOMs
	OPEN curBom;
    FETCH curBom INTO recBom;

    IF curBom%FOUND
    THEN
		RAISE DUPLICATE_BOM;
	END IF;
	
	CLOSE curBom;
	
	
	
	-- Add Blank Bom --
	INSERT INTO BOMS ( BOM_ID, BRANCH_ID, BOM_NAME_ID, BOM_COMMENTS, BOM_VERSION, BOM_LIFECYCLE, IS_READONLY, IS_HIDDEN, PARENT_BOM_ID, RTAG_ID_FK )
	VALUES ( BomId, nToBranchId, BomNameId, sBomComments, sBomVersion, sBomLifeCycle, 'N', 'N', ParentBomId, nRtagId  );
	
	
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( BomId, 'CREATED: Version '|| sBomVersion ||'.'|| sBomLifeCycle , nUserId );
	
	
	
	IF nFromBomId IS NOT NULL THEN
		/* Add Bom Clonned from ... */
		pk_Utils.Clone_BOM ( nFromBomId, BomId, nUserId );
		
	END IF;
	
	
	
	--- Set Bom State ---
	Release_To ( BomId, nUserId );
	
	
	EXCEPTION
    WHEN DUPLICATE_BOM
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'BOM "'|| sBomName ||' '|| BomVersion ||'.'|| BomLifeCycle ||'" is Already Used in this Project.');
			
END	Add_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Bom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER )  IS
					   
ReleaseName VARCHAR2(100);					   

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	--- Update Bom Properties ---
	UPDATE BOMS bo SET
		bo.RTAG_ID_FK = nRtagId,
		bo.BOM_COMMENTS = sBomComment
	WHERE bo.BOM_ID = nBomId;
	
	SELECT RTAG_NAME INTO ReleaseName
	FROM RELEASE_TAGS 
	WHERE RTAG_ID = nRtagId;
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'Updated BOM properties: Set Release Manager Reference ='||ReleaseName||' and comments ='||sBomComment, nUserId );
	
	
END	Update_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_ProdBom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER )  IS
					   
ReleaseName VARCHAR2(100);					   

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	--- Update Bom Properties ---
	UPDATE BOMS bo SET
		bo.RTAG_ID_FK = nRtagId,
		bo.BOM_COMMENTS = sBomComment
	WHERE bo.BOM_ID = nBomId;
	
	SELECT BRANCH_NAME INTO ReleaseName
	FROM BRANCHES 
	WHERE BRANCH_ID = nRtagId;
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'Updated BOM properties: Set Deployment Manager Reference ='||ReleaseName||' and comments ='||sBomComment, nUserId );
	
	
END	Update_ProdBom;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION New_Bom_Name ( sBomName IN BOM_NAMES.BOM_NAME%TYPE ) RETURN BOM_NAMES.BOM_NAME_ID%TYPE IS

ReturnValue 	 BOM_NAMES.BOM_NAME_ID%TYPE;	

CURSOR curBomNames IS
	SELECT bn.BOM_NAME_ID
	  FROM BOM_NAMES bn
	 WHERE bn.BOM_NAME = sBomName;
recBomNames curBomNames%ROWTYPE;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Check for existance of this name
	OPEN curBomNames;
	FETCH curBomNames INTO recBomNames;
	
	ReturnValue := NULL;
	IF curBomNames%FOUND THEN
		ReturnValue := recBomNames.BOM_NAME_ID;
	END IF;
	
	CLOSE curBomNames;
	
	
	-- Finnaly Create Name if required --    
	IF ReturnValue IS NULL THEN
		-- Get bom_name_id
		SELECT SEQ_BOM_NAME_ID.NEXTVAL INTO ReturnValue FROM DUAL;
		
		INSERT INTO BOM_NAMES ( BOM_NAME_ID, BOM_NAME )
		VALUES ( ReturnValue, sBomName );
	
	END IF;	
	
	
	RETURN ReturnValue;
END	New_Bom_Name;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION New_Bom_Name ( nFromBomId IN BOMS.BOM_NAME_ID%TYPE ) RETURN BOMS.BOM_NAME_ID%TYPE IS

ReturnValue 	 BOMS.BOM_NAME_ID%TYPE;	

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get bom_name_id
	SELECT bo.BOM_NAME_ID INTO ReturnValue
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nFromBomId;
	
	
	RETURN ReturnValue;
END	New_Bom_Name;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject ( nBomId IN BOMS.BOM_ID%TYPE,
						  cIsRejected IN BOMS.IS_REJECTED%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE,
						  nStateId IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE  ) IS

StateName STATES.STATE_NAME%TYPE;						  
IsOfficial BOMS.IS_READONLY%TYPE;	
IsRejected VARCHAR2(50);					  

BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check if Bom is official
	SELECT bo.IS_READONLY INTO IsOfficial
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nBomId;
	 
	IF IsOfficial != 'Y' THEN
		RAISE_APPLICATION_ERROR (-20000, 'BOM must be locked first.' );
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get State Name
	SELECT st.STATE_NAME INTO StateName
	  FROM STATES st
	 WHERE st.STATE_ID = nStateId;
	
	
	-- AcceptReject BOM
	UPDATE BOMS bo SET
		bo.IS_REJECTED = cIsRejected
	WHERE bo.BOM_ID = nBomId;
	
	
	-- Auto-Hide Bom
	IF cIsRejected = 'Y' THEN
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_REJECT_BOM') );
	ELSE
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_ACCEPT_BOM') );
	END IF;
	
	
	-- Log Rejection Trail 
	pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM'), nBomId, cIsRejected, nUserId, sComments );
	
	
	/* Log BOM_TRAIL */
	-- Get Log Title
	IsRejected := 'ACCEPTED';
	IF cIsRejected = 'Y' THEN
		IsRejected := 'REJECTED';
	END IF;
	
	-- Finnaly log it
	Log_Bom_Trail ( nBomId, IsRejected ||': By '|| StateName, nUserId, sComments );
	
END	Accept_Reject;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Set_Bom_Official ( nBomId IN BOMS.BOM_ID%TYPE,
							 cIsOfficial IN BOMS.IS_READONLY%TYPE,
							 nStateId IN BOM_STATE.STATE_ID%TYPE,
							 nUserId IN NUMBER )  IS
							 
							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	--- Set Bom Official or Not ---
	UPDATE BOMS bo SET
	bo.IS_READONLY = cIsOfficial
	WHERE bo.BOM_ID = nBomId;
	
	
	-- Auto-Hide Bom
	IF cIsOfficial = 'Y' THEN
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_LOCK_BOM') );
		
		-- Update node version
		Update_Nodes_Version ( nBomId, nUserId );
	ELSE
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_UNLOCK_BOM') );
	END IF;
	
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'LOCKED: '|| cIsOfficial, nUserId );
	
	
END	Set_Bom_Official;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Nodes_Version ( nBomId IN BOMS.BOM_ID%TYPE,
							 	 nUserId IN NUMBER )  IS

	CompareBomId NUMBER;
	ProjId NUMBER;
	BomVersion VARCHAR2(4000);
									 	
	CURSOR curUpdateNode IS
		SELECT DISTINCT
			   nlook.NODE_ID,
			   DECODE ( nlc.LAST_LIFE_CYCLE,
			   			NULL, 0,
						nlc.LAST_LIFE_CYCLE + 1 ) AS NEW_LIFE_CYCLE	   
		  FROM (
		  
		  		/* ADDED PATCHES ONLY */
				SELECT 'A' AS CHANGE_TYPE, newbom.PROD_ID, newbom.PK 
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND oldbom.PK IS NULL
				UNION 
		  		/* ADDED PATCHES ONLY TO BASE CONFIGURATION */
				SELECT 'A' AS CHANGE_TYPE, newbom.PROD_ID, newbom.PK 
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND oldbom.PK IS NULL
				UNION				
				
				/* ADDED PRODUCTS ONLY */
				SELECT DECODE( oldbom.PROD_ID,
							   NULL, 'A', 'U' ) AS CHANGE_TYPE, 
					   newbom.PROD_ID, newbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   --AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND newbom.PKG_ID  = oldbom.PKG_ID (+)
				   AND newbom.V_EXT  = oldbom.V_EXT (+) 
				   AND ( oldbom.PROD_ID != newbom.PROD_ID  OR oldbom.PROD_ID IS NULL )	
				   AND newbom.PROD_ID NOT IN 
				   (
				   	   SELECT osc.PROD_ID
					   FROM OS_CONTENTS osc,
					  	    OPERATING_SYSTEMS os,
							NETWORK_NODES nn,
							BOM_CONTENTS bc,
							PACKAGE_VERSIONS pv
					  WHERE osc.OS_ID = os.OS_ID
						AND os.NODE_ID = nn.NODE_ID
						AND bc.NODE_ID = nn.NODE_ID
						AND osc.PROD_ID = pv.PV_ID
						AND pv.IS_PATCH IS NULL
						AND bc.BOM_ID = CompareBomId
				  )	 				   
				      
				UNION

				/* ADDED PRODUCTS ONLY TO BASE CONFIGURATION */
				SELECT DECODE( oldbom.PROD_ID,
							   NULL, 'A', 'U' ) AS CHANGE_TYPE, 
					   newbom.PROD_ID, newbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   --AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND newbom.PKG_ID  = oldbom.PKG_ID (+)
				   AND newbom.V_EXT  = oldbom.V_EXT (+) 
				   AND ( oldbom.PROD_ID != newbom.PROD_ID  OR oldbom.PROD_ID IS NULL )	   
				UNION
				
				/* REMOVED PRODUCTS ONLY*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   --AND newbom.PROD_ID (+) = oldbom.PROD_ID
				   AND newbom.PKG_ID (+) = oldbom.PKG_ID
				   AND newbom.V_EXT (+) = oldbom.V_EXT   
				   AND newbom.PK IS NULL  
				UNION   
				/* REMOVED PATCHES */    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   AND newbom.PROD_ID (+) = oldbom.PROD_ID  
				   AND newbom.PK IS NULL 
				   UNION
				/* REMOVED PRODUCTS ONLY FROM BASE CONFIGURATION*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID 
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   --AND newbom.PROD_ID (+) = oldbom.PROD_ID
				   AND newbom.PKG_ID (+) = oldbom.PKG_ID
				   AND newbom.V_EXT (+) = oldbom.V_EXT   
				   AND newbom.PK IS NULL  
				UNION   
				/* REMOVED PATCHES FROM BASE CONFIGURATION*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   AND newbom.PROD_ID (+) = oldbom.PROD_ID  
				   AND newbom.PK IS NULL				    	
				   
		       ) qry,
			   (
			   
		  		SELECT nn.NODE_ID,
					   nn.NODE_NAME,
					   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
				  FROM OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   BOM_CONTENTS bc
				 WHERE os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND bc.BOM_ID = nBomId
				   
		  	   ) nlook,
			   (
			   	
				SELECT nn.NODE_NAME, 
					   MAX(nn.NODE_LIFE_CYCLE) AS LAST_LIFE_CYCLE
				  FROM BOMS bo,
				  	   BOM_CONTENTS bc,
					   BRANCHES br,
					   NETWORK_NODES nn
				 WHERE bc.BOM_ID = bo.BOM_ID
				   AND bo.BRANCH_ID = br.BRANCH_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND br.PROJ_ID = ProjId
		  	     GROUP BY nn.NODE_NAME
			   
			   ) nlc
		 WHERE nlook.PK = qry.PK 
		   AND nlc.NODE_NAME = nlook.NODE_NAME;
	recUpdateNode curUpdateNode%ROWTYPE;
	
	
								 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get some BOM details
	SELECT bo.PARENT_BOM_ID, br.PROJ_ID, bo.BOM_VERSION ||'.'|| bo.BOM_LIFECYCLE INTO CompareBomId, ProjId, BomVersion
	  FROM BOMS bo,
	  	   BRANCHES br
	 WHERE bo.BOM_ID = nBomId
	   AND bo.BRANCH_ID = br.BRANCH_ID;


	-- Update node version
	OPEN curUpdateNode;
	FETCH curUpdateNode INTO recUpdateNode;
	
	WHILE curUpdateNode%FOUND
	LOOP
		
		UPDATE NETWORK_NODES nn SET
			nn.NODE_VERSION = BomVersion,
			nn.NODE_LIFE_CYCLE = recUpdateNode.NEW_LIFE_CYCLE
		 WHERE nn.NODE_ID = recUpdateNode.NODE_ID;
		
		FETCH curUpdateNode INTO recUpdateNode;
	END LOOP;
	
	
	CLOSE curUpdateNode;	
	
END	Update_Nodes_Version;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 ) IS
						 
rowCount NUMBER;
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
nBomIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId );
		
	END IF;
	
	-- Check Requirements
	pk_Requirements.Destroy_Bom ( nBomId );
	
	/*-------------------------------------------------------*/
	
	
	
	-- Load Collector
	nBomIdCollector := IN_LIST_NUMBER( nBomId );
	
	
	-- Destroy BASE_ENV
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT be.BASE_ENV_ID
	  	   		  		   	   FROM BASE_ENV be
	 						  WHERE be.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	) 
						    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
	
	Destroy_BaseEnv ( nIdCollector );
	
	
	
	-- Destroy NETWORK_NODES
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT bc.NODE_ID
	  	   		  		   	   FROM BOM_CONTENTS bc
	 						  WHERE bc.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	) 
						    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
	pk_Network_Node.Destroy_Node ( nIdCollector, 0 );
	 
	 
	
	-- Destroy HARDWARE
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT hr.HARDWARE_NAME_ID
	  	   		  		   	   FROM HARDWARE hr
	 						  WHERE hr.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	)
							) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
	Destroy_Hardware ( nIdCollector );
	
	
	
	-- Destroy BOM_STATE
	DELETE 
	  FROM BOM_STATE bs
	 WHERE bs.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	);
	 
	
	-- Destroy BOM_TRAIL
	DELETE 
	  FROM BOM_TRAIL bt
	 WHERE bt.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	); 
	
	 
	---- Finnaly Destroy BOM ----
	DELETE 
	  FROM BOMS bo
	 WHERE bo.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	);
	 
	  
	   
END	Destroy_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_BaseEnv ( nBaseEndIdCollector IN DEPMGR_NUMBER_TAB_t ) IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Destroy BASE_ENV_CONTENTS
	DELETE
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	
	-- Destroy OS_BASE_ENV
	DELETE
	  FROM OS_BASE_ENV obe
	 WHERE obe.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	 
	 -- Destroy BASE_ENV
	DELETE
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	   
	 
END	Destroy_BaseEnv;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Hardware ( nHardwareNameIdCollector IN DEPMGR_NUMBER_TAB_t ) IS
						 
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
    -- Destroy NETWORK_NODES
    nIdCollector := NULL;
    SELECT CAST ( MULTISET ( SELECT hn.NODE_ID
	  	   		  		   	   FROM HARDWARE_NODE hn
	 						  WHERE hn.HARDWARE_NAME_ID IN ( SELECT * FROM TABLE ( CAST ( nHardwareNameIdCollector AS DEPMGR_NUMBER_TAB_t ) ) ) 
						   ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
    pk_Network_Node.Destroy_Node ( nIdCollector, 0 );
	
	
	-- Finnaly Destroy HARDWARE
	DELETE
	  FROM HARDWARE hd
	 WHERE hd.HARDWARE_NAME_ID IN ( SELECT * FROM TABLE ( CAST ( nHardwareNameIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	 
END	Destroy_Hardware;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Get_Next_Version (nFromBomId IN BOMS.BOM_ID%TYPE,
							nToBranchId IN BOMS.BRANCH_ID%TYPE,
							sNextVersion OUT BOMS.BOM_VERSION%TYPE,
							sNextLifeCycle OUT BOMS.BOM_LIFECYCLE%TYPE,
							nParentBomId IN OUT BOMS.PARENT_BOM_ID%TYPE,
							nBomNameId IN BOMS.BOM_NAME_ID%TYPE ) IS
							
IsNotHead NUMBER;
FromBranch NUMBER;
LastRootVersion NUMBER;
LastVersion BOMS.BOM_VERSION%TYPE;
							
CURSOR curBoms IS
	SELECT Max ( CAST ( bo.BOM_VERSION AS NUMBER  ) ) AS LAST_VERSION
		  FROM BOMS bo,
		  	   BRANCHES br
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID IN ( SELECT br.PROJ_ID  FROM BRANCHES br  WHERE br.BRANCH_ID = nToBranchId )	
		   AND bo.BOM_NAME_ID = nBomNameId;
recBoms curBoms%ROWTYPE;
	
				  							 
BEGIN

	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	-- Get Last Root Version
	
	OPEN curBoms;
	FETCH curBoms INTO recBoms;	
	LastRootVersion := recBoms.LAST_VERSION;
	CLOSE curBoms;
	
	
	
	
	
	IF nFromBomId IS NULL THEN
		/* Blank BOM Required */
	
		-- Get Next Available Version
		sNextVersion := '1';
		sNextLifeCycle := '0';
		IF LastRootVersion IS NOT NULL THEN
			sNextVersion := CAST( LastRootVersion + 1 AS VARCHAR2 );
		END IF;
	
	
		
	ELSE
		/* Create Bom From ... */
		
		-- Check if BOM is comming from different Iteration
		SELECT bo.BRANCH_ID INTO FromBranch
		  FROM BOMS bo
		 WHERE bo.BOM_ID = nFromBomId; 
		
		
		
		IF FromBranch = nToBranchId THEN
			/* Bom is comming from same Iteration */
			/* i.e. Continue on same version stream. */
			
			-- Set parent_id
			nParentBomId := nFromBomId;
			
			
			-- Get Last Version 
			SELECT bo.BOM_VERSION INTO LastVersion
			  FROM BOMS bo
			 WHERE bo.BOM_ID = nFromBomId;
			
			
			-- Check if Bom is Head (i.e. Last on its branch )
			SELECT COUNT(*) INTO IsNotHead
			  FROM BOMS bo,
			  	   BRANCHES br
			 WHERE bo.BRANCH_ID = br.BRANCH_ID
			   AND br.PROJ_ID IN ( SELECT br.PROJ_ID FROM BRANCHES br WHERE br.BRANCH_ID = FromBranch )
			   AND bo.PARENT_BOM_ID = nFromBomId
			   AND bo.BOM_VERSION = LastVersion
			   AND bo.BOM_ID != bo.PARENT_BOM_ID;
			   
			   
			IF IsNotHead = 0 THEN
				/* Bom IS Head (i.e. Last on its branch ) */
				
				-- Get Bom Version and LifeCycle
				SELECT bo.BOM_VERSION, bo.BOM_LIFECYCLE INTO sNextVersion, sNextLifeCycle
				  FROM BOMS bo
				 WHERE bo.BOM_ID = nFromBomId;
				
				-- Increase Lifecycle
				sNextLifeCycle := CAST (  ( CAST( sNextLifeCycle AS NUMBER ) + 1 )  AS VARCHAR2 );
				
				
			ELSE
				/* Bom IS NOT Head */
				
				-- Get Bom Version and LifeCycle
				SELECT bo.BOM_VERSION, bo.BOM_LIFECYCLE INTO sNextVersion, sNextLifeCycle
				  FROM BOMS bo
				 WHERE bo.BOM_ID = nFromBomId;
				
				-- Branch Bom Version 
				sNextVersion := sNextVersion ||'.'|| sNextLifeCycle;
				
				-- Reset Lifecycle to 1
				sNextLifeCycle := '1';
				
			END IF;	
			
			
		ELSE
			/* BOM is comming from Different Iteration */
			/* i.e. Start new version stream. */
			
			-- Get Next Available Version
			sNextVersion := '1';
			sNextLifeCycle := '0';
			IF LastRootVersion IS NOT NULL THEN
				sNextVersion := CAST( LastRootVersion + 1 AS VARCHAR2 );
			END IF;
		
		END IF;
		
	
	END IF;
	
		
END	Get_Next_Version;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Release_To ( nBomId IN BOM_STATE.BOM_ID%TYPE,
					   nUserId IN BOM_STATE.USER_ID%TYPE,
					   nToState IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
					   sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL ) IS

rowCount NUMBER;	
IsOfficial BOMS.IS_READONLY%TYPE;					 
StateId BOM_STATE.STATE_ID%TYPE;
StateName STATES.STATE_NAME%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check if already released
	SELECT Count(*) INTO rowCount
	  FROM BOM_STATE bs
	 WHERE bs.BOM_ID = nBomId
	   AND bs.STATE_ID = nToState;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'This BOM is already release' );
	END IF;
	
	
	-- Check if Bom is official
	SELECT bo.IS_READONLY INTO IsOfficial
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nBomId;
	 
	IF (IsOfficial != 'Y') AND (nToState IS NOT NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'BOM must be locked first' );
	END IF;
	/*-------------------------------------------------------*/
	
	
	-- Get State --
	StateId := nToState;
	IF StateId IS NULL THEN
		-- If state_id is null it is considered as state_type_enum = 'BOM Creator'
		SELECT st.STATE_ID INTO StateId
		  FROM BOMS bo,
		  	   BRANCHES br,
			   DM_PROJECTS proj,
			   STATES st
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID = proj.PROJ_ID
		   AND st.PROJ_ID = proj.PROJ_ID
		   AND bo.BOM_ID = nBomId
		   AND st.STATE_TYPE_ENUM = GET_SYSTEM_VALUE('enumSTATE_TYPE_CREATOR');	
	END IF;
	
	
	-- Add Bom to State --
	INSERT INTO BOM_STATE (BOM_ID, STATE_ID, DATE_STAMP, IS_DISPLAYED, USER_ID )
	VALUES( nBomId,
			StateId,
			GET_DATE(),
			'Y',
			nUserId );
	
	
	
	-- Auto-Hide Bom
	pk_State.Auto_Hide ( StateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_RELEASE_TO') );

	
	/* Log BOM_TRAIL */
	-- Get State Name --
	SELECT st.STATE_NAME INTO StateName
	  FROM STATES st
	 WHERE st.STATE_ID = StateId;
	
	-- Log Action 
	pk_Bom.Log_Bom_Trail ( nBomId, 'RELEASED TO: '|| StateName, nUserId, sComments );
	
	 
END	Release_To;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject_Comments ( nBomId IN BOMS.BOM_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE ) IS

IsRejected REJECTION_TRAIL.IS_REJECTED%TYPE;
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId  );
	END IF;
	/*-------------------------------------------------------*/
	
	IF nRejectSeq IS NULL THEN
		/* CREATE New Note */
		
		-- Get last IsRejected state
		SELECT rt.IS_REJECTED INTO IsRejected
		  FROM REJECTION_TRAIL rt
		 WHERE rt.ENTITY_ID = nBomId
		   AND rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM')
		   AND rt.REJECT_SEQ =
		   (
		   	SELECT MAX(REJECT_SEQ) FROM
			REJECTION_TRAIL rt
		 	WHERE rt.ENTITY_ID = nBomId
		   	AND rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM')
			
		   );

		 				 
		/* Log Rejection Trail */
		pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM'), nBomId, IsRejected, nUserId, sComments );
		
	ELSE
		/* EDIT Note */
		UPDATE REJECTION_TRAIL rt SET
			rt.COMMENTS = sComments,
			rt.USER_ID = nUserId,
			rt.DATE_TIME_STAMP = GET_DATETIME()
		WHERE rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM')
		  AND rt.ENTITY_ID = nBomId
		  AND rt.REJECT_SEQ = nRejectSeq;
		  
			
	END IF;
	
	
END	Accept_Reject_Comments;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Bom_Trail ( nBomId IN BOM_TRAIL.BOM_ID%TYPE,
						  sAction IN BOM_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN BOM_TRAIL.USER_ID%TYPE,
						  sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO BOM_TRAIL ( BOM_ID, TRAIL_ACTION, TRAIL_COMMENTS, USER_ID, DATE_TIME_STAMP )
	VALUES ( nBomId, sAction, sComments, nUserId, GET_DATETIME() );	
		
END	Log_Bom_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Node_Trail ( nNodeId IN NODE_TRAIL.NODE_ID%TYPE,
						  sAction IN NODE_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN NODE_TRAIL.USER_ID%TYPE  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nNodeId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nNodeId= '|| nNodeId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO NODE_TRAIL ( NODE_ID, TRAIL_ACTION, USER_ID, DATE_TIME_STAMP )
	VALUES ( nNodeId, sAction, nUserId, GET_DATETIME() );	
		
END	Log_Node_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Os_Trail ( nOsId IN OS_TRAIL.OS_ID%TYPE,
						  sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN OS_TRAIL.USER_ID%TYPE  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nOsId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId= '|| nOsId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO OS_TRAIL ( OS_ID, TRAIL_ACTION, USER_ID, DATE_TIME_STAMP )
	VALUES ( nOsId, sAction, nUserId, GET_DATETIME() );	
		
END	Log_Os_Trail;
/*--------------------------------------------------------------------------------------------------*/

END pk_Bom;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_BOM_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_BOM_TEST" 
IS
/* ---------------------------------------------------------------------------
    Version: 2.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Bom ( sBomName IN BOM_NAMES.BOM_NAME%TYPE DEFAULT NULL,
		  		  	sBomVersion IN BOMS.BOM_VERSION%TYPE,
		  		  	sBomLifeCycle IN BOMS.BOM_LIFECYCLE%TYPE,		
					sBomComments IN BOMS.BOM_COMMENTS%TYPE DEFAULT NULL,
					nToBranchId IN BOMS.BRANCH_ID%TYPE,
					nFromBomId IN BOMS.BOM_ID%TYPE DEFAULT NULL,
					nUserId IN BOM_STATE.USER_ID%TYPE ) IS

BomId BOMS.BOM_ID%TYPE;
ParentBomId BOMS.PARENT_BOM_ID%TYPE;
BomNameId BOM_NAMES.BOM_NAME_ID%TYPE;
BomVersion BOMS.BOM_VERSION%TYPE;
BomLifeCycle BOMS.BOM_LIFECYCLE%TYPE;
ProjId BRANCHES.PROJ_ID%TYPE;

CURSOR curBom IS 
		SELECT bo.BOM_ID
		  FROM BOMS bo,
		  	   BRANCHES br
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID = ProjId
		   AND bo.BOM_NAME_ID = BomNameId
		   AND bo.BOM_VERSION = BomVersion
		   AND bo.BOM_LIFECYCLE = BomLifeCycle;
recBom curBom%ROWTYPE;

DUPLICATE_BOM EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nToBranchId IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nToBranchId= '|| nToBranchId ||', nUserId='|| nUserId );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get bom_id
	SELECT SEQ_BOM_ID.NEXTVAL INTO BomId FROM DUAL;
	
	
	-- Get bom_name_id
	IF nFromBomId IS NULL THEN
		BomNameId := New_Bom_Name ( sBomName );
        ParentBomId := BomId;
	ELSE
		BomNameId := New_Bom_Name ( nFromBomId );
        ParentBomId := nFromBomId;
	END IF;		

	


	-- Get Bom Version and Lifecycle
	--Get_Next_Version ( nFromBomId, nToBranchId, BomVersion, BomLifeCycle, ParentBomId, BomNameId );

	
	
	-- Get proj_id
	SELECT br.PROJ_ID INTO ProjId
	  FROM BRANCHES br
	 WHERE br.BRANCH_ID = nToBranchId;
	
	
	
	-- Check for duplicate BOMs
	OPEN curBom;
    FETCH curBom INTO recBom;

    IF curBom%FOUND
    THEN
		RAISE DUPLICATE_BOM;
	END IF;
	
	CLOSE curBom;
	
	
	
	-- Add Blank Bom --
	INSERT INTO BOMS ( BOM_ID, BRANCH_ID, BOM_NAME_ID, BOM_COMMENTS, BOM_VERSION, BOM_LIFECYCLE, IS_READONLY, IS_HIDDEN, PARENT_BOM_ID )
	VALUES ( BomId, nToBranchId, BomNameId, sBomComments, sBomVersion, sBomLifeCycle, 'N', 'N', ParentBomId  );
	
	
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( BomId, 'CREATED: Version '|| sBomVersion ||'.'|| sBomLifeCycle , nUserId );
	
	
	
	IF nFromBomId IS NOT NULL THEN
		/* Add Bom Clonned from ... */
		pk_Utils.Clone_BOM ( nFromBomId, BomId, nUserId );
		
	END IF;
	
	
	
	--- Set Bom State ---
	Release_To ( BomId, nUserId );
	
	
	EXCEPTION
    WHEN DUPLICATE_BOM
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'BOM "'|| sBomName ||' '|| BomVersion ||'.'|| BomLifeCycle ||'" is Already Used in this Project.');
			
END	Add_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Bom ( nBomId IN BOMS.BOM_ID%TYPE,
					   nRtagId IN BOMS.RTAG_ID_FK%TYPE,
					   sBomComment IN BOMS.BOM_COMMENTS%TYPE,
					   nUserId IN NUMBER )  IS
					   
ReleaseName VARCHAR2(100);					   

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	--- Update Bom Properties ---
	UPDATE BOMS bo SET
		bo.RTAG_ID_FK = nRtagId,
		bo.BOM_COMMENTS = sBomComment
	WHERE bo.BOM_ID = nBomId;
	
	SELECT RTAG_NAME INTO ReleaseName
	FROM RELEASE_TAGS 
	WHERE RTAG_ID = nRtagId;
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'Updated BOM properties: Set Release Manager Reference ='||ReleaseName||' and comments ='||sBomComment, nUserId );
	
	
END	Update_Bom;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION New_Bom_Name ( sBomName IN BOM_NAMES.BOM_NAME%TYPE ) RETURN BOM_NAMES.BOM_NAME_ID%TYPE IS

ReturnValue 	 BOM_NAMES.BOM_NAME_ID%TYPE;	

CURSOR curBomNames IS
	SELECT bn.BOM_NAME_ID
	  FROM BOM_NAMES bn
	 WHERE bn.BOM_NAME = sBomName;
recBomNames curBomNames%ROWTYPE;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Check for existance of this name
	OPEN curBomNames;
	FETCH curBomNames INTO recBomNames;
	
	ReturnValue := NULL;
	IF curBomNames%FOUND THEN
		ReturnValue := recBomNames.BOM_NAME_ID;
	END IF;
	
	CLOSE curBomNames;
	
	
	-- Finnaly Create Name if required --    
	IF ReturnValue IS NULL THEN
		-- Get bom_name_id
		SELECT SEQ_BOM_NAME_ID.NEXTVAL INTO ReturnValue FROM DUAL;
		
		INSERT INTO BOM_NAMES ( BOM_NAME_ID, BOM_NAME )
		VALUES ( ReturnValue, sBomName );
	
	END IF;	
	
	
	RETURN ReturnValue;
END	New_Bom_Name;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION New_Bom_Name ( nFromBomId IN BOMS.BOM_NAME_ID%TYPE ) RETURN BOMS.BOM_NAME_ID%TYPE IS

ReturnValue 	 BOMS.BOM_NAME_ID%TYPE;	

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get bom_name_id
	SELECT bo.BOM_NAME_ID INTO ReturnValue
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nFromBomId;
	
	
	RETURN ReturnValue;
END	New_Bom_Name;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject ( nBomId IN BOMS.BOM_ID%TYPE,
						  cIsRejected IN BOMS.IS_REJECTED%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE,
						  nStateId IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE  ) IS

StateName STATES.STATE_NAME%TYPE;						  
IsOfficial BOMS.IS_READONLY%TYPE;	
IsRejected VARCHAR2(50);					  

BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check if Bom is official
	SELECT bo.IS_READONLY INTO IsOfficial
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nBomId;
	 
	IF IsOfficial != 'Y' THEN
		RAISE_APPLICATION_ERROR (-20000, 'BOM must be locked first.' );
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get State Name
	SELECT st.STATE_NAME INTO StateName
	  FROM STATES st
	 WHERE st.STATE_ID = nStateId;
	
	
	-- AcceptReject BOM
	UPDATE BOMS bo SET
		bo.IS_REJECTED = cIsRejected
	WHERE bo.BOM_ID = nBomId;
	
	
	-- Auto-Hide Bom
	IF cIsRejected = 'Y' THEN
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_REJECT_BOM') );
	ELSE
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_ACCEPT_BOM') );
	END IF;
	
	
	-- Log Rejection Trail 
	pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM'), nBomId, cIsRejected, nUserId, sComments );
	
	
	/* Log BOM_TRAIL */
	-- Get Log Title
	IsRejected := 'ACCEPTED';
	IF cIsRejected = 'Y' THEN
		IsRejected := 'REJECTED';
	END IF;
	
	-- Finnaly log it
	Log_Bom_Trail ( nBomId, IsRejected ||': By '|| StateName, nUserId, sComments );
	
END	Accept_Reject;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Set_Bom_Official ( nBomId IN BOMS.BOM_ID%TYPE,
							 cIsOfficial IN BOMS.IS_READONLY%TYPE,
							 nStateId IN BOM_STATE.STATE_ID%TYPE,
							 nUserId IN NUMBER )  IS

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	--- Set Bom Official or Not ---
	UPDATE BOMS bo SET
	bo.IS_READONLY = cIsOfficial
	WHERE bo.BOM_ID = nBomId;
	
	
	-- Auto-Hide Bom
	IF cIsOfficial = 'Y' THEN
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_LOCK_BOM') );
		
		-- Update node version
		Update_Nodes_Version ( nBomId, nUserId );
		
	ELSE
		pk_State.Auto_Hide ( nStateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_UNLOCK_BOM') );
	END IF;
	
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'LOCKED: '|| cIsOfficial, nUserId );
	
	
END	Set_Bom_Official;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Nodes_Version ( nBomId IN BOMS.BOM_ID%TYPE,
							 	 nUserId IN NUMBER )  IS

	CompareBomId NUMBER;
	ProjId NUMBER;
	BomVersion VARCHAR2(4000);
									 	
	CURSOR curUpdateNode IS
		SELECT DISTINCT
			   nlook.NODE_ID,
			   DECODE ( nlc.LAST_LIFE_CYCLE,
			   			NULL, 0,
						nlc.LAST_LIFE_CYCLE + 1 ) AS NEW_LIFE_CYCLE	   
		  FROM (
		  
		  		/* ADDED PATCHES ONLY */
				SELECT 'A' AS CHANGE_TYPE, newbom.PROD_ID, newbom.PK 
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND oldbom.PK IS NULL
				UNION 
		  		/* ADDED PATCHES ONLY TO BASE CONFIGURATION */
				SELECT 'A' AS CHANGE_TYPE, newbom.PROD_ID, newbom.PK 
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND oldbom.PK IS NULL
				UNION				
				
				/* ADDED PRODUCTS ONLY */
				SELECT DECODE( oldbom.PROD_ID,
							   NULL, 'A', 'U' ) AS CHANGE_TYPE, 
					   newbom.PROD_ID, newbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   --AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND newbom.PKG_ID  = oldbom.PKG_ID (+)
				   AND newbom.V_EXT  = oldbom.V_EXT (+) 
				   AND ( oldbom.PROD_ID != newbom.PROD_ID  OR oldbom.PROD_ID IS NULL )	   
				UNION

				/* ADDED PRODUCTS ONLY TO BASE CONFIGURATION */
				SELECT DECODE( oldbom.PROD_ID,
							   NULL, 'A', 'U' ) AS CHANGE_TYPE, 
					   newbom.PROD_ID, newbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK = oldbom.PK (+) 
				   --AND newbom.PROD_ID = oldbom.PROD_ID (+)
				   AND newbom.PKG_ID  = oldbom.PKG_ID (+)
				   AND newbom.V_EXT  = oldbom.V_EXT (+) 
				   AND ( oldbom.PROD_ID != newbom.PROD_ID  OR oldbom.PROD_ID IS NULL )	   
				UNION
				
				/* REMOVED PRODUCTS ONLY*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   --AND newbom.PROD_ID (+) = oldbom.PROD_ID
				   AND newbom.PKG_ID (+) = oldbom.PKG_ID
				   AND newbom.V_EXT (+) = oldbom.V_EXT   
				   AND newbom.PK IS NULL  
				UNION   
				/* REMOVED PATCHES */    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT osc.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM OS_CONTENTS osc,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE osc.OS_ID = os.OS_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND osc.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   AND newbom.PROD_ID (+) = oldbom.PROD_ID  
				   AND newbom.PK IS NULL 
				   UNION
				/* REMOVED PRODUCTS ONLY FROM BASE CONFIGURATION*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK,
							   pv.PKG_ID,
							   NVL( pv.V_EXT, '|LINK_A_NULL|' ) AS V_EXT
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID 
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   --AND newbom.PROD_ID (+) = oldbom.PROD_ID
				   AND newbom.PKG_ID (+) = oldbom.PKG_ID
				   AND newbom.V_EXT (+) = oldbom.V_EXT   
				   AND newbom.PK IS NULL  
				UNION   
				/* REMOVED PATCHES FROM BASE CONFIGURATION*/    
				SELECT 'R' AS CHANGE_TYPE, oldbom.PROD_ID, oldbom.PK
				  FROM (
				  		SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = nBomId	
				  	   ) newbom,
					   (
					   	SELECT bec.PROD_ID,
							   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
						  FROM BASE_ENV_CONTENTS bec,
						  	   OS_BASE_ENV obe,
						  	   OPERATING_SYSTEMS os,
							   NETWORK_NODES nn,
							   BOM_CONTENTS bc,
							   PACKAGE_VERSIONS pv
						 WHERE obe.OS_ID = os.OS_ID
						   AND bec.BASE_ENV_ID = obe.BASE_ENV_ID
						   AND os.NODE_ID = nn.NODE_ID
						   AND bc.NODE_ID = nn.NODE_ID
						   AND bec.PROD_ID = pv.PV_ID
						   AND NOT pv.IS_PATCH IS NULL
						   AND bc.BOM_ID = CompareBomId	
					   ) oldbom
				 WHERE newbom.PK (+) = oldbom.PK  
				   AND newbom.PROD_ID (+) = oldbom.PROD_ID  
				   AND newbom.PK IS NULL				    	
				   
		       ) qry,
			   (
			   
		  		SELECT nn.NODE_ID,
					   nn.NODE_NAME,
					   nn.NODE_NAME ||'_'|| os.OS_NAME AS PK
				  FROM OPERATING_SYSTEMS os,
					   NETWORK_NODES nn,
					   BOM_CONTENTS bc
				 WHERE os.NODE_ID = nn.NODE_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND bc.BOM_ID = nBomId
				   
		  	   ) nlook,
			   (
			   	
				SELECT nn.NODE_NAME, 
					   MAX(nn.NODE_LIFE_CYCLE) AS LAST_LIFE_CYCLE
				  FROM BOMS bo,
				  	   BOM_CONTENTS bc,
					   BRANCHES br,
					   NETWORK_NODES nn
				 WHERE bc.BOM_ID = bo.BOM_ID
				   AND bo.BRANCH_ID = br.BRANCH_ID
				   AND bc.NODE_ID = nn.NODE_ID
				   AND br.PROJ_ID = ProjId
		  	     GROUP BY nn.NODE_NAME
			   
			   ) nlc
		 WHERE nlook.PK = qry.PK 
		   AND nlc.NODE_NAME = nlook.NODE_NAME;
	recUpdateNode curUpdateNode%ROWTYPE;
	
	
								 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get some BOM details
	SELECT bo.PARENT_BOM_ID, br.PROJ_ID, bo.BOM_VERSION ||'.'|| bo.BOM_LIFECYCLE INTO CompareBomId, ProjId, BomVersion
	  FROM BOMS bo,
	  	   BRANCHES br
	 WHERE bo.BOM_ID = nBomId
	   AND bo.BRANCH_ID = br.BRANCH_ID;


	-- Update node version
	OPEN curUpdateNode;
	FETCH curUpdateNode INTO recUpdateNode;
	
	WHILE curUpdateNode%FOUND
	LOOP
		
		UPDATE NETWORK_NODES nn SET
			nn.NODE_VERSION = BomVersion,
			nn.NODE_LIFE_CYCLE = recUpdateNode.NEW_LIFE_CYCLE
		 WHERE nn.NODE_ID = recUpdateNode.NODE_ID;
		
		FETCH curUpdateNode INTO recUpdateNode;
	END LOOP;
	
	
	CLOSE curUpdateNode;	
	
END	Update_Nodes_Version;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 ) IS
						 
rowCount NUMBER;
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
nBomIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId );
		
	END IF;
	
	-- Check Requirements
	pk_Requirements.Destroy_Bom ( nBomId );
	
	/*-------------------------------------------------------*/
	
	
	
	-- Load Collector
	nBomIdCollector := IN_LIST_NUMBER( nBomId );
	
	
	-- Destroy BASE_ENV
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT be.BASE_ENV_ID
	  	   		  		   	   FROM BASE_ENV be
	 						  WHERE be.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	) 
						    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
	
	Destroy_BaseEnv ( nIdCollector );
	
	
	
	-- Destroy NETWORK_NODES
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT bc.NODE_ID
	  	   		  		   	   FROM BOM_CONTENTS bc
	 						  WHERE bc.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	) 
						    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
	pk_Network_Node.Destroy_Node ( nIdCollector, 0 );
	 
	 
	
	-- Destroy HARDWARE
	nIdCollector := NULL;
	SELECT CAST ( MULTISET ( SELECT hr.HARDWARE_NAME_ID
	  	   		  		   	   FROM HARDWARE hr
	 						  WHERE hr.BOM_ID IN   ( 
								  	  		  	      SELECT * 
													  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
												 	)
							) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
	Destroy_Hardware ( nIdCollector );
	
	
	
	-- Destroy BOM_STATE
	DELETE 
	  FROM BOM_STATE bs
	 WHERE bs.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	);
	 
	
	-- Destroy BOM_TRAIL
	DELETE 
	  FROM BOM_TRAIL bt
	 WHERE bt.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	); 
	
	 
	---- Finnaly Destroy BOM ----
	DELETE 
	  FROM BOMS bo
	 WHERE bo.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	);
	 
	  
	   
END	Destroy_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_BaseEnv ( nBaseEndIdCollector IN DEPMGR_NUMBER_TAB_t ) IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Destroy BASE_ENV_CONTENTS
	DELETE
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	
	-- Destroy OS_BASE_ENV
	DELETE
	  FROM OS_BASE_ENV obe
	 WHERE obe.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	 
	 -- Destroy BASE_ENV
	DELETE
	  FROM BASE_ENV be
	 WHERE be.BASE_ENV_ID IN ( SELECT * FROM TABLE ( CAST ( nBaseEndIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	   
	 
END	Destroy_BaseEnv;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Hardware ( nHardwareNameIdCollector IN DEPMGR_NUMBER_TAB_t ) IS
						 
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
    -- Destroy NETWORK_NODES
    nIdCollector := NULL;
    SELECT CAST ( MULTISET ( SELECT hn.NODE_ID
	  	   		  		   	   FROM HARDWARE_NODE hn
	 						  WHERE hn.HARDWARE_NAME_ID IN ( SELECT * FROM TABLE ( CAST ( nHardwareNameIdCollector AS DEPMGR_NUMBER_TAB_t ) ) ) 
						   ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
							  
    pk_Network_Node.Destroy_Node ( nIdCollector, 0 );
	
	
	-- Finnaly Destroy HARDWARE
	DELETE
	  FROM HARDWARE hd
	 WHERE hd.HARDWARE_NAME_ID IN ( SELECT * FROM TABLE ( CAST ( nHardwareNameIdCollector AS DEPMGR_NUMBER_TAB_t ) ) );
	 
END	Destroy_Hardware;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Get_Next_Version (nFromBomId IN BOMS.BOM_ID%TYPE,
							nToBranchId IN BOMS.BRANCH_ID%TYPE,
							sNextVersion OUT BOMS.BOM_VERSION%TYPE,
							sNextLifeCycle OUT BOMS.BOM_LIFECYCLE%TYPE,
							nParentBomId IN OUT BOMS.PARENT_BOM_ID%TYPE,
							nBomNameId IN BOMS.BOM_NAME_ID%TYPE ) IS
							
IsNotHead NUMBER;
FromBranch NUMBER;
LastRootVersion NUMBER;
LastVersion BOMS.BOM_VERSION%TYPE;
							
CURSOR curBoms IS
	SELECT Max ( CAST ( bo.BOM_VERSION AS NUMBER  ) ) AS LAST_VERSION
		  FROM BOMS bo,
		  	   BRANCHES br
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID IN ( SELECT br.PROJ_ID  FROM BRANCHES br  WHERE br.BRANCH_ID = nToBranchId )	
		   AND bo.BOM_NAME_ID = nBomNameId;
recBoms curBoms%ROWTYPE;
	
				  							 
BEGIN

	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	-- Get Last Root Version
	
	OPEN curBoms;
	FETCH curBoms INTO recBoms;	
	LastRootVersion := recBoms.LAST_VERSION;
	CLOSE curBoms;
	
	
	
	
	
	IF nFromBomId IS NULL THEN
		/* Blank BOM Required */
	
		-- Get Next Available Version
		sNextVersion := '1';
		sNextLifeCycle := '0';
		IF LastRootVersion IS NOT NULL THEN
			sNextVersion := CAST( LastRootVersion + 1 AS VARCHAR2 );
		END IF;
	
	
		
	ELSE
		/* Create Bom From ... */
		
		-- Check if BOM is comming from different Iteration
		SELECT bo.BRANCH_ID INTO FromBranch
		  FROM BOMS bo
		 WHERE bo.BOM_ID = nFromBomId; 
		
		
		
		IF FromBranch = nToBranchId THEN
			/* Bom is comming from same Iteration */
			/* i.e. Continue on same version stream. */
			
			-- Set parent_id
			nParentBomId := nFromBomId;
			
			
			-- Get Last Version 
			SELECT bo.BOM_VERSION INTO LastVersion
			  FROM BOMS bo
			 WHERE bo.BOM_ID = nFromBomId;
			
			
			-- Check if Bom is Head (i.e. Last on its branch )
			SELECT COUNT(*) INTO IsNotHead
			  FROM BOMS bo,
			  	   BRANCHES br
			 WHERE bo.BRANCH_ID = br.BRANCH_ID
			   AND br.PROJ_ID IN ( SELECT br.PROJ_ID FROM BRANCHES br WHERE br.BRANCH_ID = FromBranch )
			   AND bo.PARENT_BOM_ID = nFromBomId
			   AND bo.BOM_VERSION = LastVersion
			   AND bo.BOM_ID != bo.PARENT_BOM_ID;
			   
			   
			IF IsNotHead = 0 THEN
				/* Bom IS Head (i.e. Last on its branch ) */
				
				-- Get Bom Version and LifeCycle
				SELECT bo.BOM_VERSION, bo.BOM_LIFECYCLE INTO sNextVersion, sNextLifeCycle
				  FROM BOMS bo
				 WHERE bo.BOM_ID = nFromBomId;
				
				-- Increase Lifecycle
				sNextLifeCycle := CAST (  ( CAST( sNextLifeCycle AS NUMBER ) + 1 )  AS VARCHAR2 );
				
				
			ELSE
				/* Bom IS NOT Head */
				
				-- Get Bom Version and LifeCycle
				SELECT bo.BOM_VERSION, bo.BOM_LIFECYCLE INTO sNextVersion, sNextLifeCycle
				  FROM BOMS bo
				 WHERE bo.BOM_ID = nFromBomId;
				
				-- Branch Bom Version 
				sNextVersion := sNextVersion ||'.'|| sNextLifeCycle;
				
				-- Reset Lifecycle to 1
				sNextLifeCycle := '1';
				
			END IF;	
			
			
		ELSE
			/* BOM is comming from Different Iteration */
			/* i.e. Start new version stream. */
			
			-- Get Next Available Version
			sNextVersion := '1';
			sNextLifeCycle := '0';
			IF LastRootVersion IS NOT NULL THEN
				sNextVersion := CAST( LastRootVersion + 1 AS VARCHAR2 );
			END IF;
		
		END IF;
		
	
	END IF;
	
		
END	Get_Next_Version;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Release_To ( nBomId IN BOM_STATE.BOM_ID%TYPE,
					   nUserId IN BOM_STATE.USER_ID%TYPE,
					   nToState IN BOM_STATE.STATE_ID%TYPE DEFAULT NULL,
					   sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL ) IS

rowCount NUMBER;	
IsOfficial BOMS.IS_READONLY%TYPE;					 
StateId BOM_STATE.STATE_ID%TYPE;
StateName STATES.STATE_NAME%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check if already released
	SELECT Count(*) INTO rowCount
	  FROM BOM_STATE bs
	 WHERE bs.BOM_ID = nBomId
	   AND bs.STATE_ID = nToState;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'This BOM is already release' );
	END IF;
	
	
	-- Check if Bom is official
	SELECT bo.IS_READONLY INTO IsOfficial
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nBomId;
	 
	IF (IsOfficial != 'Y') AND (nToState IS NOT NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'BOM must be locked first' );
	END IF;
	/*-------------------------------------------------------*/
	
	
	-- Get State --
	StateId := nToState;
	IF StateId IS NULL THEN
		-- If state_id is null it is considered as state_type_enum = 'BOM Creator'
		SELECT st.STATE_ID INTO StateId
		  FROM BOMS bo,
		  	   BRANCHES br,
			   DM_PROJECTS proj,
			   STATES st
		 WHERE bo.BRANCH_ID = br.BRANCH_ID
		   AND br.PROJ_ID = proj.PROJ_ID
		   AND st.PROJ_ID = proj.PROJ_ID
		   AND bo.BOM_ID = nBomId
		   AND st.STATE_TYPE_ENUM = GET_SYSTEM_VALUE('enumSTATE_TYPE_CREATOR');	
	END IF;
	
	
	-- Add Bom to State --
	INSERT INTO BOM_STATE (BOM_ID, STATE_ID, DATE_STAMP, IS_DISPLAYED, USER_ID )
	VALUES( nBomId,
			StateId,
			GET_DATE(),
			'Y',
			nUserId );
	
	
	
	-- Auto-Hide Bom
	pk_State.Auto_Hide ( StateId, nBomId, GET_SYSTEM_VALUE('enumEVENT_ON_RELEASE_TO') );

	
	/* Log BOM_TRAIL */
	-- Get State Name --
	SELECT st.STATE_NAME INTO StateName
	  FROM STATES st
	 WHERE st.STATE_ID = StateId;
	
	-- Log Action 
	pk_Bom.Log_Bom_Trail ( nBomId, 'RELEASED TO: '|| StateName, nUserId, sComments );
	
	 
END	Release_To;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject_Comments ( nBomId IN BOMS.BOM_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE ) IS

IsRejected REJECTION_TRAIL.IS_REJECTED%TYPE;
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId  );
	END IF;
	/*-------------------------------------------------------*/
	
	IF nRejectSeq IS NULL THEN
		/* CREATE New Note */
		
		-- Get last IsRejected state
		SELECT rt.IS_REJECTED INTO IsRejected
		  FROM REJECTION_TRAIL rt
		 WHERE rt.ENTITY_ID = nBomId
		   AND rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM')
		   AND ROWNUM = 1
		 ORDER BY rt.REJECT_SEQ;
				 
				 
		/* Log Rejection Trail */
		pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM'), nBomId, IsRejected, nUserId, sComments );
		
		
	ELSE
		/* EDIT Note */
		UPDATE REJECTION_TRAIL rt SET
			rt.COMMENTS = sComments,
			rt.USER_ID = nUserId,
			rt.DATE_TIME_STAMP = GET_DATETIME()
		WHERE rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_BOM')
		  AND rt.ENTITY_ID = nBomId
		  AND rt.REJECT_SEQ = nRejectSeq;
		  
			
	END IF;
	
	
END	Accept_Reject_Comments;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Bom_Trail ( nBomId IN BOM_TRAIL.BOM_ID%TYPE,
						  sAction IN BOM_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN BOM_TRAIL.USER_ID%TYPE,
						  sComments IN BOM_TRAIL.TRAIL_COMMENTS%TYPE DEFAULT NULL  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nBomId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId= '|| nBomId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO BOM_TRAIL ( BOM_ID, TRAIL_ACTION, TRAIL_COMMENTS, USER_ID, DATE_TIME_STAMP )
	VALUES ( nBomId, sAction, sComments, nUserId, GET_DATETIME() );	
		
END	Log_Bom_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Node_Trail ( nNodeId IN NODE_TRAIL.NODE_ID%TYPE,
						  sAction IN NODE_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN NODE_TRAIL.USER_ID%TYPE  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nNodeId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nNodeId= '|| nNodeId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO NODE_TRAIL ( NODE_ID, TRAIL_ACTION, USER_ID, DATE_TIME_STAMP )
	VALUES ( nNodeId, sAction, nUserId, GET_DATETIME() );	
		
END	Log_Node_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Os_Trail ( nOsId IN OS_TRAIL.OS_ID%TYPE,
						  sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN OS_TRAIL.USER_ID%TYPE  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nOsId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId= '|| nOsId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO OS_TRAIL ( OS_ID, TRAIL_ACTION, USER_ID, DATE_TIME_STAMP )
	VALUES ( nOsId, sAction, nUserId, GET_DATETIME() );	
		
END	Log_Os_Trail;
/*--------------------------------------------------------------------------------------------------*/

END pk_Bom_Test;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_NETWORK_NODE
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_NETWORK_NODE" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Node ( sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
					 nBomId IN BOM_CONTENTS.BOM_ID%TYPE,
					 nUserId IN NUMBER,
					 sNotXML IN NETWORK_NODES.NOT_XML%TYPE ) IS
						 
NodeID NUMBER;

CURSOR nn_cur IS -- Check Node Name Duplicates
		SELECT bc.node_id
		  FROM BOM_CONTENTS bc,
		  	   NETWORK_NODES nn
		 WHERE bc.NODE_ID = nn.NODE_ID
		   AND bc.BOM_ID = nBomId 
		   AND UPPER( nn.NODE_NAME ) = UPPER( sNodeName );
nn_rec nn_cur%ROWTYPE;

DUPLICATE_NODE_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	OPEN nn_cur;
    FETCH nn_cur INTO nn_rec;

    IF nn_cur%FOUND
    THEN
		RAISE DUPLICATE_NODE_NAME;
	END IF;
	
	CLOSE nn_cur;
	/*-------------------------------------------------------*/
	
	
	-- Get node_id
	SELECT SEQ_NODE_ID.NEXTVAL INTO NodeID FROM DUAL;
	
	
	-- Insert new Network Node			  
	INSERT INTO NETWORK_NODES ( NODE_ID, NODE_TYPE_ID, NODE_NAME, NODE_COMMENTS, NOT_XML )
	VALUES (
			NodeID,
			nNodeTypeId,
			sNodeName,
			sNodeComments,
			sNotXML
			);
	
	
	-- Link Node with the BOM
	INSERT INTO BOM_CONTENTS ( BOM_ID, NODE_ID )
	VALUES (
		    nBomId,
			NodeID
		    );

	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'Added Network Node: '||sNodeName, nUserId );				
						
EXCEPTION
    WHEN DUPLICATE_NODE_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Node Name "'|| sNodeName ||'" is Already Used in this BOM.');
	
END	Add_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Node ( nNodeId IN NETWORK_NODES.NODE_ID%TYPE,
						sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     	nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 	sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
						nUserId IN NUMBER,
						nLifeCycle IN NUMBER,
						sNotXML IN NETWORK_NODES.NOT_XML%TYPE ) IS
						 
BomID NUMBER;

CURSOR nn_cur IS -- Check Node Name Duplicates
		SELECT bc.node_id
		  FROM BOM_CONTENTS bc,
		  	   NETWORK_NODES nn
		 WHERE bc.NODE_ID = nn.NODE_ID
		   AND bc.BOM_ID = BomId 
		   AND UPPER( nn.NODE_NAME ) = UPPER( sNodeName )
		   AND nn.NODE_ID != nNodeId;
nn_rec nn_cur%ROWTYPE;

DUPLICATE_NODE_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check all parameters
	IF (nNodeId IS NULL)  OR  (nNodeTypeId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nNodeId='|| nNodeId  ||', nNodeTypeId='|| nNodeTypeId  );
		
	END IF;
	
	-- Get bom_id
	SELECT bc.BOM_ID INTO BomId
	  FROM BOM_CONTENTS bc
	 WHERE bc.NODE_ID = nNodeId; 
	
	-- Check for duplicate names
	OPEN nn_cur;
    FETCH nn_cur INTO nn_rec;

    IF nn_cur%FOUND
    THEN
		RAISE DUPLICATE_NODE_NAME;
	END IF;
	
	CLOSE nn_cur;
	/*-------------------------------------------------------*/
	
	-- Update Network Node Details
	UPDATE NETWORK_NODES nn SET
		nn.NODE_NAME = sNodeName,
		nn.NODE_COMMENTS = sNodeComments,
		nn.NODE_TYPE_ID = nNodeTypeId,
		nn.NODE_LIFE_CYCLE = nLifeCycle,
		nn.NOT_XML = sNotXML
	 WHERE nn.NODE_ID = nNodeId;

 	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( BomID, 'Updated Network Node: '||sNodeName, nUserId );

			
EXCEPTION
    WHEN DUPLICATE_NODE_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Node Name "'|| sNodeName ||'" is Already Used in this BOM.');
	
END	Update_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Node ( sNodeIdList IN VARCHAR2,
					  	nBomId IN BOM_CONTENTS.BOM_ID%TYPE ) IS
						 


NODE_ID_REQUIRED EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
    IF sNodeIdList IS NULL
    THEN
		RAISE NODE_ID_REQUIRED;
	END IF;
	/*-------------------------------------------------------*/
	
	
	
	-- Remove from Bom Contents
	DELETE FROM BOM_CONTENTS
	WHERE bom_id = nBomId
	  AND node_id IN (
		  		  	  SELECT *
   	   		   	      FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )
		  		  	 );
					 
	
	-- Remove Network Nodes
	DELETE FROM NETWORK_NODES
	WHERE node_id IN (
		  		  	  SELECT *
   	   		   	      FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )
		  		  	 );
	

			
EXCEPTION
    WHEN NODE_ID_REQUIRED
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'You must select at least one Network Node.');
	
END	Remove_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Node ( nNodeIdCollection IN DEPMGR_NUMBER_TAB_t, nUserId IN NUMBER ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
NodeName VARCHAR2(4000);
BomId NUMBER;
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	IF (nNodeIdCollection.COUNT > 0) THEN  
	
	   			--Log Action Trail
		FOR i IN 1..nNodeIdCollection.COUNT
		LOOP
			-- Get bom_id
			SELECT bc.BOM_ID INTO BomId
	  		FROM BOM_CONTENTS bc
			WHERE bc.NODE_ID = nNodeIdCollection(i);	
			
			--Get node_name
			SELECT nn.NODE_NAME INTO NodeName
	  		FROM NETWORK_NODES nn
			WHERE nn.NODE_ID = nNodeIdCollection(i);	
			
			/* Log BOM_TRAIL */
			pk_Bom.Log_Bom_Trail ( BomId, 'Deleted Network Node: '||NodeName, nUserId );

		END LOOP;				  
	
		-- Destroy Hardware Node Link
		DELETE 
		  FROM HARDWARE_NODE hn
		 WHERE hn.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy Node from Bom Contents
		DELETE 
		  FROM BOM_CONTENTS bc
		 WHERE bc.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy Operation Systems
		nIdCollector := NULL;
		SELECT CAST ( MULTISET ( SELECT os.OS_ID
		  	   		  		   	   FROM OPERATING_SYSTEMS os
		 						  WHERE os.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) )
							    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
								  
		pk_Operating_System.Destroy_Os ( nIdCollector, nUserId );
		
		---- Finnaly destroy Network Node ----
		DELETE
		  FROM NETWORK_NODES nn
		 WHERE nn.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );		 
		 
	END IF; 
		
	
END	Destroy_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Node ( nNodeIdList IN VARCHAR2, nUserId IN NUMBER ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	nIdCollector := IN_LIST_NUMBER ( nNodeIdList );
	
	Destroy_Node ( nIdCollector, nUserId);	
		
	
END	Destroy_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Sort_Nodes ( sNodeIdOrder IN VARCHAR2,
		  			   nBomId IN BOMS.BOM_ID%TYPE,
					   nUserId IN NUMBER ) IS
		
	nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();						 
	SeqNum NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (sNodeIdOrder IS NULL)  OR (nBomId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId='|| nBomId  ||' sNodeIdOrder='|| sNodeIdOrder  );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( sNodeIdOrder );
	
	-- Set start sequence number
	SeqNum := 1;
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
	
		-- Update new sequence number
		UPDATE BOM_CONTENTS bc SET
			bc.SEQ_NUM = SeqNum
		 WHERE bc.BOM_ID = nBomId
		   AND bc.NODE_ID = nIdCollector(i);
		   
		SeqNum := SeqNum + 1;
			   
	END LOOP;
	
	
END	Sort_Nodes;
/*--------------------------------------------------------------------------------------------------*/
END pk_Network_Node;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_NETWORK_NODE_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_NETWORK_NODE_TEST" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Node ( sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE,
					 nBomId IN BOM_CONTENTS.BOM_ID%TYPE,
					 nUserId IN NUMBER ) IS
						 
NodeID NUMBER;

CURSOR nn_cur IS -- Check Node Name Duplicates
		SELECT bc.node_id
		  FROM BOM_CONTENTS bc,
		  	   NETWORK_NODES nn
		 WHERE bc.NODE_ID = nn.NODE_ID
		   AND bc.BOM_ID = nBomId 
		   AND UPPER( nn.NODE_NAME ) = UPPER( sNodeName );
nn_rec nn_cur%ROWTYPE;

DUPLICATE_NODE_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	OPEN nn_cur;
    FETCH nn_cur INTO nn_rec;

    IF nn_cur%FOUND
    THEN
		RAISE DUPLICATE_NODE_NAME;
	END IF;
	
	CLOSE nn_cur;
	/*-------------------------------------------------------*/
	
	
	-- Get node_id
	SELECT SEQ_NODE_ID.NEXTVAL INTO NodeID FROM DUAL;
	
	
	-- Insert new Network Node			  
	INSERT INTO NETWORK_NODES ( NODE_ID, NODE_TYPE_ID, NODE_NAME, NODE_COMMENTS )
	VALUES (
			NodeID,
			nNodeTypeId,
			sNodeName,
			sNodeComments
			);
	
	
	-- Link Node with the BOM
	INSERT INTO BOM_CONTENTS ( BOM_ID, NODE_ID )
	VALUES (
		    nBomId,
			NodeID
		    );
			
	/* Log BOM_TRAIL */
	pk_Bom.Log_Bom_Trail ( nBomId, 'ADDED NETWORK NODE: '||sNodeName, nUserId );			
						
EXCEPTION
    WHEN DUPLICATE_NODE_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Node Name "'|| sNodeName ||'" is Already Used in this BOM.');
	
END	Add_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Node ( nNodeId IN NETWORK_NODES.NODE_ID%TYPE,
						sNodeName IN NETWORK_NODES.NODE_NAME%TYPE,
  		   	   	     	nNodeTypeId IN NETWORK_NODES.NODE_TYPE_ID%TYPE,
					 	sNodeComments IN NETWORK_NODES.NODE_COMMENTS%TYPE ) IS
						 
BomID NUMBER;

CURSOR nn_cur IS -- Check Node Name Duplicates
		SELECT bc.node_id
		  FROM BOM_CONTENTS bc,
		  	   NETWORK_NODES nn
		 WHERE bc.NODE_ID = nn.NODE_ID
		   AND bc.BOM_ID = BomId 
		   AND UPPER( nn.NODE_NAME ) = UPPER( sNodeName )
		   AND nn.NODE_ID != nNodeId;
nn_rec nn_cur%ROWTYPE;

DUPLICATE_NODE_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check all parameters
	IF (nNodeId IS NULL)  OR  (nNodeTypeId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nNodeId='|| nNodeId  ||', nNodeTypeId='|| nNodeTypeId  );
		
	END IF;
	
	-- Get bom_id
	SELECT bc.BOM_ID INTO BomId
	  FROM BOM_CONTENTS bc
	 WHERE bc.NODE_ID = nNodeId; 
	
	-- Check for duplicate names
	OPEN nn_cur;
    FETCH nn_cur INTO nn_rec;

    IF nn_cur%FOUND
    THEN
		RAISE DUPLICATE_NODE_NAME;
	END IF;
	
	CLOSE nn_cur;
	/*-------------------------------------------------------*/
	
	-- Update Network Node Details
	UPDATE NETWORK_NODES nn SET
		nn.NODE_NAME = sNodeName,
		nn.NODE_COMMENTS = sNodeComments,
		nn.NODE_TYPE_ID = nNodeTypeId
	 WHERE nn.NODE_ID = nNodeId;


			
EXCEPTION
    WHEN DUPLICATE_NODE_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Node Name "'|| sNodeName ||'" is Already Used in this BOM.');
	
END	Update_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Node ( sNodeIdList IN VARCHAR2,
					  	nBomId IN BOM_CONTENTS.BOM_ID%TYPE ) IS
						 


NODE_ID_REQUIRED EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
    IF sNodeIdList IS NULL
    THEN
		RAISE NODE_ID_REQUIRED;
	END IF;
	/*-------------------------------------------------------*/
	
	
	
	-- Remove from Bom Contents
	DELETE FROM BOM_CONTENTS
	WHERE bom_id = nBomId
	  AND node_id IN (
		  		  	  SELECT *
   	   		   	      FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )
		  		  	 );
					 
	
	-- Remove Network Nodes
	DELETE FROM NETWORK_NODES
	WHERE node_id IN (
		  		  	  SELECT *
   	   		   	      FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )
		  		  	 );
	

			
EXCEPTION
    WHEN NODE_ID_REQUIRED
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'You must select at least one Network Node.');
	
END	Remove_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Node ( nNodeIdCollection IN DEPMGR_NUMBER_TAB_t ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	IF (nNodeIdCollection.COUNT > 0) THEN   
	
		-- Destroy Hardware Node Link
		DELETE 
		  FROM HARDWARE_NODE hn
		 WHERE hn.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy Node from Bom Contents
		DELETE 
		  FROM BOM_CONTENTS bc
		 WHERE bc.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy Operation Systems
		nIdCollector := NULL;
		SELECT CAST ( MULTISET ( SELECT os.OS_ID
		  	   		  		   	   FROM OPERATING_SYSTEMS os
		 						  WHERE os.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) )
							    ) AS DEPMGR_NUMBER_TAB_t ) INTO nIdCollector FROM DUAL;
								  
		pk_Operating_System.Destroy_Os ( nIdCollector );
		
		
		
		
		---- Finnaly destroy Network Node ----
		DELETE
		  FROM NETWORK_NODES nn
		 WHERE nn.NODE_ID IN ( SELECT * FROM TABLE ( CAST ( nNodeIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		 
		 
	END IF; 
		
	
END	Destroy_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Node ( nNodeIdList IN VARCHAR2 ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	nIdCollector := IN_LIST_NUMBER ( nNodeIdList );
	
	Destroy_Node ( nIdCollector );	
		
	
END	Destroy_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Sort_Nodes ( sNodeIdOrder IN VARCHAR2,
		  			   nBomId IN BOMS.BOM_ID%TYPE,
					   nUserId IN NUMBER ) IS
		
	nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();						 
	SeqNum NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (sNodeIdOrder IS NULL)  OR (nBomId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nBomId='|| nBomId  ||' sNodeIdOrder='|| sNodeIdOrder  );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( sNodeIdOrder );
	
	-- Set start sequence number
	SeqNum := 1;
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
	
		-- Update new sequence number
		UPDATE BOM_CONTENTS bc SET
			bc.SEQ_NUM = SeqNum
		 WHERE bc.BOM_ID = nBomId
		   AND bc.NODE_ID = nIdCollector(i);
		   
		SeqNum := SeqNum + 1;
			   
	END LOOP;
	
	
END	Sort_Nodes;
/*--------------------------------------------------------------------------------------------------*/
END pk_Network_Node_test;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_OPERATING_SYSTEM
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_OPERATING_SYSTEM" 
IS
/* ---------------------------------------------------------------------------
    Modified: Rupesh Solanki
	Date: 20/03/2006
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Os ( sOsName IN OPERATING_SYSTEMS.OS_NAME%TYPE,
		  		   sOsComments IN OPERATING_SYSTEMS.OS_COMMENTS%TYPE,
				   sNodeSpecFile IN OPERATING_SYSTEMS.NODE_SPEC_FILE%TYPE,
 				   sNotXML IN OPERATING_SYSTEMS.NOT_XML%TYPE,
				   nNodeId IN OPERATING_SYSTEMS.NODE_ID%TYPE,
				   nBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
				   nBosId IN BASE_ENV.BOS_ID%TYPE DEFAULT NULL,
				   nUserId IN NUMBER ) IS
						 
OsID NUMBER;
BosId NUMBER;
BaseEnvId NUMBER;
BomId NUMBER;

CURSOR curOs IS -- Check Node Name Duplicates
		SELECT os.OS_ID
		  FROM OPERATING_SYSTEMS os
		 WHERE os.NODE_ID = nNodeId
		   AND UPPER(os.OS_NAME) = UPPER(sOsName);
recOs curOs%ROWTYPE;

DUPLICATE_OS_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for duplicate names
	OPEN curOs;
    FETCH curOs INTO recOs;

    IF curOs%FOUND
    THEN
		RAISE DUPLICATE_OS_NAME;
	END IF;
	
	CLOSE curOs;
	/*-------------------------------------------------------*/
	
	-- Get os_id
	SELECT SEQ_OS_ID.NEXTVAL INTO OsID FROM DUAL;
	
	-- Insert new OS
	INSERT INTO OPERATING_SYSTEMS ( OS_ID, OS_NAME, NODE_ID, OS_COMMENTS, NODE_SPEC_FILE, NOT_XML ) 
	VALUES ( OsID, sOsName, nNodeId, sOsComments, sNodeSpecFile, sNotXML  );
	
	
	
	/* Set up Base Configuration */
	IF nBaseEnvName IS NOT NULL THEN
		
		-- Get bom_id
		SELECT bc.BOM_ID INTO BomId
		  FROM BOM_CONTENTS bc
		 WHERE bc.NODE_ID = nNodeId;
		
		
		-- Set bos _id to default
		BosId := 5;		-- This is Generic OS in BOS_TYPES table
		IF nBosId IS NOT NULL THEN
			BosId := nBosId;
		END IF;
		
		
		-- Get base_env_id
		BaseEnvId := pk_Base_Env.Get_Base_Env_Id ( nBaseEnvName, BomId );
		
		
		-- Create Base Env if Required
		IF BaseEnvId IS NULL THEN
		
			pk_Base_Env.Add_Base_Env ( nBaseEnvName, BosId, BomId, OsID, nUserId );
			
			BaseEnvId := pk_Base_Env.Get_Base_Env_Id ( nBaseEnvName, BomId );
			
		END IF;
		
		-- Finally assign this  base env to OS
		INSERT INTO OS_BASE_ENV ( OS_ID, BASE_ENV_ID )
		VALUES ( OsID, BaseEnvId );
		
	END IF;
	
	
	/* Log BOM_TRAIL */
	pk_Bom.Log_Node_Trail ( nNodeId, 'Added Operating System: '||sOsName, nUserId );	

	
	EXCEPTION
    WHEN DUPLICATE_OS_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'OS Name "'|| sOsName ||'" is Already Used in this Network Node.');
		
END	Add_Os;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Os ( nOsId IN OPERATING_SYSTEMS.OS_ID%TYPE,
					  sOsName IN OPERATING_SYSTEMS.OS_NAME%TYPE,
		  		   	  sOsComments IN OPERATING_SYSTEMS.OS_COMMENTS%TYPE,
				  	  nBaseEnvName IN BASE_ENV.BASE_ENV_NAME%TYPE,
					  sNodeSpecFile IN OPERATING_SYSTEMS.NODE_SPEC_FILE%TYPE,
					  sNotXML IN OPERATING_SYSTEMS.NOT_XML%TYPE,
					  nUserId IN NUMBER  ) IS

BomId NUMBER;
BosId NUMBER;
BaseEnvId NUMBER;
NodeId NUMBER;

CURSOR curOs IS
		SELECT os.OS_ID
		  FROM OPERATING_SYSTEMS os
		 WHERE os.NODE_ID = NodeId
		   AND UPPER(os.OS_NAME) = UPPER(sOsName)
		   AND os.OS_ID != nOsId;
recOs curOs%ROWTYPE;

DUPLICATE_OS_NAME EXCEPTION;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (nOsId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId='|| nOsId   );
		
	END IF;
	
	
	-- Get node_id
	SELECT os.NODE_ID INTO NodeId
	  FROM OPERATING_SYSTEMS os
	 WHERE os.OS_ID = nOsId;
		 
	
	-- Check for duplicate names
	OPEN curOs;
    FETCH curOs INTO recOs;

    IF curOs%FOUND
    THEN
		RAISE DUPLICATE_OS_NAME;
	END IF;
	
	CLOSE curOs;
	/*-------------------------------------------------------*/
	
	-- Delete current Base Env
	DELETE
	  FROM OS_BASE_ENV obe
	 WHERE obe.OS_ID = nOsId;
	 
	
	/* Set up Base Configuration */
	IF nBaseEnvName IS NOT NULL THEN
		
		-- Get bom_id
		SELECT bc.BOM_ID INTO BomId
		  FROM BOM_CONTENTS bc
		 WHERE bc.NODE_ID = NodeId;
		
		
		-- Get base_env_id
		BaseEnvId := pk_Base_Env.Get_Base_Env_Id ( nBaseEnvName, BomId );
		
		
		-- Set bos _id to default
		BosId := 5;		-- This is Generic OS in BOS_TYPES table
		
		
		-- Create Base Env if Required
		IF BaseEnvId IS NULL THEN
		
			pk_Base_Env.Add_Base_Env ( nBaseEnvName, BosId, BomId, nOsId, nUserId );
			
			BaseEnvId := pk_Base_Env.Get_Base_Env_Id ( nBaseEnvName, BomId );
			
		ELSE
			-- Get bos_id
			SELECT be.BOS_ID INTO BosId
			  FROM BASE_ENV be
			 WHERE be.BASE_ENV_ID = BaseEnvId;
			
		END IF;
		
		
		-- Finally assign this  base env to OS
		INSERT INTO OS_BASE_ENV ( OS_ID, BASE_ENV_ID )
		VALUES ( nOsId, BaseEnvId );
		
	END IF;
	
	
	
	-- Update Os Details
	UPDATE OPERATING_SYSTEMS os SET
		os.OS_NAME = sOsName,
		os.OS_COMMENTS = sOsComments,
		os.NODE_SPEC_FILE = sNodeSpecFile,
		os.NOT_XML = sNotXML
	 WHERE os.OS_ID = nOsId;

	/* Log BOM_TRAIL */
	pk_Bom.Log_Node_Trail ( NodeId, 'Updated Operating System: '||sOsName, nUserId );		 
	
	
	EXCEPTION
    WHEN DUPLICATE_OS_NAME
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'OS Name "'|| sOsName ||'" is Already Used in this Network Node.');
	
END	Update_Os;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product_Wrap ( nPkgId IN NUMBER,
		  				   	 sProdVersion IN VARCHAR2,
							 nOsId IN OS_CONTENTS.OS_ID%TYPE,
							 nUserId IN NUMBER,
							 sComments IN VARCHAR2 DEFAULT NULL ) IS
						 
ProdId PACKAGE_VERSIONS.PV_ID%TYPE;
ProdName PACKAGES.PKG_NAME%TYPE;

CURSOR prod_cur IS
	SELECT osc.PROD_ID
	  FROM OS_CONTENTS osc
	 WHERE osc.PROD_ID = ProdId
	   AND osc.OS_ID = nOsId;
prod_rec prod_cur%ROWTYPE;
				  						 
BEGIN
	/*--------------- Business Rules Here -------------------*/                             
	IF (NOT nPkgId IS NULL) AND (sProdVersion IS NULL) AND (NOT nOsId IS NULL) AND (NOT nUserId IS NULL) THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Please provide Product Version.' );

	ELSIF (nPkgId IS NULL) OR (sProdVersion IS NULL) OR (nOsId IS NULL) OR (nUserId IS NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nPkgId= '|| nPkgId ||', sProdVersion='|| sProdVersion ||', nOsId='|| nOsId ||', nUserId='|| nUserId );
		
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get Product Name --
	SELECT PKG_NAME INTO ProdName FROM PACKAGES WHERE PKG_ID = nPkgId;
	
	
	-- Create Product Version in Release Manager if Required, and Get Prod_id --
	Seed_Package_Names_Versions ( ProdName, sProdVersion, nUserId, ProdId );
	
	
	-- Add Product To OS --
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	IF prod_cur%NOTFOUND THEN
	   pk_Product.Add_Product ( ProdId, nOsId, nUserId );
	   
	   -- Add comments if required
	   IF (NOT sComments IS NULL) THEN
	   		UPDATE OS_CONTENTS osc SET
				osc.PRODUCT_COMMENTS = sComments
			 WHERE osc.OS_ID = nOsId
			   AND osc.PROD_ID = ProdId;
			    
	   END IF;
	   
	   
	END IF;
	
	CLOSE prod_cur;
	
	
END	Add_Product_Wrap;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Set_Base_Conf ( nOsId IN OS_BASE_ENV.OS_ID%TYPE,
		  				  nBaseEnvId IN OS_BASE_ENV.BASE_ENV_ID%TYPE ) IS
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/

	-- Delete current Base Conf setting
	DELETE FROM OS_BASE_ENV
	WHERE OS_ID = nOsId;

	-- Set new Base Conf	
	IF NOT nBaseEnvId IS NULL
	THEN
		INSERT INTO OS_BASE_ENV ( OS_ID, BASE_ENV_ID )
		VALUES ( nOsId, nBaseEnvId );
	END IF;
	 
END	Set_Base_Conf;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Shift_Product_SeqNum ( nProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  				 	     nOsId IN OS_CONTENTS.OS_ID%TYPE,
						 		 nToSeqNum IN OS_CONTENTS.SEQ_NUM%TYPE ) IS
						 
currSeqNum NUMBER;
FromSeqNum NUMBER;
						 
BEGIN
	 -- Get Current seq_num
	 SELECT osc.SEQ_NUM INTO currSeqNum
	   FROM OS_CONTENTS osc
	  WHERE osc.OS_ID = nOsId
	    AND osc.PROD_ID = nProdId;
	
		
	 IF currSeqNum > nToSeqNum
	 THEN
	 	
		FromSeqNum := nToSeqNum;
		 
			-- Shift others Up
		UPDATE OS_CONTENTS osc SET
			   osc.SEQ_NUM = osc.SEQ_NUM + 1
		 WHERE osc.OS_ID = nOsId 
		   AND osc.PROD_ID != nProdId
		   AND osc.SEQ_NUM BETWEEN  FromSeqNum  AND  currSeqNum - 1;
		   
		 
	 ELSIF currSeqNum < nToSeqNum
	 THEN
	 	 
		FromSeqNum := currSeqNum + 1;  
		
		 -- Shift others Down
		UPDATE OS_CONTENTS osc SET
		   	   osc.SEQ_NUM = osc.SEQ_NUM - 1
		 WHERE osc.OS_ID = nOsId 
		   AND osc.PROD_ID != nProdId
		   AND osc.SEQ_NUM BETWEEN  FromSeqNum  AND  nToSeqNum;
		 
	 END IF;
	
	 
	 -- Move Product to new seq_num
	 UPDATE OS_CONTENTS osc SET
		    osc.SEQ_NUM = nToSeqNum
	  WHERE osc.OS_ID = nOsId 
	    AND osc.PROD_ID = nProdId;
		
		

END Shift_Product_SeqNum;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Shift_Bulk_Products_SeqNum ( sProdIdList IN VARCHAR2,
		  							   nOsId IN OS_CONTENTS.OS_ID%TYPE,
									   cDirection IN CHAR ) IS
									   
MinSeqNum	  NUMBER := 1;									   
FirstSeqNum   NUMBER;
LastSeqNum 	  NUMBER;
MaxSeqNum	  NUMBER;


CURSOR osc_down_cur IS
	SELECT osc.prod_id, osc.seq_num
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   )
	ORDER BY osc.seq_num ASC;
osc_down_rec osc_down_cur%ROWTYPE;	

CURSOR osc_up_cur IS
	SELECT osc.prod_id, osc.seq_num
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   )
	ORDER BY osc.seq_num DESC;
osc_up_rec osc_up_cur%ROWTYPE;

	
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF sProdIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Products.');
		
	ELSIF (nOsId IS NULL) OR (cDirection IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId= '|| nOsId ||', cDirection='|| cDirection );
		
	END IF;
	/*-------------------------------------------------------*/



	-- Get First seq_num
	SELECT MIN( osc.SEQ_NUM ) INTO FirstSeqNum
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   );
						   
	-- Get Last seq_num
	SELECT MAX( osc.SEQ_NUM ) INTO LastSeqNum
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   );

    -- Get Max seq_num
	SELECT MAX( osc.SEQ_NUM ) INTO MaxSeqNum
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId;
	 
	 
						   
    IF cDirection = '+'
	THEN
		-- Shift UP
		IF LastSeqNum < MaxSeqNum
		THEN
			/* Can only shift up if maxseqnum is not reached */
			OPEN osc_up_cur;
			FETCH osc_up_cur INTO osc_up_rec;
			
			WHILE osc_up_cur%FOUND
			LOOP
				Shift_Product_SeqNum ( osc_up_rec.PROD_ID, nOsId, osc_up_rec.SEQ_NUM + 1  );
				
				FETCH osc_up_cur INTO osc_up_rec;
			END LOOP;
			
			
			CLOSE osc_up_cur;
			
			
		END IF;
		
	
	
	
	ELSIF cDirection = '-'
	THEN
		-- Shift DOWN
		IF FirstSeqNum > MinSeqNum
		THEN
			/* Can only shift down if minseqnum is not reached */
			OPEN osc_down_cur;
			FETCH osc_down_cur INTO osc_down_rec;
			
			WHILE osc_down_cur%FOUND
			LOOP
				Shift_Product_SeqNum ( osc_down_rec.PROD_ID, nOsId, osc_down_rec.SEQ_NUM - 1  );
				
				FETCH osc_down_cur INTO osc_down_rec;
			END LOOP;
			
			CLOSE osc_down_cur;

			
		END IF;
		
	
	END IF;
	
	
END Shift_Bulk_Products_SeqNum;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Os ( nOsIdCollection IN DEPMGR_NUMBER_TAB_t, nUserId IN NUMBER ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
NodeId NUMBER;
OsName VARCHAR2(4000);
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	IF (nOsIdCollection.COUNT > 0) THEN   
			--Log Action Trail
		FOR i IN 1..nOsIdCollection.COUNT
		LOOP
			-- Get node_id
			SELECT os.NODE_ID INTO NodeId
	  		FROM OPERATING_SYSTEMS os
			WHERE os.OS_ID = nOsIdCollection(i);	
			
			--Get os_name
			SELECT os.OS_NAME INTO OsName
	  		FROM OPERATING_SYSTEMS os
			WHERE os.OS_ID = nOsIdCollection(i);	
			
			/* Log BOM_TRAIL */
			pk_Bom.Log_Node_Trail ( NodeId, 'Deleted Operating System: '||OsName, nUserId );		 
	
		
		
		END LOOP;
	
		-- Destroy PROCESSES_CONFIG
		DELETE 
		  FROM PROCESSES_CONFIG pc
		 WHERE pc.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy OS_CONTENTS
		DELETE 
		  FROM OS_CONTENTS osc
		 WHERE osc.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		
		
		-- Destroy OS_BASE_ENV
		DELETE 
		  FROM OS_BASE_ENV obe
		 WHERE obe.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		 
		 
		-- Destroy HIDE_PRODUCTS
		DELETE 
		  FROM HIDE_PRODUCTS hp
		 WHERE hp.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) ); 
		
		
		-- Destroy OS_TRAIL
		DELETE
		  FROM OS_TRAIL ot
		 WHERE ot.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) ); 
		

		---- Finnaly destroy Operating System ----
		DELETE
		  FROM OPERATING_SYSTEMS os
		 WHERE os.OS_ID IN ( SELECT * FROM TABLE ( CAST ( nOsIdCollection AS DEPMGR_NUMBER_TAB_t ) ) );
		 
		 

		 
	END IF; 
	
		
	
END	Destroy_Os;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Os ( nOsIdList IN VARCHAR2, nUserId IN NUMBER ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
						 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF nOsIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Operating Systems.');
		
	END IF;
	/*-------------------------------------------------------*/
	nIdCollector := IN_LIST_NUMBER ( nOsIdList );
	
	Destroy_Os ( nIdCollector, nUserId );					 
						 	
END	Destroy_Os;

/*-------------------------------------------------------------------------------------------------------*/
  PROCEDURE add_package_process (
      nprocidlist   IN   VARCHAR2,
	  npvid 		IN 	 processes_config.prod_id%TYPE,	  
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      processname   VARCHAR2 (4000);
	  PkgName 		VARCHAR2 (4000);
	  nPkgId		NUMBER;
	  
	  nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
	  


/*Rupesh Release on 17/05/2006*/
     CURSOR proc_cur
      IS
         SELECT prc.proc_id, prc.proc_name, prc.proc_description, 
		 		prc.run_as, prc.pkg_owner, prc.is_interface
           FROM processes prc
          WHERE prc.proc_id IN (
                   SELECT *
                     FROM THE
                             (SELECT CAST
                                        (in_list_number (nprocidlist) AS DEPMGR_NUMBER_TAB_t
                                        )
                                FROM DUAL
                             ));

      proc_rec      proc_cur%ROWTYPE;
   BEGIN
   
   BEGIN
   	 SELECT PKG.PKG_NAME, PKG.PKG_ID INTO PkgName, nPkgId
	 FROM PACKAGE_VERSIONS PV, PACKAGES PKG
	 WHERE PV.PV_ID = npvid
	 AND PKG.PKG_ID = PV.PKG_ID;
	 EXCEPTION
	 WHEN NO_DATA_FOUND THEN
	 	  RAISE_APPLICATION_ERROR (-20000, 'Select the Package Name and Version: ' );
   END;	 
	  
   
      OPEN proc_cur;

      FETCH proc_cur
       INTO proc_rec;

      WHILE proc_cur%FOUND
      LOOP
         INSERT INTO processes_config
                     (proc_id, prod_id, os_id, pkg_health_tag, cmd_interface, pkg_owner, is_interface, proc_description, pkg_id
                     )
              VALUES (proc_rec.proc_id, npvid, nosid, proc_rec.proc_name, proc_rec.run_as, proc_rec.pkg_owner, proc_rec.is_interface, proc_rec.proc_description, nPkgId
                     );

         SELECT prc.proc_name
           INTO processname
           FROM processes prc
          WHERE prc.proc_id = proc_rec.proc_id;

	-- Log Action --
	pk_Bom.Log_Os_Trail ( nosid,  
						  'Added process with health tag '|| processname ||' for product '|| PkgName,
						  nUserId );

         FETCH proc_cur
          INTO proc_rec;
      END LOOP;
   END;

/*--------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------------*/
  PROCEDURE add_packages_process (
      npvidlist   	IN   VARCHAR2,	  
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      processname   VARCHAR2 (4000);
	  PkgName 		VARCHAR2 (4000);
	  
	  nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
	  


/*Rupesh Release on 17/05/2006*/
     CURSOR proc_cur
      IS
         SELECT prc.proc_id, prc.proc_name, prc.proc_description, 
		 		prc.run_as, prc.pkg_owner, prc.is_interface, pp.PV_ID
           FROM processes prc, release_manager.package_processes pp
          WHERE pp.pv_id IN (
                   SELECT *
                     FROM THE
                             (SELECT CAST
                                        (in_list_number (npvidlist) AS DEPMGR_NUMBER_TAB_t
                                        )
                                FROM DUAL
                             ))
			AND	pp.PROC_ID = prc.PROC_ID;

      proc_rec      proc_cur%ROWTYPE;
   BEGIN
   

   
      OPEN proc_cur;

      FETCH proc_cur
       INTO proc_rec;

      WHILE proc_cur%FOUND
      LOOP
	  
   	 SELECT PKG.PKG_NAME INTO PkgName
	 FROM PACKAGE_VERSIONS PV, PACKAGES PKG
	 WHERE PV.PV_ID = proc_rec.pv_id
	 AND PKG.PKG_ID = PV.PKG_ID;	  
	  
         INSERT INTO processes_config
                     (proc_id, prod_id, os_id, pkg_health_tag, cmd_interface, pkg_owner, is_interface, proc_description
                     )
              VALUES (proc_rec.proc_id, proc_rec.pv_id, nosid, proc_rec.proc_name, proc_rec.run_as, proc_rec.pkg_owner, proc_rec.is_interface, proc_rec.proc_description
                     );

         SELECT prc.proc_name
           INTO processname
           FROM processes prc
          WHERE prc.proc_id = proc_rec.proc_id;

	-- Log Action --
	pk_Bom.Log_Os_Trail ( nosid,  
						  'Added process with health tag '|| processname ||' for product '|| PkgName,
						  nUserId );

         FETCH proc_cur
          INTO proc_rec;
      END LOOP;
   END;

/*--------------------------------------------------------------------------------------------------*/



END pk_Operating_System;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_PRODUCT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_PRODUCT" 
IS
/* ---------------------------------------------------------------------------
    Modified By: Rupesh Solanki
   Date: 20/03/2006
   --------------------------------------------------------------------------- */

   /*--------------------------------------------------------------------------------------------------*/
   PROCEDURE new_product (
      sprodname      IN       PACKAGES.pkg_name%TYPE,
      sprodversion   IN       package_versions.pkg_version%TYPE,
      nuserid        IN       NUMBER,
      outprodid      OUT      package_versions.pv_id%TYPE
   )
   IS
   BEGIN
/*--------------- Business Rules Here -------------------*/
/*-------------------------------------------------------*/

      -- Call Release Manager to add new product
      seed_package_names_versions (sprodname,
                                   sprodversion,
                                   nuserid,
                                   outprodid
                                  );
   END new_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE add_product (
      sprodidlist   IN   VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      nextseqnum   NUMBER;
      pkgname      VARCHAR2 (100);
      pkgversion   VARCHAR2 (100);
      nodename     VARCHAR2 (4000);

      CURSOR prod_cur
      IS
         SELECT   qry.pv_id AS prod_id, qry.pv_description, qry.comments,
                  qry.is_patch
             FROM (
                   /* Products */
                   SELECT pv.pv_id, TO_NUMBER (NULL) AS install_order,
                          TO_NUMBER (NULL) AS pkg_id, pv.pv_description,
                          pv.comments, pv.is_patch
                     FROM package_versions pv
                    WHERE pv.is_patch IS NULL
                      AND pv.pv_id IN (
                             SELECT *
                               FROM THE
                                       (SELECT CAST
                                                  (in_list_number (sprodidlist) AS depmgr_number_tab_t
                                                  )
                                          FROM DUAL
                                       ))
                   UNION ALL
                   /* Patches */
                   SELECT pv.pv_id, pp.install_order, pv.pkg_id,
                          pv.pv_description, pv.comments, pv.is_patch
                     FROM package_patches pp, package_versions pv
                    WHERE pp.patch_id = pv.pv_id
                      AND pp.patch_id IN (
                             SELECT *
                               FROM THE
                                       (SELECT CAST
                                                  (in_list_number (sprodidlist) AS depmgr_number_tab_t
                                                  )
                                          FROM DUAL
                                       ))) qry
         ORDER BY qry.pkg_id, qry.install_order;

      prod_rec     prod_cur%ROWTYPE;
   BEGIN
/*--------------- Business Rules Here -------------------*/
/*-------------------------------------------------------*/

      -- Get seq_num
      SELECT MAX (osc.seq_num)
        INTO nextseqnum
        FROM os_contents osc
       WHERE osc.os_id = nosid;

      -- Set NextSeqNum to 0 if no existing products are found
      IF nextseqnum IS NULL
      THEN
         nextseqnum := 1;
      ELSE
         nextseqnum := nextseqnum + 1;
      END IF;

      -- Insert into OS Contents
      OPEN prod_cur;

      FETCH prod_cur
       INTO prod_rec;

      WHILE prod_cur%FOUND
      LOOP
         IF (prod_rec.is_patch IS NULL)
         THEN
            INSERT INTO os_contents
                        (os_id, seq_num, prod_id,
                         product_comments
                        )
                 VALUES (nosid, nextseqnum, prod_rec.prod_id,
                         prod_rec.pv_description
                        );
         ELSE
            INSERT INTO os_contents
                        (os_id, seq_num, prod_id,
                         product_comments
                        )
                 VALUES (nosid, nextseqnum, prod_rec.prod_id,
                         prod_rec.comments
                        );
         END IF;

         SELECT pkg.pkg_name
           INTO pkgname
           FROM PACKAGES pkg, package_versions pv
          WHERE pv.pv_id = prod_rec.prod_id AND pkg.pkg_id = pv.pkg_id;

         SELECT pkg_version
           INTO pkgversion
           FROM package_versions pv
          WHERE pv.pv_id = prod_rec.prod_id;

         SELECT nn.node_name
           INTO nodename
           FROM network_nodes nn, operating_systems os
          WHERE nn.node_id = os.node_id AND os.os_id = nosid;

         -- Log Action --
         pk_bom.log_os_trail (nosid,
                                 'Added Product: '
                              || pkgname
                              || ' (Version: '
                              || pkgversion
                              || ') with Install Order: '
                              || nextseqnum
                              || ' on Node: '
                              || nodename,
                              nuserid
                             );
         nextseqnum := nextseqnum + 1;

         FETCH prod_cur
          INTO prod_rec;
      END LOOP;

      CLOSE prod_cur;
   END add_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE hide_product (
      sprodidlist   IN   VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
   BEGIN
/*--------------- Business Rules Here -------------------*/
/*-------------------------------------------------------*/
      INSERT INTO hide_products
                  (os_id, prod_id
                  )
           VALUES (nosid, sprodidlist
                  );
   END hide_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE unhide_product (
      sprodidlist   IN   NUMBER,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
   BEGIN
/*--------------- Business Rules Here -------------------*/
/*-------------------------------------------------------*/
      DELETE FROM hide_products
            WHERE prod_id = sprodidlist AND os_id = nosid;
   END unhide_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE update_process (
      nprocid         IN   processes_config.proc_id%TYPE,
      nprodid         IN   processes_config.prod_id%TYPE,
      nisinterface    IN   processes_config.is_interface%TYPE,
      nosid           IN   processes_config.os_id%TYPE,
      ncmdinterface   IN   processes_config.cmd_interface%TYPE,
      npkgowner       IN   processes_config.pkg_owner%TYPE,
      spkghealthtag   IN   processes_config.pkg_health_tag%TYPE,
      nproduct        IN   processes_config.prod_id%TYPE
   )
   IS
   BEGIN
      UPDATE processes_config
         SET cmd_interface = ncmdinterface,
             pkg_owner = npkgowner,
             is_interface = nisinterface,
             pkg_health_tag = spkghealthtag,
             prod_id = nproduct
       WHERE prod_id = nprodid AND os_id = nosid AND proc_id = nprocid;
   END update_process;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE update_product (
      noldprodid             IN       os_contents.prod_id%TYPE,
      snewprodidversion      IN       VARCHAR2,
      nosid                  IN       os_contents.os_id%TYPE,
      nseqnum                IN       os_contents.seq_num%TYPE,
      scomments              IN       os_contents.product_comments%TYPE,
      ssunoselectronicname   IN       PACKAGES.sunos_electronic_name%TYPE,
      swinelectronicname     IN       PACKAGES.win_electronic_name%TYPE,
      sisnodespec            IN       os_contents.is_node_spec%TYPE,
      snotxml                IN       os_contents.not_xml%TYPE,
      spatchelectronicname   IN       package_versions.patch_electronic_name%TYPE,
      nuserid                IN       NUMBER,
      outprodid              OUT      package_versions.pv_id%TYPE
   )
   IS
      nnewprodid   os_contents.prod_id%TYPE;
      sprodname    PACKAGES.pkg_name%TYPE;

      CURSOR curprod
      IS
         SELECT pv.pv_id
           FROM package_versions pv
          WHERE pv.pkg_id IN (SELECT pv.pkg_id
                                FROM package_versions pv
                               WHERE pv.pv_id = noldprodid)
            AND pv.pkg_version = snewprodidversion;

      recprod      curprod%ROWTYPE;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF    (noldprodid IS NULL)
         OR (snewprodidversion IS NULL)
         OR (nosid IS NULL)
         OR (nseqnum IS NULL)
      THEN
         raise_application_error (-20000,
                                     'Missing some parameters! nOldProdId= '
                                  || noldprodid
                                  || ', sNewProdIdVersion= '
                                  || snewprodidversion
                                  || ', nOsId= '
                                  || nosid
                                  || ', nSeqNum= '
                                  || nseqnum
                                 );
      END IF;

/*-------------------------------------------------------*/

      -- Get new prod_id
      OPEN curprod;

      FETCH curprod
       INTO recprod;

      -- Get the Package Name
      SELECT pkg.pkg_name
        INTO sprodname
        FROM PACKAGES pkg, package_versions pv
       WHERE pkg.pkg_id = pv.pkg_id AND pv.pv_id = noldprodid;

      IF curprod%FOUND
      THEN
         nnewprodid := recprod.pv_id;
      ELSE
         -- Call Release Manager to add new product
         seed_package_names_versions (sprodname,
                                      snewprodidversion,
                                      nuserid,
                                      outprodid
                                     );
         nnewprodid := outprodid;
      /*RAISE_APPLICATION_ERROR (-20000, 'Version '|| sNewProdIdVersion ||' does not exist.');*/
      END IF;

      CLOSE curprod;

      -- Update Product Details
      UPDATE os_contents
         SET prod_id = nnewprodid,
             product_comments = scomments,
             not_xml = snotxml,
             is_node_spec = sisnodespec
       WHERE os_id = nosid AND prod_id = noldprodid;

      --Update Package Electronic Name Details In Packages Table
      UPDATE PACKAGES
         SET sunos_electronic_name = ssunoselectronicname,
             win_electronic_name = swinelectronicname
       WHERE pkg_id = (SELECT pkg_id
                         FROM package_versions
                        WHERE package_versions.pv_id = nnewprodid);

      --Update Patch Electronic Name In Package Versions Table
      UPDATE package_versions
         SET patch_electronic_name = spatchelectronicname
       WHERE pv_id = nnewprodid;

      -- Update seq_num
      pk_operating_system.shift_product_seqnum (nnewprodid, nosid, nseqnum);
   END update_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE replace_product (
      noldprodid   IN   os_contents.prod_id%TYPE,
      nnewprodid   IN   os_contents.prod_id%TYPE,
      nosid        IN   os_contents.os_id%TYPE,
      nuserid      IN   NUMBER
   )
   IS
      oldversion   VARCHAR2 (100);
      newversion   VARCHAR2 (100);
      pkgname      VARCHAR2 (100);
      nodename     VARCHAR2 (4000);
   BEGIN
/*--------------- Business Rules Here -------------------*/
/*-------------------------------------------------------*/
      UPDATE os_contents
         SET prod_id = nnewprodid
       WHERE prod_id = noldprodid AND os_id = nosid;

      SELECT pkg_version
        INTO oldversion
        FROM package_versions
       WHERE pv_id = noldprodid;

      SELECT pkg_version
        INTO newversion
        FROM package_versions
       WHERE pv_id = nnewprodid;

      SELECT pkg.pkg_name
        INTO pkgname
        FROM PACKAGES pkg, package_versions pv
       WHERE pv.pv_id = nnewprodid AND pkg.pkg_id = pv.pkg_id;

      SELECT nn.node_name
        INTO nodename
        FROM network_nodes nn, operating_systems os
       WHERE nn.node_id = os.node_id AND os.os_id = nosid;

      -- Log Action --
      pk_bom.log_os_trail (nosid,
                              'Updated Product: '
                           || pkgname
                           || ' from Version: '
                           || oldversion
                           || ' to '
                           || newversion
                           || ' on Node: '
                           || nodename,
                           nuserid
                          );
   END replace_product;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE remove_process (
      sprocname       IN   processes_config.pkg_health_tag%TYPE,
      sprodidlist     IN   VARCHAR2,
      nosid           IN   os_contents.os_id%TYPE,
      scmdinterface   IN   processes_config.cmd_interface%TYPE,
      spkgowner       IN   processes_config.pkg_owner%TYPE,
      sinterface      IN   processes_config.is_interface%TYPE,
      nuserid         IN   NUMBER
   )
   IS
      processname   VARCHAR2 (4000);
      nodename      VARCHAR2 (4000);
   BEGIN
      -- Log Action --
      pk_bom.log_os_trail (nosid,
                           'Deleted Process: ' || sprocname || ' From Node. ',
                           nuserid
                          );

/*Select the Correct Details - Begin*/
      IF scmdinterface IS NOT NULL
      THEN
         IF sinterface IS NOT NULL
         THEN
            IF spkgowner IS NOT NULL
            THEN
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface = scmdinterface
                       AND pkg_owner = spkgowner
                       AND is_interface = sinterface;
            ELSE
      /*sPkgOwner IS NULL, sInterface IS NOT NULL, sCmdInterface IS NOT NULL*/
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface = scmdinterface
                       AND pkg_owner IS NULL
                       AND is_interface = sinterface;
            END IF;
         ELSE               /* sInterface IS NULL, sCmdInterface IS NOT NULL*/
            IF spkgowner IS NOT NULL
            THEN
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface = scmdinterface
                       AND pkg_owner = spkgowner
                       AND is_interface IS NULL;
            ELSE
         /* sPkqOwner IS NULL, sInterface IS NULL, sCmdInterface IS NOT NULL*/
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface = scmdinterface
                       AND pkg_owner IS NULL
                       AND is_interface IS NULL;
            END IF;
         END IF;
      ELSE                                          /* sCmdInterface IS NULL*/
         IF sinterface IS NOT NULL
         THEN
            IF spkgowner IS NOT NULL
            THEN
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface IS NULL
                       AND pkg_owner = spkgowner
                       AND is_interface = sinterface;
            ELSE
        /* sPkgOwner is NULL, sInterface IS NOT NULL, sCmdInterface IS NULL */
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface IS NULL
                       AND pkg_owner IS NULL
                       AND is_interface = sinterface;
            END IF;
         ELSE                   /* sInterface IS NULL, sCmdInterface IS NULL*/
            IF spkgowner IS NOT NULL
            THEN
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface IS NULL
                       AND pkg_owner = spkgowner
                       AND is_interface IS NULL;
            ELSE
             /* sPkgOwner IS NULL, sInterface IS NULL, sCmdInterface IS NULL*/
               DELETE FROM processes_config
                     WHERE os_id = nosid
                       AND prod_id = sprodidlist
                       AND pkg_health_tag = sprocname
                       AND cmd_interface IS NULL
                       AND pkg_owner IS NULL
                       AND is_interface IS NULL;
            END IF;
         END IF;
      END IF;
/*Select the Correct Details - End*/
   END remove_process;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE remove_products (
      sprodidlist   IN   VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      pkgname        VARCHAR2 (100);
      pkgversion     VARCHAR2 (100);
      nextseqnum     NUMBER              := 1;
      nidcollector   depmgr_number_tab_t := depmgr_number_tab_t ();
      nodename       VARCHAR2 (4000);

      CURSOR prod_cur
      IS
         SELECT   osc.prod_id, osc.seq_num
             FROM os_contents osc
            WHERE osc.os_id = nosid
         ORDER BY osc.seq_num;

      prod_rec       prod_cur%ROWTYPE;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF sprodidlist IS NULL
      THEN
         raise_application_error (-20000,
                                  'Please select one or more Products.'
                                 );
      END IF;

/*-------------------------------------------------------*/
      SELECT nn.node_name
        INTO nodename
        FROM network_nodes nn, operating_systems os
       WHERE nn.node_id = os.node_id AND os.os_id = nosid;

      -- Remove Products
      DELETE FROM os_contents
            WHERE os_id = nosid
              AND prod_id IN (
                     SELECT *
                       FROM THE
                               (SELECT CAST
                                          (in_list_number (sprodidlist) AS depmgr_number_tab_t
                                          )
                                  FROM DUAL
                               ));

      -- Remove Processes
      DELETE FROM processes_config
            WHERE os_id = nosid
              AND pkg_id IN (
                     SELECT pkg_id
                       FROM package_versions
                      WHERE pv_id IN (
                               SELECT *
                                 FROM THE
                                         (SELECT CAST
                                                    (in_list_number
                                                                  (sprodidlist) AS depmgr_number_tab_t
                                                    )
                                            FROM DUAL
                                         )));

      -- Update Seq Numbers
      OPEN prod_cur;

      FETCH prod_cur
       INTO prod_rec;

      WHILE prod_cur%FOUND
      LOOP
         UPDATE os_contents
            SET seq_num = nextseqnum
          WHERE os_id = nosid AND prod_id = prod_rec.prod_id;

         nextseqnum := nextseqnum + 1;

         FETCH prod_cur
          INTO prod_rec;
      END LOOP;

      CLOSE prod_cur;

      nidcollector := in_list_number (sprodidlist);

      FOR i IN 1 .. nidcollector.COUNT
      LOOP
         SELECT pkg.pkg_name
           INTO pkgname
           FROM PACKAGES pkg, package_versions pv
          WHERE pv.pv_id = nidcollector (i) AND pkg.pkg_id = pv.pkg_id;

         SELECT pkg_version
           INTO pkgversion
           FROM package_versions pv
          WHERE pv.pv_id = nidcollector (i);

         -- Log Action --
         pk_bom.log_os_trail (nosid,
                                 'Deleted Product: '
                              || pkgname
                              || ' (Version: '
                              || pkgversion
                              || ') from Node: '
                              || nodename,
                              nuserid
                             );
      END LOOP;
   END remove_products;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE add_products_nodespec (
      sprodidlist   IN   VARCHAR2,
      nosid         IN   os_contents.os_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      nextseqnum   NUMBER             := 1;

      CURSOR prod_cur
      IS
         SELECT   osc.prod_id, osc.seq_num
             FROM os_contents osc
            WHERE osc.os_id = nosid
         ORDER BY osc.seq_num;

      prod_rec     prod_cur%ROWTYPE;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF sprodidlist IS NULL
      THEN
         raise_application_error (-20000,
                                  'Please select one or more Products.'
                                 );
      END IF;

/*-------------------------------------------------------*/
      UPDATE os_contents
         SET is_node_spec = ''
       WHERE os_id = nosid;

      -- Set Nodespec On
      UPDATE os_contents
         SET is_node_spec = 'on'
       WHERE os_id = nosid
         AND prod_id IN (
                SELECT *
                  FROM THE
                          (SELECT CAST
                                     (in_list_number (sprodidlist) AS depmgr_number_tab_t
                                     )
                             FROM DUAL
                          ));
   END add_products_nodespec;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE accept_reject (
      nprodid     IN   product_details.prod_id%TYPE,
      nbomid      IN   boms.bom_id%TYPE,
      scomments   IN   rejection_trail.comments%TYPE DEFAULT NULL,
      nuserid     IN   rejection_trail.user_id%TYPE,
      nosid       IN   NUMBER
   )
   IS
      isrejected   product_details.is_rejected%TYPE;
      pkgname      VARCHAR2 (100);
      pkgversion   VARCHAR2 (100);
      nodename     VARCHAR2 (4000);

      CURSOR curprod
      IS
         SELECT pd.*
           FROM product_details pd
          WHERE pd.prod_id = nprodid;

      recprod      curprod%ROWTYPE;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF (nprodid IS NULL)
      THEN
         raise_application_error (-20000,
                                  'Please select at least one product.'
                                 );
      END IF;

/*-------------------------------------------------------*/

      -- Set new Accept-Reject state
      isrejected := 'Y';

      SELECT nn.node_name
        INTO nodename
        FROM network_nodes nn, operating_systems os
       WHERE nn.node_id = os.node_id AND os.os_id = nosid;

      OPEN curprod;

      FETCH curprod
       INTO recprod;

      IF curprod%FOUND
      THEN
         /* Product Details found */
         SELECT pkg.pkg_name
           INTO pkgname
           FROM PACKAGES pkg, package_versions pv
          WHERE pv.pv_id = nprodid AND pkg.pkg_id = pv.pkg_id;

         SELECT pkg_version
           INTO pkgversion
           FROM package_versions pv
          WHERE pv.pv_id = nprodid;

         -- Set new Accept-Reject state
         IF recprod.is_rejected = 'Y'
         THEN
            isrejected := 'N';
            -- Log Action --
            pk_bom.log_os_trail (nosid,
                                    'Accepted Product: '
                                 || pkgname
                                 || ' '
                                 || pkgversion
                                 || ' on Node: '
                                 || nodename,
                                 nuserid
                                );
         ELSE
            isrejected := 'Y';
            -- Log Action --
            pk_bom.log_os_trail (nosid,
                                    'Rejected Product: '
                                 || pkgname
                                 || ' '
                                 || pkgversion
                                 || ' on Node: '
                                 || nodename,
                                 nuserid
                                );
         END IF;

         -- Toggle Accept-Reject State
         UPDATE product_details pd
            SET pd.is_rejected = isrejected
          WHERE pd.prod_id = nprodid;
      ELSE
         /* Product Details NOT found */

         -- Toggle Accept-Reject State
         INSERT INTO product_details
                     (prod_id, is_rejected
                     )
              VALUES (nprodid, isrejected
                     );
      END IF;

      CLOSE curprod;

      -- Reflect changes to the BOM level

      /* Log Rejection Trail */
      pk_utils.log_rejection_trail
                                 (get_system_value ('enumENTITY_TYPE_PRODUCT'),
                                  nprodid,
                                  isrejected,
                                  nuserid,
                                  scomments
                                 );
   END accept_reject;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE accept_reject_list (
      nprodidlist   IN   VARCHAR2,
      nbomid        IN   boms.bom_id%TYPE,
      scomments     IN   rejection_trail.comments%TYPE DEFAULT NULL,
      nuserid       IN   rejection_trail.user_id%TYPE,
      nosid         IN   NUMBER
   )
   IS
      nidcollector   depmgr_number_tab_t := depmgr_number_tab_t ();
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF (nprodidlist IS NULL)
      THEN
         raise_application_error (-20000,
                                  'Please select at least one product.'
                                 );
      END IF;

/*-------------------------------------------------------*/
      nidcollector := in_list_number (nprodidlist);

      FOR i IN 1 .. nidcollector.COUNT
      LOOP
         accept_reject (nidcollector (i), nbomid, scomments, nuserid, nosid);
      END LOOP;
   END accept_reject_list;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE accept_reject_comments (
      nprodid      IN   product_details.prod_id%TYPE,
      nrejectseq   IN   rejection_trail.reject_seq%TYPE,
      scomments    IN   rejection_trail.comments%TYPE DEFAULT NULL,
      nuserid      IN   rejection_trail.user_id%TYPE
   )
   IS
      isrejected   rejection_trail.is_rejected%TYPE;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF (nprodid IS NULL)
      THEN
         raise_application_error (-20000,
                                     'Missing some parameters! nProdId= '
                                  || nprodid
                                 );
      END IF;

/*-------------------------------------------------------*/
      IF nrejectseq IS NULL
      THEN
         /* CREATE New Note */

         -- Get last IsRejected state
         SELECT   rt.is_rejected
             INTO isrejected
             FROM rejection_trail rt
            WHERE rt.entity_id = nprodid
              AND rt.enum_entity_type =
                                  get_system_value ('enumENTITY_TYPE_PRODUCT')
              AND ROWNUM = 1
         ORDER BY rt.reject_seq;

         /* Log Rejection Trail */
         pk_utils.log_rejection_trail
                                 (get_system_value ('enumENTITY_TYPE_PRODUCT'),
                                  nprodid,
                                  isrejected,
                                  nuserid,
                                  scomments
                                 );
      ELSE
         /* EDIT Note */
         UPDATE rejection_trail rt
            SET rt.comments = scomments,
                rt.user_id = nuserid,
                rt.date_time_stamp = get_datetime ()
          WHERE rt.enum_entity_type =
                                  get_system_value ('enumENTITY_TYPE_PRODUCT')
            AND rt.entity_id = nprodid
            AND rt.reject_seq = nrejectseq;
      END IF;
   END accept_reject_comments;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE sort_products (
      sprodidorder   IN   VARCHAR2,
      nosid          IN   os_contents.os_id%TYPE,
      nuserid        IN   NUMBER
   )
   IS
      nidcollector   depmgr_number_tab_t := depmgr_number_tab_t ();
      seqnum         NUMBER;
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      -- Check for parameters
      IF (sprodidorder IS NULL) OR (nosid IS NULL)
      THEN
         raise_application_error (-20000,
                                     'Missing some parameters! nOsId='
                                  || nosid
                                  || ' sProdIdOrder='
                                  || sprodidorder
                                 );
      END IF;

/*-------------------------------------------------------*/
      nidcollector := in_list_number (sprodidorder);
      -- Set start sequence number
      seqnum := 1;

      FOR i IN 1 .. nidcollector.COUNT
      LOOP
         -- Update new sequence number
         UPDATE os_contents osc
            SET osc.seq_num = seqnum
          WHERE osc.os_id = nosid AND osc.prod_id = nidcollector (i);

         seqnum := seqnum + 1;
      END LOOP;
   END sort_products;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE remove_obsolete_patches (
      nosid     IN   os_contents.os_id%TYPE,
      nuserid   IN   NUMBER
   )
   IS
      nextseqnum         NUMBER                     := 1;

      CURSOR prod_cur
      IS
         SELECT   osc.prod_id, osc.seq_num
             FROM os_contents osc
            WHERE osc.os_id = nosid
         ORDER BY osc.seq_num;

      prod_rec           prod_cur%ROWTYPE;

      CURSOR log_obsolete_cur
      IS
         SELECT osc.prod_id, pv.pkg_version
           FROM os_contents osc, package_versions pv
          WHERE pv.pv_id = osc.prod_id
            AND pv.is_obsolete = 'Y'
            AND osc.os_id = nosid;

      log_obsolete_rec   log_obsolete_cur%ROWTYPE;
   BEGIN
      -- Log Obsolete Patches
      OPEN log_obsolete_cur;

      FETCH log_obsolete_cur
       INTO log_obsolete_rec;

      WHILE log_obsolete_cur%FOUND
      LOOP
         pk_bom.log_os_trail (nosid,
                                 'Removed Obsolete Patch: '
                              || log_obsolete_rec.pkg_version,
                              nuserid
                             );

         FETCH log_obsolete_cur
          INTO log_obsolete_rec;
      END LOOP;

      CLOSE log_obsolete_cur;

      -- Delete Obsolete Patches in the OS.
      DELETE FROM os_contents
            WHERE os_id = nosid
              AND prod_id IN (
                     SELECT osc.prod_id
                       FROM os_contents osc, package_versions pv
                      WHERE pv.pv_id = osc.prod_id
                        AND pv.is_obsolete = 'Y'
                        AND osc.os_id = nosid);

      -- Update Seq Numbers
      OPEN prod_cur;

      FETCH prod_cur
       INTO prod_rec;

      WHILE prod_cur%FOUND
      LOOP
         UPDATE os_contents
            SET seq_num = nextseqnum
          WHERE os_id = nosid AND prod_id = prod_rec.prod_id;

         nextseqnum := nextseqnum + 1;

         FETCH prod_cur
          INTO prod_rec;
      END LOOP;

      CLOSE prod_cur;
   END remove_obsolete_patches;

/*--------------------------------------------------------------------------------------------------*/
   FUNCTION get_os_id (pvid IN NUMBER)
      RETURN NUMBER
   IS
      returnvalue   NUMBER;
   BEGIN
      SELECT osc.os_id
        INTO returnvalue
        FROM os_contents osc
       WHERE osc.prod_id = pvid;

      RETURN returnvalue;
   END;

/*--------------------------------------------------------------------------------------------------*/
   FUNCTION get_node_id (osid IN NUMBER)
      RETURN NUMBER
   IS
      returnvalue   NUMBER;
   BEGIN
      SELECT os.node_id
        INTO returnvalue
        FROM operating_systems os
       WHERE os.os_id = osid;

      RETURN returnvalue;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE add_product_note (
      nprodid   IN   NUMBER,
      snotes    IN   VARCHAR2,
      nuserid   IN   NUMBER
   )
   IS
   BEGIN
      INSERT INTO product_notes
           VALUES (nprodid, snotes);

      /* log Product_Trail*/
      pk_product.log_product_trail (nprodid, snotes, nuserid);
   END add_product_note;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE edit_product_note (
      nprodid   IN   NUMBER,
      snotes    IN   VARCHAR2,
      nuserid   IN   NUMBER
   )
   IS
   BEGIN
      UPDATE product_notes
         SET notes = snotes
       WHERE prod_id = nprodid;

      /* log Product_Trail*/
      pk_product.log_product_trail (nprodid, snotes, nuserid);
   END edit_product_note;

/*--------------------------------------------------------------------------------------------------*/
   PROCEDURE log_product_trail (
      nprodid   IN   product_trail.prod_id%TYPE,
      saction   IN   product_trail.trail_action%TYPE,
      nuserid   IN   product_trail.user_id%TYPE
   )
   IS
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF (nprodid IS NULL) OR (saction IS NULL) OR (nuserid IS NULL)
      THEN
         raise_application_error (-20000,
                                     'Missing some parameters! nProdId= '
                                  || nprodid
                                  || ', sAction='
                                  || saction
                                  || ', nUserId='
                                  || nuserid
                                 );
      END IF;

/*-------------------------------------------------------*/
      INSERT INTO product_trail
                  (prod_id, trail_action, user_id, date_time_stamp
                  )
           VALUES (nprodid, saction, nuserid, get_datetime ()
                  );
   END log_product_trail;
/*--------------------------------------------------------------------------------------------------*/
END pk_product;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_PRODUCTION
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_PRODUCTION" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
   
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Release_Authorisation_Tester ( nPvId IN NUMBER,
  					  				   	 nBomId IN NUMBER,
										 nTesterId IN NUMBER,
										 sTesterComments IN VARCHAR2 ) IS

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	
	
	-- Insert Into Release Authorisation Table --
	INSERT INTO RELEASE_AUTHORISATION ( PV_ID, BOM_ID, TESTER_ID, TESTER_COMMENTS, TESTER_DATESTAMP )
	VALUES ( nPvId, nBomId, nTesterId, sTesterComments, GET_DATETIME() );
	
	
	
END	Release_Authorisation_Tester;

/*--------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Release_Authorisation_Manager ( nPvId IN NUMBER,
  					  				   	  nBomId IN NUMBER,
										  nManagerId IN NUMBER,
										  sManagerComments IN VARCHAR2 ) IS

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	
	
	-- Insert Into Release Authorisation Table --
	UPDATE RELEASE_AUTHORISATION 
	SET MANAGER_ID = nManagerId,
		MANAGER_COMMENTS = sManagerComments,
		MANAGER_DATESTAMP = GET_DATETIME(),
		IS_OFFICIAL = 'Y'
	WHERE PV_ID = nPvId
	AND BOM_ID = nBomId;	
	
	
	
END	Release_Authorisation_Manager;

/*--------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Pkg_Trail ( nPkgId IN OS_TRAIL.OS_ID%TYPE,
						  sAction IN OS_TRAIL.TRAIL_ACTION%TYPE,
						  nUserId IN OS_TRAIL.USER_ID%TYPE  ) IS
							
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nPkgId IS NULL) OR (sAction IS NULL) OR (nUserId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId= '|| nPkgId ||', sAction='|| sAction ||', nUserId='|| nUserId );
	END IF;
	/*-------------------------------------------------------*/
	
	INSERT INTO PACKAGE_TRAIL ( PKG_ID, TRAIL_ACTION, USER_ID, DATE_TIME_STAMP )
	VALUES ( nPkgId, sAction, nUserId, GET_DATETIME() );	
		
END	Log_Pkg_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Reject_Product ( nPvId IN NUMBER,
						   nTesterId IN NUMBER,
						   sTesterComments IN VARCHAR2 ) IS
						   
IsRejected PRODUCT_DETAILS.IS_REJECTED%TYPE;		

CURSOR curProd IS
	SELECT pd.*
	  FROM PRODUCT_DETAILS pd
	 WHERE pd.PROD_ID = nPvId;
recProd curProd%ROWTYPE;				   
						   
BEGIN

	-- Set new Accept-Reject state
	IsRejected := 'Y';
	
	
	OPEN curProd;
	FETCH curProd INTO recProd;	
	
	IF curProd%FOUND THEN
		/* Product Details found */	
		
		-- Toggle Accept-Reject State 
		UPDATE PRODUCT_DETAILS pd SET
		pd.IS_REJECTED = IsRejected
		WHERE pd.PROD_ID = nPvId;
		
	ELSE
		/* Product Details NOT found */
			
		-- Toggle Accept-Reject State	
		INSERT INTO PRODUCT_DETAILS ( PROD_ID, IS_REJECTED )
		VALUES ( nPvId, IsRejected );	
		
	
	
	END IF;	
	CLOSE curProd;	
	
	/* Log Rejection Trail */
	pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_PRODUCT'), nPvId, IsRejected, nTesterId, sTesterComments );
	
				   
						   
						   

END Reject_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Production_Bom ( nBomId IN NUMBER, 
  							   nBranchId IN NUMBER, 
							   nStateId IN NUMBER, 
							   nProjId IN NUMBER ) IS
							   							   							 								 
BEGIN
	 
	 --Update the boms table with the branch_id
	 UPDATE BOMS
	 SET RTAG_ID_FK = nBranchId
	 WHERE BOM_ID = nBomId;
	 
	 --Add the production bom into the production_bom table
	 INSERT INTO PRODUCTION_BOM (PROJ_ID, BOM_ID, STATE_ID)
	 VALUES(nProjId, nBomId, nStateId);
	 							 
								 
								 
								 
END Add_Production_Bom;
/*--------------------------------------------------------------------------------------------------*/								 
END pk_production;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_PRODUCT_TEST
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_PRODUCT_TEST" 
IS
/* ---------------------------------------------------------------------------
    Modified By: Rupesh Solanki
	Date: 20/03/2006
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE New_Product ( sProdName IN PACKAGES.PKG_NAME%TYPE,
						sProdVersion IN PACKAGE_VERSIONS.PKG_VERSION%TYPE,
						nUserId IN NUMBER,
						outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE ) IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Call Release Manager to add new product
	Seed_Package_Names_Versions ( sProdName, sProdVersion, nUserId, outProdId );
	
	
END	New_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER ) IS
						 
NextSeqNum NUMBER;
PkgName VARCHAR2(100);
PkgVersion VARCHAR2(100);
NodeName VARCHAR2(4000);

CURSOR prod_cur IS
	SELECT qry.PV_ID AS PROD_ID,
		   qry.PV_DESCRIPTION,
           qry.COMMENTS,
           qry.IS_PATCH
	  FROM ( 
      		/* Products */
			SELECT pv.PV_ID, TO_NUMBER(NULL) AS INSTALL_ORDER, TO_NUMBER(NULL) AS PKG_ID, pv.PV_DESCRIPTION, pv.COMMENTS, pv.IS_PATCH
			  FROM PACKAGE_VERSIONS pv
			 WHERE pv.IS_PATCH IS NULL
			   AND pv.PV_ID IN (
			 	   			   	SELECT * 
								  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
								)
			                      
			UNION ALL
			
            /* Patches */  
			SELECT pv.PV_ID, pp.INSTALL_ORDER, pv.PKG_ID, pv.PV_DESCRIPTION, pv.COMMENTS, pv.IS_PATCH
			  FROM PACKAGE_PATCHES pp,
			  	   PACKAGE_VERSIONS pv
			 WHERE pp.PATCH_ID = pv.PV_ID
			   AND pp.PATCH_ID IN (
			 	   			   	SELECT * 
								  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
								)  		
	        ) qry  
	 ORDER BY qry.PKG_ID, qry.INSTALL_ORDER;
prod_rec prod_cur%ROWTYPE;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get seq_num
	SELECT MAX( osc.SEQ_NUM ) INTO NextSeqNum
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId;
	
	-- Set NextSeqNum to 0 if no existing products are found
	IF NextSeqNum IS NULL THEN
	   NextSeqNum := 1;
	ELSE
	   NextSeqNum := NextSeqNum + 1;
	END IF;
	
	
	-- Insert into OS Contents
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
        IF (prod_rec.IS_PATCH IS NULL) THEN
            INSERT INTO OS_CONTENTS ( OS_ID, SEQ_NUM, PROD_ID, PRODUCT_COMMENTS )
       		VALUES (
       				nOsId, 
       				NextSeqNum, 
       				prod_rec.PROD_ID,
       				prod_rec.PV_DESCRIPTION
       				);
        ELSE
            INSERT INTO OS_CONTENTS ( OS_ID, SEQ_NUM, PROD_ID, PRODUCT_COMMENTS )
    		VALUES (
    				nOsId, 
    				NextSeqNum, 
    				prod_rec.PROD_ID,
    				prod_rec.COMMENTS
    				);
        END IF;
		

		SELECT PKG.PKG_NAME INTO PkgName
		FROM PACKAGES PKG, PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = prod_rec.PROD_ID
		AND PKG.PKG_ID = PV.PKG_ID;
		
		SELECT PKG_VERSION INTO PkgVersion
		FROM PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = prod_rec.PROD_ID;
		
		SELECT NN.NODE_NAME INTO NodeName
		FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
		WHERE NN.NODE_ID = OS.NODE_ID
		AND OS.OS_ID = nOsId;
		
		
		-- Log Action --
		pk_Bom.Log_Os_Trail ( nOsId,  
							  'Added Product: '||PkgName||' (Version: '||PkgVersion||') with Install Order: '||NextSeqNum||' on Node: '||NodeName,
							  nUserId );
		
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
	END LOOP;
	
	
	CLOSE prod_cur;
	
	
END	Add_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Hide_Product ( sProdIdList IN VARCHAR2,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER ) IS
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/


            INSERT INTO HIDE_PRODUCTS ( OS_ID, PROD_ID )
       		VALUES (
       				nOsId, 
       				sProdIdList
       				);
    
END	Hide_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Unhide_Product ( sProdIdList IN NUMBER,
		  			  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
						nUserId IN NUMBER ) IS
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/


            DELETE FROM HIDE_PRODUCTS WHERE 
			PROD_ID = sProdIdList
			AND OS_ID = nOsId;
    
END	Unhide_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Process (nProcId IN PROCESSES_CONFIG.PROC_ID%TYPE,
		  				  nProdId IN PROCESSES_CONFIG.PROD_ID%TYPE,
		  				  nIsInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE, 		  
		  				  nOsId IN PROCESSES_CONFIG.OS_ID%TYPE,		  
						  nCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
						  nPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
						  sPkgHealthTag IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,
						  nProduct IN PROCESSES_CONFIG.PROD_ID%TYPE) IS

BEGIN

	 UPDATE PROCESSES_CONFIG SET
	 	CMD_INTERFACE = nCmdInterface,
		PKG_OWNER = nPkgOwner,
		IS_INTERFACE = nIsInterface,
		PKG_HEALTH_TAG = sPkgHealthTag,
		PROD_ID = nProduct 
			
			
	 WHERE PROD_ID = nProdId
	 AND OS_ID = nOsId
	 AND PROC_ID = nProcId	;						  
						  

END Update_Process;

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
						   sNewProdIdVersion IN VARCHAR2,
						   nOsId IN OS_CONTENTS.OS_ID%TYPE,
						   nSeqNum IN OS_CONTENTS.SEQ_NUM%TYPE,
						   sComments IN OS_CONTENTS.PRODUCT_COMMENTS%TYPE,
						   sSunOSElectronicName IN PACKAGES.SUNOS_ELECTRONIC_NAME%TYPE,
						   sWinElectronicName IN PACKAGES.WIN_ELECTRONIC_NAME%TYPE,
						   sIsNodeSpec IN OS_CONTENTS.IS_NODE_SPEC%TYPE,
						   sNotXML IN OS_CONTENTS.NOT_XML%TYPE,
						   sPatchElectronicName IN PACKAGE_VERSIONS.PATCH_ELECTRONIC_NAME%TYPE,
						   nUserId IN NUMBER,
						   outProdId OUT PACKAGE_VERSIONS.PV_ID%TYPE ) IS
						   
nNewProdId OS_CONTENTS.PROD_ID%TYPE;

sProdName PACKAGES.PKG_NAME%TYPE;


	
				  
						   
CURSOR curProd IS
 	SELECT pv.PV_ID
	  FROM PACKAGE_VERSIONS pv
	 WHERE pv.pkg_id IN ( SELECT pv.pkg_id FROM PACKAGE_VERSIONS pv WHERE pv.PV_ID = nOldProdId )
	   AND pv.PKG_VERSION = sNewProdIdVersion;
recProd curProd%ROWTYPE;						   
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nOldProdId IS NULL) OR (sNewProdIdVersion IS NULL) OR (nOsId IS NULL) OR (nSeqNum IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOldProdId= '|| nOldProdId  ||', sNewProdIdVersion= '|| sNewProdIdVersion ||', nOsId= '|| nOsId ||', nSeqNum= '|| nSeqNum);
	END IF;
	/*-------------------------------------------------------*/
	
	-- Get new prod_id
	OPEN curProd;
	FETCH curProd INTO recProd;
	
	SELECT pkg.pkg_name into sProdName
	FROM PACKAGES pkg, PACKAGE_VERSIONS pv
	WHERE pkg.pkg_id = pv.pkg_id 
	AND pv.pv_id = nOldProdId;
	
	
	IF curProd%FOUND THEN
	   nNewProdId := recProd.PV_ID;
	ELSE
		Seed_Package_Names_Versions ( sProdName, sNewProdIdVersion, nUserId, outProdId );
		nNewProdId := outProdId;
		/*RAISE_APPLICATION_ERROR (-20000, 'Version '|| sNewProdIdVersion ||' does not exist.');*/
	END IF;
	
	CLOSE curProd;
	
	
	-- Update Product Details
	UPDATE OS_CONTENTS SET
		PROD_ID = nNewProdId,
		PRODUCT_COMMENTS = sComments,
		NOT_XML = sNotXML,
		IS_NODE_SPEC = sIsNodeSpec
	 WHERE OS_ID = nOsId
	   AND PROD_ID = nOldProdId;
	
	--Update Package Electronic Name Details In Packages Table 
	UPDATE PACKAGES SET
		SUNOS_ELECTRONIC_NAME = sSunOSElectronicName,
		WIN_ELECTRONIC_NAME = sWinElectronicName
	WHERE  PKG_ID = 
	(
	 SELECT PKG_ID
	 FROM PACKAGE_VERSIONS 
	 WHERE PACKAGE_VERSIONS.PV_ID = nNewProdId
	);
	
	--Update Patch Electronic Name In Package Versions Table
	UPDATE PACKAGE_VERSIONS SET
		   PATCH_ELECTRONIC_NAME = sPatchElectronicName
	WHERE PV_ID = nNewProdId;	   
	   
       
	-- Update seq_num
	pk_Operating_System.Shift_Product_SeqNum ( nNewProdId, nOsId, nSeqNum );
	
END	Update_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Replace_Product ( nOldProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  				  	nNewProdId IN OS_CONTENTS.PROD_ID%TYPE,
		  			  	    nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER ) IS
						 
OldVersion VARCHAR2(100);
NewVersion VARCHAR2(100);
PkgName    VARCHAR2(100);	
NodeName VARCHAR2(4000);	
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
		
	UPDATE OS_CONTENTS SET
	PROD_ID = nNewProdId
	WHERE PROD_ID = nOldProdId
	  AND OS_ID = nOsId;
	  
	SELECT PKG_VERSION INTO OldVersion
	FROM PACKAGE_VERSIONS
	WHERE PV_ID = nOldProdId;

	SELECT PKG_VERSION INTO NewVersion
	FROM PACKAGE_VERSIONS
	WHERE PV_ID = nNewProdId;	
	
	SELECT PKG.PKG_NAME INTO PkgName
	FROM PACKAGES PKG, PACKAGE_VERSIONS PV
	WHERE PV.PV_ID = nNewProdId
	AND PKG.PKG_ID = PV.PKG_ID; 
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId;
	
	  	  
	-- Log Action --
	pk_Bom.Log_Os_Trail ( nOsId, 
						  'Updated Product: '||PkgName|| ' from Version: '||OldVersion||' to '||NewVersion||' on Node: '||NodeName,
						  nUserId );
	
END	Replace_Product;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Process ( sProcName IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,	
		  				   sProdIdList IN VARCHAR2,
		  			  	   nOsId IN OS_CONTENTS.OS_ID%TYPE,
  						   sCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
						   sPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
						   sInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,
						   nUserId IN NUMBER ) IS
	
ProcessName VARCHAR2(4000);
NodeName VARCHAR2(4000);
							
BEGIN


/*Select the Correct Details - Begin*/

		 IF sCmdInterface IS NOT NULL THEN
		 	IF sInterface IS NOT NULL THEN
			   IF sPkgOwner IS NOT NULL THEN
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE = sInterface;			   	  
				  
			   ELSE/*sPkgOwner IS NULL, sInterface IS NOT NULL, sCmdInterface IS NOT NULL*/
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE = sInterface;				   
			   END IF ;  
			ELSE 
				 IF sInterface IS NULL OR sInterface = 'F' THEN/* sInterface IS NULL, sCmdInterface IS NOT NULL*/
			   	   IF sPkgOwner IS NOT NULL THEN
			   	   	 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER = sPkgOwner
					 AND (IS_INTERFACE = 'F' OR IS_INTERFACE IS NULL);				    
				   ELSE/* sPkqOwner IS NULL, sInterface IS NULL, sCmdInterface IS NOT NULL*/
			   	   	 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE IS NULL;				   
				   END IF;
				  END IF; 
			END IF;
		 ELSE/* sCmdInterface IS NULL*/
		 		IF sInterface IS NOT NULL THEN
				   IF sPkgOwner IS NOT NULL THEN				   	  
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE = sInterface;					   
				   ELSE/* sPkgOwner is NULL, sInterface IS NOT NULL, sCmdInterface IS NULL */
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE = sInterface;						   
				   END IF;
				ELSE
				  IF sInterface IS NULL OR sInterface = 'F' THEN/* sInterface IS NULL, sCmdInterface IS NULL*/
					IF sPkgOwner IS NOT NULL THEN
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER = sPkgOwner
					 AND (IS_INTERFACE IS NULL OR IS_INTERFACE = 'F');						   
					ELSE/* sPkgOwner IS NULL, sInterface IS NULL, sCmdInterface IS NULL*/
			   		 SELECT PKG_HEALTH_TAG INTO ProcessName
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE IS NULL;										
					END IF;
				  END IF;	
				END IF;
		 END IF;


/*Select the Correct Details - End */
	 
	-- Log Action --
	pk_Bom.Log_Os_Trail ( nOsId, 
						  'Deleted Process: '||ProcessName||' From Node: '||NodeName,
						  nUserId );
						  
						  
/*Select the Correct Details - Begin*/	
		 IF sCmdInterface IS NOT NULL THEN
		 	IF sInterface IS NOT NULL THEN
			   IF sPkgOwner IS NOT NULL THEN
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE = sInterface;			   	  
				  
			   ELSE/*sPkgOwner IS NULL, sInterface IS NOT NULL, sCmdInterface IS NOT NULL*/
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE = sInterface;				   
			   END IF;
			ELSE/* sInterface IS NULL, sCmdInterface IS NOT NULL*/
			   	   IF sPkgOwner IS NOT NULL THEN
			   	   	 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE IS NULL;				    
				   ELSE/* sPkqOwner IS NULL, sInterface IS NULL, sCmdInterface IS NOT NULL*/
			   	   	 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE = sCmdInterface
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE IS NULL;				   
				   END IF;
			END IF;
		 ELSE/* sCmdInterface IS NULL*/
		 		IF sInterface IS NOT NULL THEN
				   IF sPkgOwner IS NOT NULL THEN				   	  
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE = sInterface;					   
				   ELSE/* sPkgOwner is NULL, sInterface IS NOT NULL, sCmdInterface IS NULL */
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE = sInterface;						   
				   END IF;
				ELSE/* sInterface IS NULL, sCmdInterface IS NULL*/
					IF sPkgOwner IS NOT NULL THEN
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER = sPkgOwner
					 AND IS_INTERFACE IS NULL;						   
					ELSE/* sPkgOwner IS NULL, sInterface IS NULL, sCmdInterface IS NULL*/
			   		 DELETE
					 FROM PROCESSES_CONFIG
					 WHERE OS_ID = nOsId
					 AND PROD_ID = sProdIdList
					 AND PKG_HEALTH_TAG = sProcName
					 AND CMD_INTERFACE IS NULL
					 AND PKG_OWNER IS NULL
					 AND IS_INTERFACE IS NULL;										
					END IF;
				END IF;
		 END IF;


/*Select the Correct Details - End*/				  
		
END Remove_Process;							
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Products ( sProdIdList IN VARCHAR2,
		  			  	  	nOsId IN OS_CONTENTS.OS_ID%TYPE,
							nUserId IN NUMBER ) IS

PkgName VARCHAR2(100);
PkgVersion VARCHAR2(100);		
NextSeqNum NUMBER := 1;
nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();	
NodeName VARCHAR2(4000);
							
CURSOR prod_cur IS
	SELECT osc.PROD_ID, osc.SEQ_NUM
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	 ORDER BY osc.SEQ_NUM;
prod_rec prod_cur%ROWTYPE;	  
 							
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF sProdIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Products.');
		
	END IF;
	/*-------------------------------------------------------*/
	
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId; 	
	
	-- Remove Products
	DELETE 
	FROM OS_CONTENTS
	WHERE OS_ID = nOsId
	  AND PROD_ID IN ( 
	  	  		  	    SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 );
	
	
	-- Remove Processes
	DELETE 
	FROM PROCESSES_CONFIG
	WHERE OS_ID = nOsId
	  AND PROD_ID IN ( 
	  	  		  	    SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 );  
	
	
	-- Update Seq Numbers
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		UPDATE OS_CONTENTS SET
		SEQ_NUM = NextSeqNum
		WHERE OS_ID = nOsId
		  AND PROD_ID = prod_rec.PROD_ID;
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
			
		
	END LOOP;
	
	CLOSE prod_cur;
	
	nIdCollector := IN_LIST_NUMBER ( sProdIdList );
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
		SELECT PKG.PKG_NAME INTO PkgName
		FROM PACKAGES PKG, PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nIdCollector(i)
		AND PKG.PKG_ID = PV.PKG_ID;
		
		SELECT PKG_VERSION INTO PkgVersion
		FROM PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nIdCollector(i);	
		
		-- Log Action --
		pk_Bom.Log_Os_Trail ( nOsId,  
							  'Deleted Product: '||PkgName||' (Version: '||PkgVersion||') from Node: '||NodeName,
							  nUserId );
	
	END LOOP;
	

	

	
				 					 
	  
END Remove_Products; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Products_NodeSpec ( sProdIdList IN VARCHAR2,
		  			  	  		  nOsId IN OS_CONTENTS.OS_ID%TYPE,
								  nUserId IN NUMBER ) IS
		
NextSeqNum NUMBER := 1;
							
CURSOR prod_cur IS
	SELECT osc.PROD_ID, osc.SEQ_NUM
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	 ORDER BY osc.SEQ_NUM;
prod_rec prod_cur%ROWTYPE;	  
 							
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF sProdIdList IS NULL
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one or more Products.');
		
	END IF;
	/*-------------------------------------------------------*/
	UPDATE OS_CONTENTS
	SET IS_NODE_SPEC = ''
	WHERE OS_ID = nOsId;
	
	-- Set Nodespec On
	UPDATE OS_CONTENTS
	SET IS_NODE_SPEC = 'on'
	WHERE OS_ID = nOsId
	  AND PROD_ID IN ( 
	  	  		  	    SELECT * 
						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 );
	  
END Add_Products_NodeSpec; 
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,	
						  nBomId IN BOMS.BOM_ID%TYPE,
						  sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						  nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
						  nOsId IN NUMBER ) IS

IsRejected PRODUCT_DETAILS.IS_REJECTED%TYPE;

PkgName VARCHAR2(100);
PkgVersion VARCHAR2(100);
NodeName VARCHAR2(4000);

CURSOR curProd IS
	SELECT pd.*
	  FROM PRODUCT_DETAILS pd
	 WHERE pd.PROD_ID = nProdId;
recProd curProd%ROWTYPE;
 			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nProdId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select at least one product.' );
	END IF;
	/*-------------------------------------------------------*/
	
	-- Set new Accept-Reject state
	IsRejected := 'Y';
		
	SELECT NN.NODE_NAME INTO NodeName
	FROM NETWORK_NODES NN, OPERATING_SYSTEMS OS
	WHERE NN.NODE_ID = OS.NODE_ID
	AND OS.OS_ID = nOsId; 	
	
	
	OPEN curProd;
	FETCH curProd INTO recProd;
	
	IF curProd%FOUND THEN
		/* Product Details found */
		
		SELECT PKG.PKG_NAME INTO PkgName
		FROM PACKAGES PKG, PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nProdId
		AND PKG.PKG_ID = PV.PKG_ID;
		
		SELECT PKG_VERSION INTO PkgVersion
		FROM PACKAGE_VERSIONS PV
		WHERE PV.PV_ID = nProdId;	
		
		-- Set new Accept-Reject state
		IF recProd.IS_REJECTED = 'Y' THEN
			IsRejected := 'N';
			-- Log Action --
			pk_Bom.Log_Os_Trail ( nOsId, 
								  'Accepted Product: '||PkgName||' '||PkgVersion||' on Node: '||NodeName,
						  		  nUserId );
			
		ELSE
			IsRejected := 'Y';
			-- Log Action --
			pk_Bom.Log_Os_Trail ( nOsId, 
								  'Rejected Product: '||PkgName||' '||PkgVersion||' on Node: '||NodeName,
						  		  nUserId );			
		END IF;
		
		-- Toggle Accept-Reject State 
		UPDATE PRODUCT_DETAILS pd SET
		pd.IS_REJECTED = IsRejected
		WHERE pd.PROD_ID = nProdId;
		
		
	ELSE
		/* Product Details NOT found */
			
		-- Toggle Accept-Reject State	
		INSERT INTO PRODUCT_DETAILS ( PROD_ID, IS_REJECTED )
		VALUES ( nProdId, IsRejected );
		
		
	END IF;
	
	CLOSE curProd;
	
	
	-- Reflect changes to the BOM level
	

	
	/* Log Rejection Trail */
	pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_PRODUCT'), nProdId, IsRejected, nUserId, sComments );
	
	
END	Accept_Reject;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject_List ( nProdIdList IN VARCHAR2,
							   nBomId IN BOMS.BOM_ID%TYPE,
						  	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
							   nOsId IN NUMBER ) IS

nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nProdIdList IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select at least one product.' );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( nProdIdList );
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
		Accept_Reject ( nIdCollector(i), nBomId, sComments, nUserId, nOsId);
	END LOOP;
	
	
END	Accept_Reject_List;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Accept_Reject_Comments ( nProdId IN PRODUCT_DETAILS.PROD_ID%TYPE,
		  						   nRejectSeq IN REJECTION_TRAIL.REJECT_SEQ%TYPE,
						  	   	   sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL,
						       	   nUserId IN REJECTION_TRAIL.USER_ID%TYPE ) IS

IsRejected REJECTION_TRAIL.IS_REJECTED%TYPE;
			  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	IF (nProdId IS NULL)
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nProdId= '|| nProdId  );
	END IF;
	/*-------------------------------------------------------*/
	
	IF nRejectSeq IS NULL THEN
		/* CREATE New Note */
		
		-- Get last IsRejected state
		SELECT rt.IS_REJECTED INTO IsRejected
		  FROM REJECTION_TRAIL rt
		 WHERE rt.ENTITY_ID = nProdId
		   AND rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_PRODUCT')
		   AND ROWNUM = 1
		 ORDER BY rt.REJECT_SEQ;
				 
				 
		/* Log Rejection Trail */
		pk_Utils.Log_Rejection_Trail ( GET_SYSTEM_VALUE('enumENTITY_TYPE_PRODUCT'), nProdId, IsRejected, nUserId, sComments );
		
		
	ELSE
		/* EDIT Note */
		UPDATE REJECTION_TRAIL rt SET
			rt.COMMENTS = sComments,
			rt.USER_ID = nUserId,
			rt.DATE_TIME_STAMP = GET_DATETIME()
		WHERE rt.ENUM_ENTITY_TYPE = GET_SYSTEM_VALUE('enumENTITY_TYPE_PRODUCT')
		  AND rt.ENTITY_ID = nProdId
		  AND rt.REJECT_SEQ = nRejectSeq;
		  
			
	END IF;
	
	
END	Accept_Reject_Comments;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Sort_Products ( sProdIdOrder IN VARCHAR2,
		  			  	  nOsId IN OS_CONTENTS.OS_ID%TYPE,
						  nUserId IN NUMBER ) IS
		
	nIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();						 
	SeqNum NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for parameters
	IF (sProdIdOrder IS NULL)  OR (nOsId IS NULL) 
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Missing some parameters! nOsId='|| nOsId  ||' sProdIdOrder='|| sProdIdOrder  );
	END IF;
	/*-------------------------------------------------------*/
	
	nIdCollector := IN_LIST_NUMBER ( sProdIdOrder );
	
	-- Set start sequence number
	SeqNum := 1;
	
	FOR i IN 1..nIdCollector.COUNT
	LOOP
	
		-- Update new sequence number
		UPDATE OS_CONTENTS osc SET
			osc.SEQ_NUM = SeqNum
		 WHERE osc.OS_ID = nOsId
		   AND osc.PROD_ID = nIdCollector(i);
		   
		SeqNum := SeqNum + 1;
			   
	END LOOP;
	
	
END	Sort_Products;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Obsolete_Patches ( nOsId IN OS_CONTENTS.OS_ID%TYPE,
		  						  	nUserId IN NUMBER ) IS
									
									
NextSeqNum NUMBER := 1;									
									
									
CURSOR prod_cur IS
	SELECT osc.PROD_ID, osc.SEQ_NUM
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nOsId
	 ORDER BY osc.SEQ_NUM;
prod_rec prod_cur%ROWTYPE;										
									
									
BEGIN

	 -- Delete Obsolete Patches in the OS.
	 DELETE FROM OS_CONTENTS 
	 WHERE OS_ID = nOsId
	 AND PROD_ID IN 
	 (
	 	  SELECT osc.PROD_ID
	 	  FROM OS_CONTENTS osc, PACKAGE_VERSIONS pv
		  WHERE pv.PV_ID = osc.PROD_ID
		  AND pv.IS_OBSOLETE = 'Y'
		  AND osc.OS_ID = nOsId
	 );  	
	 

	-- Update Seq Numbers
	OPEN prod_cur;
	FETCH prod_cur INTO prod_rec;
	
	WHILE prod_cur%FOUND
	LOOP
		
		UPDATE OS_CONTENTS SET
		SEQ_NUM = NextSeqNum
		WHERE OS_ID = nOsId
		  AND PROD_ID = prod_rec.PROD_ID;
		
		NextSeqNum := NextSeqNum + 1;
		FETCH prod_cur INTO prod_rec;
			
		
	END LOOP;
	
	CLOSE prod_cur;	 								

END Remove_Obsolete_Patches;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION GET_OS_ID ( PvId IN NUMBER ) RETURN NUMBER IS

	ReturnValue NUMBER;

BEGIN
	SELECT osc.OS_ID INTO ReturnValue
	  FROM OS_CONTENTS osc
	 WHERE osc.PROD_ID = PvId;

	RETURN ReturnValue;
END;
/*--------------------------------------------------------------------------------------------------*/
FUNCTION GET_NODE_ID ( OsId IN NUMBER ) RETURN NUMBER IS

	ReturnValue NUMBER;

BEGIN
	SELECT os.NODE_ID INTO ReturnValue
	  FROM OPERATING_SYSTEMS os
	 WHERE os.OS_ID = OsId;

	RETURN ReturnValue;
END;
/*-------------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------------*/


END pk_Product_Test;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_PROJECT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_PROJECT" 
IS
/* ---------------------------------------------------------------------------
    Version: 2.0
   --------------------------------------------------------------------------- */
   
   
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Project ( sProjName IN DM_PROJECTS.PROJ_NAME%TYPE,
		  			  	nRMProjFk IN DM_PROJECTS.RM_PROJECTS_FK%TYPE ) IS

ProjId NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get proj_id --
	SELECT SEQ_PROJ_ID.NEXTVAL INTO ProjId FROM DUAL;
	
	
	-- Insert Branch --
	INSERT INTO DM_PROJECTS ( PROJ_ID, PROJ_NAME, RM_PROJECTS_FK, IS_HIDDEN )
	VALUES ( ProjId, sProjName, nRMProjFk, NULL );
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Project Name "'|| sProjName ||'" is Already Used.');
		
END	Add_Project;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Project ( nProjId IN DM_PROJECTS.PROJ_ID%TYPE,
		  				   sProjName IN DM_PROJECTS.PROJ_NAME%TYPE,
						   nRMProjFk IN DM_PROJECTS.RM_PROJECTS_FK%TYPE ) IS

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	
	-- Update Project --
	UPDATE DM_PROJECTS SET
	PROJ_NAME = sProjName,
	RM_PROJECTS_FK = nRMProjFk
	WHERE PROJ_ID = nProjId;
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Project Name "'|| sProjName ||'" is Already Used.');
		
END	Update_Project;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE ShowHide_Project ( nProjId DM_PROJECTS.PROJ_ID%TYPE ) IS

currIsHidden DM_PROJECTS.IS_HIDDEN%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get current IsHidden State
	SELECT dmp.IS_HIDDEN INTO currIsHidden
	  FROM DM_PROJECTS dmp
	 WHERE dmp.PROJ_ID = nProjId;
	
	
	-- Toggle IsHidden State --
	IF currIsHidden = 'Y' THEN
	   currIsHidden := NULL;
	ELSE
	   currIsHidden := 'Y';
	END IF;
	
	
	-- Update State --
	UPDATE DM_PROJECTS dmp SET
	IS_HIDDEN = currIsHidden
	WHERE dmp.PROJ_ID = nProjId;

	
END	ShowHide_Project;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Project ( nProjId IN DM_PROJECTS.PROJ_ID%TYPE ) IS

rowCount NUMBER DEFAULT 0;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for Dependencies (Branches)
	SELECT Count(*) INTO rowCount
	  FROM BRANCHES br
	 WHERE br.PROJ_ID = nProjId;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Some Branches are still using this Project ( Counted '|| rowCount ||' ).' );
	END IF; 
	
	-- Check for Dependencies (States)
	SELECT Count(*) INTO rowCount
	  FROM STATES st
	  WHERE st.PROJ_ID = nProjId;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Some States are still using this Project ( Counted '|| rowCount ||' ).' );
	END IF; 
	/*-------------------------------------------------------*/
	
	
	-- Remove Notificaitons --
	DELETE 
	  FROM NOTIFY_USER nu
	 WHERE nu.PROJ_ID = nProjId;
	 
    -- Remove Product Queue --
	DELETE 
	  FROM PRODUCTS_QUEUE pq
	 WHERE pq.PROJ_ID = nProjId; 
	
	-- Remove Project --
	DELETE 
	  FROM DM_PROJECTS dmp
	 WHERE dmp.PROJ_ID = nProjId; 

	
END	Remove_Project;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Branch ( sBranchName IN BRANCHES.BRANCH_NAME%TYPE,
		  			   nProjId IN BRANCHES.PROJ_ID%TYPE,
					   nRMVTreeId IN BRANCHES.RM_VTREE_FK%TYPE,
                       sBranchComments IN BRANCHES.BRANCH_COMMENTS%TYPE ) IS

BranchId NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get branch_id --
	SELECT SEQ_BRANCH_ID.NEXTVAL INTO BranchId FROM DUAL;
	
	
	-- Insert Branch --
	INSERT INTO BRANCHES (BRANCH_ID, PROJ_ID, BRANCH_NAME, RM_VTREE_FK, BRANCH_COMMENTS )
	VALUES ( BranchId, nProjId, sBranchName, nRMVTreeId, sBranchComments );
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Branch Name "'|| sBranchName ||'" is Already Used.');
		
END	Add_Branch;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE ) IS

rowCount NUMBER DEFAULT 0;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for Dependencies
	SELECT Count(*) INTO rowCount
	  FROM BOMS bo
	 WHERE bo.BRANCH_ID = nBranchId;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Some BOMS are still using this Branch ( Counted '|| rowCount ||' ).' );
	END IF; 
	/*-------------------------------------------------------*/
	
	-- Remove Branch --
	DELETE 
	  FROM BRANCHES br
	 WHERE br.BRANCH_ID = nBranchId; 

	
END	Remove_Branch;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE,
		  				  sBranchName IN BRANCHES.BRANCH_NAME%TYPE,
					   	  nRMVTreeId IN BRANCHES.RM_VTREE_FK%TYPE,
                          sBranchComments IN BRANCHES.BRANCH_COMMENTS%TYPE ) IS

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	
	-- Update Branch --
	UPDATE BRANCHES SET
	BRANCH_NAME = sBranchName,
	RM_VTREE_FK = nRMVTreeId,
    BRANCH_COMMENTS = sBranchComments
	WHERE BRANCH_ID = nBranchId;
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Branch Name "'|| sBranchName ||'" is Already Used.');
		
END	Update_Branch;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE ShowHide_Branch ( nBranchId IN BRANCHES.BRANCH_ID%TYPE ) IS

currIsHidden BRANCHES.IS_HIDDEN%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get current IsHidden State
	SELECT br.IS_HIDDEN INTO currIsHidden
	  FROM BRANCHES br
	 WHERE br.BRANCH_ID = nBranchId;
	
	
	-- Toggle IsHidden State --
	IF currIsHidden = 'Y' THEN
	   currIsHidden := NULL;
	ELSE
	   currIsHidden := 'Y';
	END IF;
	
	
	-- Update State --
	UPDATE BRANCHES SET
	IS_HIDDEN = currIsHidden
	WHERE BRANCH_ID = nBranchId;

	
END	ShowHide_Branch;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE ShowHide_Download ( nProjId DM_PROJECTS.PROJ_ID%TYPE ) IS

currIsDownload DM_PROJECTS.IS_DOWNLOAD%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get current IsHidden State
	SELECT dmp.IS_DOWNLOAD INTO currIsDownload
	  FROM DM_PROJECTS dmp
	 WHERE dmp.PROJ_ID = nProjId;
	
	
	-- Toggle IsHidden State --
	IF currIsDownload = 'Y' THEN
	   currIsDownload := NULL;
	ELSE
	   currIsDownload := 'Y';
	END IF;
	
	
	-- Update State --
	UPDATE DM_PROJECTS dmp SET
	IS_DOWNLOAD = currIsDownload
	WHERE dmp.PROJ_ID = nProjId;

	
END	ShowHide_Download;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE ShowHide_StateDownload ( nStateId STATES.STATE_ID%TYPE ) IS

currIsDownload DM_PROJECTS.IS_DOWNLOAD%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get current IsHidden State
	SELECT st.IS_DOWNLOAD INTO currIsDownload
	  FROM STATES st
	 WHERE st.STATE_ID = nStateId;
	
	
	-- Toggle IsHidden State --
	IF currIsDownload = 'Y' THEN
	   currIsDownload := NULL;
	ELSE
	   currIsDownload := 'Y';
	END IF;
	
	
	-- Update State --
	UPDATE STATES st SET
	IS_DOWNLOAD = currIsDownload
	WHERE st.STATE_ID = nStateId;

	
END	ShowHide_StateDownload;

END pk_Project;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_REPORT
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_REPORT" IS

/*
------------------------------
||  Last Modified:  S.Vukovic
||  Modified Date:  8/Mar/2005
||  Body Version:   1.0
------------------------------
*/


/*-------------------------------------------------------------------------------------------------------*/
FUNCTION BOM_PRODUCTS_BY_NODE_BY_OS ( BomId NUMBER ) RETURN typeCur IS

	records typeCur;

BEGIN


	OPEN records FOR
	SELECT nn.NODE_ID,
		   nt.NODE_ICON,
		   nn.NODE_NAME,
		   bt.BOS_ICON,
		   obe.BASE_ENV_ID,
		   os.OS_ID,
		   os.OS_NAME,
		   osc.SEQ_NUM,
		   osc.PROD_ID,
		   pkg.PKG_NAME,
		   pv.PKG_VERSION
	  FROM BOM_CONTENTS bc,
	  	   OPERATING_SYSTEMS os,
		   OS_CONTENTS osc,
		   OS_BASE_ENV obe,
		   BASE_ENV be,
		   BOS_TYPES bt,
		   NETWORK_NODES nn,
		   NODE_TYPES nt,
		   PACKAGE_VERSIONS pv,
		   PACKAGES pkg
	 WHERE os.NODE_ID (+) = nn.NODE_ID 
	   AND nn.NODE_ID = bc.NODE_ID 
	   AND nn.NODE_TYPE_ID = nt.NODE_TYPE_ID (+)
	   AND bc.BOM_ID = BomId
	   AND pv.PKG_ID = pkg.PKG_ID (+)
	   AND osc.OS_ID (+) = os.OS_ID 
	   AND osc.PROD_ID = pv.PV_ID (+)
	   AND obe.OS_ID (+) = os.OS_ID
	   AND obe.BASE_ENV_ID = be.BASE_ENV_ID (+)
	   AND be.BOS_ID = bt.BOS_ID (+)
	ORDER BY UPPER(nn.NODE_NAME), UPPER(os.OS_NAME), osc.SEQ_NUM;
	   

	RETURN records;	   

END;
/*-------------------------------------------------------------------------------------------------------*/
END PK_REPORT;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_REQUIREMENTS
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_REQUIREMENTS" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Bom ( nFromBomId IN BOMS.BOM_ID%TYPE,
					   nToBranchId IN BOMS.BRANCH_ID%TYPE ) IS

rowCount NUMBER;
FromBranchId NUMBER;
									
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get FromBranchId
	SELECT bo.BRANCH_ID INTO FromBranchId
	  FROM BOMS bo
	 WHERE bo.BOM_ID = nFromBomId;
	 
	
	IF FromBranchId = nToBranchId THEN
		/* Only check for rules if same branch_id */
	
	
		-- REQUIREMENT: Cannot Branch more then 2 BOMs from parent
		SELECT COUNT(*) INTO rowCount
		  FROM BOMS bo
		 WHERE bo.PARENT_BOM_ID = nFromBomId
		   AND bo.BRANCH_ID = nToBranchId
		   AND bo.BOM_ID != bo.PARENT_BOM_ID;
		
		IF rowCount >= 2 THEN
		   RAISE_APPLICATION_ERROR (-20000, 'This BOM is already branched.' );
		END IF;  
		 
	END IF;
	
	
END	Add_Bom;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Destroy_Bom ( nBomId IN VARCHAR2 ) IS

rowCount NUMBER;
nBomIdCollector DEPMGR_NUMBER_TAB_t := DEPMGR_NUMBER_TAB_t();
									
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	nBomIdCollector := IN_LIST_NUMBER( nBomId );
	
	-- REQUIREMENT: For now if can only deal with one bom at a time
	IF nBomIdCollector.Count > 1 THEN
		RAISE_APPLICATION_ERROR (-20000, 'Please select one BOM only.' );
	END IF;
	
	
	-- REQUIREMENT: BOM need to be Unlocked
	SELECT Count(*) INTO rowCount
	  FROM BOMS bo
	 WHERE bo.IS_READONLY = 'Y'
	   AND bo.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	 );
								
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Cannot Destroy as selected BOM is locked.' );
	END IF;
	
	
	-- REQUIREMENT: Cannot be a parent to someone
	SELECT Count(*) INTO rowCount
	  FROM BOMS bo
	 WHERE bo.BOM_ID != bo.PARENT_BOM_ID
	   AND bo.PARENT_BOM_ID IN ( 
			  	  		  	      SELECT * 
								  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
							 	);
								
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Cannot Destroy as some BOMs are created or branched from this BOM.' );
	END IF;								
	
	
	-- Check for Dependencies (Knowledge Base Notes)
	SELECT Count(*) INTO rowCount
	  FROM BOM_NOTES bn
	 WHERE bn.BOM_ID IN ( 
	  	  		  	      SELECT * 
						  FROM THE ( SELECT CAST( nBomIdCollector AS DEPMGR_NUMBER_TAB_t ) FROM dual )  
					 	);
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Cannot Destroy as some knowledge base notes are attached to this BOM.' );
	END IF;
	
	
END	Destroy_Bom;
/*--------------------------------------------------------------------------------------------------*/


END pk_Requirements;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_STATE
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_STATE" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */
   
   
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Add_State ( sStateName IN STATES.STATE_NAME%TYPE,
		  			  nStateTypeEnum IN STATES.STATE_TYPE_ENUM%TYPE,
		  			  nProjId IN STATES.PROJ_ID%TYPE ) IS

StateId NUMBER;
NextStateSeq NUMBER;

				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get state_id --
	SELECT SEQ_STATE_ID.NEXTVAL INTO StateId FROM DUAL;
	
	-- Get Next Release Sequence
	SELECT MAX( st.STATE_SEQ ) INTO NextStateSeq
	  FROM STATES st
	 WHERE st.PROJ_ID = nProjId;
	
	IF NextStateSeq IS NULL THEN
	   NextStateSeq := 1;
	ELSE
	   NextStateSeq := NextStateSeq + 1;
	END IF;
	
	
	-- Insert Branch --
	INSERT INTO STATES ( STATE_ID, PROJ_ID, STATE_NAME, STATE_SEQ, STATE_TYPE_ENUM )
	VALUES ( StateId, nProjId, sStateName, NextStateSeq, nStateTypeEnum );
	
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'State Name "'|| sStateName ||'" is Already Used.');
		
END	Add_State;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Update_State ( nStateId IN STATES.STATE_ID%TYPE,
		  			   	 sStateName IN STATES.STATE_NAME%TYPE,
						 nStateSeq IN STATES.STATE_SEQ%TYPE,
						 nStateTypeEnum IN STATES.STATE_TYPE_ENUM%TYPE ) IS

currSeqNum NUMBER;
FromSeqNum NUMBER;
ProjId NUMBER;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	-- Get State proj_id --
	SELECT st.PROJ_ID INTO ProjId  FROM STATES st WHERE st.STATE_ID = nStateId;
	
	
	-- Get Current seq_num
	SELECT st.STATE_SEQ INTO currSeqNum
	  FROM STATES st
	 WHERE st.STATE_ID = nStateId;
	
		
	IF currSeqNum > nStateSeq
	THEN
	 	
		FromSeqNum := nStateSeq;
		 
		-- Shift others Up
		UPDATE STATES st SET
			   st.STATE_SEQ = st.STATE_SEQ + 1
		 WHERE st.PROJ_ID = ProjId
		   AND st.STATE_SEQ BETWEEN  FromSeqNum  AND  currSeqNum - 1;
		   
		 
	ELSIF currSeqNum < nStateSeq
	THEN
	 	 
		FromSeqNum := currSeqNum + 1;  
		
		 -- Shift others Down
		UPDATE STATES st SET
		   	   st.STATE_SEQ = st.STATE_SEQ - 1
		 WHERE st.PROJ_ID = ProjId
		   AND st.STATE_SEQ BETWEEN  FromSeqNum  AND  nStateSeq;
		 
	END IF;
	
	
	-- Update State --
	UPDATE STATES st SET
	st.STATE_NAME = sStateName,
	st.STATE_SEQ = nStateSeq,
	st.STATE_TYPE_ENUM = nStateTypeEnum
	WHERE st.STATE_ID = nStateId;
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'State Name "'|| sStateName ||'" is Already Used.');
		
END	Update_State;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_State ( nStateId IN STATES.STATE_ID%TYPE ) IS

rowCount NUMBER DEFAULT 0;
ProjId NUMBER;
NextSeqNum NUMBER;

CURSOR curStates IS
	SELECT st.STATE_ID
	  FROM STATES st
	 WHERE st.PROJ_ID = ProjId
	 ORDER BY st.STATE_SEQ;
recStates curStates%ROWTYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- Check for Dependencies (BOMS)
	SELECT Count(*) INTO rowCount
	  FROM BOM_STATE bs
	 WHERE bs.STATE_ID = nStateId;
	 
	IF rowCount > 0 THEN
	   RAISE_APPLICATION_ERROR (-20000, 'Some BOMS are still using this State ( Counted '|| rowCount ||' ).' );
	END IF; 
	/*-------------------------------------------------------*/
	
	-- Get State proj_id --
	SELECT st.PROJ_ID INTO ProjId  FROM STATES st WHERE st.STATE_ID = nStateId;
	
	
	-- Remove State --
	DELETE 
	  FROM STATES st
	 WHERE st.STATE_ID = nStateId;
	 
	 
	-- Update Seq Number --
	OPEN curStates;
	FETCH curStates INTO recStates;
	
	NextSeqNum := 1;
	WHILE curStates%FOUND
	LOOP
		
		UPDATE STATES st SET
		st.STATE_SEQ = NextSeqNum
		WHERE st.STATE_ID = recStates.STATE_ID;
		
		NextSeqNum := NextSeqNum + 1;
		FETCH curStates INTO recStates;
	END LOOP;
	
	CLOSE curStates; 

	
END	Remove_State;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Toggle_Is_Displayed ( nStateId IN BOM_STATE.STATE_ID%TYPE,
								nBomId IN BOM_STATE.BOM_ID%TYPE,
								cIsDisplay IN BOM_STATE.IS_DISPLAYED%TYPE ) IS

IsDisplayed BOM_STATE.IS_DISPLAYED%TYPE;
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get Current is_displayed
	/*SELECT bs.IS_DISPLAYED INTO IsDisplayed
	  FROM BOM_STATE bs
	 WHERE bs.BOM_ID = nBomId
	   AND bs.STATE_ID = nStateId;
	
	
	-- Toggle is_displayed
	IF IsDisplayed = 'Y' THEN
		IsDisplayed := 'N';
	ELSE
		IsDisplayed := 'Y';
	END IF;*/
	
	
	-- Update bom state
	UPDATE BOM_STATE SET
	IS_DISPLAYED = cIsDisplay
	WHERE BOM_ID = nBomId
	  AND STATE_ID = nStateId;
	  
	
END	Toggle_Is_Displayed;

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Auto_Hide ( nStateId IN BOM_STATE.STATE_ID%TYPE,
					  nInsertedBomId IN BOM_STATE.BOM_ID%TYPE,
					  nEventValue IN SYSTEM_CONFIGURATION.ITEM_VALUE%TYPE ) IS

CURSOR curParent IS
	SELECT bo.BOM_ID,
		   bo.IS_READONLY,
		   bo.IS_REJECTED
	  FROM BOMS bo
	 WHERE bo.BOM_ID IN (	
	 					 SELECT bo.PARENT_BOM_ID
						   FROM BOMS bo
						  WHERE bo.BOM_ID = nInsertedBomId
						    AND bo.BOM_ID != bo.PARENT_BOM_ID
	 					);
recParent curParent%ROWTYPE;	

ParentBomId BOMS.PARENT_BOM_ID%TYPE;
ParentOfficial BOMS.IS_READONLY%TYPE;	
ParentRejected BOMS.IS_REJECTED%TYPE;			  
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get Parent Details
	OPEN curParent;
	FETCH curParent INTO recParent;
	
	ParentBomId := recParent.BOM_ID;
	ParentOfficial := recParent.IS_READONLY;
	ParentRejected := recParent.IS_REJECTED;
	
	CLOSE curParent;
	
	
	IF ParentOfficial = 'Y' THEN
		/* Auto-Hide BOM */
		
		IF nEventValue = GET_SYSTEM_VALUE('enumEVENT_ON_RELEASE_TO') THEN
			/* REQUIREMENT: Hide Rejected parent */
			
			IF ParentRejected = 'Y' THEN
				-- Update bom state
				UPDATE BOM_STATE SET
					IS_DISPLAYED = 'N'
				WHERE BOM_ID = ParentBomId
				  AND STATE_ID = nStateId;
				
			END IF;
			
			
		ELSIF ( nEventValue = GET_SYSTEM_VALUE('enumEVENT_ON_LOCK_BOM') )  OR  (nEventValue = GET_SYSTEM_VALUE('enumEVENT_ON_ACCEPT_BOM') ) THEN
			/* REQUIREMENT: Hide Rejected or Accepted parent */
			
			IF NOT ParentRejected IS NULL THEN
				-- Update bom state
				UPDATE BOM_STATE SET
					IS_DISPLAYED = 'N'
				WHERE BOM_ID = ParentBomId
				  AND STATE_ID = nStateId;
				
			END IF;
		
		
		ELSIF ( nEventValue = GET_SYSTEM_VALUE('enumEVENT_ON_UNLOCK_BOM') )  OR  (nEventValue = GET_SYSTEM_VALUE('enumEVENT_ON_REJECT_BOM') ) THEN
			/* REQUIREMENT: Show parent */
			
			-- Update bom state
			UPDATE BOM_STATE SET
				IS_DISPLAYED = 'Y'
			WHERE BOM_ID = ParentBomId
			  AND STATE_ID = nStateId;
			
		END IF;
		
		
		
	END IF;
	
	
	
	
END	Auto_Hide;
/*--------------------------------------------------------------------------------------------------*/


END pk_State;

/

/
--------------------------------------------------------
--  DDL for Package Body PK_UTILS
--------------------------------------------------------

  CREATE OR REPLACE PACKAGE BODY "PK_UTILS" 
IS
/* ---------------------------------------------------------------------------
    Version: 1.0.0
   --------------------------------------------------------------------------- */

/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Clone_BOM ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  			  nToBomId IN BOMS.BOM_ID%TYPE,
					  nUserId IN NUMBER ) IS
						 
FromBom VARCHAR2(4000);
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	 
	-- Define old_bom_id
	UPDATE BOMS bo SET
	bo.OLD_BOM_ID = nFromBomId
	WHERE bo.BOM_ID = nToBomId;
	
	-- Copy Bom Contents
	Copy_BOM_Contents ( nFromBomId, nToBomId );
	
	
	-- Finnaly Clean old_id columns
	Clean_Old_Id_Column();
	
	
	
	
	/* Log BOM_TRAIL */
	-- Get FromBom Details
	SELECT proj.PROJ_NAME ||' / '|| br.BRANCH_NAME ||' / '|| bn.BOM_NAME ||' '|| bo.BOM_VERSION ||'.'|| BOM_LIFECYCLE
		   INTO FromBom 
	  FROM DM_PROJECTS proj,
	  	   BOMS bo,
		   BOM_NAMES bn,
		   BRANCHES br
	 WHERE br.PROJ_ID = proj.PROJ_ID
	   AND bo.BRANCH_ID = br.BRANCH_ID
	   AND bo.BOM_NAME_ID = bn.BOM_NAME_ID
	   AND bo.BOM_ID = nFromBomId;
    
	-- Log Action
	pk_Bom.Log_Bom_Trail ( nToBomId, 'COPIED: From '|| FromBom, nUserId );
	
	 
END	Clone_BOM;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Copy_BOM_Contents ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  					  nToBomId IN BOMS.BOM_ID%TYPE ) IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Copy Network Nodes
	Copy_Network_Node ( nFromBomId, nToBomId ); 
	
	
	-- Copy Hardware Names
	INSERT INTO HARDWARE ( HARDWARE_NAME_ID, BOM_ID, HARDWARE_NAME, TIER_NAME, OLD_HARDWARE_NAME_ID )
	SELECT SEQ_HARDWARE_NAME_ID.NEXTVAL AS HARDWARE_NAME_ID, 
		   nToBomId AS BOM_ID, 
		   hw.HARDWARE_NAME, 
		   hw.TIER_NAME, 
		   hw.HARDWARE_NAME_ID AS OLD_HARDWARE_NAME_ID
	  FROM HARDWARE hw
	 WHERE hw.BOM_ID = nFromBomId;
	
	
	-- Copy Hardware Matrix
	INSERT INTO HARDWARE_NODE ( HARDWARE_NAME_ID, NODE_ID) 
	SELECT hw.HARDWARE_NAME_ID, 
		   nn.NODE_ID
	  FROM HARDWARE hw,
	  	   NETWORK_NODES nn,
		   HARDWARE_NODE hd
	 WHERE hd.NODE_ID = nn.OLD_NODE_ID
	   AND hd.HARDWARE_NAME_ID = hw.OLD_HARDWARE_NAME_ID
	   AND hw.BOM_ID = nToBomId;
	
	
	-- Copy Base Environments
	INSERT INTO BASE_ENV ( BASE_ENV_ID, BOS_ID, BASE_ENV_NAME, OLD_BASE_ENV_ID, BOM_ID )
	SELECT SEQ_BASE_ENV_ID.NEXTVAL AS BASE_ENV_ID, 
		   be.BOS_ID, 
		   be.BASE_ENV_NAME, 
		   be.BASE_ENV_ID AS OLD_BASE_ENV_ID, 
		   nToBomId AS BOM_ID   
	  FROM BASE_ENV be
	 WHERE be.BOM_ID = nFromBomId;
	 
	
	-- Relink Base Environment Contents
	INSERT INTO BASE_ENV_CONTENTS ( BASE_ENV_ID, PROD_ID, BASE_ENV_COMMENTS, SEQ_NUM, IS_NODE_SPEC, NOT_XML )
	SELECT be.BASE_ENV_ID AS BASE_ENV_ID, 
		   bec.PROD_ID, 
		   bec.BASE_ENV_COMMENTS, 
		   bec.SEQ_NUM,
		   bec.IS_NODE_SPEC,
		   bec.NOT_XML
	  FROM BASE_ENV be,
	  	   BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = be.OLD_BASE_ENV_ID
	   AND be.BOM_ID = nToBomId;
   
   
    -- Copy OS
	INSERT INTO OPERATING_SYSTEMS ( OS_ID, OS_NAME, NODE_ID, OS_COMMENTS, OLD_OS_ID, NODE_SPEC_FILE, NOT_XML ) 
	SELECT SEQ_OS_ID.NEXTVAL AS OS_ID, 
		   os.OS_NAME, 
		   nn.NODE_ID AS NODE_ID, 
		   os.OS_COMMENTS, 
		   os.OS_ID AS OLD_OS_ID,
		   os.NODE_SPEC_FILE,
		   os.NOT_XML
	  FROM OPERATING_SYSTEMS os,
	  	   NETWORK_NODES nn,
		   BOM_CONTENTS bc
	 WHERE bc.NODE_ID = nn.NODE_ID
	   AND os.NODE_ID = nn.OLD_NODE_ID
	   AND bc.BOM_ID = nToBomId;
	      
	   
	   
    -- Relink OS Base Environments
	INSERT INTO OS_BASE_ENV ( OS_ID, BASE_ENV_ID ) 
	SELECT os.OS_ID AS OS_ID,
		   be.BASE_ENV_ID AS BASE_ENV_ID
	  FROM OPERATING_SYSTEMS os,
	  	   NETWORK_NODES nn,
		   BOM_CONTENTS bc,
		   OS_BASE_ENV osbe,
		   BASE_ENV be
	 WHERE osbe.OS_ID = os.OLD_OS_ID
	   AND osbe.BASE_ENV_ID = be.OLD_BASE_ENV_ID
	   AND bc.NODE_ID = nn.NODE_ID
	   AND os.NODE_ID = nn.NODE_ID
	   AND bc.BOM_ID = nToBomId;
	   
	   
	-- Copy OS Contents
	INSERT INTO OS_CONTENTS ( OS_ID, SEQ_NUM, PROD_ID, PRODUCT_COMMENTS, CHANGE_LOG_FLAG, IS_NODE_SPEC, NOT_XML ) 	  
	SELECT os.OS_ID AS OS_ID, 
		   osc.SEQ_NUM, 
		   osc.PROD_ID, 
		   osc.PRODUCT_COMMENTS, 
		   osc.CHANGE_LOG_FLAG,
		   osc.IS_NODE_SPEC,
		   osc.NOT_XML
	  FROM OPERATING_SYSTEMS os,
	  	   BOM_CONTENTS bc,
		   OS_CONTENTS osc
	 WHERE bc.NODE_ID = os.NODE_ID
	   AND osc.OS_ID = os.OLD_OS_ID
	   AND bc.BOM_ID = nToBomId;
	   
	      
    -- Copy Processes Config
	INSERT INTO PROCESSES_CONFIG ( PROC_ID, PROD_ID, OS_ID, PKG_HEALTH_TAG, CMD_INTERFACE, PKG_OWNER, IS_INTERFACE, PROC_DESCRIPTION, PKG_ID ) 	  
	SELECT pc.PROC_ID, 
		   pc.PROD_ID, 
		   os.OS_ID AS OS_ID, 
		   pc.PKG_HEALTH_TAG, 
		   pc.CMD_INTERFACE,
		   pc.PKG_OWNER,
		   pc.IS_INTERFACE,
		   pc.PROC_DESCRIPTION,
		   pc.PKG_ID 
	  FROM OPERATING_SYSTEMS os,
	  	   BOM_CONTENTS bc,
		   PROCESSES_CONFIG pc
	 WHERE bc.NODE_ID = os.NODE_ID
	   AND pc.OS_ID = os.OLD_OS_ID
	   AND bc.BOM_ID = nToBomId;
	   

	-- Copy Hide Products/Patches
	INSERT INTO HIDE_PRODUCTS (OS_ID, PROD_ID)
	SELECT os.OS_ID, 
		   hp.PROD_ID
	FROM   HIDE_PRODUCTS hp,
		   OPERATING_SYSTEMS os,
		   BOM_CONTENTS bc
	WHERE  hp.OS_ID = os.OLD_OS_ID
	AND    bc.NODE_ID = os.NODE_ID
	AND    bc.BOM_ID = nToBomId ; 	   

    
END	Copy_BOM_Contents;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Copy_Network_Node ( nFromBomId IN BOMS.BOM_ID%TYPE,
		  					  nToBomId IN BOMS.BOM_ID%TYPE,
							  nFromNodeId IN NUMBER DEFAULT 0  ) IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/

	-- Copy Network Nodes
	INSERT INTO NETWORK_NODES ( NODE_ID, NODE_TYPE_ID, NODE_NAME, NODE_COMMENTS, OLD_NODE_ID, NODE_VERSION, NODE_LIFE_CYCLE )
	SELECT SEQ_NODE_ID.NEXTVAL AS NODE_ID, 
		   nn.NODE_TYPE_ID, 
		   nn.NODE_NAME, 
		   nn.NODE_COMMENTS, 
		   nn.NODE_ID AS OLD_NODE_ID,
		   nn.NODE_VERSION, 
		   nn.NODE_LIFE_CYCLE
	  FROM NETWORK_NODES nn,
	  	   BOM_CONTENTS bc
	 WHERE bc.NODE_ID = nn.NODE_ID
	   AND (
	   	    ( bc.NODE_ID = bc.NODE_ID  AND  nFromNodeId = 0 ) OR
			( bc.NODE_ID = nFromNodeId     AND  nFromNodeId != 0 )
	   	   )
	   AND bc.BOM_ID = nFromBomId;
	 
	 
	-- Relink Bom Contents
	INSERT INTO BOM_CONTENTS ( BOM_ID, NODE_ID, SEQ_NUM)
	SELECT bo.BOM_ID,
		   nn.NODE_ID,
		   bc.SEQ_NUM
	  FROM BOMS bo,
	  	   BOM_CONTENTS bc,
		   NETWORK_NODES nn
	 WHERE bc.BOM_ID = bo.OLD_BOM_ID
	   AND bc.NODE_ID = nn.OLD_NODE_ID
	   AND (
	   	    ( nn.OLD_NODE_ID = nn.OLD_NODE_ID  AND  nFromNodeId = 0 ) OR
			( nn.OLD_NODE_ID = nFromNodeId     AND  nFromNodeId != 0 )
	   	   )
	   AND bo.BOM_ID = nToBomId;  
	 
END	Copy_Network_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Paste_Products ( sProdIdList IN VARCHAR2,
						   nFromOsId IN OS_CONTENTS.OS_ID%TYPE, 
						   nToOsId IN OS_CONTENTS.OS_ID%TYPE ) IS

MaxSeqNum NUMBER;						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get Max seq_num
	SELECT COUNT(*) INTO MaxSeqNum
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nToOsId;
	    
	   
	-- Copy OS Contents
	INSERT INTO OS_CONTENTS ( OS_ID, SEQ_NUM, PROD_ID, PRODUCT_COMMENTS, CHANGE_LOG_FLAG ) 	  
	SELECT nToOsId AS OS_ID, 
		   ROWNUM + MaxSeqNum, 
		   osc.PROD_ID, 
		   osc.PRODUCT_COMMENTS, 
		   osc.CHANGE_LOG_FLAG
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nFromOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   );
						   
		
    -- Copy Processes Config
	INSERT INTO PROCESSES_CONFIG ( PROC_ID, PROD_ID, OS_ID, PKG_HEALTH_TAG, CMD_INTERFACE ) 	  
	SELECT pc.PROC_ID, 
		   pc.PROD_ID, 
		   nToOsId AS OS_ID, 
		   pc.PKG_HEALTH_TAG, 
		   pc.CMD_INTERFACE 
	  FROM OPERATING_SYSTEMS os,
	  	   BOM_CONTENTS bc,
		   PROCESSES_CONFIG pc
	 WHERE bc.NODE_ID = os.NODE_ID
	   AND pc.OS_ID = os.OLD_OS_ID
	   AND bc.BOM_ID = nFromOsId
	   AND pc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   );


    EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Cannot Paste Duplicate Product.');	
		
END	Paste_Products;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Paste_Products_To_Base_Env ( sProdIdList IN VARCHAR2,
									   nFromOsId IN OS_CONTENTS.OS_ID%TYPE, 
									   nToBaseEnvId IN BASE_ENV.BASE_ENV_ID%TYPE ) IS

MaxSeqNum NUMBER;						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get Max seq_num
	SELECT COUNT(*) INTO MaxSeqNum
	  FROM BASE_ENV_CONTENTS bec
	 WHERE bec.BASE_ENV_ID = nToBaseEnvId;
	    
	   
	-- Copy OS Contents
	INSERT INTO BASE_ENV_CONTENTS ( BASE_ENV_ID, PROD_ID, BASE_ENV_COMMENTS, SEQ_NUM ) 	  
	SELECT nToBaseEnvId AS BASE_ENV_ID, 
		   osc.PROD_ID,
		   osc.PRODUCT_COMMENTS, 
		   ROWNUM + MaxSeqNum 
	  FROM OS_CONTENTS osc
	 WHERE osc.OS_ID = nFromOsId
	   AND osc.PROD_ID IN (
						   SELECT *
					   	     FROM THE ( SELECT CAST( IN_LIST_NUMBER( sProdIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
	   	   			   	   );
						   
		
        EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Cannot Paste Duplicate Product.');	
		
END	Paste_Products_To_Base_Env;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Paste_Os ( sOsIdList IN VARCHAR2,
					 nFromNodeId IN OPERATING_SYSTEMS.NODE_ID%TYPE,
					 nToNodeId IN OPERATING_SYSTEMS.NODE_ID%TYPE ) IS
					 
ToBomId NUMBER;					 

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	-- Get ToBomId
	SELECT bc.BOM_ID INTO ToBomId
	  FROM BOM_CONTENTS bc
 	 WHERE bc.NODE_ID = nToNodeId;
	
	
    -- Copy OS
	INSERT INTO OPERATING_SYSTEMS ( OS_ID, OS_NAME, NODE_ID, OS_COMMENTS, OLD_OS_ID ) 
	SELECT SEQ_OS_ID.NEXTVAL AS OS_ID, 
		   os.OS_NAME, 
		   nToNodeId AS NODE_ID, 
		   os.OS_COMMENTS, 
		   os.OS_ID AS OLD_OS_ID
	  FROM OPERATING_SYSTEMS os
	 WHERE os.NODE_ID = nFromNodeId
	   AND os.OS_ID IN (
					   	  SELECT *
				   	     	FROM THE ( SELECT CAST( IN_LIST_NUMBER( sOsIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
   	   			   	    );
	   
	   
	-- Paste OS Contents
	Paste_OS_Contents();
	 
	 
	 
	-- Finnaly Clean old_id columns
	Clean_Old_Id_Column();
	
	
	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN		
		RAISE_APPLICATION_ERROR (-20000, 'Cannot Paste Duplicate Operating Systems.');	
		
END	Paste_Os;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Paste_Node ( sNodeIdList IN VARCHAR2,
					   nFromBomId IN OPERATING_SYSTEMS.NODE_ID%TYPE,
					   nToBomId IN OPERATING_SYSTEMS.NODE_ID%TYPE ) IS

CURSOR curNodeName IS -- Check Node Name Duplicates			
	SELECT nn.NODE_ID
	  FROM (
	  		SELECT UPPER( nn.NODE_NAME ) AS NODE_NAME
		      FROM NETWORK_NODES nn
		     WHERE nn.NODE_ID IN (
		  							SELECT *
		     						  FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual )
		  					     )  
	  	   ) nname,
		   BOM_CONTENTS bc,
	  	   NETWORK_NODES nn	
	 WHERE bc.NODE_ID = nn.NODE_ID
	   AND bc.BOM_ID = nToBomId
	   AND UPPER( nn.NODE_NAME ) = UPPER( nname.NODE_NAME );
recNodeName curNodeName%ROWTYPE;	   					 

BEGIN
	/*--------------- Business Rules Here -------------------*/
	OPEN curNodeName;
    FETCH curNodeName INTO recNodeName;

    IF curNodeName%FOUND
    THEN
		RAISE_APPLICATION_ERROR (-20000, 'Cannot Paste Duplicate Network Nodes.'   );
		
	END IF;
	
	CLOSE curNodeName;
	/*-------------------------------------------------------*/
	
	-- Copy Network Nodes
	INSERT INTO NETWORK_NODES ( NODE_ID, NODE_TYPE_ID, NODE_NAME, NODE_COMMENTS, OLD_NODE_ID, NODE_VERSION, NODE_LIFE_CYCLE )
	SELECT SEQ_NODE_ID.NEXTVAL AS NODE_ID, 
		   nn.NODE_TYPE_ID, 
		   nn.NODE_NAME, 
		   nn.NODE_COMMENTS, 
		   nn.NODE_ID AS OLD_NODE_ID,
		   nn.NODE_VERSION, 
		   nn.NODE_LIFE_CYCLE
	  FROM NETWORK_NODES nn,
	  	   BOM_CONTENTS bc
	 WHERE bc.NODE_ID = nn.NODE_ID
	   AND bc.BOM_ID = nFromBomId
	   AND nn.NODE_ID IN (
					   	  SELECT *
				   	     	FROM THE ( SELECT CAST( IN_LIST_NUMBER( sNodeIdList ) AS DEPMGR_NUMBER_TAB_t ) FROM dual ) 
   	   			   	     );
	 
	 
	-- Relink Bom Contents
	INSERT INTO BOM_CONTENTS ( BOM_ID, NODE_ID )
	SELECT nToBomId,
		   nn.NODE_ID
	  FROM NETWORK_NODES nn
	 WHERE NOT nn.OLD_NODE_ID IS NULL;
	  
	
    -- Copy OS
	INSERT INTO OPERATING_SYSTEMS ( OS_ID, OS_NAME, NODE_ID, OS_COMMENTS, OLD_OS_ID ) 
	SELECT SEQ_OS_ID.NEXTVAL AS OS_ID, 
		   os.OS_NAME, 
		   nn.NODE_ID, 
		   os.OS_COMMENTS, 
		   os.OS_ID AS OLD_OS_ID
	  FROM OPERATING_SYSTEMS os,
	  	   NETWORK_NODES nn
	 WHERE os.NODE_ID = nn.OLD_NODE_ID;
	   
	
	-- Paste OS Contents
	Paste_OS_Contents();
	 
	 
	 
	-- Finnaly Clean old_id columns
	Clean_Old_Id_Column();
	
		
END	Paste_Node;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Paste_OS_Contents IS
					 

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	   
	
	/* Base environment copy is a bit more complex when is copied from different bom. TODO */   
	/* CODE HERE */
	   
	-- Copy OS Contents
	INSERT INTO OS_CONTENTS ( OS_ID, SEQ_NUM, PROD_ID, PRODUCT_COMMENTS, CHANGE_LOG_FLAG ) 	  
	SELECT os.OS_ID AS OS_ID, 
		   osc.SEQ_NUM, 
		   osc.PROD_ID, 
		   osc.PRODUCT_COMMENTS, 
		   osc.CHANGE_LOG_FLAG
	  FROM OS_CONTENTS osc,
	  	   OPERATING_SYSTEMS os
	 WHERE osc.OS_ID = os.OLD_OS_ID;

	      
    -- Copy Processes Config
	INSERT INTO PROCESSES_CONFIG ( PROC_ID, PROD_ID, OS_ID, PKG_HEALTH_TAG, CMD_INTERFACE, PKG_ID ) 	  
	SELECT pc.PROC_ID, 
		   pc.PROD_ID, 
		   os.OS_ID AS OS_ID, 
		   pc.PKG_HEALTH_TAG, 
		   pc.CMD_INTERFACE,
		   pc.PKG_ID 
	  FROM PROCESSES_CONFIG pc,
	  	   OPERATING_SYSTEMS os
	 WHERE pc.OS_ID = os.OLD_OS_ID;
	 
	
		
END	Paste_OS_Contents;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Clean_Old_Id_Column IS
						 
				  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/

	-- Clean all old_id columns
	UPDATE BOMS SET OLD_BOM_ID = NULL;
	UPDATE NETWORK_NODES SET OLD_NODE_ID = NULL;
	UPDATE HARDWARE SET OLD_HARDWARE_NAME_ID = NULL;
	UPDATE OPERATING_SYSTEMS SET OLD_OS_ID = NULL;
	UPDATE BASE_ENV SET OLD_BASE_ENV_ID = NULL;
	
	  
	 
END	Clean_Old_Id_Column;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Rejection_Trail ( nEnumEntityType IN REJECTION_TRAIL.ENUM_ENTITY_TYPE%TYPE,
								nEntityId IN REJECTION_TRAIL.ENTITY_ID%TYPE,
								cIsRejected IN REJECTION_TRAIL.IS_REJECTED%TYPE,
								nUserId IN REJECTION_TRAIL.USER_ID%TYPE,
								sComments IN REJECTION_TRAIL.COMMENTS%TYPE DEFAULT NULL ) IS

nNextRejectSeq REJECTION_TRAIL.REJECT_SEQ%TYPE;
								
CURSOR curRejectTrail IS
	SELECT MAX(rt.REJECT_SEQ) AS LAST_REJECT_SEQ
	  FROM REJECTION_TRAIL rt
	 WHERE rt.ENTITY_ID = nEntityId
	   AND rt.ENUM_ENTITY_TYPE = nEnumEntityType;
recRejectTrail curRejectTrail%ROWTYPE;
						 	  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/

	-- Get nNextRejectSeq
	OPEN curRejectTrail;
	FETCH curRejectTrail INTO recRejectTrail;
	
	nNextRejectSeq := 1;
	IF recRejectTrail.LAST_REJECT_SEQ IS NOT NULL THEN
	   nNextRejectSeq := recRejectTrail.LAST_REJECT_SEQ + 1;
	END IF;
	
	CLOSE curRejectTrail;
	
	
	
	-- Insert Rejection Comment
	INSERT INTO REJECTION_TRAIL ( ENUM_ENTITY_TYPE, ENTITY_ID, REJECT_SEQ, IS_REJECTED, COMMENTS, DATE_TIME_STAMP, USER_ID )
	VALUES ( 
		   nEnumEntityType,
		   nEntityId,
		   nNextRejectSeq,
		   cIsRejected,
		   sComments,
		   GET_DATETIME(),
		   nUserId
		    );
			 
END	Log_Rejection_Trail;
/*--------------------------------------------------------------------------------------------------*/
PROCEDURE Log_Action ( nUserId IN ACTION_TRAIL.USER_ID%TYPE, 
					   nEvent IN ACTION_TRAIL.ACTION_EVENT%TYPE,
					   sMethod IN ACTION_TRAIL.METHOD_CALL%TYPE DEFAULT NULL,
					   sActionScript IN ACTION_TRAIL.ACTION_SCRIPT%TYPE DEFAULT NULL,
					   sDescription IN ACTION_TRAIL.ACTION_DESCRIPTION%TYPE DEFAULT NULL ) IS
						 	  							 
BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/
	
	INSERT INTO ACTION_TRAIL ( ACTION_STAMP, USER_ID, ACTION_EVENT, METHOD_CALL, ACTION_SCRIPT, ACTION_DESCRIPTION )
	VALUES (
		    TO_DATE( TO_CHAR( SYSDATE,'DD-MON-YYYY HH24:MI:SS' ),'DD-MON-YYYY HH24:MI:SS' ),
			nUserId,
			nEvent,
			sMethod,
			sActionScript,
			sDescription
		   );
	 
END	Log_Action;
/*--------------------------------------------------------------------------------------------------*/


END pk_Utils;

/

/
--------------------------------------------------------
--  DDL for Procedure ADD_PROCESS
--------------------------------------------------------
set define off;

  CREATE OR REPLACE PROCEDURE "ADD_PROCESS" (nProcId IN PROCESSES_CONFIG.PROC_ID%TYPE,
	   	  		  						 sHealthTag IN PROCESSES_CONFIG.PKG_HEALTH_TAG%TYPE,
	   	  		  						 nOsId IN PROCESSES_CONFIG.OS_ID%TYPE,
										 sCmdInterface IN PROCESSES_CONFIG.CMD_INTERFACE%TYPE,
										 sPkgOwner IN PROCESSES_CONFIG.PKG_OWNER%TYPE,
										 sIsInterface IN PROCESSES_CONFIG.IS_INTERFACE%TYPE,
										 nProdId IN PROCESSES_CONFIG.PROD_ID%TYPE,
										 nUserId IN NUMBER) IS
										 
										 
PkgName VARCHAR2(100);
nPkgId	NUMBER;										 


/*Rupesh Release 6/02/2006*/
BEGIN

	 SELECT PKG.PKG_NAME, PKG.PKG_ID INTO PkgName, nPkgId
	 FROM PACKAGE_VERSIONS PV, PACKAGES PKG
	 WHERE PV.PV_ID = nProdId
	 AND PKG.PKG_ID = PV.PKG_ID;

	 INSERT INTO PROCESSES_CONFIG (PROC_ID, PKG_HEALTH_TAG, OS_ID, CMD_INTERFACE, PKG_OWNER, IS_INTERFACE, PROD_ID, PKG_ID )
	 VALUES (nProcId, sHealthTag, nOsId, sCmdInterface, sPkgOwner, sIsInterface, nProdId, nPkgId);
	 

	 
	-- Log Action --
	pk_Bom.Log_Os_Trail ( nOsId,  
						  'Added process with health tag '|| sHealthTag ||' for product '|| PkgName,
						  nUserId );


END ADD_PROCESS;

/

/
--------------------------------------------------------
--  DDL for Procedure LOG_ACTION
--------------------------------------------------------
set define off;

  CREATE OR REPLACE PROCEDURE "LOG_ACTION" ( nPvId IN PACKAGE_VERSIONS.PV_ID%TYPE,
										 enumActionTypeName IN ACTION_TYPE.NAME%TYPE,
                                         nUserId IN ACTION_LOG.USER_ID%TYPE,
										 sAdditionalComments IN ACTION_LOG.DESCRIPTION%TYPE DEFAULT NULL  ) IS
/* ---------------------------------------------------------------------------
    Version: 3.0
   --------------------------------------------------------------------------- */

    ActionTypeId NUMBER;

BEGIN

    -- Get Action Type FK
    SELECT act.ACTTYPE_ID INTO ActionTypeId
      FROM ACTION_TYPE act
     WHERE act.NAME = enumActionTypeName;


    INSERT INTO ACTION_LOG ( USER_ID, ACTION_DATETIME, PV_ID, DESCRIPTION, ACTTYPE_ID )
    VALUES ( nUserId, ORA_SYSDATETIME, nPvId, sAdditionalComments, ActionTypeId );


END Log_Action;

/

/
--------------------------------------------------------
--  DDL for Procedure NEW_UNIT_TEST
--------------------------------------------------------
set define off;

  CREATE OR REPLACE PROCEDURE "NEW_UNIT_TEST" ( nPv_id IN NUMBER,
                                            nTestTypeId IN UNIT_TESTS.TEST_TYPES_FK%TYPE,
                                            sTest_summary IN UNIT_TESTS.TEST_SUMMARY%TYPE,
                                            sDpkg_path IN VARCHAR2,
                                            sResults IN VARCHAR2,
                                            sCompletion_date IN VARCHAR2,
                                            nCompleted_by IN NUMBER,
											enumTEST_TYPE_AUTOMATIC IN NUMBER,
											enumTEST_TYPE_NOT_DONE IN NUMBER,
											outFileName OUT VARCHAR2
                                           ) IS
/* ---------------------------------------------------------------------------
    Version: 3.2.0
   --------------------------------------------------------------------------- */

   	FILENAME_PREFIX CONSTANT VARCHAR2(50) := 'DM_TEST';

    newID NUMBER;
    ResultsURL UNIT_TESTS.RESULTS_URL%TYPE DEFAULT NULL;
    ResultsAttachment UNIT_TESTS.RESULTS_ATTACHMENT_NAME%TYPE DEFAULT NULL;

BEGIN

	-- Get new ID --
   	SELECT SEQ_UNIT_TESTS.NEXTVAL INTO newID FROM DUAL;


       	IF (sResults <> '') OR ( NOT sResults IS NULL ) THEN
        	outFileName := sResults;
    		ResultsAttachment := sDpkg_path || '/' || outFileName;
        END IF;


	-- Remove NOT_DONE entry if exists
	DELETE FROM UNIT_TESTS
	 WHERE pv_id = nPv_id
	   AND test_types_fk = enumTEST_TYPE_NOT_DONE;

	--- Add Additional Note ---
    INSERT INTO UNIT_TESTS ( TEST_ID,
								PV_ID,
								TEST_TYPES_FK,
								TEST_SUMMARY,
								COMPLETION_DATE,
								COMPLETED_BY,
								RESULTS_URL,
								RESULTS_ATTACHMENT_NAME )
    VALUES (
       	newID,
           nPv_id,
           nTestTypeId,
           sTest_summary,
           SYSDATE(),
           nCompleted_by,
           ResultsURL,
           ResultsAttachment );

END New_Unit_Test;

/

/
--------------------------------------------------------
--  DDL for Procedure SP_GET_TOC
--------------------------------------------------------
set define off;

  CREATE OR REPLACE PROCEDURE "SP_GET_TOC" (toc_Cursor IN OUT PK_BOM_RPT.TOC_Type,Test_Parameter IN varchar2)
AS
  LAST_PAGE INTEGER;
  BEGIN
       DELETE FROM TableOfContents;
       COMMIT;
       EXECUTE IMMEDIATE Test_Parameter;
       SELECT cast(MAX(PAGE) as INTEGER) into LAST_PAGE FROM TableOfContents;
       LAST_PAGE := LAST_PAGE + 2;
       insert into tableofcontents
       values
       ('Base Configurations',LAST_PAGE,GET_DATE());
       COMMIT;
  	   OPEN toc_Cursor FOR
	   	 SELECT * FROM TableOfContents;
END sp_get_toc;


/

/
--------------------------------------------------------
--  DDL for Procedure WRITE_PRODUCTION_HISTORY
--------------------------------------------------------
set define off;

  CREATE OR REPLACE PROCEDURE "WRITE_PRODUCTION_HISTORY" (nBomId IN NUMBER,
	   	  		  									  sBomName IN VARCHAR2,
													  sNodeName IN VARCHAR2,
													  sComment IN VARCHAR2,
													  nUserId IN NUMBER
	   	  		  									  )IS

/******************************************************************************
   NAME:       WRITE_PRODUCTION_HISTORY
   PURPOSE:    

   REVISIONS:
   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
   1.0        6/07/2006          1. Created this procedure.

   NOTES:

   Automatically available Auto Replace Keywords:
      Object Name:     WRITE_PRODUCTION_HISTORY
      Sysdate:         6/07/2006
      Date and Time:   6/07/2006, 11:52:11 AM, and 6/07/2006 11:52:11 AM
      Username:         (set in TOAD Options, Procedure Editor)
      Table Name:       (set in the "New PL/SQL Object" dialog)

******************************************************************************/
BEGIN
   INSERT INTO PRODUCTION_HISTORY (BOM_ID, BOM_NAME, NODE_NAME, COMMENTS, DATE_TIME_STAMP, USER_ID)
   VALUES (nBomId, sBomName, sNodeName, sComment, GET_DATETIME(), nUserId);
   
   
END WRITE_PRODUCTION_HISTORY;

/

/
