CREATE PACKAGE BODY "RELEASE_MANAGER"."PK_BUILDAPI_TEST" IS

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


/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE Add_Product_Component ( nPvId IN NUMBER,
								  sOsName IN VARCHAR2,
								  sOrigFilePath IN VARCHAR2,
								  sFileName IN VARCHAR2,
								  sDestFilePath IN VARCHAR2,
								  nByteSize IN NUMBER,
								  sCRCcksum IN VARCHAR2 ) IS

	nOsId NUMBER;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	-- OS Name Requirements --
	IF (sOsName IS NULL) THEN
		RAISE_APPLICATION_ERROR (-20000, 'OsName cannot be NULL.');
	END IF;


	-- File Requirements --
	IF (NOT sFileName IS NULL) THEN

		IF (nByteSize IS NULL) THEN
			RAISE_APPLICATION_ERROR (-20000, 'Byte Size cannot be NULL.');

		ELSIF (sCRCcksum IS NULL) THEN
			RAISE_APPLICATION_ERROR (-20000, 'CRC CKSUM cannot be NULL.');

		END IF;

	END IF;


	-- Folder Requirements --
	-- No requirements for now.

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

	-- Get OsId
	nOsId := Get_OsId ( sOsName );


	-- Insert component entry
	INSERT INTO PRODUCT_COMPONENTS ( PV_ID, OS_ID, FILE_PATH, FILE_NAME, DESTINATION_PATH, BYTE_SIZE, CRC_CKSUM )
    VALUES (
			  nPvId,
			  nOsId,
			  sOrigFilePath,
			  sFileName,
			  sDestFilePath,
			  nByteSize,
			  sCRCcksum
			);


	EXCEPTION
    WHEN DUP_VAL_ON_INDEX
	THEN
		RAISE_APPLICATION_ERROR (-20000, 'Cannot have duplicate product components.');

END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE Remove_All_Product_Components ( nPvId IN NUMBER, sOsName IN VARCHAR2 ) IS

	nOsId NUMBER;

BEGIN
	/*--------------- Business Rules Here -------------------*/
	/*-------------------------------------------------------*/

	-- Get OsId
	nOsId := Get_OsId ( sOsName );


	-- Delete component entry
	DELETE
	  FROM PRODUCT_COMPONENTS pc
	 WHERE pc.PV_ID = nPvId
	   AND pc.OS_ID = nOsId;


END;
/*-------------------------------------------------------------------------------------------------------*/
FUNCTION Get_OsId ( sOsName IN VARCHAR2 ) RETURN NUMBER IS
	Code NUMBER;
	
BEGIN

	-- Get Platform Code --
	SELECT pf.CODE INTO Code
	  FROM PLATFORMS pf
	 WHERE UPPER( pf.NAME ) = UPPER( sOsName );
	   
	
	RETURN Code;	   

	EXCEPTION
    	WHEN NO_DATA_FOUND THEN
       		RAISE_APPLICATION_ERROR (-20000, 'Platform '|| sOsName ||' is not valid. It needs to be added to PLATFORMS table in Release Manager.' );
   	RAISE;
END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE Update_Build_Service ( sDatabaseServer IN VARCHAR2,
			  					 sWebServer IN VARCHAR2,
								 sMailServer IN VARCHAR2,
								 sMailSender IN VARCHAR2) IS
									 
BEGIN


	 UPDATE BUILD_SERVICE_CONFIG
	 SET CONFIG = sDatabaseServer
	 WHERE SERVICE = 'DATABASE SERVER'; 

	 UPDATE BUILD_SERVICE_CONFIG
	 SET CONFIG = sWebServer
	 WHERE SERVICE = 'WEB SERVER'; 
	 
	 UPDATE BUILD_SERVICE_CONFIG
	 SET CONFIG = sMailServer
	 WHERE SERVICE = 'MAIL SERVER'; 
	 
	 UPDATE BUILD_SERVICE_CONFIG
	 SET CONFIG = sMailSender
	 WHERE SERVICE = 'BUILD FAILURE MAIL SENDER'; 	 	 									 
									 
									 
									 
END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE ADD_GBE_MACHTYPE ( sGBEValue in VARCHAR2 ) IS

GBE_ID NUMBER;

BEGIN

	-- Get GBE_ID
	SELECT SEQ_GBE_ID.NEXTVAL INTO GBE_ID FROM DUAL;
	
	INSERT INTO GBE_MACHTYPE (GBE_ID, GBE_VALUE)
	VALUES( GBE_ID, sGBEValue);
		 

END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE REMOVE_GBE_MACHTYPE ( nGBE_ID IN NUMBER ) IS

BEGIN

	 DELETE FROM GBE_MACHTYPE WHERE GBE_ID = nGBE_ID;

END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE UPDATE_GBE_MACHTYPE ( nGBE_ID IN NUMBER, sGBEValue IN VARCHAR2 ) IS

BEGIN

	 UPDATE GBE_MACHTYPE
	 SET GBE_VALUE = sGBEValue 
	 WHERE GBE_ID = nGBE_ID;


END;
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE ADD_DAEMON ( sDaemonHostname IN VARCHAR2, nRtagId IN NUMBER, nGbeId IN NUMBER ) IS
        
nRecordNumber NUMBER;
nRconId NUMBER;
BEGIN
	-- Get RCON_ID
	SELECT SEQ_RCON_ID.NEXTVAL INTO nRconId FROM DUAL; 
        
        SELECT count(*) INTO nRecordNumber 
        FROM RELEASE_CONFIG
        WHERE RTAG_ID = nRtagId;
        
        IF nRecordNumber = 0 THEN
       	   INSERT INTO RELEASE_CONFIG (RCON_ID, RTAG_ID, DAEMON_HOSTNAME, DAEMON_MODE, GBE_ID)
           VALUES (nRconId, nRtagId, sDaemonHostname, 'M', nGbeId);
        ELSE
        	INSERT INTO RELEASE_CONFIG (RCON_ID, RTAG_ID, DAEMON_HOSTNAME, DAEMON_MODE, GBE_ID)
        	VALUES (nRconId, nRtagId, sDaemonHostname, 'S', nGbeId);
        END IF;
        


END;
/*-------------------------------------------------------------------------------------------------------*/        
END PK_BUILDAPI_TEST;
/
ALTER PACKAGE "RELEASE_MANAGER"."PK_BUILDAPI_TEST" 
  COMPILE BODY 
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  FALSE
 REUSE SETTINGS TIMESTAMP '2007-07-04 14:13:56'
/
