// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   Installation.java

package com.ptc.windchill.upgrade.history;

import com.ptc.windchill.instassm.ReleaseId;
import java.util.ArrayList;
import java.util.Date;
import wt.util.uuid.UUID;

// Referenced classes of package com.ptc.windchill.upgrade.history:
//            Invalidateable, Assembly, UpgradePhase, IUHException, 
//            IUHPersistenceDelegate, UpgradePhaseType, IUApplication

public final class Installation extends Invalidateable
{

    Installation(IUHPersistenceDelegate iuhpersistencedelegate, int i, boolean flag, Date date)
    {
        workingUpgradePhase_ = null;
        persistenceDelegate_ = iuhpersistencedelegate;
        id_ = i;
        complete_ = new Boolean(flag);
        timestamp_ = date;
        assemblies_ = null;
        upgradePhases_ = null;
        persistenceDelegate_.addInvalidateable(this);
    }

    public synchronized Assembly[] getAssemblies()
        throws IUHException
    {
        assertValidity();
        if(assemblies_ == null || persistenceDelegate_.areAssembliesDirty(this))
            assemblies_ = persistenceDelegate_.getAssemblies(this);
        return (Assembly[])assemblies_.toArray(new Assembly[0]);
    }

    public UpgradePhase[] getUpgradePhases()
        throws IUHException
    {
        assertValidity();
        if(upgradePhases_ == null || persistenceDelegate_.areUpgradePhasesDirty(this))
            upgradePhases_ = persistenceDelegate_.getUpgradePhases(this);
        return (UpgradePhase[])upgradePhases_.toArray(new UpgradePhase[0]);
    }

    public ReleaseId[] getAssemblyReleaseIds()
        throws IUHException
    {
        assertValidity();
        Assembly aassembly[] = getAssemblies();
        ReleaseId areleaseid[] = new ReleaseId[aassembly.length];
        for(int i = 0; i < aassembly.length; i++)
            areleaseid[i] = aassembly[i].getReleaseId();

        return areleaseid;
    }

    public synchronized Assembly addAssembly(ReleaseId releaseid)
        throws IUHException
    {
        assertValidity();
        Assembly assembly = persistenceDelegate_.createAssembly(this, releaseid);
        if(assemblies_ != null)
            assemblies_.add(assembly);
        return assembly;
    }

    public synchronized UpgradePhase addUpgradePhase(UpgradePhaseType upgradephasetype)
        throws IUHException
    {
        assertValidity();
        UpgradePhase upgradephase = persistenceDelegate_.createUpgradePhase(this, upgradephasetype);
        if(upgradePhases_ != null)
            upgradePhases_.add(upgradephase);
        return upgradephase;
    }

    public synchronized void markComplete()
        throws IUHException
    {
        assertValidity();
        UpgradePhase aupgradephase[] = getUpgradePhases();
        for(int i = 0; i < aupgradephase.length; i++)
        {
            UpgradePhase upgradephase = aupgradephase[i];
            if(!upgradephase.isComplete())
                throw new IUHException("Cannot mark installation complete until all upgrade phases are completed (id=" + getId() + ")");
        }

        complete_ = null;
        persistenceDelegate_.markComplete(this, true);
        complete_ = new Boolean(true);
    }

    synchronized void markIncomplete()
        throws IUHException
    {
        assertValidity();
        complete_ = null;
        persistenceDelegate_.markComplete(this, false);
        complete_ = new Boolean(false);
    }

    int getId()
    {
        assertValidity();
        return id_;
    }

    public synchronized boolean isComplete()
        throws IUHException
    {
        assertValidity();
        if(complete_ == null)
            complete_ = new Boolean(persistenceDelegate_.isComplete(this));
        return complete_.booleanValue();
    }

    public Date getTimestamp()
    {
        assertValidity();
        return timestamp_;
    }

    protected synchronized void onInvalidate()
    {
        super.onInvalidate();
        complete_ = null;
        assemblies_ = null;
        upgradePhases_ = null;
    }

    public final void markApplied(UUID uuid, String s)
        throws IUHException
    {
        assertValidity();
        getWorkingUpgradePhase().addIUApplication(uuid, s);
    }

    public synchronized void activateWorkingPhase(UpgradePhaseType upgradephasetype)
        throws IUHException
    {
        assertValidity();
        UpgradePhase upgradephase = null;
        UpgradePhase aupgradephase[] = getUpgradePhases();
        for(int i = 0; i < aupgradephase.length; i++)
        {
            UpgradePhaseType upgradephasetype1 = aupgradephase[i].getPhaseType();
            if(upgradephasetype1.equals(upgradephasetype))
                upgradephase = aupgradephase[i];
            if(upgradephase != null)
                upgradephase.markIncomplete();
        }

        if(upgradephase == null)
            upgradephase = addUpgradePhase(upgradephasetype);
        workingUpgradePhase_ = upgradephase;
    }

    public synchronized UpgradePhaseType determineStartingPhase(UpgradePhaseType aupgradephasetype[])
        throws IUHException
    {
        UpgradePhase aupgradephase[] = getUpgradePhases();
        UpgradePhase upgradephase = null;
        for(int i = 0; i < aupgradephase.length; i++)
            if(aupgradephase[i].isComplete())
                upgradephase = aupgradephase[i];

        if(upgradephase == null)
            return aupgradephasetype[0];
        if(aupgradephase.length == 3 && upgradephase.getPhaseType().equals(UpgradePhaseType.COMPARE_SCHEMA) || aupgradephase.length == 6 && upgradephase.getPhaseType().equals(UpgradePhaseType.VALIDATION_CHECKPOINT_1))
        {
            for(int j = 1; j < aupgradephase.length; j++)
                aupgradephase[j].markIncomplete();

            return aupgradephasetype[1];
        } else
        {
            return upgradephase.getPhaseType().getNext();
        }
    }

    public UpgradePhase getUpgradePhase(UpgradePhaseType upgradephasetype)
        throws IUHException
    {
        UpgradePhase upgradephase = null;
        assertValidity();
        UpgradePhase aupgradephase[] = getUpgradePhases();
        for(int i = 0; i < aupgradephase.length && upgradephase == null; i++)
            if(upgradephasetype.equals(aupgradephase[i].getPhaseType()))
                upgradephase = aupgradephase[i];

        return upgradephase;
    }

    public synchronized UpgradePhase getWorkingUpgradePhase()
        throws IUHException
    {
        assertValidity();
        if(workingUpgradePhase_ == null)
            throw new IUHException("There is no active working upgrade phase.");
        else
            return workingUpgradePhase_;
    }

    public final synchronized IUApplication findIUApplicationFor(UUID uuid)
        throws IUHException
    {
        assertValidity();
        IUApplication iuapplication = null;
        UpgradePhase aupgradephase[] = getUpgradePhases();
        for(int i = aupgradephase.length - 1; iuapplication == null && i >= 0; i--)
            iuapplication = aupgradephase[i].findIUApplicationFor(uuid);

        return iuapplication;
    }

    private IUHPersistenceDelegate persistenceDelegate_;
    private int id_;
    private Boolean complete_;
    private Date timestamp_;
    private ArrayList assemblies_;
    private ArrayList upgradePhases_;
    private UpgradePhase workingUpgradePhase_;
}
