Subversion Repositories DevTools

Rev

Rev 7070 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erggroup.buildtool.ripple.BuildFile.BuildFileState;
import com.erggroup.buildtool.ripple.ReleaseManager.BuildReason;
import com.erggroup.buildtool.smtp.CreateUrls;
import com.erggroup.buildtool.smtp.Smtpsend;
import com.erggroup.buildtool.utilities.StringAppender;
import com.erggroup.buildtool.utilities.XmlBuilder;

/**Plans release impact by generating a set of Strings containing build file content.
 */
public class RippleEngine
{

    /**configured mail server
     * @attribute
     */
    private String mMailServer = "";

    /**configured mail sender user
     * @attribute
     */
    private String mMailSender = "";

    /**configured global email target
     * @attribute
     */
    private String mMailGlobalTarget = "";

    /** Vector of email addresses for global and project wide use
     *  Most emails will be sent to this list of email recipients
     */
    public List<String> mMailGlobalCollection = new ArrayList<String>();

    /**name associated with the baseline
     * @attribute
     */
    public String mBaselineName = "";

    /**Collection of build exceptions associated with the baseline
     * Used to determine (and report) what change in build exceptions happens as part of planRelease
     * Daemon centric
     * @aggregation shared
     * @attribute
     */
    ArrayList<BuildExclusion> mBuildExclusionCollection = new ArrayList<BuildExclusion>();

    /**Logger
     * @attribute
     */
    private static final Logger mLogger = LoggerFactory.getLogger(RippleEngine.class);

    /** Escrow information - commands to set up escrow
     * @attribute
     */
    private String mEscrowSetup;

    /** Escrow information - Raw data (May not be used)
     * @attribute
     */
    private String mEscrowRawData;

    /** Collections of packages
     */
    private ArrayList<Package> mPackageCollection = new ArrayList<Package>();
    private ArrayList<Package> mPackageCollectionWip = new ArrayList<Package>();
    private ArrayList<Package> mPackageCollectionTest = new ArrayList<Package>();
    private ArrayList<Package> mPackageCollectionRipple = new ArrayList<Package>();
    private ArrayList<Package> mPackageCollectionAll = new ArrayList<Package>();
    

    /**index to current String item
     * @attribute
     */
    private int mBuildIndex;

    /**Database abstraction
     * @attribute
     */
    ReleaseManager mReleaseManager;

    /**Baseline identifier (rtag_id for a release manager baseline, bom_id for deployment manager baseline)
     * @attribute
     */
    private int mBaseline;

    /** Escrow Only: SBOM_ID
     */
    private int mSbomId;

    /** RTAG_ID
     *  Set from mBaseline
     */
    private int mRtagId;

    /**When true, mBuildCollection contains one item based on a release manager rtag_id and contains a daemon property
     * When false, mBuildCollection contains at least one item based on a deployment manager bom_id
     * Will be accessed by the Package class to calculate its mAlias
     * @attribute
     */
    public boolean mDaemon;

    /**collection of build file content in String form
     * @attribute
     */
    private ArrayList<BuildFile> mBuildCollection = new ArrayList<BuildFile>();

    /** List of packages that we plan to build
     * Used to provide feedback into RM
     * Only the first entry is about to be built as we re-plan every cycle
     */
    private ArrayList<Package> mBuildOrder = new ArrayList  <Package>();
    /**Warning message
     * @attribute
     */
    private static final String mAnyBuildPlatforms = "Warning. The following package versions are not reproducible on any build platform: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mAnyBuildPlatformsFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mAssocBuildPlatforms = "Warning. The following package versions are not reproducible on the build platforms associated with this baseline: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mAssocBuildPlatformsFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mNotInBaseline = "Warning. The following package versions are not reproducible as they are directly dependent upon package versions not in the baseline: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mNotInBaselineFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mDependent = "Warning. The following package versions are not reproducible as they are directly/indirectly dependent upon not reproducible package versions: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mDependentFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mCircularDependency = "Warning. The following package versions are not reproducible as they have circular dependencies: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mCircularDependencyFlag = true;

    /** String used to terminate lines
     * @attribute
     */
    private static final  String mlf = System.getProperty("line.separator");

    /** XML File Prefix
     */
    private static final String mXmlHeader = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" + mlf;

    /**RippleEngine constructor
     * @param releaseManager  - Associated releaseManager instance
     * @param rtagId         - Release Identifier
     * @param isDaemon        - Mode of operation. False: Escrow, True: Daemon
     */
    public RippleEngine(ReleaseManager releaseManager, int rtagId, boolean isDaemon)
    {
        mLogger.debug("RippleEngine rtag_id {} isDaemon {}", rtagId, isDaemon);
        mReleaseManager = releaseManager;
        mBaseline = rtagId;
        mRtagId = rtagId;
        mDaemon = isDaemon;
        mReleaseManager.setDaemonMode(mDaemon);
    }

    /**
     * getRtagId
     * @return The rtagId of the Release attached to this instance of the RippleEngine
     */
    public int getRtagId()
    {
        return mRtagId;
    }

    /**Plan what is to be built
     *  <br>Discards all build file content
     *  <br>Generates new build file content
     * 
     * @param lastBuildActive           - False. Daemon Mode. The last build was a dummy. 
     *                                This planning session may not result in a build
     *                              - True. Daemon Mode. The last build was not a dummy.
     *                                There is a very good chance that this planning session
     *                                will result in a build, so it is given priority to connect 
     *                                to the database.  
     */
    public void planRelease(final boolean lastBuildActive) throws SQLException, Exception
    {
        mLogger.warn("planRelease mDaemon {}", mDaemon);

        mBuildCollection.clear();
        mPackageCollection.clear();
        mPackageCollectionRipple.clear();
        mPackageCollectionTest.clear();
        mPackageCollectionWip.clear();
        mBuildOrder.clear();
        mEscrowRawData = "";
        mEscrowSetup = "";
        Phase phase = new Phase("Plan");

        // use finally block in planRelease to ensure the connection is released
        try
        {
            phase.setPhase("connectForPlanning");
            mReleaseManager.connectForPlanning(lastBuildActive);

            if ( mDaemon )
            {
                // claim the mutex
                mLogger.warn("planRelease claimMutex");
                phase.setPhase("claimMutex");
                mReleaseManager.claimMutex();

                // Populate the mBuildExclusionCollection
                //
                // Builds are either 'Directly excluded' or 'Indirectly excluded'
                // Direct excludes result from:
                //      User request
                //      Build failure
                //      Inability to build package
                // Indirectly excluded packages result from have a build dependency on a package
                // that is directly excluded.
                //  
                // In the following code we will extract from the Database all build exclusions
                // We will then add 'Relevant' entries to mBuildExclusionCollection
                // and delete, from the database those that are no longer relevant - ie indirectly excluded
                // items where the root cause is no longer in the set.
                phase.setPhase("mBuildExclusionCollection");
                mBuildExclusionCollection.clear();
                ArrayList<BuildExclusion> tempBuildExclusionCollection = new ArrayList<BuildExclusion>();

                mLogger.debug("planRelease queryBuildExclusions");
                mReleaseManager.queryBuildExclusions(tempBuildExclusionCollection, mBaseline);

                // only populate mBuildExclusionCollection with tempBuildExclusionCollection entries which have a relevant root_pv_id
                // The entry is relevant if:
                //     It is for a directly excluded package,other than a RippleStop
                //     It is for an indirectly excluded package AND the reason for exclusion still exists
                //

                for (Iterator<BuildExclusion> it = tempBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();

                    if ( buildExclusion.isRelevant(tempBuildExclusionCollection) )
                    {
                        mBuildExclusionCollection.add(buildExclusion);
                    }
                    else
                    {
                        // Remove the indirectly excluded entry as its root cause
                        // is no longer present.
                        buildExclusion.includeToBuild(mReleaseManager, mBaseline);
                    }
                }
            }

            //-----------------------------------------------------------------------
            //  Query package versions
            //
            phase.setPhase("queryPackageVersions");
            mLogger.debug("planRelease queryPackageVersions");
            mReleaseManager.queryPackageVersions(this, mPackageCollection, mBaseline);
            mReleaseManager.queryWips(this, mPackageCollectionWip, mBaseline);
            mReleaseManager.queryTest(this, mPackageCollectionTest, mBaseline);
            mReleaseManager.queryRipples(this, mPackageCollectionRipple, mBaseline);
            mPackageCollectionAll.addAll(mPackageCollection);
            mPackageCollectionAll.addAll(mPackageCollectionWip);
            mPackageCollectionAll.addAll(mPackageCollectionTest);
            mPackageCollectionAll.addAll(mPackageCollectionRipple);
            
            // Sort the collection by PVID
            //      Unit Test output order is known
            //      May assist in creating repeatable build orders
            //
            Collections.sort(mPackageCollectionAll, Package.SeqComparator);

            //------------------------------------------------------------------------
            //    Process packages collected
            //    Determine and tag those we can't build
            phase.setPhase("processPackages");
            processPackages();

            //-----------------------------------------------------------------------
            //    At this point we have tagged all the packages that we cannot build
            //    Now we can determine what we are building
            phase.setPhase("planBuildOrder");
            mLogger.debug("planRelease process Remaining");
            planBuildOrder();

            //  Report excluded packages and the build plan
            //  This is being done with the MUTEX being held, but the 
            //  trade off is the cost of getting a connection.
            //
            if ( mDaemon )
            {
                phase.setPhase("Report Change");
                reportChange();
                phase.setPhase("Report Plan");            
                reportPlan();
            }
            
            //
            //  Generate the build Files
            //
            phase.setPhase("generateBuildFiles");
            generateBuildFiles();
        }
        finally
        {
            mLogger.debug("planRelease finally");
            // this block is executed regardless of what happens in the try block
            // even if an exception is thrown
            // ensure the SELECT FOR UPDATE is released
            try
            {
                if ( mDaemon )
                {
                    // attempt to release the SELECT FOR UPDATE through a commit
                    // a commit must be done in the normal case
                    // a commit may as well be done in the Exception case
                    // in the case of a SQLException indicating database connectivity has been lost
                    // having a go at the commit is superfluous
                    // as the SELECT FOR UPDATE will have been released upon disconnection
                    phase.setPhase("releaseMutex");
                    mReleaseManager.releaseMutex();
                }
            }
            finally
            {
                // ensure disconnect under all error conditions
                phase.setPhase("disconnectForPlanning");
                mReleaseManager.disconnectForPlanning(lastBuildActive);
            }
        }

        mLogger.warn("planRelease mDaemon {} returned", mDaemon);
        phase.setPhase("EndPlan");
    }
    
    /** Process packages that have been collected as a part of the plan
     * @param phase
     * @throws SQLException
     * @throws Exception
     */
    private void processPackages() throws SQLException, Exception {

        // Deal with test builds here as they may impact upon package attributes
        //    eg: dependency collection and build standard differences
        //    Note: Done before mPackageDependencyCollection is setup
        //
        if ( mDaemon )
        {
            // Process test builds - they are in their own collection
            for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                mLogger.info("planRelease package test build {}", p.mAlias);

                //
                //    Cannot test build an SDK based package or a Pegged Package
                //        Remove the test build request from the database
                //        Send a warning email
                //
                if(p.mIsPegged || p.mIsSdk)
                {
                    String reason;
                    reason = (p.mIsPegged) ? "Pegged" : "SDK Based";

                    mLogger.error("planRelease Daemon Instruction (testBuild) of {} package deleted: {}", reason, p.mAlias);
                    mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
                    emailRejectedDaemonInstruction("Cannot 'Test Build' a " + reason + " package",p);
                }

                // force patch for test build numbering
                p.mDirectlyPlanned = true;
                p.mChangeType.setPatch();
                p.mRequiresSourceControlInteraction = false;
                p.mBuildReason = BuildReason.Test;
                p.mIndirectlyPlanned = true;
            }
        }

        // Set up mPackageDependencyCollection on each package
        //    Examine the dependencies by alias and convert this to a 'package' selected from the released package set
        mLogger.debug("planRelease setup setPackageDependencyCollection");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
            {
                String alias = it2.next();
                Package dependency = findPackage(alias);
                p.mPackageDependencyCollection.add(dependency);
            }
        }    

        // Detect and deal with circular dependencies
        // Examine all packages under consideration
        //
        mLogger.debug("planRelease deal with circular dependencies");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( p.hasCircularDependency( this ) )
            {
                mLogger.info("planRelease circular dependency detected {}", p.mAlias);
                
                //  Force this package to be marked as having a circular dependency - even if its been excluded
                p.mBuildFile = 0;
                
                // exclude all dependent packages
                // max 50 chars
                rippleBuildExclude(p, p.mId, "Package has circular dependency", null, null, true, false);

                // take the package out of the build
                p.mBuildFile = -6;
                mLogger.info("planRelease set mBuildFile to -6 for package {}", p.mAlias );
                standardOut(mCircularDependency, p.mAlias, mCircularDependencyFlag);
                mCircularDependencyFlag = false;
            }
        }

        // Scan for packages with missing dependencies
        //    ie: The dependent package is not in the package Collection
        //        DEVI 55483 now use the fully built mPackageDependencyCollection (in rippleBuildExclude)
        mLogger.debug("planRelease use the fully built mPackageDependencyCollection");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( mDaemon )
            {
                //  Daemon Mode Only
                //  Not interested in the dependencies of a pegged package or a package provided from an SDK.
                //  Such packages will be deemed to not have missing dependencies
                if (p.mIsPegged || p.mIsSdk)
                {
                    continue;
                }
            }

            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
            {
                String alias = it2.next();
                Package dependency = findPackage(alias);

                if (dependency == ReleaseManager.NULL_PACKAGE)
                {
                    mLogger.info("planRelease dependency is not in the baseline {}", alias);
                    // exclude all dependent packages
                    // max 50 chars
                    rippleBuildExclude(p, p.mId, "Package build dependency not in the release", null, null, true, true);

                    // take the package out of the build
                    p.mBuildFile = -4;
                    mLogger.info("planRelease set mBuildFile to -4 for package {}", p.mAlias );
                    standardOut(mNotInBaseline, p.mAlias, mNotInBaselineFlag);
                    mNotInBaselineFlag = false;
                    break;
                }
            }
        }

        // Detect packages with no build standard and exclude them from the build
        mLogger.debug("planRelease process packages which are not reproducible");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if (p.mBuildFile == 0)
            {
                if ( mDaemon )
                {
                    //  Daemon Mode Only
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
                    //  Such packages will be deemed to be reproducible - but out side of this release
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }
                }

                // Does the package have a build standard. If not then we can't reproduce it.
                // Escrow - Assume the package is provided
                // Daemon - Exclude this package, but not its consumers. If the package is available then we can use it.
                if (!p.isReproducible())
                {
                    mLogger.info("planRelease package not reproducible {}" ,p.mName);
                    // max 50 chars
                    rippleBuildExclude(p, p.mId, "Package has no build environment", null, null, false, true);

                    // package is not reproducible, discard
                    p.mBuildFile = -1;
                    mLogger.info("planRelease set mBuildFile to -1 for package {}", p.mAlias );
                    standardOut(mAnyBuildPlatforms, p.mAlias, mAnyBuildPlatformsFlag);
                    mAnyBuildPlatformsFlag = false;
                }
            }
        }

        //    Process packages which are not reproducible on the configured set of build machines.
        //
        //    Test each package and determine if the package contains a buildStandard that
        //    can be processed by one of the machines in the build set
        //
        //    ie: Package: Win32:Production and we have a BM with a class of 'Win32'
        //
        //    Only exclude the failing package and not its dependents
        //    May be legitimate in the release
        //
        mLogger.debug("planRelease process packages which are not reproducible2");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if (p.mBuildFile == 0)
            {
                if ( mDaemon )
                {
                    //  Daemon Mode Only
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
                    //  Such packages will be deemed to be reproducible - but out side of this release
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }
                }

                // package has yet to be processed
                // assume it does not need to be reproduced for this baseline
                //
                //    For each machineClass in the buildset
                boolean reproduce = false;

                for (Iterator<String> it2 = mReleaseManager.mReleaseConfigCollection.mMachineClasses.iterator(); it2.hasNext(); )
                {
                    String machineClass = it2.next();

                    if ( p.canBeBuildby(machineClass))
                    {
                        reproduce = true;
                        mLogger.info("planRelease package built on {} {}", machineClass, p.mAlias );
                        break;
                    }
                }

                if ( !reproduce )
                {
                    mLogger.info("planRelease package not reproducible on the build platforms configured for this baseline {}", p.mName);

                    // max 50 chars
                    rippleBuildExclude(p, p.mId, "Package not built for configured platforms", null, null, false, true);

                    // package is not reproducible on the build platforms configured for this baseline, discard
                    p.mBuildFile = -2;
                    mLogger.info("planRelease set mBuildFile to -2 for package {}", p.mAlias );
                    standardOut(mAssocBuildPlatforms, p.mAlias, mAssocBuildPlatformsFlag);
                    mAssocBuildPlatformsFlag = false;
                }
            }
        }      

        if (mDaemon)
        {
            // Daemon Mode Only
            // Process packages which are not ripple buildable, and all packages dependent upon them
            //      I would have thought that all consumer packages had already been excluded ??
            mLogger.debug("planRelease process packages which are not ripple buildable");
            for (ListIterator<BuildExclusion> it = mBuildExclusionCollection.listIterator(); it.hasNext(); )
            {
                BuildExclusion be = it.next();

                for (Iterator<Package> it1 = mPackageCollectionAll.iterator(); it1.hasNext(); )
                {
                    Package p = it1.next();

                    // ensure only root cause, non test build, build exclusions are excluded
                    // mBuildExclusionCollection is at this point based on
                    // relevant (direct and indirect) excluded pv's in the database
                    if ( be.compare(p.mId) && be.isARootCause() && p.mTestBuildInstruction == 0 && p.mForcedRippleInstruction == 0 )
                    {
                        // package is not reproducible, discard

                        rippleBuildExclude( p, p.mId, null, it, be, true, true );
                        p.mBuildFile = -3;
                        mLogger.info("planRelease set mBuildFile to -3 for package {}", p.mAlias );
                        break;
                    }
                }
            }

            //  Daemon Mode Only
            //  Process packages which need to be ripple built
            mLogger.debug("planRelease process packages which need to be ripple built");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    //  Not interested in a pegged package or a package provided from an SDK.
                    //  Such packages are not rippled
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }

                    //    Examine this packages dependencies
                    //    If one of them does not exist in the 'official release' set then
                    //    the package needs to be built against the official release set
                    //    and we can't build any of its dependent packages
                    Iterator<Integer> it2 = p.mDependencyIDCollection.iterator();
                    Iterator<Package> it3 = p.mPackageDependencyCollection.iterator();
                    while ( it2.hasNext() && it3.hasNext() )
                    {
                        Integer dpvId = it2.next();
                        Package dependency = it3.next();

                        if ( !dependency.mAdvisoryRipple )
                        {
                            // not advisory, ie: has ripple build impact
                            if ( !isInRelease(dpvId) )
                            {
                                // the package is out of date
                                // exclude all dependent package versions
                                mLogger.info("planRelease package out of date {}", p.mName);
                                p.mBuildReason = BuildReason.Ripple;
                                rippleIndirectlyPlanned(p);
                                
                                //  This package needs to be rippled
                                //  If this package has a rippleStop marker of 's', then we cannot
                                //  build this package at the moment.
                                //  Packages that depend on this package have been excluded
                                //  We need to exclude this one too
                                //
                                if (p.mRippleStop == 's' || p.mRippleStop == 'w') 
                                {
                                    // Package marked as a rippleStop
                                    // max 50 chars
                                    rippleBuildExclude(p, -2, "Ripple Required." + " Waiting for user", null, null, false, true);

                                    // package is unBuildable, mark reason and discard
                                    p.mBuildFile = -11;
                                    
                                    if (p.mRippleStop == 's' ) {
                                        // Need to flag to users that the package build is waiting user action
                                        mLogger.info("planRelease Ripple Required. Stopped by flag {}", p.mName);
                                        mReleaseManager.setRippleStopWait(mRtagId,p);
                                    }
                                }
                                
                                break;
                            }
                        }
                    }
                }
            }

            //  Daemon Mode Only
            //  Process packages which do not exist in the archive
            //  For unit test purposes, assume all packages exist in the archive if released
            mLogger.debug("planRelease process packages which do not exist in the archive");
            if ( mReleaseManager.mUseDatabase )
            {
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if (p.mBuildFile == 0)
                    {
                        // package has yet to be processed
                        if (!p.mDirectlyPlanned && !p.mIndirectlyPlanned && p.mForcedRippleInstruction == 0)
                        {
                            // check package version archive existence
                            if (!p.existsInDpkgArchive())
                            {
                                if (! p.mIsBuildable)
                                {
                                    //  Package does not exist in dpkg_archive and it has been flagged as unbuildable
                                    //  This may be because is Unbuildable or Manually built
                                    mLogger.info("planRelease Unbuildable package not found in archive {}", p.mName);
                                    // max 50 chars
                                    rippleBuildExclude(p, p.mId, "Unbuildable" + " package not found in archive", null, null, true, true);

                                    // package is unBuildable, mark reason and discard
                                    p.mBuildFile = -10;
                                    
                                }
                                //  Not interested in a pegged package or a package provided from an SDK.
                                //  Such packages are not rippled
                                else if (p.mIsPegged || p.mIsSdk)
                                {
                                    String reason;
                                    reason = (p.mIsPegged) ? "Pegged" : "SDK";

                                    //  Pegged packages or packages provided from an SDK MUST exist in dpkg_archive
                                    //  They will not be built within the context of this release. It is the responsibility
                                    //  of another release to build them.
                                    mLogger.info("planRelease {} package not found in archive {}", reason, p.mName);
                                    // max 50 chars
                                    rippleBuildExclude(p, p.mId, reason + " package not found in archive", null, null, true, true);

                                    // package is not reproducible, mark reason and discard
                                    p.mBuildFile = -7;

                                }
                                else if (p.mForcedRippleInstruction == 0)
                                {
                                    //  [JATS-331] Unable to rebuild package with Advisory Ripple dependencies
                                    //    Examine this packages dependencies
                                    //    If one of them does not exist in the 'official release' set then
                                    //    the package cannot be rebuilt in this release.
                                    for (Iterator<Integer> it2 = p.mDependencyIDCollection.iterator() ; it2.hasNext() ;)
                                    {
                                        Integer dpvId = it2.next();
                                        if ( !isInRelease(dpvId) )
                                        {
                                            // This package cannot be rebuilt as one of its dependents is NOT in this release
                                            // exclude all dependent package versions
                                            
                                            mLogger.info("planRelease package not found in archive. Cannot be rebuilt due to {}", p.mName);
                                            // max 50 chars
                                            rippleBuildExclude(p, p.mId, "Package cannot be rebuilt in this release", null, null, true, true);

                                            // package is not reproducible, mark reason and discard
                                            p.mBuildFile = -4;
                                            break;
                                        }
                                    }
                                    
                                    //  The package has not been excluded from the build
                                    if (p.mBuildFile == 0)
                                    {
                                        mLogger.info("planRelease package not found in archive {}", p.mName);
                                        // DEVI 47395 the cause of this build is not WIP or ripple induced,
                                        // it simply does not exist in the archive (has been removed)
                                        // prevent source control interaction
                                        p.mRequiresSourceControlInteraction = false;
                                        p.mBuildReason = BuildReason.Restore;
                                        rippleIndirectlyPlanned(p);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //  Daemon Mode Only
            //  Detect bad forced ripples requests and reject them
            mLogger.debug("planRelease process forced ripples");
            for (Iterator<Package> it = mPackageCollectionRipple.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    //
                    //    Cannot force a ripple on an SDK based package or a Pegged Package
                    //        Remove the daemon instruction from the database
                    //        Send a warning email
                    //
                    if(p.mIsPegged || p.mIsSdk)
                    {
                        String reason;
                        reason = (p.mIsPegged) ? "Pegged" : "SDK Based";

                        mLogger.error("planRelease Daemon Instruction of {} package deleted: {}", reason, p.mName);
                        mReleaseManager.markDaemonInstCompleted( p.mForcedRippleInstruction );
                        emailRejectedDaemonInstruction("Cannot 'Ripple' a " + reason + " package",p);

                        p.mBuildFile = -8; 
                    }
                }
            }

            //  Daemon Mode Only
            //  Mark Pegged and SDK packages as not to be built
            //  Have previously detected conflicts between pegged/sdk packages and daemon instructions
            //
            mLogger.debug("planRelease remove pegged and SDK packages from the build set");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    //  Not interested in a pegged package or a package provided from an SDK.
                    //  Such packages are not built
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        String reason;
                        reason = (p.mIsPegged) ? "Pegged" : "SDK";

                        mLogger.info("planRelease {} not built in this release {}", reason, p.mName);
                        p.mBuildFile = -8;
                    }
                }
            }

            //  Daemon Mode Only
            //  Locate Test Build Requests that cannot be satisfied - will not be built due to
            //  errors in dependent packages. Report the error to the user and remove the request
            //
            for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile < 0)
                {
                    String reason;
                    switch (p.mBuildFile)
                    {
                    case -1:  reason = "Not reproducible"; break;
                    case -2:  reason = "Not reproducible on configured build platforms"; break;
                    case -3:  reason = "Marked as 'Do not ripple'"; break;
                    case -4:  reason = "Dependent on a package not in the release"; break;
                    case -5:  reason = "Indirectly dependent on a package not reproducible in the release"; break;
                    case -6:  reason = "Has a circular dependency"; break;
                    case -7:  reason = "Pegged or SDK package not in dpkg_archive"; break;
                    case -8:  reason = "Is a Pegged or SDK package"; break;
                    case -9:  reason = "Rejected Daemon Instruction"; break;
                    case -10: reason = "Unbuildable package not in dpkg_archive"; break;
                    case -11: reason = "Marked as 'RippleStop'"; break;
                    default:  reason = "Unknown reason. Code:" + p.mBuildFile; break;
                    }
                    mLogger.error("planRelease Test Build of an unbuildable of package deleted: {}", p.mName);
                    mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
                    emailRejectedDaemonInstruction(reason,p);
                }
            }
            
        }
        else
        {
            // escrow reporting only
            // Report packages that are not reproducible
            //
            //  Note: I don't believe this code can be executed
            //        The -3 is only set in daemon mode
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == -3)
                {
                    standardOut(mDependent, p.mAlias, mDependentFlag);
                    mDependentFlag = false;
                }
            }
        }
    }

    /** Plan the build order.
     *  Assumes that a great deal of work has been done.
     *  This is a stand alone method to contain the work
     *  
     * @throws Exception
     * @throws SQLException
     */
    private void planBuildOrder() throws Exception, SQLException 
    {
        
//TODO - Planning is not working well
/**
 * Current status
 * The selection of the package to build next is deeply flawed
 * Currently it will select a directly planned package over a ripple - (perhaps that is good)
 * 
 * Currently on the buildPlan of the 'selected' package is included in the BuildOrder
 * 
 * The current algorithm is based on time - but all packages have zero build time ( in UTF )
 * Even if this is fixed the algorithm is broken as we will select the complete build with the shortest time
 * which will be the ripple of a leaf package ( perhaps that is good )
 * 
 * Also:
 *      [Done] Need to include all packages that we can now build into the final build plan, not just the first one and its build tree
 * 
 *      [Done-ish]Need to generate a single package set - so that WIPS and others replace those in the final build set
 *      [Done]Remove duplicates from the complete package set
 *      
 *      [Being Done] Need more test cases
 *      
 *      [Done, in the UTF] Need to report all build options, not just the selected one
 *                         Data to RM may need some more info
 *      
 *      Cleanup the ANT build file - don't list all the release dependencies, just those needed to build the current package
 *      Chances are - all we need is the new package version number
 *      
 *      [Fixed] If a WIP on a package that has circular dependencies is added, then the WIP will be excluded. This prevents the problem being fixed.
 *              Should not exclude a WIP of a package with a circular dependency
 */

        // Process remaining packages which are need to be reproduced for this baseline.
        //    Determine the build file for each package
        //    For daemon builds:
        //      Determine the next package that can be built now
        //          Sounds simple - doesn't it
        //      Set its mBuildNumber to 1, all remaining reproducible packages to 2
        //    For escrow builds:
        //      Determine the package versions that can be built in the build iteration
        //      Set their mBuildNumber to the build iteration
        //      Increment the build iteration and repeat until all package versions 
        //      that need to be reproduced have been assigned a build iteration        
        
        boolean allProcessed = false;
        int buildFile = 1;

        do
        {
            //
            //  Create the mBuildOrder collection
            //      Same for both Daemon and Escrow modes
            //      Will have package build order and build level
            boolean allDependenciesProcessed = true;
            do
            {
                allDependenciesProcessed = true;
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mBuildFile < 0  ) 
                    {
                        // Daemon: Flag packages that cannot be built
                        // Escrow: Flag packages with a foreign build environment as processed
                        p.mProcessed = true;
                        mLogger.info("planRelease package not buildable {}", p.mName);            
                    }
                    else if ( p.mBuildFile == 0 )
                    {
                        // package yet to be processed and
                        //  Daemon mode: Could be built
                        //  Escrow mode: Can be reproduced
                        boolean canBeBuiltNow = true;
                        boolean allDependenciesForThisPackageProcessed = true;

                        //  Scan the packages dependencies
                        for ( Iterator<Package> it2 = p.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                        {
                            Package dependency = it2.next();

                            if ( !dependency.mProcessed )
                            {
                                //  If all the dependencies have been processed, then we can potentially build this package
                                //  If any of them have not been processed, then cannot calculate canBeBuiltNow until this 
                                //  dependency has been processed
                                allDependenciesForThisPackageProcessed = false;
                                allDependenciesProcessed = false;
                            }
                            else if (  (  mDaemon && (   dependency.mBuildFile == 0 ) ) 
                                    || ( !mDaemon && ( ( dependency.mBuildFile == 0 ) ||
                                                           ( dependency.mBuildFile == buildFile &&
                                                           ( !p.haveSameBuildStandards(dependency)  ) ) ) ) )
                            {
                                // Daemon mode:
                                //    This processed dependency has not been assigned to a build iteration - so can't built yet
                                // Escrow mode: This package cannot be built now if
                                //    This processed dependency has not been assigned to a build iteration or
                                //    This processed dependency has been assigned to this build iteration, but the 
                                //    build standards of the package and this dependency prevent the package being
                                //    built in this iteration.   
                                canBeBuiltNow = false;
                                mLogger.info("planRelease package cannot be built in this iteration {}", p.mName);
                                break;
                            }
                        }

                        //  All dependencies have been processed. May be able to build this package
                        //      Add the package to the build order list
                        //
                        if (allDependenciesForThisPackageProcessed)
                        {
                            p.mProcessed = true;
                            if ( canBeBuiltNow )
                            {
                                mBuildOrder.add(p);
                                p.mBuildFile = buildFile;
                                mLogger.info("planRelease set mBuildFile to {} for package {}",buildFile, p.mAlias );
                            }
                        }
                    }
                }
            } while( !allDependenciesProcessed );
            
            //  Have process all the packages that we can at the moment
            //      Examine all the packages in the collection to see if there are more that need to be extracted
            //      These will be done at a different build-level
            //  
            allProcessed = true;
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                if ( p.mBuildFile == 0 )
                {
                    // more build files are required
                    allProcessed = false;
                    mLogger.info("planRelease more build files are required for {}", p.mName);
                    break;
                }
            }

            buildFile++;
            if (buildFile % 500 == 0 ) {
                mLogger.error("planRelease. Too many build levels {}", buildFile);
            }
        } while( !allProcessed );
            

        if ( mDaemon )
        {

            //
            //  Daemon Mode:
            //

            //  Create a list of build candidates
            //
            //  Add in the WIPs, Tests and Ripple Requests so that we can process them as one collections
            //  Now have a collection of packages that we would like to build right now
            //  See if any of them can be excluded due to issues with creating a new package version
            ArrayList<Package> buildCandidates = new ArrayList<Package>();
            buildCandidates.addAll(mPackageCollectionWip);
            buildCandidates.addAll(mPackageCollectionTest);
            buildCandidates.addAll(mPackageCollectionRipple);

            //  Locate all packages that we can build now
            //      Packages that have a build requirement and have no reason we cannot build them

            for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                if ( p.mProcessed && p.mBuildFile > 0 )
                {
                    if (p.mBuildReason != null)
                    {
                        // Have a reason to build this package
                        buildCandidates.add(p);
                        mLogger.info("planRelease Candidate {}",p.mAlias);
                    }
                }
            }
            
            //  Examine the build candidates and verify that a new version number can be calculated for each
            //  version that we need to build.
            //
            //  If we can't generate a new version number, then this is considered to be a build failure
            //  The package will be excluded and the user will be emailed
            //
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile >= 0 )
                {
                    int pvApplied = p.applyPV(mReleaseManager);

                    if ( pvApplied == 1 )
                    {
                        // max 50 chars
                        rippleBuildExclude(p, p.mId, "Package has non standard versioning", null, null, true, true);
                    }
                    else if ( pvApplied == 2 )
                    {
                        // max 50 chars
                        rippleBuildExclude(p, p.mId, "Package has reached ripple field limitations", null, null, true, true);
                    }
                    else if ( pvApplied == 3 )
                    {
                        // max 50 chars
                        rippleBuildExclude(p, p.mId, "Package has invalid change type", null, null, true, true);
                    }
                    
                    if ( pvApplied != 0) 
                    {
                        // If this package is not a WIP/TEST/RIPPLE, then remove it from this collection
                        // Want to keep all WIP/TEST/RIPPLE items to simplify reporting
                        if (! p.mIsNotReleased )
                        {
                            it.remove();
                        }
                    }
                    
                    //
                    //  Ensure that the mRippleTime is known for each package
                    //  Collection includes: naturalRipples, ForcedRipples, TestBuilds and NewVersions
                    if ( pvApplied == 0)
                    {
                        if (p.mTestBuildInstruction != 0)
                        {
                            // Test Build - there is no ripple effect
                            // The time impact will be the estimated build time of this one package
                            p.mRippleTime = p.mBuildTime;
                            
                        }
                        else 
                        {
                            //  Calculate the time to build the package AND all of the packages that depend on it
                            //  Calculate the build plan for the package
                            //
                            calcBuildPlan(p, mPackageCollection);
                        }
                    }
                }
            }
            
            //  Determine the first test build package
            //  Assume the collection is in build instruction id order, 
            //      So that the first request will be built first
            //  Add all buildable test-builds to the mBuildOrder collection 
            Package testBuild = ReleaseManager.NULL_PACKAGE;
            mBuildOrder.clear();

            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
            {
                
                Package p = it.next();
                if ( p.mBuildFile >= 0 )
                {
                    if (p.mTestBuildInstruction != 0)
                    {
                        //  Not a Test Build
                        if (testBuild == ReleaseManager.NULL_PACKAGE)
                        {
                            testBuild = p;
                        }
                        mBuildOrder.add(p);
                    }
                }
            }
            
            //
            //  Determine the NON-test packages the we will build next
            //  This will be the one with the lowest ripple time
            //
            Package build = ReleaseManager.NULL_PACKAGE;
            ArrayList<Package> buildNext = new ArrayList<Package>();
            
            //  Extract candidates that are left
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                if ( p.mBuildFile >= 0  &&p.mTestBuildInstruction == 0)
                {
                    buildNext.add(p);
                }
            }
            
            if ( !buildNext.isEmpty() )
            {
                //  Sort on mRippleTime so that
                //      We can get the lowest first
                //      We can generate the complete plan in a nice order
                //
                Collections.sort(buildNext, Package.RippleTimeComparator);
                
                //  At the moment extract the first one - this is the fastest
                //  Allowing for a 20% extension.
                //
                //  TODO - what if two build sets don't overlap. Do we build the longer one first ?
                //  TODO - what if two build sets do overlay. Can we merge them somehow
                //
                build = buildNext.get(0);
                
                //  Determine the amount of time we would allow the 'best' plan to be extended
                //      Allow for a 20% extension in time
                //      Minimum of 10 minutes
                int baseTime = build.mRippleTime;
                baseTime = ( baseTime * 1200) / 1000;
                if ( baseTime < 10 * 60) {
                    baseTime = (10 * 60);
                }
                mLogger.error("Scan for package to build: {} {}", build.mRippleTime, baseTime);

                //  Find last package that does not exceed the extended ripple time
                for (Iterator<Package> it = buildNext.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if (p.mRippleTime > baseTime)
                    {
                        break;
                    }
                    build = p;
                }

                //
                //  Extend mBuildOrder with the selected non-test build package and its build plan
                //  Really only for display purposes
                //
                Package.resetProcessed(buildNext);
                mBuildOrder.add(build);
                updateBuildList(build.mRipplePlan, buildNext);
                mBuildOrder.addAll(build.mRipplePlan);
                
                //  Add in other build candidates
                for (Iterator<Package> it = buildNext.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if ( p.mBuildFile >= 0 )
                    {
                        if ( p != build && ! p.mIsProcessed)
                        {
                            //  Not our selected package and not already processed
                            mBuildOrder.add(p);
                            updateBuildList(p.mRipplePlan, buildNext);
                            mBuildOrder.addAll(p.mRipplePlan);
                        }
                    }
                }
            }
            
            //
            //  Mark the first package in the build order as the one to be built
            //  This will give test builds priority, while indicating what will be built after that
            //
            
            if (!mBuildOrder.isEmpty()) {
                build = mBuildOrder.get(0);
                build.mBuildFile = 1;
                
                if ( build.mForcedRippleInstruction > 0 )
                {
                    mReleaseManager.markDaemonInstCompleted( build.mForcedRippleInstruction );
                }

                if ( build.mTestBuildInstruction > 0 )
                {
                    mReleaseManager.markDaemonInstInProgress( build.mTestBuildInstruction );
                }
                
                //  Now that we know which package we are building
                //      Set the previously calculated nextVersion as the packages version number
                //      Claim the version number to prevent other builds from using it. Even if doing a test build
                //
                if (build.mNextVersion != null)
                {
                    mReleaseManager.claimVersion(build.mPid, build.mNextVersion + build.mExtension, mBaseline);
                    build.mVersion = build.mNextVersion;
                }
            }
            
            //
            //  Massage the package collection
            //      Replace WIP/TEST/RIPPLE so that the mPackageCollection is a collection of packages we would like in the release
            //      Don't replace those that we can't build due to errors
            //
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                if ( p.mBuildFile >= 0 )
                {
                    Package pReleased = findPackage(p.mAlias);
                    if ( pReleased != ReleaseManager.NULL_PACKAGE)
                    {
                        int index = mReleaseManager.findPackageLastIndex;
                        mPackageCollection.set(index, p);
                        p.mIsNotReleased = false;
                    }
                }
            }
                        
            
            //
            //  To fit in with the old algorithm ( ie: could be improved )
            //  Insert marks into all packages
            //  Not sure exactly why - Its used in the generation of the ant build file
            //                     Want to set mNoBuildReason, mBuildFile
            //
            //      Package we have selected to build: 0     , 1
            //      Package we could have built      : 0     , 2
            //      Packages we can't build          : reason, 3
            //      Packages that are OK             : 3     , 3
            //      ????                             : 0     , 3
            
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();
                if (p == build ) {
                    p.mNoBuildReason = 0;
                    p.mBuildFile = 1;
                } 
                else if ( p.mBuildFile < 0 )
                {
                    p.mNoBuildReason = p.mBuildFile;
                    p.mBuildFile = 3;
                }
                else if (p.mBuildReason != null)
                {
                    p.mNoBuildReason = 0;
                    p.mBuildFile = 2;
                }
                else
                {
                    p.mNoBuildReason = 0;
                    p.mBuildFile = 3;
                }
            }

        }

    }

    /** Update a collection of packages such that the collection contains packages from the WIP/RIPPLE collection
     *  in preference to the package in the main collection
     *  
     * @param packageList   - List to process and update
     * @param repList - List of replacement candidates. Assumes valid packages
     */
    private void updateBuildList(ArrayList<Package> packageList, ArrayList<Package> repList ) {

        int baseIndex = 0;
        for (Iterator<Package> it = packageList.iterator(); it.hasNext(); baseIndex++)
        {
            Package basePkg = it.next();

            for (Iterator<Package> repit = repList.iterator(); repit.hasNext(); )
            {
                Package repPkg = repit.next();
                if ( basePkg.mAlias.compareTo( repPkg.mAlias ) == 0 )                    
                {
                    //  Replacement package has been found
                    //  Only looking for non-test packages that can be built
                    //  Flag that the package is now a part of the official release set
                    //  Flag that the package has been processed to prevent it being included in a future plan
                    
                    packageList.set(baseIndex, repPkg);
                    repPkg.mIsNotReleased = false;
                    repPkg.mIsProcessed = true;
                    break;
                    
                }
            }
        }
    }

    /**
     * Calculate a build plan for the specified package
     * Calculates : mRippleTime - time to build the package and the ripple effect
     * Calculates : mBuildPlan - collection of packages in the plan that can be built at the moment
     * 
     * Needs to determine all packages that consume the package - and all packages that consume them
     *  Note: The package may not be in the mPackageCollection ( it may be a WIP/TEST/RIPPLE )
     *  
     * Note: The rippleTime of PackageA is not the sum of the rippleTimes of the packages that consume PackageA.
     *  
     * Need to:
     *      Only include a package once.
     *      Only include packages that can/need to be built at the moment
     * 
     * @param p                 - Package to process
     * @param pkgCollection     - Package Collection to scan

     */
    private void calcBuildPlan(Package p, ArrayList<Package> pkgCollection) {
        

        p.mRipplePlan = new ArrayList<Package>(); 
        p.mRippleTime = p.mBuildTime;
        
        //  Need to add elements to the end of the list while processing
        //  Sum the buildTimes of the packages that we add to the list
        ArrayList<Package> toProcess = usedByPackages(p, pkgCollection, 1);
        while ( ! toProcess.isEmpty())
        {
            Package pkg = toProcess.remove(0);
            if ( ! p.mRipplePlan.contains( pkg ))
            {
                p.mRipplePlan.add(pkg);
                p.mRippleTime += pkg.mBuildTime;
            }
            
            ArrayList<Package> usedBy = usedByPackages(pkg, pkgCollection, pkg.mRippleOrder + 1);
            toProcess.addAll(usedBy);
        }
        
        //
        //  Need to determine the rippleTime for each package in the plan
        //      Have a much smaller set of packages to process (need only scan those in p.mRipplePlan)
        //  Similar to the above code, but 
        //      Don't care about the mRipplePlan item in the packages
        //      Don't modify the mRippleOrder
        //
        for (ListIterator<Package> it = p.mRipplePlan.listIterator(); it.hasNext(); )
        {
            Package p1 = it.next();
            
            int rippleTime = 0;
            ArrayList<Package> ripplePlan = new ArrayList<Package>();
            ArrayList<Package> toProcess1 = new ArrayList<Package>();
            
            toProcess1.add(p1);
            while ( ! toProcess1.isEmpty())
            {
                Package pkg = toProcess1.remove(0);
                if ( ! ripplePlan.contains( pkg ))
                {
                    ripplePlan.add(pkg);
                    rippleTime += pkg.mBuildTime;
                }
                
                ArrayList<Package> usedBy = usedByPackages(pkg, p.mRipplePlan, -1);
                toProcess1.addAll(usedBy);
            }
            
            p1.mRippleTime = rippleTime;
        }
        
        
        //
        //  Sort the ripplePlan so that we build the fastest elements first
        //  Only care about the ripplePlan for the main Package
        //
        Collections.sort(p.mRipplePlan, Package.PlanComparator);
        
        //  Debugging
        mLogger.error("calcBuildPlan. Pkg:{} pvid:{}, Order:{} RippleT:{} BuildT:{}", p.mAlias, p.mId, p.mRippleOrder, p.mRippleTime, p.mBuildTime);
        for (ListIterator<Package> it = p.mRipplePlan.listIterator(); it.hasNext(); )
        {
            Package pkg = it.next();
            mLogger.error("calcBuildPlan. Plan: Pkg:{} pvid:{}, Order:{} RippleT:{} BuildT:{}", pkg.mAlias, pkg.mId, pkg.mRippleOrder, pkg.mRippleTime, pkg.mBuildTime);
        }
    }

    /** 
     *  Calculate a collection of packages that actively use the named package
     *  A consumer package does NOT use the named package,
     *      If the consumer is an SDK or is Pegged
     *      If the consumer is marked as advisoryRipple
     *      If the consumer cannot be built
     *   
     * @param pkg - Package to process
     * @param pkgCollection - collection of packages to scan
     * @param rippleOrder - Set the ripple oder, if >= 0
     * @return A collection of packages that actively 'use' the specified package

     */
    private ArrayList<Package> usedByPackages(Package pkg, ArrayList<Package> pkgCollection, int rippleOrder) {
        
        ArrayList<Package> usedBy = new ArrayList<Package>();
        
        for (Iterator<Package> it = pkgCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();
            
            //  Is this package 'actively used' in the current build
            if (p.mBuildFile >= 0)
            {
                for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
                {
                    String alias = it2.next();
                    if (  pkg.mAlias.compareTo( alias ) == 0  ) {
                        //  Have found a consumer of 'pkg'
                        usedBy.add(p);
                        if (rippleOrder >= 0)
                            p.mRippleOrder = rippleOrder;
                        break;
                    }
                }
            }
        }
        
        return usedBy;
    }

    /** Determine if a given PVID is a member of the current release.
     *  Used to determine if a package dependency is out of date
     * 
     * @param dpvId
     * @return true - specified pvid is a full member of the Release
     */
    private boolean isInRelease(Integer dpvId) {
        
        boolean inRelease = false;

        for ( Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( p.mId == dpvId )
            {
                inRelease = ! p.mIsNotReleased;
                break;
            }
        }
        return inRelease;
    }


    /**reports what change in build exceptions happens as part of planRelease
     */
    public void reportChange() throws SQLException, Exception
    {
        int counter = 0;
        for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
        {
            BuildExclusion buildExclusion = it.next();

            if ( !buildExclusion.isProcessed() )
            {
                if (buildExclusion.isImported() && ! buildExclusion.isARootCause() ) {
                    // Remove from the exclusion list
                    buildExclusion.includeToBuild(mReleaseManager, mBaseline);
                    mLogger.error("reportChange remove unused exclusion: {}", buildExclusion.info());
                } else {
                    // Exclude and notify
                    buildExclusion.excludeFromBuild(mReleaseManager, mBaseline);
                    buildExclusion.email(this, mPackageCollectionAll);
                    counter++;
                }
            }
        }
        mLogger.error("reportChange exclusion count: {}", counter);
    }

    /**reports the build plan
     */
    public void reportPlan() throws SQLException, Exception
    {
        mReleaseManager.reportPlan(mRtagId, mBuildOrder);
    }

    /**Returns the first build file from the collection
     * The build file will be flagged as empty if none exists
     */
    public BuildFile getFirstBuildFileContent()
    {
        mLogger.debug("getFirstBuildFileContent");

        mBuildIndex = -1;
        return getNextBuildFileContent();
    }

    /**Returns next build file from the collection
     * The build file will be flagged as empty if none exists
     */
    public BuildFile getNextBuildFileContent()
    {
        mLogger.debug("getNextBuildFileContent");
        BuildFile retVal = null;

        try
        {
            mBuildIndex++;
            retVal = mBuildCollection.get( mBuildIndex );
        }
        catch( IndexOutOfBoundsException e )
        {
            // Ignore exception. retVal is still null.
        }

        if (retVal == null)
        {
            retVal = new BuildFile();
        }

        mLogger.debug("getNextBuildFileContent returned {}", retVal.state.name() );
        return retVal;
    }


    /**collects meta data associated with the baseline
     * this is sufficient to send an indefinite pause email notification 
     *  
     * Escrow: Used once to collect information about the SBOM and associated Release 
     *         mBaseline is an SBOMID 
     * Daemon: Used each build cycle to refresh the information 
     *          mBaseline is an RTAGID
     */
    public void collectMetaData() throws SQLException, Exception
    {
        Phase phase = new Phase("cmd");
        mLogger.debug("collectMetaData mDaemon {}", mDaemon);

        try
        {
            phase.setPhase("connect");
            mReleaseManager.connect();

            if (! mDaemon)
            {
                mSbomId = mBaseline;
                phase.setPhase("queryRtagIdForSbom");
                mRtagId = mReleaseManager.queryRtagIdForSbom(mBaseline);
                if (mRtagId == 0)
                {
                    mLogger.error("SBOM does not have a matching Release. Cannot be used as a base for an Escrow"); 
                    throw new Exception("rtagIdForSbom show stopper. SBOM does not have an associated Release");
                }
            }

            phase.setPhase("queryReleaseConfig");
            mReleaseManager.queryReleaseConfig(mRtagId);

            if (mDaemon)
            {
                phase.setPhase("queryMailServer");
                setMailServer(mReleaseManager.queryMailServer());
                phase.setPhase("queryMailSender");
                setMailSender(mReleaseManager.queryMailSender());
                phase.setPhase("queryGlobalAddresses");
                setMailGlobalTarget(mReleaseManager.queryGlobalAddresses());
                phase.setPhase("queryProjectEmail");
                mMailGlobalCollection = mReleaseManager.queryProjectEmail(mBaseline);
                phase.setPhase("mMailGlobalTarget");
                mMailGlobalCollection.add(0,getMailGlobalTarget());
            }
            phase.setPhase("queryBaselineName");
            mBaselineName = mReleaseManager.queryBaselineName(mBaseline);
            phase.setPhase("Done");
        }
        finally
        {
            // this block is executed regardless of what happens in the try block
            // even if an exception is thrown
            // ensure disconnect
            mReleaseManager.disconnect();
        }
    }

    /**
     * Find Package by package alias
     * Searches the released package collection
     * @param   alias               - alias of package to locate
     * @return  Package with the matching mAlias or NULL_PACKAGE if no package has the mAlias
     */
    public Package findPackage(String alias)
    {
        mLogger.debug("findPackage");

        Package retVal = mReleaseManager.findPackage(alias, mPackageCollection);

        mLogger.info("findPackage returned {}", retVal.mName);
        return retVal;
    }
    
    /**
     * Sets the mBuildFile to -5 (indirectly dependent on package versions which are not reproducible) 
     * for the package and all dependent packages that are a part of the release set.
     *  
     * @param p             The package being excluded 
     * @param rootPvId      The PVID of the package that is causing the exclusion. Null or -ve values are special
     *                      This package is the root cause, -2: Excluded by Ripple Stop 
     * @param rootCause     Text message. Max 50 characters imposed by RM database 
     * @param list          If not null, add build exclusion to specified list, else use default(mBuildExclusionCollection) list. Allows insertions while iterating the list.
     * @param be            If not null, process provided BuildExclusion element, otherwise find-existing/create a BuildExclusion element
     * @param dependentToo  True: Exclude all packages that depend on the excluded package 
     * @param depWipsToo    False: Don't exclude WIP packages that depend on the excluded package
     */
    private void rippleBuildExclude(Package p, int rootPvId, String rootCause, ListIterator<BuildExclusion> list, BuildExclusion be, boolean dependentToo, boolean depWipsToo )
    {
        mLogger.debug("rippleBuildExclude");
     
        if ( p.mBuildFile == 0 || p.mBuildFile == 1 )
        {
            p.mBuildFile = -5;
            mLogger.info("rippleBuildExclude set mBuildFile to -5 for package {}", p.mAlias );

            if ( be != null )
            {
                be.setProcessed();
            }
            else
            {
                //  If no be item has been provided, then scan the complete collection looking for a matching item
                //  If found, process it, else add it (unprocessed)
                boolean found = false;
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();

                    if ( buildExclusion.compare(p.mId, rootPvId, rootCause))
                    {
                        buildExclusion.setProcessed();
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // Entry not found in the mBuildExclusionCollection
                    // Mark all occurrences for this package as processed
                    // These will be superseded by a new build exclusion entry
                    for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                    {
                        BuildExclusion buildExclusion = it.next();

                        if ( buildExclusion.compare(p.mId))
                        {
                            buildExclusion.setProcessed();
                        }
                    }

                    BuildExclusion buildExclusion = new BuildExclusion(p.mId, rootPvId, rootCause, p.mTestBuildInstruction);

                    //
                    //  Add the new build exclusion to a list
                    //    Either the default list or a user-specified list
                    if ( list == null )
                    {
                        mBuildExclusionCollection.add(buildExclusion);
                    }
                    else
                    {
                        // must use the ListIterator interface to add to the collection whilst iterating through it
                        list.add(buildExclusion);
                    }
                }
            }

            //  Locate ALL packages that depend on this package and exclude them too
            //  This process will be recursive
            //    Not sure that this it is efficient 
            //  Only ripple a build exclusion through for packages that are a part of the full release set
            //  NewWIPs, ForcedRipples and TestBuilds will not force consumer packages to be excluded
            //
            if ( ! p.mIsNotReleased && dependentToo)
            {
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
                {
                    Package pkg = it.next();
                    
                    //  Skip myself
                    if (pkg == p)
                        continue;

                    //  Ignore dependent WIPS if required
                    if (!depWipsToo && pkg.mDirectlyPlanned)
                        continue;
                    
                    for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                    {
                        Package dependency = it2.next();

                        if ( dependency == p )
                        {
                            rippleBuildExclude( pkg, rootPvId, null, list, null, dependentToo, depWipsToo );
                            break;
                        }
                    }

                }
            }
        }
        mLogger.info("rippleBuildExclude set {} {}", p.mName, p.mBuildFile);
    }

    /**Simple XML string escaping
     * 
     * @param xml               - String to escape
     * @return          - A copy of the string with XML-unfriendly characters escaped 
     */
    public static String escapeXml( String xml )
    {
        xml = xml.replaceAll("&", "&amp;");
        xml = xml.replaceAll("<", "&lt;");
        xml = xml.replaceAll(">", "&gt;");
        xml = xml.replaceAll("\"","&quot;");
        xml = xml.replaceAll("'", "&apos;");
        xml = xml.replaceAll("\\$", "\\$\\$");

        return xml;
    }

    /** Quote a string or a string pair
     *  If two strings are provided, then they will be joined with a comma.
     *   
     * @param text              - First string to quote
     * @param text2             - Optional, second string
     * @return A string of the form 'text','text2'
     */
    public static String quoteString(String text, String text2)
    {
        String result;
        result =  "\'" + text + "\'";
        if (text2 != null )
        {
            result +=  ",\'" + text2 + "\'";  
        }
        return result;
    }

    /** Generate build file information
     * 
     */
    private void generateBuildFiles() 
    {

        // persist the build files
        boolean allProcessed = false;
        int buildFile = 1;
        StringBuilder rawData = new StringBuilder();
        StringBuilder setUp = new StringBuilder();

        mLogger.debug("generateBuildFiles");

        if ( mDaemon )
        {
            // all interesting packages in daemon mode match the following filter
            buildFile = 3;
        }

        //-----------------------------------------------------------------------
        //    Generate the build file
        do
        {
            BuildFile buildEntry = new  BuildFile();
            buildEntry.state = BuildFileState.Dummy;
            XmlBuilder xml = generateBuildFileHeader();


            //  Generate properties for each package in this build level or lower build levels
            //  The properties link the packageAlias to the PackageName and PackageVersion
            //
            //  [DEVI 54816] In escrow mode all unreproducible packages are included 
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) ) || ( !mDaemon && p.mBuildFile == -2 ) )
                {
                    xml.addProperty(p.mAlias, p.mName + " " + p.mVersion + p.mExtension);
                }
            }

            //  UTF Support
            //  Insert additional info into the build file to provide extra checking
            //
            if ( ! mReleaseManager.mUseDatabase )
            {
                // UTF Support
                // Insert per-package planning information
                //
                xml.addComment("mPackageCollection");
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    generatePackageInfo(xml, p);
                }

                xml.addComment("mPackageCollectionWip");
                for (Iterator<Package> it = mPackageCollectionWip.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if (p.mIsNotReleased )
                        generatePackageInfo(xml, p);
                }
                
                xml.addComment("mPackageCollectionTest");
                for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if (p.mIsNotReleased )
                        generatePackageInfo(xml, p);
                }

                xml.addComment("mPackageCollectionTestRipple");
                for (Iterator<Package> it = mPackageCollectionRipple.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if (p.mIsNotReleased )
                        generatePackageInfo(xml, p);
                }

                // UTF Support
                // Insert build exclusion information
                xml.addComment("mBuildExclusionCollection");
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();
                    if (!buildExclusion.isProcessed())
                    {
                        xml.addComment(buildExclusion.info());
                    }
                }
                
                // UTF Support
                // Insert build Plan
                if (mDaemon)
                {
                    xml.addComment("mBuildOrder");
                    int order = 0;
                    for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
                    {
                        Package p = it.next();
                        String comment =
                                "pvid="+ p.mId +
                                " order=" + (++order) +
                                " Rorder=" + (p.mRippleOrder) +
                                " time=" + p.mRippleTime +
                                " name=\"" + p.mAlias + "\"";
                        xml.addComment(comment);
                    }
                }
            }

            //  Generate Taskdef information
            generateTaskdef(xml);

            //
            //  Insert known Machine Information
            //  Escrow usage: 
            //      Map machType to machClass
            //  Also serves as a snapshot of the required build configuration
            //  ie: machine types and buildfilters
            //
            if (!mDaemon)
            {
                generateMachineInfo(xml, null);
            }

            //
            //  Generate target rules for each package to be built within the current build file
            //
            boolean daemonHasTarget = false;

            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile > 0 && p.mBuildFile <= buildFile )
                {
                    generateTarget(xml, buildEntry, p, buildFile);

                    if ( p.mBuildFile == 1 )
                    {
                        daemonHasTarget = true;
                        
                        // Retain information about the target package
                        buildEntry.mPkgId = p.mPid;
                        buildEntry.mPvId = p.mId;
                    }
                }

                //      Generate escrow extraction commands
                //      Only done on the first pass though the packages
                //
                if ( !mDaemon && buildFile == 1 )
                {
                    setUp.append("jats jats_vcsrelease -extractfiles"
                                + " \"-label=" + p.mVcsTag + "\""
                                + " \"-view=" + p.mAlias + "\""
                                + " -root=. -noprefix"
                                + mlf );
                }

                //      Generate escrow raw CSV data
                //      Note: I don't think this data is used at all

                if ( !mDaemon && ( p.mBuildFile == buildFile))
                {
                    StringAppender machines = new StringAppender(",");
                    for (Iterator<BuildStandard> it1 = p.mBuildStandardCollection.iterator(); it1.hasNext();)
                    {
                        BuildStandard bs = it1.next();
                        machines.append(bs.mMachClass);
                    }

                    rawData.append(p.mAlias + "," +
                                    buildFile + "," +
                                    machines +
                                    mlf);
                }
            }

            if ( mDaemon && !daemonHasTarget )
            {
                // must have AbtTestPath, AbtSetUp, AbtTearDown, and AbtPublish targets
                XmlBuilder target = xml.addNewElement("target");
                target.addAttribute("name", "AbtTestPath");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtSetUp");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtTearDown");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtPublish");
            }

            generateDefaultTarget( xml, buildFile);

            //  Convert the Xml structure into text and save it in the build file
            //  Add this build file to the mBuildCollection
            buildEntry.content = mXmlHeader + xml.toString();
            mBuildCollection.add(buildEntry);

            // are more build files required
            allProcessed = true;

            if (!mDaemon)
            {
                // this is escrow mode centric
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mBuildFile > buildFile )
                    {
                        // more build files are required
                        allProcessed = false;
                        mLogger.info("planRelease reiterating package has no build requirement {} {} {}",p.mName, p.mBuildFile,  buildFile);
                        break;
                    }
                } 

                buildFile++;
            }

        } while( !allProcessed );

        //      Save additional escrow data
        if ( !mDaemon )
        {
            mEscrowSetup = setUp.toString();
            mEscrowRawData = rawData.toString();
        }
    }

    /**returns a build file header for the mBaseline
     */
    private XmlBuilder generateBuildFileHeader()
    {
        mLogger.debug("generateBuildFileHeader");
        XmlBuilder element = new XmlBuilder("project");

        element.addAttribute("name", "mass");
        element.addAttribute("default", "full");
        element.addAttribute("basedir", ".");

        if ( mDaemon )
        {
            element.addProperty("abt_mail_server", getMailServer());
            element.addProperty("abt_mail_sender", getMailSender()); 
            element.addProperty("abt_rtag_id", mBaseline);
            element.addProperty("abt_daemon", mReleaseManager.currentTimeMillis());
            element.makePropertyTag("abt_packagetarball", true);
            element.makePropertyTag("abt_usetestarchive", !ReleaseManager.getUseMutex());

            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == 1 )
                {
                    element.addProperty("abt_package_name", p.mName);
                    element.addProperty("abt_package_version", p.mVersion + p.mExtension);
                    element.addProperty("abt_package_extension", p.mExtension);
                    element.addProperty("abt_package_location", getBuildLocation(p));
                    element.addProperty("abt_package_ownerlist", p.emailInfoNonAntTask(this));
                    element.addProperty("abt_package_build_info", buildInfoText(p));

                    // depends in the form 'cs','25.1.0000.cr';'Dinkumware_STL','1.0.0.cots'
                    StringAppender depends = new StringAppender(";");

                    for (Iterator<Package> it3 = p.mPackageDependencyCollection.iterator(); it3.hasNext(); )
                    {
                        Package depend = it3.next();
                        depends.append( quoteString(depend.mName, depend.mVersion + depend.mExtension) );
                    }

                    element.addProperty("abt_package_depends", depends.toString());
                    element.addProperty("abt_is_ripple", p.mDirectlyPlanned ? "0" : "1");
                    element.addProperty("abt_build_reason", p.mBuildReason.toString());
                    element.addProperty("abt_package_version_id", p.mId);
                    element.addProperty("abt_does_not_require_source_control_interaction", ! p.mRequiresSourceControlInteraction ? "true" : "false" );
                    element.addProperty("abt_test_build_instruction", p.mTestBuildInstruction);
                }
            }
        }
        else
        {
            //    Escrow Mode
            element.addProperty("abt_rtag_id", mRtagId);
            element.addProperty("abt_sbom_id", mSbomId);
        }

        element.addProperty("abt_release", escapeXml(mBaselineName));
        element.addProperty("abt_buildtool_version", mReleaseManager.getMajorVersionNumber() );

        element.addNewElement("condition")
        .addAttribute("property", "abt_family")
        .addAttribute("value", "windows")
        .addNewElement("os")
        .addAttribute("family", "windows");
        element.addProperty("abt_family", "unix");

        return element;
    }

    /** Add task def XML items taskdef for the abt ant task
     * @param xml       - xml item to extend
     */
    private void generateTaskdef(XmlBuilder xml)
    {
        mLogger.debug("generateTaskdef");
        xml.addNewElement("taskdef")
        .addAttribute("name", "abt")
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABT");

        xml.addNewElement("taskdef")
        .addAttribute("name", "abtdata")
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABTData");
    }

    /** returns the command abtdata items
     *  <br>Common machine information
     *  <br>Common email address
     *  
     *  
     *  @param  xml - Xml element to extend   
     *  @param    p - Package (May be null)
     */
    private void generateMachineInfo(XmlBuilder xml, Package p)
    {
        XmlBuilder element = xml.addNewElement("abtdata");
        element.addAttribute("id", "global-abt-data");

        //
        //  Iterate over all the machines and create a nice entry
        //
        for (Iterator<ReleaseConfig> it = mReleaseManager.mReleaseConfigCollection.mReleaseConfig.iterator(); it.hasNext(); )
        {
            ReleaseConfig rc = it.next();
            element.addElement(rc.getMachineEntry());
        }

        //
        //  Now the email information
        //
        if ( p != null)
        {
            p.emailInfo(this, element);
        }
    }

    /** Generate package information
     *  Only when running unit tests
     *  
     * @param xml       - An xml element to append data to
     * @param p - Package to process
     */
    private void generatePackageInfo (XmlBuilder xml, Package p)
    {
        StringBuilder comment = new StringBuilder();
        StringBuilder deps = new StringBuilder();
        
        String joiner = "";
        for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
        {
            String alias = it2.next();
            deps.append(joiner).append(alias); 
            joiner = ",";
        }
        
        comment.append("pvid=").append(p.mId);
        comment.append(" name=").append('"').append(p.mAlias).append('"');
        comment.append(" reason=").append(p.mNoBuildReason);
        comment.append(" buildFile=").append(p.mBuildFile);
        comment.append(" directlyPlanned=").append(p.mDirectlyPlanned);
        comment.append(" indirectlyPlanned=").append(p.mIndirectlyPlanned);
        comment.append(" depends=[").append(deps).append("]");

        xml.addComment(comment.toString());            
    }

    /**returns an ant target for the passed Package
     * in daemon mode:
     *  packages are categorized with one of three mBuildFile values:
     *   1 the package to be built by this buildfile
     *   2 the packages with a future build requirement
     *   3 the packages with no build requirement
     *  the returned target depends on this categorization and will have
     *   1 full abt info
     *   2 full dependency info to determine future build ordering but no abt info (will not build this package)
     *   3 only a name attribute (will not build this package)
     * in escrow mode:
     *  if the passed Package's mBuildFile is different (less than) the passed build file,
     *  the returned target have only a name attribute (will not build this package)
     *   
     * @param xml - Xml element to extend
     * @param buildEntry - Record build type (dummy/generic/not generic)
     * @param p - Package to process
     * @param buildFile - buildfile level being processed
     */
    private void generateTarget(XmlBuilder xml, BuildFile buildEntry, Package p, int buildFile)
    {
        mLogger.debug("generateTarget");

        //---------------------------------------------------------------------
        //  Generate the AbtData - common machine and email information
        //  Only used by the daemon builds
        //
        if ( mDaemon && p.mBuildFile == 1 )
        {
            generateMachineInfo(xml, p );
        }

        //-------------------------------------------------------------------------
        //  Generate the <target name=... /> construct
        //  There are two types
        //      1) Simple dummy place holder. Just has the PackageName.PackageExt
        //      2) Full target. Has all information to build the package including
        //              AbtSetUp, AbtTearDown and AbtPublish targets
        //

        if ( !mDaemon && ( p.mBuildFile < buildFile ) )
        {
            XmlBuilder target = xml.addNewElement("target");
            target.addAttribute("name", p.mAlias);
        }
        else
        {
            if (!mDaemon) 
            {
                //  Escrow Only:
                //  Generate the 'wrapper' target
                //  This is used to ensure that the required dependencies have been built - I think
                //
                StringAppender dependList = new StringAppender(",");
                if ( !p.mPackageDependencyCollection.isEmpty() )
                {
                    for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                    {
                        Package dependency = it.next();
                        if ( !mDaemon && dependency.mBuildFile == -2 )
                        {
                            // ignore targets which build in foreign environments in escrow mode
                            continue;
                        }
                        dependList.append(dependency.mAlias);
                    }
                }
    
                XmlBuilder target = xml.addNewElement("target").isExpanded();
                target.addAttribute("name", p.mAlias + ".wrap");
    
                if (dependList.length() > 0)
                {
                    target.addAttribute("depends", dependList.toString() );
                }
    
                if ( !mDaemon )
                {
                    boolean hasDependenciesBuiltInThisIteration = false;
                    if ( ( !p.mPackageDependencyCollection.isEmpty()) )
                    {
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                        {
                            Package dependency = it.next();
    
                            if ( dependency.mBuildFile == buildFile )
                            {
                                hasDependenciesBuiltInThisIteration = true;
                                break;
                            }
                        }
                    }
    
                    if ( hasDependenciesBuiltInThisIteration )
                    {
                        XmlBuilder condition = target.addNewElement("condition");
                        condition.addAttribute("property",  p.mAlias + ".build");
    
                        XmlBuilder and = condition.addNewElement("and");
    
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                        {
                            Package dependency = it.next();
    
                            if ( dependency.mBuildFile == buildFile )
                            {
                                XmlBuilder or = and.addNewElement("or");
    
                                XmlBuilder equal1 = or.addNewElement("equals");
                                equal1.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
                                equal1.addAttribute("arg2", "0");
    
                                XmlBuilder equal2 = or.addNewElement("equals");
                                equal2.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
                                equal2.addAttribute("arg2", "257");
                            }
                        }
                    }
                    else
                    {
                        target.addProperty(p.mAlias + ".build", "");
                    }
                }
            }


            //
            //  Generate the 'body' of the target package
            //  Escrow Mode: Always
            //  Daemon Mode: Only for the one target we are building
            //                  Simplifies the XML
            //                  Reduces noise in the logs
            //              Don't add target dependencies. 
            //                  We are only building one target and the 
            //                  dependency management has been done way before now.
            //                  All it does is makes the logs noisy.
            //
            if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
            {
                XmlBuilder target = xml.addNewElement("target").isExpanded();
                target.addAttribute("name", p.mAlias);
    
                if ( !mDaemon )
                {
                    target.addAttribute("depends",  p.mAlias + ".wrap");
                    target.addAttribute("if",p.mAlias + ".build");
                }
    
                if ( mDaemon && p.mBuildFile == 1 )
                {
                    target.addProperty(p.mAlias + "pkg_id",p.mPid);
                    target.addProperty(p.mAlias + "pv_id",p.mId);
                }
    
                target.addProperty(p.mAlias + "packagename",p.mName);                   
                target.addProperty(p.mAlias + "packageversion",p.mVersion);
                target.addProperty(p.mAlias + "packageextension",p.mExtension);
                target.addProperty(p.mAlias + "packagevcstag",p.mVcsTag);

                target.makePropertyTag(p.mAlias + "directchange", p.mDirectlyPlanned); 
                target.makePropertyTag(p.mAlias + "doesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);

                buildEntry.state = BuildFile.BuildFileState.NonGeneric;
                if ( p.isGeneric() )
                {
                    buildEntry.state = BuildFile.BuildFileState.Generic;
                    target.makePropertyTag(p.mAlias + "generic", true); 
                }

                target.addProperty(p.mAlias + "loc", getBuildLocation(p));
                target.makePropertyTag(p.mAlias + "unittests", p.mHasAutomatedUnitTests && mDaemon);

                //    Add our own task and associated information
                //
                XmlBuilder abt = target.addNewElement("abt").isExpanded();
    
                for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                {
                    Package dependency = it.next();
                    XmlBuilder depend = abt.addNewElement("depend");
                    depend.addAttribute("package_alias", "${" + dependency.mAlias + "}");
                }

                buildInfo(abt,p);
    
                if ( mDaemon && p.mBuildFile == 1 )
                {
                    //    AbtTestPath
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtTestPath");
                    target.addProperty("AbtTestPathpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtTestPathpackagename", p.mName);
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
 
                    
                    //    AbtSetUp
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtSetUp");
                    target.addProperty("AbtSetUppackagevcstag", p.mVcsTag);
                    target.addProperty("AbtSetUppackagename", p.mName);
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
                    //    AbtTearDown
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtTearDown");
                    target.addProperty("AbtTearDownpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtTearDownpackagename", p.mName);
                    target.addProperty("AbtTearDownpackageversion", p.mVersion);
                    target.addProperty("AbtTearDownpackageextension", p.mExtension);
                    target.makePropertyTag(p.mAlias + "generic", p.isGeneric());
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
    
                    //  AbtPublish
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtPublish");
    
                    target.addProperty("AbtPublishpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtPublishpackagename", p.mName);
                    target.addProperty("AbtPublishpackageversion", p.mVersion);
                    target.addProperty("AbtPublishpackageextension", p.mExtension);
                    target.makePropertyTag("AbtPublishdirectchange", p.mDirectlyPlanned);
                    target.makePropertyTag("AbtPublishdoesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
                    target.makePropertyTag("AbtPublishgeneric", p.isGeneric());
                    target.addProperty("AbtPublishloc", getBuildLocation(p));
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
                }
            }
        }
    }

    /** Extends the xml object. Adds ant default target for the current build iteration
     * @param xml - The XmlBuilder Object to extend
     * @param buildFile - The current build file level. This differs for Daemon and Escrow mode. In Daemon mode it will not be a '1' 
     */
    private void generateDefaultTarget(XmlBuilder xml, int buildFile)
    {
        mLogger.debug("generateDefaultTarget");

        XmlBuilder target = xml.addNewElement("target").isExpanded();
        target.addAttribute("name", "fullstart");

        if (buildFile == 1)
        {
            antEcho(target, "${line.separator}" + mAnyBuildPlatforms + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -1 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mAssocBuildPlatforms + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -2 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mNotInBaseline + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -4 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mDependent + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -5 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }
        }

        if ( !mDaemon )
        {
            antEcho(target, "${line.separator}Build Started:${line.separator}${line.separator}");
        }

        //
        //      Create a comma separated list of all the required targets
        //      Escrow : All packages
        //      Daemon : Just the package being built
        //
        StringAppender dependList = new StringAppender(",");
        dependList.append("fullstart");
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
            {
                if ((mDaemon && p.mBuildFile == 1) || !mDaemon)
                {
                    dependList.append(p.mAlias);                    
                }
            }
        }

        target = xml.addNewElement("target").isExpanded();
        target.addAttribute("name", "full");
        target.addAttribute("depends", dependList.toString());

        if ( !mDaemon )
        {
            antEcho(target, "${line.separator}Build Finished${line.separator}");
        }
    }

    /** Internal helper function to create an ant 'echo statement
     *  Many of the parameters are fixed
     */
    private void antEcho( XmlBuilder xml, String message)
    {
        XmlBuilder msg = xml.addNewElement("echo");
        msg.addAttribute("message", message);
        msg.addAttribute("file", "publish.log");
        msg.addAttribute("append", "true");
    }

    /**sets the mIndirectlyPlanned true for the package and all dependent packages
     */
    private void rippleIndirectlyPlanned(Package p)
    {
        mLogger.debug("rippleIndirectlyPlanned");
        if ( !p.mIndirectlyPlanned && p.mBuildFile == 0 )
        {
            p.mIndirectlyPlanned = true;

            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
            {
                Package pkg = it.next();

                if ( pkg != p )
                {
                    for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                    {
                        Package dependency = it2.next();

                        if ( dependency == p )
                        {
                            rippleIndirectlyPlanned( pkg );
                            break;
                        }
                    }
                }
            }
        }
        mLogger.info("rippleIndirectlyPlanned set {} {}", p.mName, p.mIndirectlyPlanned);    
    }

    /**accessor method
     */
    public String getEscrowSetUp()
    {
        mLogger.debug("getEscrowSetUp");
        String retVal = mEscrowSetup;

        mLogger.debug("getEscrowSetUp returned {}", retVal);
        return retVal;
    }

    /**accessor method
     */
    public String getRawData()
    {
        mLogger.debug("getRawData");
        String retVal = mEscrowRawData;

        mLogger.debug("getRawData returned {}", retVal);
        return retVal;
    }

    /**Get the build loc (location)
     * This is package specific and will depend on the build mode (Escrow/Daemon)
     * 
     * @param   p - Package being built
     * @return A string that describes the build location
     */
    private String getBuildLocation(Package p)
    {
        mLogger.debug("locationProperty");
        String location = "";

        if (mDaemon)
        {
            // Daemon: Start in root of view/workspace
            location += mBaseline;
        }
        else
        {
            // Escrow: mAlias used with jats -extractfiles -view
            location += p.mAlias;
        }

        //
        //  Always use '/' as a path separator - even if user has specified '\'
        //  Ant can handle it.
        //
        location = location.replace('\\', '/');
        return location;
    }

    /**Adds package build into as XML elements 
     * @param     xml   - Xml element to extend
     * @param   p       - Package to process
     */
    private void buildInfo(XmlBuilder xml, Package p)
    {
        mLogger.debug("buildInfo");

        //
        // Create the xml build information
        // <platform gbe_machtype="linux_i386" type="jats" arg="all"/>
        //
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext();)
        {
            BuildStandard bs = it.next();
            bs.getBuildStandardXml(xml);
        }
    }

    /**returns the buildInfo as a single line of text
     * Used for reporting purposes only
     * @param   p       - Package to process
     * 
     */
    public String buildInfoText(Package p)
    {
        mLogger.debug("buildInfoText");

        StringAppender result = new StringAppender (";");

        //
        //  Create platform:standards
        //      
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
        {
            BuildStandard bs = it.next();

            if ( bs.isActive() )
            {
                String info = bs.getBuildStandardText();
                result.append(info);
            }
        }

        mLogger.info("buildInfoText returned {}", result);
        return result.toString();
    }

    /**prints to standard out in escrow mode only
     * <br>Prints a title and information. The title is only printed once.
     * 
     * @param header - The message title to display, if printMessage is true
     * @param text - A package name. Really just the 2nd line of the message
     * @param printHeader -  Controls the printing of the message argument
     */
    private void standardOut(final String header, final String text, boolean printHeader)
    {
        mLogger.debug("standardOut");
        if (!mDaemon)
        {
            if ( printHeader )
            {
                System.out.println(header);
            }

            System.out.println(text);
        }
    }


    /**
     *  Email users about a rejected daemon instruction
     *  @param  reason  - Reason for the rejection
     *  @param  pkg     - Package to be affected by the instruction
     *  
     */
    public void emailRejectedDaemonInstruction(String reason, Package p)
    {
        mLogger.debug("emailRejectedDaemonInstruction");

        //  Email Subject
        String subject = "BUILD FAILURE of Daemon Instruction on package " + p.mAlias;

        // Email Body
        String mailBody = "The build system reject the the Daemon Instruction";
        mailBody += "<br>Reason: " + reason; 

        mailBody += "<p>Release: " + mBaselineName 
                +  "<br>Package: " + p.mAlias 
                +  "<br>Rm Ref: " + CreateUrls.generateRmUrl(getRtagId(), p.mId); 

        mailBody += "<p><hr>";

        String target = p.emailInfoNonAntTask(this);

        mLogger.error("emailRejectedDaemonInstruction Server: {}", getMailServer());
        mLogger.error("emailRejectedDaemonInstruction Sender: {}", getMailSender());
        mLogger.error("emailRejectedDaemonInstruction Target: {}", target);

        try
        {
            //    
            Smtpsend.send(getMailServer(),  // mailServer
                    getMailSender(),        // source
                    target,                 // target
                    getMailSender(),        // cc
                    null,                   // bcc
                    subject,                // subject
                    mailBody,               // body
                    null                    // attachment
                    );
        } catch (Exception e)
        {
            mLogger.warn("Email Failure: emailRejectedDaemonInstruction:{}", e.getMessage());
        }
    }

    /**
     * @return the mMailServer
     */
    public String getMailServer() {
        return mMailServer;
    }

    /**
     * @param mMailServer the mMailServer to set
     */
    public void setMailServer(String mMailServer) {
        this.mMailServer = mMailServer;
    }

    /**
     * @return the mMailSender
     */
    public String getMailSender() {
        return mMailSender;
    }

    /**
     * @param mMailSender the mMailSender to set
     */
    public void setMailSender(String mMailSender) {
        this.mMailSender = mMailSender;
    }

    /**
     * @return the mMailGlobalTarget
     */
    public String getMailGlobalTarget() {
        return mMailGlobalTarget;
    }

    /**
     * @param mMailGlobalTarget the mMailGlobalTarget to set
     */
    public void setMailGlobalTarget(String mMailGlobalTarget) {
        this.mMailGlobalTarget = mMailGlobalTarget;
    }

}