CREATE PACKAGE BODY "RELEASE_MANAGER"."PK_PACKAGE" 
IS
/*
------------------------------
||  Last Modified:  J. Tweddle
||  Modified Date:  24/08/2007
||  Body Version:   1.8
------------------------------
*/

   /*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE new_version (
      nlastpvid                   IN       NUMBER,
      snewpkgversion              IN       VARCHAR2 DEFAULT NULL,
      cbuildtype                  IN       CHAR,
      nsettopvid                  IN       NUMBER DEFAULT NULL,
      nrtagid                     IN       NUMBER,
      nuserid                     IN       NUMBER,
      enumissues_state_imported   IN       NUMBER,
      returnpvid                  OUT      NUMBER
   )
   IS
      origpkg_id                   package_versions.pkg_id%TYPE;
      origdlocked                  package_versions.dlocked%TYPE;
      ssv_mm                       package_versions.v_mm%TYPE;
      ssv_nmm                      package_versions.v_nmm%TYPE;
      ssv_ext                      package_versions.v_ext%TYPE;
      spackageversion              VARCHAR2 (4000);
      nissuestypes                 NUMBER;
      nviewid                      NUMBER;
      reccount                     NUMBER;
      isreleased                   package_versions.dlocked%TYPE       := 'N';
      slabel                       VARCHAR2 (4000)                    := NULL;

      CURSOR package_versions_cur
      IS
         SELECT pv.pv_id, pv.is_patch, pv.dlocked
           FROM package_versions pv
          WHERE pv.pkg_version = snewpkgversion
            AND pv.pkg_id IN (SELECT DISTINCT origpv.pkg_id
                                         FROM package_versions origpv
                                        WHERE origpv.pv_id = nlastpvid);

      package_versions_rec         package_versions_cur%ROWTYPE;

      CURSOR clone_package_versions_cur
      IS
         SELECT DISTINCT pkg_id, dlocked
                    FROM package_versions
                   WHERE pv_id = nlastpvid;

      clone_package_versions_rec   clone_package_versions_cur%ROWTYPE;
   BEGIN
      spackageversion := snewpkgversion;

      IF nsettopvid IS NULL
      THEN
         -- SetToPv_id is not supplied, hence proceed.

         /* ---------------------------------------------------- */
/* Find id package_version exists                       */
/* ---------------------------------------------------- */
         OPEN package_versions_cur;

         FETCH package_versions_cur
          INTO package_versions_rec;

         IF package_versions_cur%NOTFOUND
         THEN
            ---  Create brand new package ---
            SELECT seq_pv_id.NEXTVAL
              INTO returnpvid
              FROM DUAL;

            -- Split Version to get extention + other
            split_version (spackageversion, ssv_mm, ssv_nmm, ssv_ext);

            -- Get previous package to clone from
            OPEN clone_package_versions_cur;

            FETCH clone_package_versions_cur
             INTO clone_package_versions_rec;

            origpkg_id := clone_package_versions_rec.pkg_id;
            origdlocked := clone_package_versions_rec.dlocked;

            CLOSE clone_package_versions_cur;

            -- Automated built config
            IF (cbuildtype = 'A')
            THEN
               spackageversion := '(' || returnpvid || ')' || ssv_ext;
                                    -- Make sure that version is still unique
            END IF;

            -- Clone Package Version Details --
            INSERT INTO package_versions
                        (pv_id, pkg_id, pkg_version, dlocked, created_stamp,
                         creator_id, modified_stamp, modifier_id, v_mm, v_nmm,
                         v_ext, src_path, pv_description, pv_overview,
                         last_pv_id, owner_id, is_deployable,
                         is_build_env_required, build_type, bs_id, is_autobuildable, ripple_field)
               SELECT returnpvid AS pv_id, origpkg_id AS pkg_id,
                      spackageversion AS pkg_version, 'N' AS dlocked,
                      ora_sysdate AS created_stamp, nuserid AS creator_id,
                      ora_sysdatetime AS modified_stamp,
                      nuserid AS modifier_id, ssv_mm AS v_mm,
                      ssv_nmm AS v_nmm, ssv_ext AS v_ext, pv.src_path,
                      pv.pv_description, pv.pv_overview,
                      nlastpvid AS last_pv_id, pv.owner_id, pv.is_deployable,
                      pv.is_build_env_required, cbuildtype, pv.bs_id, pv.is_autobuildable, pv.ripple_field
                 FROM package_versions pv
                WHERE pv.pv_id = nlastpvid;

            -- Set Issues Type for cloning ---
            IF origdlocked = 'Y'
            THEN
               nissuestypes := enumissues_state_imported;
            ELSE
               nissuestypes := NULL;
            END IF;

            -- Update Label for automated built
            IF (cbuildtype = 'A')
            THEN
               slabel := get_automated_label (returnpvid);

               UPDATE package_versions pv
                  SET pv.pkg_label = slabel
                WHERE pv.pv_id = returnpvid;
            END IF;

            basic_clone (nlastpvid,
                         returnpvid,
                         nrtagid,
                         nuserid,
                         origpkg_id,
                         nissuestypes
                        );
         ELSE
            --- Package already exists, hence reuse ---
            returnpvid := package_versions_rec.pv_id;
            isreleased := package_versions_rec.dlocked;
         END IF;

         CLOSE package_versions_cur;
      ELSE
         returnpvid := nsettopvid;
      END IF;
   END new_version;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE change_state (
      pvid       IN   NUMBER,
      newstate   IN   package_versions.dlocked%TYPE,
      userid     IN   NUMBER
   )
   IS
   BEGIN
      -- Set package in Released mode
      UPDATE package_versions pv
         SET pv.dlocked = newstate,
             pv.modified_stamp = ora_sysdatetime,
             pv.modifier_id = userid
       WHERE pv.pv_id = pvid;

      -- Log action --
      IF newstate = 'Y'
      THEN
         -- RELEASED --
         log_action (pvid,
                     'makeofficial',
                     userid,
                     'Package state change to: Released'
                    );
      ELSIF newstate = 'N'
      THEN
         -- UNLOCKED --
         log_action (pvid,
                     'makeunofficial',
                     userid,
                     'Package state change to: Ulocked'
                    );
      ELSIF newstate = 'P'
      THEN
         -- PENDING APPROVAL --
         log_action (pvid,
                     'add_to_planned',
                     userid,
                     'Package state change to: Pending Approval'
                    );
      ELSIF newstate = 'R'
      THEN
         -- REJECTED --
         log_action (pvid,
                     'reject_package',
                     userid,
                     'Package state change to: Rejected'
                    );
      ELSIF newstate = 'A'
      THEN
         -- APPROVED --
         log_action (pvid,
                     'approve_package',
                     userid,
                     'Package state change to: Approved'
                    );
      END IF;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE destroy_package (
      pvid               IN       NUMBER,
      overridewarnings   IN       CHAR DEFAULT 'N',
      problemstring      OUT      VARCHAR2
   )
   IS
      LOCKED     CHAR;
      pkgid      NUMBER;
      ROWCOUNT   NUMBER;
   BEGIN
      /*
      || This will destroy all package details from database.
      || It will only be used to remove unwanted work in progress packages,
      || or mestaken versions
      */

      /*--------------- Business Rules Here 
-------------------*/
      problemstring := NULL;

      IF overridewarnings = 'N'
      THEN
         -- Package must not be official
         SELECT pv.dlocked
           INTO LOCKED
           FROM package_versions pv
          WHERE pv.pv_id = pvid;

         IF LOCKED = 'Y'
         THEN
            problemstring :=
                  problemstring
               || '- Package is locked and released.'
               || UTL_TCP.crlf;
         END IF;

         -- Cannot remove if used in BOMs
         SELECT COUNT (osc.prod_id)
           INTO ROWCOUNT
           FROM deployment_manager.os_contents osc
          WHERE osc.prod_id = pvid;

         IF ROWCOUNT > 0
         THEN
            problemstring :=
                  problemstring
               || '- Package is part of Bill-Of-Material (BOM) in Deployment Manager.'
               || UTL_TCP.crlf;
         END IF;

         -- Cannot remove if Referenced as build dependency
         SELECT COUNT (dep.pv_id)
           INTO ROWCOUNT
           FROM package_dependencies dep
          WHERE dep.dpv_id = pvid;

         IF ROWCOUNT > 0
         THEN
            problemstring :=
                  problemstring
               || '- Package is referenced by other package as build dependency.'
               || UTL_TCP.crlf;
         END IF;

         -- Cannot remove if Referenced as runtime dependency
         SELECT COUNT (rd.pv_id)
           INTO ROWCOUNT
           FROM runtime_dependencies rd
          WHERE rd.rtd_id = pvid;

         IF ROWCOUNT > 0
         THEN
            problemstring :=
                  problemstring
               || '- Package is referenced by other package as runtime dependency.'
               || UTL_TCP.crlf;
         END IF;

         -- Cannot remove if Referenced as patch
         SELECT COUNT (pp.pv_id)
           INTO ROWCOUNT
           FROM package_patches pp
          WHERE pp.patch_id = pvid;

         IF ROWCOUNT > 0
         THEN
            problemstring :=
                  problemstring
               || '- Package is used as patch by other package.'
               || UTL_TCP.crlf;
         END IF;
      END IF;

/*-------------------------------------------------------*/
      IF (problemstring IS NULL)
      THEN
         --- Remove From Work in Progress
         DELETE FROM work_in_progress wip
               WHERE wip.pv_id = pvid;

         --- Remove From Pending
         DELETE FROM planned pl
               WHERE pl.pv_id = pvid;

         --- Remove From Released area
         DELETE FROM release_content rc
               WHERE rc.pv_id = pvid;

         ---Remove From Package Processes
         DELETE FROM package_processes pp
               WHERE pp.pv_id = pvid;

         --- Remove Dependencies
         DELETE FROM package_dependencies dep
               WHERE dep.pv_id = pvid;

         DELETE FROM package_dependencies dep
               WHERE dep.dpv_id = pvid;

         --- Remove Runtime dependencies
         DELETE FROM runtime_dependencies rtd
               WHERE rtd.pv_id = pvid;

         DELETE FROM runtime_dependencies rtd
               WHERE rtd.rtd_id = pvid;

         --- Remove components
         DELETE FROM product_components pc
               WHERE pc.pv_id = pvid;

         DELETE FROM release_components rc
               WHERE rc.pv_id = pvid;

         --- Remove From Notification History
         DELETE FROM notification_history nh
               WHERE nh.pv_id = pvid;

         --- Remove From Ignore Warnings
         DELETE FROM ignore_warnings iw
               WHERE iw.pv_id = pvid;

         --- Remove From Additional Notes
         DELETE FROM additional_notes an
               WHERE an.pv_id = pvid;

         --- Remove From CQ Issues
         DELETE FROM cq_issues cq
               WHERE cq.pv_id = pvid;

         --- Remove from Package Patches
         DELETE FROM package_patches pp
               WHERE pp.pv_id = pvid;

         DELETE FROM package_patches pp
               WHERE pp.patch_id = pvid;

         --- Remove From Package Documents
         DELETE FROM package_documents pd
               WHERE pd.pv_id = pvid;

         --- Remove from Code Review
         DELETE FROM code_reviews cr
               WHERE cr.pv_id = pvid;
               
         --- Remove from Code Review URL
         DELETE FROM code_review_url cru
               WHERE cru.pv_id = pvid;

         --- Remove from Unit Tests
         DELETE FROM unit_tests ut
               WHERE ut.pv_id = pvid;

         --- Remove from Package BuildEnv
         DELETE FROM package_build_env pbe
               WHERE pbe.pv_id = pvid;
			   
		 --- Remove from Package Build Info
		 DELETE FROM package_build_info pbi
		 	   WHERE pbi.pv_id = pvid;    
			   

         --- Remove from Build Order
         DELETE FROM build_order bo
               WHERE bo.pv_id = pvid;

         --- Remove from Note Manager
         DELETE FROM note_manager nm
               WHERE nm.nid = pvid;

         --- Remove from Action log
         DELETE FROM action_log al
               WHERE al.pv_id = pvid;
			   
		 --- Remove from Do Not Ripple 
		 DELETE FROM DO_NOT_RIPPLE dnr
		 	   WHERE dnr.PV_ID = pvid;
			   
		 --- Remove from Advisory Ripple 
		 DELETE FROM ADVISORY_RIPPLE ar
		 	   WHERE ar.PV_ID = pvid;			   
			   
		 --- Remove from Jira Issues 
		 DELETE FROM JIRA_ISSUES jira
		 	   WHERE jira.PV_ID = pvid;
			   			   
         --- Finally Remove From Package Versions
         --- Get Package name
         SELECT pv.pkg_id
           INTO pkgid
           FROM package_versions pv
          WHERE pv.pv_id = pvid;

         DELETE FROM package_versions pv
               WHERE pv.pv_id = pvid;

         --- Remove package name if not used any more
         SELECT COUNT (pv.pv_id)
           INTO ROWCOUNT
           FROM package_versions pv
          WHERE pv.pkg_id = pkgid;

         IF ROWCOUNT < 1
         THEN
            DELETE FROM PACKAGES pkg
                  WHERE pkg.pkg_id = pkgid;
         END IF;
      END IF;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE new_patch (
      snewpatchversion   IN       package_versions.pkg_version%TYPE,
      nparentpvid        IN       NUMBER,
      spatchidlist       IN       VARCHAR2,
      nuserid            IN       NUMBER,
      returnpatchid      OUT      NUMBER
   )
   IS
      patchpv_id           NUMBER;
      parpkg_id            NUMBER;
      lastinstallorder     NUMBER;
      ispatchdlocked       package_versions.dlocked%TYPE;
      ssv_mm               package_versions.v_mm%TYPE;
      ssv_nmm              package_versions.v_nmm%TYPE;
      ssv_ext              package_versions.v_ext%TYPE;
      opatchdepcollector   relmgr_number_tab_t      := relmgr_number_tab_t
                                                                          ();

      CURSOR parent_cur
      IS
         SELECT pv.*, pkg.pkg_name
           FROM package_versions pv, PACKAGES pkg
          WHERE pv.pv_id = nparentpvid AND pv.pkg_id = pkg.pkg_id;

      parent_rec           parent_cur%ROWTYPE;

      CURSOR patch_cur
      IS
         SELECT pv.*, pg.pkg_name
           FROM package_versions pv, PACKAGES pg
          WHERE pv.pkg_id = parpkg_id
            AND pv.pkg_version = snewpatchversion
            AND pv.pkg_id = pg.pkg_id;

      patch_rec            patch_cur%ROWTYPE;

      CURSOR releases_cur
      IS
         SELECT rc.pv_id
           FROM release_content rc
          WHERE rc.pv_id = patch_rec.pv_id;

      releases_rec         releases_cur%ROWTYPE;
   BEGIN
      -- Get Last Install Order
      SELECT COUNT (*)
        INTO lastinstallorder
        FROM package_patches pp
       WHERE pp.pv_id = nparentpvid;

      -- Get parent details
      OPEN parent_cur;

      FETCH parent_cur
       INTO parent_rec;

      parpkg_id := parent_rec.pkg_id;

      -- Find if patch exists in database
      OPEN patch_cur;

      FETCH patch_cur
       INTO patch_rec;

      -- Parent must be official
      IF parent_rec.dlocked = 'Y'
      THEN
         IF patch_cur%NOTFOUND
         THEN
            ispatchdlocked 
:= 'N';

            -- Create new patch version --
            SELECT seq_pv_id.NEXTVAL
              INTO patchpv_id
              FROM DUAL;

            split_version (snewpatchversion, ssv_mm, ssv_nmm, ssv_ext);

            INSERT INTO package_versions
                        (pv_id, pkg_id, pkg_version,
                         dlocked, created_stamp, creator_id,
                         modified_stamp, modifier_id, v_mm, v_nmm, v_ext,
                         src_path,
                         pv_description,
                         owner_id, is_patch, last_pv_id, build_type, is_build_env_required, bs_id,
						 is_autobuildable, ripple_field
                        )
                 VALUES (patchpv_id, parpkg_id, snewpatchversion,
                         ispatchdlocked, ora_sysdate, nuserid,
                         ora_sysdatetime, nuserid, ssv_mm, ssv_nmm, ssv_ext,
                         parent_rec.src_path,
                            'This is a patch to '
                         || parent_rec.pkg_name
                         || ' '
                         || parent_rec.pkg_version,
                         nuserid, 'Y', patchpv_id, 'M', 'N', 3, 'N', parent_rec.ripple_field
                        );

            INSERT INTO package_patches
                        (pv_id, patch_id, install_order)
               (SELECT nparentpvid AS pv_id, pv.pv_id AS patch_id,
                       lastinstallorder + 1 AS install_order
                  FROM package_versions pv
                 WHERE pv.pv_id = patchpv_id AND pv.is_patch = 'Y');

            /* LOG ACTION */
            log_action (patchpv_id,
                        'new_version',
                        nuserid,
                        'Patch version created: ' || snewpatchversion
                       );
            log_action (nparentpvid,
                        'patch_add',
                        nuserid,
                        'New patch created and attached: ' || snewpatchversion
                       );
         ELSE
            patchpv_id := patch_rec.pv_id;
            ispatchdlocked := patch_rec.dlocked;

            -- Find if pv_id exists in release content (i.e. it cannot be a patch)
            OPEN releases_cur;

            FETCH releases_cur
             INTO releases_rec;

            IF releases_cur%NOTFOUND
            THEN
               -- This pv_id is trully a patch, hence add Y to column IS_PATCH
               UPDATE package_versions
                  SET is_patch = 'Y'
                WHERE pv_id = patchpv_id;

               INSERT INTO package_patches
                           (pv_id, patch_id, install_order)
                  (SELECT nparentpvid AS pv_id, pv.pv_id AS patch_id,
                          lastinstallorder + 1 AS install_order
                     FROM package_versions pv
                    WHERE pv.pv_id = patchpv_id AND pv.is_patch = 'Y');
            END IF;

            CLOSE releases_cur;

            /* LOG ACTION */
            log_action (nparentpvid,
                        'patch_add',
                        nuserid,
                           'Patch version was found and attached: '
                        || snewpatchversion
                       );
         END IF;
      END IF;

      /* Create Patch Dependencies */
      opatchdepcollector := in_list_number (spatchidlist);

      -- Make sure patch is unofficial before altering its dependencies
      IF (opatchdepcollector.COUNT > 0) AND (ispatchdlocked = 'N')
      THEN
         -- Delete Existing Dependencies
         DELETE FROM package_dependencies dep
               WHERE dep.pv_id = patchpv_id;

         -- Insert new dependencies
         INSERT INTO package_dependencies
                     (pv_id, dpv_id, pkg_id, dpkg_id, build_type)
            SELECT patchpv_id AS pv_id, pv.pv_id AS dpv_id,
                   parpkg_id AS pkg_id, pv.pkg_id AS dpkg_id,
                   'L' AS build_type
              FROM package_versions pv
             WHERE pv.pv_id IN (
                      SELECT *
                        FROM TABLE
                                (CAST
                                    (opatchdepcollector AS relmgr_number_tab_t)
                                ));
      END IF;

      -- Return patch_id
      returnpatchid := patchpv_id;

      CLOSE parent_cur;

      CLOSE patch_cur;
   EXCEPTION
      WHEN DUP_VAL_ON_INDEX
      THEN
         raise_application_error (-20000,
                                     'Patch version '
                                  || snewpatchversion
                                  || ' already exist.'
                                 );
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE obsolete_patch (
      patchid            IN   NUMBER,
      isobsolete         IN   CHAR,
      obsoletecomments   IN   VARCHAR2,
      userid             IN   NUMBER
   )
   IS
   BEGIN
      -- Update patch
      UPDATE package_versions pv
         SET pv.is_obsolete = isobsolete,
             pv.obsolete_comments = obsoletecomments
       WHERE pv.pv_id = patchid;

      /*
      -- Update patch children
      UPDATE PACKAGE_VERSIONS pv SET
      pv.IS_OBSOLETE = IsObsolete,
      pv.OBSOLETE_COMMENTS = ObsoleteComments
      WHERE pv.PV_ID IN (
                     SELECT DISTINCT dep.DPV_ID
                       FROM PACKAGE_DEPENDENCIES dep
                      WHERE dep.PV_ID = PatchId
                     );


      -- Update patch parent
      UPDATE PACKAGE_VERSIONS pv SET
      pv.IS_OBSOLETE = IsObsolete,
      pv.OBSOLETE_COMMENTS = ObsoleteComments
      WHERE pv.PV_ID IN (
                     SELECT DISTINCT dep.PV_ID
                       FROM PACKAGE_DEPENDENCIES dep
                      WHERE dep.DPV_ID = PatchId
                     );

         */

      /* LOG ACTION */
      IF isobsolete IS NOT NULL
      THEN
         log_action (patchid,
                     'patch_obsolete',
                     userid,
                     'Obsolete patch. ' || obsoletecomments
                    );
      ELSE
         log_action (patchid,
                     'patch_obsolete',
                     userid,
                     'Undo patch obsolete.'
                    );
      END IF;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE obsolete_patches (
      spatchidlist       IN   VARCHAR2,
      isobsolete         IN   CHAR,
      obsoletecomments   IN   VARCHAR2,
      userid             IN   NUMBER
   )
   IS
   BEGIN
      /*--------------- Business Rules Here -------------------*/
      IF spatchidlist IS NULL
      THEN
         raise_application_error (-20000,
                                  'Please select one or more Patches.'
                                 );
      END IF;

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

      -- Update patch
      UPDATE package_versions pv
         SET pv.is_obsolete = isobsolete,
             pv.obsolete_comments = obsoletecomments
       WHERE pv.pv_id IN (
                SELECT *
                  FROM THE
                          (SELECT CAST
                                     (in_list_number (spatchidlist) AS relmgr_number_tab_t
                                     )
                             FROM DUAL
                          ));
   /*
   -- Update patch children
   UPDATE PACKAGE_VERSIONS pv SET
   pv.IS_OBSOLETE = IsObsolete,
   pv.OBSOLETE_COMMENTS = ObsoleteComments
   WHERE pv.PV_ID IN (
                  SELECT DISTINCT dep.DPV_ID
                    FROM PACKAGE_DEPENDENCIES dep
                   WHERE dep.PV_ID = PatchId
                  );


   -- Update patch parent
   UPDATE PACKAGE_VERSIONS pv SET
   pv.IS_OBSOLETE = IsObsolete,
   pv.OBSOLETE_COMMENTS = ObsoleteComments
   WHERE pv.PV_ID IN (
                  SELECT DISTINCT dep.PV_ID
                    FROM PACKAGE_DEPENDENCIES dep
                   WHERE dep.DPV_ID = PatchId
                  );

      */

   /* LOG ACTION
   IF IsObsolete IS NOT NULL THEN
         Log_Action ( PatchId, 
'patch_obsolete', UserId,
                  'Obsolete patch. '|| ObsoleteComments );
   ELSE
      Log_Action ( PatchId, 'patch_obsolete', UserId,
                  'Undo patch obsolete.' );
   END IF;  */
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE add_process (
      nprocid         IN   processes.proc_id%TYPE,
      shealthtag      IN   processes.proc_name%TYPE,
      sprocdesc       IN   processes.proc_description%TYPE,
      scmdinterface   IN   processes.run_as%TYPE,
      spkgowner       IN   processes.pkg_owner%TYPE,
      sisinterface    IN   processes.is_interface%TYPE,
      npvid           IN   package_processes.pv_id%TYPE,
      nuserid         IN   NUMBER
   )
   IS
      pkgname   VARCHAR2 (100);
/*Rupesh Release on 17/05/2006*/
   BEGIN
      INSERT INTO processes
                  (proc_id, proc_name, proc_description, run_as, pkg_owner,
                   is_interface
                  )
           VALUES (nprocid, shealthtag, sprocdesc, scmdinterface, spkgowner,
                   sisinterface
                  );

      pk_package.add_package_process (nprocid, npvid, nuserid);
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE add_package_process (
      nprocidlist   IN   VARCHAR2,
      npvid         IN   package_processes.pv_id%TYPE,
      nuserid       IN   NUMBER
   )
   IS
      processname   VARCHAR2 (4000);

/*Rupesh Release on 17/05/2006*/
      CURSOR proc_cur
      IS
         SELECT prc.proc_id
           FROM processes prc
          WHERE prc.proc_id IN (
                   SELECT *
                     FROM THE
                             (SELECT CAST
                                        (in_list_number (nprocidlist) AS relmgr_number_tab_t
                                        )
                                FROM DUAL
                             ));

      proc_rec      proc_cur%ROWTYPE;
   BEGIN
      OPEN proc_cur;

      FETCH proc_cur
       INTO proc_rec;

      WHILE proc_cur%FOUND
      LOOP
         INSERT INTO package_processes
                     (proc_id, pv_id
                     )
              VALUES (proc_rec.proc_id, npvid
                     );

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

         -- Log Action --
         log_action (npvid,
                     'process_add',
                     nuserid,
                     'Added process with health tag ' || processname
                    );

         FETCH proc_cur
          INTO proc_rec;
      END LOOP;
   END;

/*-------------------------------------------------------------------------------------------------------*/
   PROCEDURE remove_process (
      nprocid   IN   package_processes.proc_id%TYPE,
      npvid     IN   package_processes.pv_id%TYPE,
      nuserid   IN   NUMBER
   )
   IS
      processname   VARCHAR2 (4000);
/* Rupesh Release on 17/05/2006*/
   BEGIN
      SELECT prc.proc_name
        INTO processname
        FROM processes prc
       WHERE prc.proc_id = nprocid;

      DELETE FROM package_processes
            WHERE proc_id = nprocid AND pv_id = npvid;

      -- Log Action --
      log_action (npvid,
                  'process_remove',
                  nuserid,
                  'Removed process with health tag ' || processname
                 );
   END;
/*-------------------------------------------------------------------------------------------------------*/
  /* 
  Author: Rupesh Solanki
  Purpose: To move package versions from one release to another
  Release: 4th September 2006 
  
  */
  PROCEDURE move_package (
   	  npvid  			  IN package_versions.pv_id%TYPE,
	  nrtagid 			  IN release_tags.rtag_id%TYPE,
	  nnewrtagid 		  IN release_tags.rtag_id%TYPE,
	  nuserid			  IN NUMBER	
  )	
  IS	
  
  oldrtag_name VARCHAR2(4000);
  newrtag_name VARCHAR2(4000);

   
    
  BEGIN
  
  
  SELECT rtag_name into oldrtag_name
  FROM RELEASE_TAGS
  WHERE rtag_id = nrtagid;
  
  SELECT rtag_name into newrtag_name
  FROM RELEASE_TAGS
  WHERE rtag_id = nnewrtagid;
  
  /* Table Work In Progress*/
  	   UPDATE WORK_IN_PROGRESS
	   SET RTAG_ID = nnewrtagid
	   WHERE RTAG_ID = nrtagid
	   AND PV_ID = npvid;
	   
  /* Table PLANNED*/	
  	   UPDATE PLANNED
	   SET RTAG_ID = nnewrtagid
	   WHERE RTAG_ID = nrtagid
	   AND PV_ID = npvid;   
	   
      -- Log Action --
      log_action (npvid,
                  'move_package_version',
                  nuserid,
                  'Moved package version from ' || oldrtag_name || ' to ' || newrtag_name
                 );  	   	     
  
  
  END;
  																							   																										   
/*-------------------------------------------------------------------------------------------------------*/
PROCEDURE modify_product_state (
   	  npvid IN package_versions.pv_id%TYPE,
	  nrtagid IN release_tags.rtag_id%TYPE,
	  nstateid IN product_states.state_id%TYPE,
	  nuserid IN NUMBER
   ) IS
   
   sStateName VARCHAR2(4000);
   sRtagName VARCHAR2(4000);
   
  /* 
  Author: Rupesh Solanki
  Purpose: To modify the product state from integration to test to deployment
  Release: 25th January 2006 
  */   
   
  BEGIN
  
  UPDATE RELEASE_CONTENT
  SET PRODUCT_STATE = nstateid
  WHERE PV_ID = npvid
  AND RTAG_ID = nrtagid;
  
  
  SELECT STATE INTO sStateName
  FROM PRODUCT_STATES 
  WHERE STATE_ID = nstateid;
  
  SELECT RTAG_NAME into sRtagName
  FROM RELEASE_TAGS
  WHERE RTAG_ID = nrtagid;
  
  
  -- Log Action --
  log_action (npvid,
              'modify_product_state',
               nuserid,
               sStateName || ' in '|| sRtagName
              );  
			  
			  
  END;			  	   	     
/*-------------------------------------------------------------------------------------------------------*/
  PROCEDURE add_code_review_url (
        npvid           IN    NUMBER,
        nprojid         IN    NUMBER,
        surl            IN    VARCHAR2,
        sreason         IN    VARCHAR2,
        ddateofreview   IN    DATE
  ) IS
  
  ncrid NUMBER;
  
  /* 
  Author: Jeremy Tweddle
  Date: 24/Aug/2007 
  */   
  
  BEGIN
  
      SELECT seq_cr_id.NEXTVAL INTO ncrid FROM DUAL;
      
      
      INSERT INTO code_review_url ( cr_id, pv_id, proj_id, url, reason, date_of_review,
                                    last_modified )
      VALUES ( ncrid, npvid, nprojid, surl, sreason, ddateofreview, ora_sysdate );
  
  END;
/*-------------------------------------------------------------------------------------------------------*/
  PROCEDURE update_code_review_url (
        ncrid           IN    NUMBER,
        nprojid         IN    NUMBER,
        surl            IN    VARCHAR2,
        sreason         IN    VARCHAR2
  ) IS
   
  /* 
  Author: Jeremy Tweddle
  Date: 22/Aug/2007 
  */ 
  
  BEGIN
  
      UPDATE code_review_url
      SET url = surl,
          proj_id = nprojid,
          reason = sreason,
          last_modified = ora_sysdate
      WHERE cr_id = ncrid;
  
  END;
/*-------------------------------------------------------------------------------------------------------*/
  PROCEDURE remove_code_review_url (
        ncrid           IN    NUMBER
  ) IS
  
  /* 
  Author: Jeremy Tweddle
  Date: 22/Aug/2007 
  */
  
  BEGIN
  
      DELETE FROM code_review_url
      WHERE cr_id = ncrid;
  
  END;
/*-------------------------------------------------------------------------------------------------------*/
END pk_package;
/
ALTER PACKAGE "RELEASE_MANAGER"."PK_PACKAGE" 
  COMPILE BODY 
    PLSQL_OPTIMIZE_LEVEL=  2
    PLSQL_CODE_TYPE=  INTERPRETED
    PLSQL_DEBUG=  TRUE
 REUSE SETTINGS TIMESTAMP '2008-04-11 12:58:37'
/
