CREATE PACKAGE BODY "RELEASE_MANAGER"."PK_BUILDAPI" 
IS
/*
------------------------------
||  Last Modified:  Jeremy Tweddle
||  Modified Date:  14/Dec/2007
||  Body Version:   3.1
------------------------------
*/

   /*-------------------------------------------------------------------------------------------------------*/
   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,
      sdiskspace        IN   VARCHAR2,
      ssbommanagement   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';

      UPDATE build_service_config
         SET config = sdiskspace
       WHERE service = 'DPKG_ARCHIVE DISK SPACE USED';

      UPDATE build_service_config
         SET config = ssbommanagement
       WHERE service = 'SBOM MANAGEMENT';
   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,
      sgbebuildfilter   IN   VARCHAR2
   )
   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, gbe_buildfilter
                     )
              VALUES (nrconid, nrtagid, sdaemonhostname, 'M',
                      ngbeid, sgbebuildfilter
                     );
      ELSE
         INSERT INTO release_config
                     (rcon_id, rtag_id, daemon_hostname, daemon_mode,
                      gbe_id, gbe_buildfilter
                     )
              VALUES (nrconid, nrtagid, sdaemonhostname, 'S',
                      ngbeid, sgbebuildfilter
                     );
      END IF;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE delete_daemon (srconidlist IN VARCHAR2)
   IS
   BEGIN
      -- Entries in the daemon_action_log, run_level and abt_action_log tables must be deleted first.
      DELETE FROM daemon_action_log
            WHERE rcon_id IN (
                     SELECT *
                       FROM THE
                               (SELECT CAST
                                          (in_list_number (srconidlist) AS relmgr_number_tab_t
                                          )
                                  FROM DUAL
                               ));
                               
      DELETE FROM run_level
            WHERE rcon_id IN (
                     SELECT *
                       FROM THE
                               (SELECT CAST
                                          (in_list_number (srconidlist) AS relmgr_number_tab_t
                                          )
                                  FROM DUAL
                               ));
                               
      DELETE FROM abt_action_log
            WHERE rcon_id IN (
                     SELECT *
                       FROM THE
                               (SELECT CAST
                                          (in_list_number (srconidlist) AS relmgr_number_tab_t
                                          )
                                  FROM DUAL
                               ));
                               
      -- Once entries in daemon_action_log, run_level and abt_action_log are removed, the daemon is deleted.
      DELETE FROM release_config
            WHERE rcon_id IN (
                     SELECT *
                       FROM THE
                               (SELECT CAST
                                          (in_list_number (srconidlist) AS relmgr_number_tab_t
                                          )
                                  FROM DUAL
                               ));
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE update_daemon (
      sdaemonhostname   IN   VARCHAR2,
      nrconid           IN   NUMBER,
      ngbeid            IN   NUMBER,
      sgbebuildfilter   IN   VARCHAR2
   )
   IS
   BEGIN
      UPDATE release_config
         SET daemon_hostname = sdaemonhostname,
             gbe_id = ngbeid,
             gbe_buildfilter = sgbebuildfilter
       WHERE rcon_id = nrconid;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE insert_schedule_info (
      dschedulepause     IN   DATE,
      dscheduleresume    IN   DATE,
      crepeat            IN   VARCHAR2,
      cindefinitepause   IN   VARCHAR2
   )
   IS
      nscheduledid   NUMBER;
   BEGIN
      -- Get Next Available Scheduled Id
      SELECT seq_scheduled_id.NEXTVAL
        INTO nscheduledid
        FROM DUAL;

      INSERT INTO run_level_schedule
           VALUES (nscheduledid, dschedulepause, dscheduleresume, crepeat,
                   cindefinitepause);
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE delete_schedule (nscheduleid IN NUMBER)
   IS
   BEGIN
      DELETE FROM run_level_schedule
            WHERE scheduled_id = nscheduleid;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE set_infinite_pause
   IS
      nscheduledid   NUMBER;
   BEGIN
      -- Get Next Available Scheduled Id
      SELECT seq_scheduled_id.NEXTVAL
        INTO nscheduledid
        FROM DUAL;

      INSERT INTO run_level_schedule
           VALUES (nscheduledid, NULL, NULL, NULL, 'P');
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE set_resume
   IS
   BEGIN
      DELETE FROM run_level_schedule
            WHERE indefinite_pause = 'P';
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE delete_out_of_date_schedule
   IS
   BEGIN
      DELETE FROM run_level_schedule
            WHERE scheduled_resume < ora_sysdatetime AND repeat = 0;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE set_daemon_resume (nrconid IN NUMBER)
   IS
   BEGIN
      UPDATE run_level
         SET PAUSE = NULL
       WHERE rcon_id = nrconid;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE set_daemon_pause (nrconid IN NUMBER)
   IS
   BEGIN
      UPDATE run_level
         SET PAUSE = 1
       WHERE rcon_id = nrconid;
   END;
/*-------------------------------------------------------------------------------------------------------*/
END pk_buildapi;
/
ALTER PACKAGE "RELEASE_MANAGER"."PK_BUILDAPI" 
  COMPILE BODY 
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  TRUE
 REUSE SETTINGS TIMESTAMP '2008-05-02 10:57:20'
/
