/*
 * IMPORTANT!!!! XSL Autogenerated class, DO NOT EDIT!!!!!
 * Template: Infrastructure8.2 rev3 [oneit.objstore.BusinessObjectTemplate.xsl]
 *
 *   Version: 1.0
 *   Vendor: Apache Software Foundation (Xalan XSLTC)
 *   Vendor URL: http://xml.apache.org/xalan-j
 */


    package performa.orm;

import java.io.*;
import java.util.*;
import oneit.appservices.config.*;
import oneit.logging.*;
import oneit.objstore.*;
import oneit.objstore.assocs.*;
import oneit.objstore.attributes.*;
import oneit.objstore.rdbms.filters.*;
import oneit.objstore.parser.*;
import oneit.objstore.validator.*;
import oneit.objstore.utils.*;
import oneit.utils.*;
import oneit.utils.filter.Filter;
import oneit.utils.transform.*;
import oneit.utils.parsers.FieldException;

import performa.orm.types.*;



public abstract class BaseWorkFlow extends BaseBusinessClass
{
    // Reference instance for the object
    public static final WorkFlow REFERENCE_WorkFlow = new WorkFlow ();

    // Reference instance for the object
    public static final WorkFlow DUMMY_WorkFlow = new DummyWorkFlow ();

    // Static constants corresponding to field names
    
    public static final String FIELD_Name = "Name";
    public static final String FIELD_SortOrder = "SortOrder";
    public static final String FIELD_ApplicationStatus = "ApplicationStatus";
    public static final String SINGLEREFERENCE_Job = "Job";
    public static final String BACKREF_Job = "";

    // Static constants corresponding to searches
    public static final String SEARCH_All = "All";
    

    // Static constants corresponding to attribute helpers
    private static final DefaultAttributeHelper<WorkFlow> HELPER_Name = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<WorkFlow> HELPER_SortOrder = DefaultAttributeHelper.INSTANCE;
    private static final EnumeratedAttributeHelper<WorkFlow, ApplicationStatus> HELPER_ApplicationStatus =  new EnumeratedAttributeHelper<WorkFlow, ApplicationStatus> (ApplicationStatus.FACTORY_ApplicationStatus);
    

    // Private attributes corresponding to business object data
    private String _Name;
    private Integer _SortOrder;
    private ApplicationStatus _ApplicationStatus;
    

    // Private attributes corresponding to single references
    private SingleAssociation<WorkFlow, Job> _Job;
    

    // Private attributes corresponding to multiple references
    

    // Map of maps of metadata
    private static final Map ATTRIBUTES_METADATA_WorkFlow = new HashMap ();

    // Arrays of validators for each attribute
    private static final AttributeValidator[] FIELD_Name_Validators;
    private static final AttributeValidator[] FIELD_SortOrder_Validators;
    private static final AttributeValidator[] FIELD_ApplicationStatus_Validators;
    

    // Arrays of behaviour decorators
    private static final WorkFlowBehaviourDecorator[] WorkFlow_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Job = Job.BACKREF_WorkFlows;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Job();
            FIELD_Name_Validators = (AttributeValidator[])setupAttribMetaData_Name(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_SortOrder_Validators = (AttributeValidator[])setupAttribMetaData_SortOrder(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_ApplicationStatus_Validators = (AttributeValidator[])setupAttribMetaData_ApplicationStatus(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_WorkFlow.initialiseReference ();
            DUMMY_WorkFlow.initialiseReference ();
            WorkFlow_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(WorkFlow.class).toArray(new WorkFlowBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

    
    // Meta Info setup
    private static void setupAssocMetaData_Job()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "WorkFlows");
        metaInfo.put ("dbcol", "job_id");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "Job");
        metaInfo.put ("type", "Job");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for WorkFlow.Job:", metaInfo);
        ATTRIBUTES_METADATA_WorkFlow.put (SINGLEREFERENCE_Job, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_Name(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "name");
        metaInfo.put ("length", "100");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "Name");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for WorkFlow.Name:", metaInfo);
        ATTRIBUTES_METADATA_WorkFlow.put (FIELD_Name, Collections.unmodifiableMap (metaInfo));

        List validators = BaseBusinessClass.getAttribValidators(WorkFlow.class, "Name", metaInfo, validatorMapping);
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Validators for WorkFlow.Name:", validators);        

        return validators;
    }
    
    // Meta Info setup
    private static List setupAttribMetaData_SortOrder(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "sort_order");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "SortOrder");
        metaInfo.put ("type", "Integer");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for WorkFlow.SortOrder:", metaInfo);
        ATTRIBUTES_METADATA_WorkFlow.put (FIELD_SortOrder, Collections.unmodifiableMap (metaInfo));

        List validators = BaseBusinessClass.getAttribValidators(WorkFlow.class, "SortOrder", metaInfo, validatorMapping);
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Validators for WorkFlow.SortOrder:", validators);        

        return validators;
    }
    
    // Meta Info setup
    private static List setupAttribMetaData_ApplicationStatus(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("attribHelper", "EnumeratedAttributeHelper");
        metaInfo.put ("dbcol", "application_status");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "ApplicationStatus");
        metaInfo.put ("type", "ApplicationStatus");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for WorkFlow.ApplicationStatus:", metaInfo);
        ATTRIBUTES_METADATA_WorkFlow.put (FIELD_ApplicationStatus, Collections.unmodifiableMap (metaInfo));

        List validators = BaseBusinessClass.getAttribValidators(WorkFlow.class, "ApplicationStatus", metaInfo, validatorMapping);
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Validators for WorkFlow.ApplicationStatus:", validators);        

        return validators;
    }
    
    // END OF STATIC METADATA DEFINITION    
    
    
    
    // This constructor should not be called
    protected BaseWorkFlow ()
    {
    }


    protected BBCBehaviourDecorator[] getBehaviours()
    {
        return WorkFlow_BehaviourDecorators;
    }
    
    


    


    // Initialise the attributes
    protected void _initialiseNewObjAttributes (ObjectTransaction transaction) throws StorageException
    {
        super._initialiseNewObjAttributes (transaction);

        
        _Name = (String)(HELPER_Name.initialise (_Name));
        _SortOrder = (Integer)(HELPER_SortOrder.initialise (_SortOrder));
        _ApplicationStatus = (ApplicationStatus)(HELPER_ApplicationStatus.initialise (_ApplicationStatus));
    }


    // Initialise the associations
    protected void _initialiseAssociations ()
    {
        super._initialiseAssociations ();

        _Job = new SingleAssociation<WorkFlow, Job> (this, SINGLEREFERENCE_Job, Job.MULTIPLEREFERENCE_WorkFlows, Job.REFERENCE_Job, "tl_work_flow");
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Job = new SingleAssociation<WorkFlow, Job> (this, SINGLEREFERENCE_Job, Job.MULTIPLEREFERENCE_WorkFlows, Job.REFERENCE_Job, "tl_work_flow");
        
        
        return this;
    }

    

    /**
     * Get the attribute Name
     */
    public String getName ()
    {
        assertValid();
        String valToReturn = _Name;

            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                valToReturn = bhd.getName ((WorkFlow)this, valToReturn);
            }
            
            return valToReturn;
        
    }


    /**
     * Called prior to the attribute changing.  Subclasses need not call super.  If a field exception
     * is thrown, the attribute change will fail.  The new value is different to the old value.
     */
    protected void preNameChange (String newName) throws FieldException
    {
    }


    /**
     * Called after the attribute changes.
     * If a field exception is thrown, the value is still changed, however it 
     * may lead to the TX being rolled back
     */
    protected void postNameChange () throws FieldException
    {
    }

    
    public FieldWriteability getWriteability_Name ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }

    /**
     * Set the attribute Name.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setName (String newName) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_Name.compare (_Name, newName);
        
        try
        {
            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                newName = bhd.setName ((WorkFlow)this, newName);
                oldAndNewIdentical = HELPER_Name.compare (_Name, newName);
            }

            BusinessObjectParser.assertFieldCondition (newName != null, this, FIELD_Name, "mandatory");
            

            if (FIELD_Name_Validators.length > 0)
            {
                Object newNameObj = HELPER_Name.toObject (newName);

                if (newNameObj != null)
                {
                    int     loopMax = FIELD_Name_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_WorkFlow.get (FIELD_Name);

                    for (int v = 0 ; v < loopMax ; ++v)
                    {
                        FIELD_Name_Validators[v].checkAttribute (this, FIELD_Name, metadata, newNameObj);
                    }
                }
            }
        }
        catch (FieldException e)
        {
            if (!oldAndNewIdentical)
            {
                e.setWouldModify ();
            }
            
            throw e;
        }

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_Name () != FieldWriteability.FALSE, "Field  Name is not writeable");
            preNameChange (newName);
            markFieldChange (FIELD_Name);
            _Name = newName;            
            postFieldChange (FIELD_Name);
            postNameChange ();
        }
    }

    /**
     * Get the attribute SortOrder
     */
    public Integer getSortOrder ()
    {
        assertValid();
        Integer valToReturn = _SortOrder;

            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                valToReturn = bhd.getSortOrder ((WorkFlow)this, valToReturn);
            }
            
            return valToReturn;
        
    }


    /**
     * Called prior to the attribute changing.  Subclasses need not call super.  If a field exception
     * is thrown, the attribute change will fail.  The new value is different to the old value.
     */
    protected void preSortOrderChange (Integer newSortOrder) throws FieldException
    {
    }


    /**
     * Called after the attribute changes.
     * If a field exception is thrown, the value is still changed, however it 
     * may lead to the TX being rolled back
     */
    protected void postSortOrderChange () throws FieldException
    {
    }

    
    public FieldWriteability getWriteability_SortOrder ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }

    /**
     * Set the attribute SortOrder.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setSortOrder (Integer newSortOrder) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_SortOrder.compare (_SortOrder, newSortOrder);
        
        try
        {
            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                newSortOrder = bhd.setSortOrder ((WorkFlow)this, newSortOrder);
                oldAndNewIdentical = HELPER_SortOrder.compare (_SortOrder, newSortOrder);
            }

            BusinessObjectParser.assertFieldCondition (newSortOrder != null, this, FIELD_SortOrder, "mandatory");
            

            if (FIELD_SortOrder_Validators.length > 0)
            {
                Object newSortOrderObj = HELPER_SortOrder.toObject (newSortOrder);

                if (newSortOrderObj != null)
                {
                    int     loopMax = FIELD_SortOrder_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_WorkFlow.get (FIELD_SortOrder);

                    for (int v = 0 ; v < loopMax ; ++v)
                    {
                        FIELD_SortOrder_Validators[v].checkAttribute (this, FIELD_SortOrder, metadata, newSortOrderObj);
                    }
                }
            }
        }
        catch (FieldException e)
        {
            if (!oldAndNewIdentical)
            {
                e.setWouldModify ();
            }
            
            throw e;
        }

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_SortOrder () != FieldWriteability.FALSE, "Field  SortOrder is not writeable");
            preSortOrderChange (newSortOrder);
            markFieldChange (FIELD_SortOrder);
            _SortOrder = newSortOrder;            
            postFieldChange (FIELD_SortOrder);
            postSortOrderChange ();
        }
    }

    /**
     * Get the attribute ApplicationStatus
     */
    public ApplicationStatus getApplicationStatus ()
    {
        assertValid();
        ApplicationStatus valToReturn = _ApplicationStatus;

            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                valToReturn = bhd.getApplicationStatus ((WorkFlow)this, valToReturn);
            }
            
            return valToReturn;
        
    }


    /**
     * Called prior to the attribute changing.  Subclasses need not call super.  If a field exception
     * is thrown, the attribute change will fail.  The new value is different to the old value.
     */
    protected void preApplicationStatusChange (ApplicationStatus newApplicationStatus) throws FieldException
    {
    }


    /**
     * Called after the attribute changes.
     * If a field exception is thrown, the value is still changed, however it 
     * may lead to the TX being rolled back
     */
    protected void postApplicationStatusChange () throws FieldException
    {
    }

    
    public FieldWriteability getWriteability_ApplicationStatus ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }

    /**
     * Set the attribute ApplicationStatus.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setApplicationStatus (ApplicationStatus newApplicationStatus) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_ApplicationStatus.compare (_ApplicationStatus, newApplicationStatus);
        
        try
        {
            for (WorkFlowBehaviourDecorator bhd : WorkFlow_BehaviourDecorators)
            {
                newApplicationStatus = bhd.setApplicationStatus ((WorkFlow)this, newApplicationStatus);
                oldAndNewIdentical = HELPER_ApplicationStatus.compare (_ApplicationStatus, newApplicationStatus);
            }

            BusinessObjectParser.assertFieldCondition (newApplicationStatus != null, this, FIELD_ApplicationStatus, "mandatory");
            

            if (FIELD_ApplicationStatus_Validators.length > 0)
            {
                Object newApplicationStatusObj = HELPER_ApplicationStatus.toObject (newApplicationStatus);

                if (newApplicationStatusObj != null)
                {
                    int     loopMax = FIELD_ApplicationStatus_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_WorkFlow.get (FIELD_ApplicationStatus);

                    for (int v = 0 ; v < loopMax ; ++v)
                    {
                        FIELD_ApplicationStatus_Validators[v].checkAttribute (this, FIELD_ApplicationStatus, metadata, newApplicationStatusObj);
                    }
                }
            }
        }
        catch (FieldException e)
        {
            if (!oldAndNewIdentical)
            {
                e.setWouldModify ();
            }
            
            throw e;
        }

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_ApplicationStatus () != FieldWriteability.FALSE, "Field  ApplicationStatus is not writeable");
            preApplicationStatusChange (newApplicationStatus);
            markFieldChange (FIELD_ApplicationStatus);
            _ApplicationStatus = newApplicationStatus;            
            postFieldChange (FIELD_ApplicationStatus);
            postApplicationStatusChange ();
        }
    }



    /**
     * A list of multi assoc names e.g. list of strings.
     */
    public List<String> getSingleAssocs()
    {
        List result = super.getSingleAssocs ();

        
        result.add("Job");
        

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return _Job.getReferencedType ();
        }
        else
        {
            return super.getSingleAssocReferenceInstance (assocName);
        }
    }


    public String getSingleAssocBackReference(String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return Job.MULTIPLEREFERENCE_WorkFlows ;
        }
        else
        {
            return super.getSingleAssocBackReference (assocName);
        }
    }
    
    
    public BaseBusinessClass getSingleAssoc (String assocName) throws StorageException
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return getJob ();
        }
        else
        {
            return super.getSingleAssoc (assocName);
        }
    }


    public BaseBusinessClass getSingleAssoc (String assocName, Get getType) throws StorageException
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return getJob (getType);
        }
        else
        {
            return super.getSingleAssoc (assocName, getType);
        }
    }


    public Long getSingleAssocID (String assocName) throws StorageException
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return getJobID ();
        }
        else
        {
            return super.getSingleAssocID (assocName);
        }
    }


    public void setSingleAssoc (String assocName, BaseBusinessClass newValue) throws StorageException, FieldException
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            setJob ((Job)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



    /**
     * Get the reference Job
     */
    public Job getJob () throws StorageException
    {
        assertValid();
        
        try
        {
            return (Job)(_Job.get ());
        }
        catch (ClassCastException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Cache collision in WorkFlow:", this.getObjectID (), ", was trying to get Job:",  getJobID ());
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Instead I got:", _Job.get ().getClass ());
            throw e;
        }
    }


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Job getJob (Get getType) throws StorageException
    {
        assertValid();
        return _Job.get(getType);
    }


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getJobID ()
    {
        assertValid();
        
        if (_Job == null)
        {
            return null;
        }
        else
        {
            return _Job.getID ();
        }
    }

    /**
     * Called prior to the assoc changing.  Subclasses need not call super.  If a field exception
     * is thrown, the attribute change will fail.  The new value is different to the old value.
     */
    protected void preJobChange (Job newJob) throws FieldException
    {
    }


    /**
     * Called after the assoc changes.
     * If a field exception is thrown, the value is still changed, however it 
     * may lead to the TX being rolled back
     */
    protected void postJobChange () throws FieldException
    {
    }



    
    public FieldWriteability getWriteability_Job ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    /**
     * Set the reference Job.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setJob (Job newJob) throws StorageException, FieldException
    {
        BusinessObjectParser.assertFieldCondition (newJob != null, this, SINGLEREFERENCE_Job, "mandatory");
        

        if (_Job.wouldReferencedChange (newJob))
        {
            assertValid();
            Debug.assertion (getWriteability_Job () != FieldWriteability.FALSE, "Assoc Job is not writeable");
            preJobChange (newJob);
            Job oldJob = getJob ();

                    if (oldJob != null)
                    {
                        // This is to stop validation from triggering when we are removed
                        _Job.set (null);
                        oldJob.removeFromWorkFlows ((WorkFlow)(this));
                    }

                    _Job.set (newJob);

                    if (newJob != null)
                    {
                        newJob.addToWorkFlows ((WorkFlow)(this));
                    }
                
            postJobChange ();
        }
    }

    /**
     * A list of multi assoc names e.g. list of strings.
     */
    public List<String> getMultiAssocs()
    {
        List result = super.getMultiAssocs ();

        

        return result;
    }


    /**
     * Get the reference instance for the multi assoc name.
     */
    public BaseBusinessClass getMultiAssocReferenceInstance(String attribName)
    {
        

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        

        return super.getMultiAssocBackReference(attribName);
    }


    /**
     * Get the assoc count for the multi assoc name.
     */
    public int getMultiAssocCount(String attribName) throws StorageException
    {
        

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {
        

        return super.getMultiAssocAt(attribName, index);
    }


    /**
     * Add to a multi assoc by attribute name
     */
    public void addToMultiAssoc(String attribName, BaseBusinessClass newElement) throws StorageException
    {
        

        super.addToMultiAssoc(attribName, newElement);
    }

    /**
     * Remove from a multi assoc by attribute name
     */
    public void removeFromMultiAssoc(String attribName, BaseBusinessClass oldElement) throws StorageException
    {
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


    protected void __loadMultiAssoc (String attribName, BaseBusinessClass[] elements)
    {
        

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        

        return super.__isMultiAssocLoaded(attribName);
    }

    



    public void onDelete ()
    {
        try 
        {
            // Ensure we are removed from any loaded multi-associations that aren't mandatory
            if (_Job.isLoaded () || getTransaction ().isObjectLoaded (_Job.getReferencedType (), getJobID ()))
            {
                Job  referenced = getJob ();

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null WorkFlows from ", getObjectID (), " to ", referenced.getObjectID ());
                    _Job.set (null);
                    referenced.removeFromWorkFlows ((WorkFlow)this);
                }
            }
        
        }
        catch (Exception e)
        {
            throw NestedException.wrap(e);
        }

        super.onDelete ();
    }


    public WorkFlow newInstance ()
    {
        return new WorkFlow ();
    }


    public WorkFlow referenceInstance ()
    {
        return REFERENCE_WorkFlow;
    }


    public WorkFlow getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getWorkFlowByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_WorkFlow;
    }


    public String getBaseSetName ()
    {
        return "tl_work_flow";
    }





    /**
     * This is where an object returns the Persistent sets that will
     * store it into the database.
     * The should be entered into allSets
     */
    public void getPersistentSets (PersistentSetCollection allSets)
    {
        ObjectStatus        myStatus = getStatus ();
        PersistentSetStatus myPSetStatus = myStatus.getPSetStatus();
        ObjectID            myID = getID();

        super.getPersistentSets (allSets);

        
        PersistentSet   tl_work_flowPSet = allSets.getPersistentSet (myID, "tl_work_flow", myPSetStatus);

        tl_work_flowPSet.setAttrib (FIELD_ObjectID, myID);
        tl_work_flowPSet.setAttrib (FIELD_Name, HELPER_Name.toObject (_Name)); //
        tl_work_flowPSet.setAttrib (FIELD_SortOrder, HELPER_SortOrder.toObject (_SortOrder)); //
        tl_work_flowPSet.setAttrib (FIELD_ApplicationStatus, HELPER_ApplicationStatus.toObject (_ApplicationStatus)); //
        _Job.getPersistentSets (allSets);
        
    }


    /**
     * Sets the objects state based on Persistent sets.
     */
    public void setFromPersistentSets (ObjectID objectID, PersistentSetCollection allSets)
    {
        super.setFromPersistentSets (objectID, allSets);

        
        PersistentSet   tl_work_flowPSet = allSets.getPersistentSet (objectID, "tl_work_flow");

        _Name = (String)(HELPER_Name.fromObject (_Name, tl_work_flowPSet.getAttrib (FIELD_Name))); //
        _SortOrder = (Integer)(HELPER_SortOrder.fromObject (_SortOrder, tl_work_flowPSet.getAttrib (FIELD_SortOrder))); //
        _ApplicationStatus = (ApplicationStatus)(HELPER_ApplicationStatus.fromObject (_ApplicationStatus, tl_work_flowPSet.getAttrib (FIELD_ApplicationStatus))); //
        _Job.setFromPersistentSets (objectID, allSets);
        
    }





    public void setAttributesFrom (BaseBusinessClass other, MultiException e)
    {
        super.setAttributesFrom (other, e);

        if (other instanceof WorkFlow)
        {
            WorkFlow otherWorkFlow = (WorkFlow)other;
            
            try
            {
                setName (otherWorkFlow.getName ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
            try
            {
                setSortOrder (otherWorkFlow.getSortOrder ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
            try
            {
                setApplicationStatus (otherWorkFlow.getApplicationStatus ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
        }
    }


    /**
     * Set the attributes in this to copies of the attributes in source.
     */
    public void copyAttributesFrom (BaseBusinessClass source)
    {
        super.copyAttributesFrom (source);

        if (source instanceof BaseWorkFlow)
        {
            BaseWorkFlow sourceWorkFlow = (BaseWorkFlow)(source);

            _Name = sourceWorkFlow._Name;
            _SortOrder = sourceWorkFlow._SortOrder;
            _ApplicationStatus = sourceWorkFlow._ApplicationStatus;
            
        }
    }


    /**
     * Set the associations in this to copies of the attributes in source.
     */
    public void copySingleAssociationsFrom (BaseBusinessClass source, boolean linkToGhosts)
    {
        super.copySingleAssociationsFrom (source, linkToGhosts);

        if (source instanceof BaseWorkFlow)
        {
            BaseWorkFlow sourceWorkFlow = (BaseWorkFlow)(source);

            _Job.copyFrom (sourceWorkFlow._Job, linkToGhosts);
            
        }
    }


    /**
     * Set the associations in this to copies of the attributes in source.
     */
    public void copyAssociationsFrom (BaseBusinessClass source, boolean linkToGhosts)
    {
        super.copyAssociationsFrom (source, linkToGhosts);

        if (source instanceof BaseWorkFlow)
        {
            BaseWorkFlow sourceWorkFlow = (BaseWorkFlow)(source);

            
        }
    }


    public void validate (ValidationContext context)
    {
        super.validate (context);

        
        context.check (getJobID() != null, this, SINGLEREFERENCE_Job, "mandatory");
        
    }


    /**
     * Subclasses must override this to read in their attributes
     */
    protected void readExternalData(Map<String, Object> vals) throws IOException, ClassNotFoundException
    {
        super.readExternalData(vals);

        _Name = (String)(HELPER_Name.readExternal (_Name, vals.get(FIELD_Name))); //
        _SortOrder = (Integer)(HELPER_SortOrder.readExternal (_SortOrder, vals.get(FIELD_SortOrder))); //
        _ApplicationStatus = (ApplicationStatus)(HELPER_ApplicationStatus.readExternal (_ApplicationStatus, vals.get(FIELD_ApplicationStatus))); //
        _Job.readExternalData(vals.get(SINGLEREFERENCE_Job));
        
    }


    /**
     * Subclasses must override this to write out their attributes
     */
    protected void writeExternalData(Map<String, Object> vals) throws IOException
    {
        super.writeExternalData(vals);

        vals.put (FIELD_Name, HELPER_Name.writeExternal (_Name));
        vals.put (FIELD_SortOrder, HELPER_SortOrder.writeExternal (_SortOrder));
        vals.put (FIELD_ApplicationStatus, HELPER_ApplicationStatus.writeExternal (_ApplicationStatus));
        vals.put (SINGLEREFERENCE_Job, _Job.writeExternalData());
        
    }


    public void compare (BaseBusinessClass other, AttributeChangeListener listener) throws StorageException
    {
        super.compare (other, listener);

        if (other instanceof BaseWorkFlow)
        {
            BaseWorkFlow otherWorkFlow = (BaseWorkFlow)(other);

            
            if (!HELPER_Name.compare(this._Name, otherWorkFlow._Name))
            {
                listener.notifyFieldChange(this, other, FIELD_Name, HELPER_Name.toObject(this._Name), HELPER_Name.toObject(otherWorkFlow._Name));
            }
            if (!HELPER_SortOrder.compare(this._SortOrder, otherWorkFlow._SortOrder))
            {
                listener.notifyFieldChange(this, other, FIELD_SortOrder, HELPER_SortOrder.toObject(this._SortOrder), HELPER_SortOrder.toObject(otherWorkFlow._SortOrder));
            }
            if (!HELPER_ApplicationStatus.compare(this._ApplicationStatus, otherWorkFlow._ApplicationStatus))
            {
                listener.notifyFieldChange(this, other, FIELD_ApplicationStatus, HELPER_ApplicationStatus.toObject(this._ApplicationStatus), HELPER_ApplicationStatus.toObject(otherWorkFlow._ApplicationStatus));
            }

            // Compare single assocs
            _Job.compare (otherWorkFlow._Job, listener);
            

            // Compare multiple assocs
            
        }
    }


    public void visitTransients (AttributeVisitor visitor) throws StorageException
    {
        super.visitAttributes (visitor);

        
    }


    public void visitAttributes (AttributeVisitor visitor) throws StorageException
    {
        super.visitAttributes (visitor);

        visitor.visitField(this, FIELD_Name, HELPER_Name.toObject(getName()));
        visitor.visitField(this, FIELD_SortOrder, HELPER_SortOrder.toObject(getSortOrder()));
        visitor.visitField(this, FIELD_ApplicationStatus, HELPER_ApplicationStatus.toObject(getApplicationStatus()));
        visitor.visitAssociation (_Job);
        
    }


    public void visitAssociations (AssociationVisitor visitor, AssociatedScope scope) throws StorageException
    {
        super.visitAssociations (visitor, scope);

        if (scope.includes (_Job))
        {
            visitor.visit (_Job);
        }
        
    }


    public static WorkFlow createWorkFlow (ObjectTransaction transaction) throws StorageException
    {
        WorkFlow     result = new WorkFlow ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static WorkFlow getWorkFlowByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (WorkFlow)(transaction.getObjectByID (REFERENCE_WorkFlow, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Name))
        {
            return filter.matches (getName ());
        }
        else if (attribName.equals (FIELD_SortOrder))
        {
            return filter.matches (getSortOrder ());
        }
        else if (attribName.equals (FIELD_ApplicationStatus))
        {
            return filter.matches (getApplicationStatus ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Job))
        {
            return filter.matches (getJob ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


    public static SearchAll SearchByAll () { return new SearchAll (); }

    public static class SearchAll extends SearchObject<WorkFlow>
    {
        
            public SearchAll andObjectID (QueryFilter<Long> filter)
            {
                filter.addFilter (context, "tl_work_flow.object_id", FIELD_ObjectID);
                return this;
            }

            public SearchAll andObjectCreated (QueryFilter<Date> filter)
            {
                filter.addFilter (context, "tl_work_flow.object_created_date", FIELD_ObjectCreated);
                return this;
            }

            public SearchAll andObjectLastModified (QueryFilter<Date> filter)
            {
                filter.addFilter (context, "tl_work_flow.object_last_updated_date", FIELD_ObjectLastModified);
                return this;
            }

            
            public SearchAll andName (QueryFilter<String> filter)
            {
                filter.addFilter (context, "tl_work_flow.name", "Name");
                return this;
            }
            
            public SearchAll andSortOrder (QueryFilter<Integer> filter)
            {
                filter.addFilter (context, "tl_work_flow.sort_order", "SortOrder");
                return this;
            }
            
            public SearchAll andApplicationStatus (QueryFilter<ApplicationStatus> filter)
            {
                filter.addFilter (context, "tl_work_flow.application_status", "ApplicationStatus");
                return this;
            }
            
            public SearchAll andJob (QueryFilter<Job> filter)
            {
                filter.addFilter (context, "tl_work_flow.job_id", "Job");
                return this;
            }
            
                public WorkFlow[]
             search (ObjectTransaction transaction) throws StorageException
        {
            
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_WorkFlow, SEARCH_All, criteria);
            Set<WorkFlow>   typedResults = new LinkedHashSet <WorkFlow> ();
            for (BaseBusinessClass bbcResult : results)
            {
                WorkFlow aResult = (WorkFlow)bbcResult;
                
                typedResults.add (aResult);
            }
            
                    return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new WorkFlow[0]);
                            
        }
    }

        
                public static WorkFlow[]
             searchAll (ObjectTransaction transaction) throws StorageException
        {
            return SearchByAll ()
            .search (transaction);
        }



    public Object getAttribute (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Name))
        {
            return HELPER_Name.toObject (getName ());
        }
        else if (attribName.equals (FIELD_SortOrder))
        {
            return HELPER_SortOrder.toObject (getSortOrder ());
        }
        else if (attribName.equals (FIELD_ApplicationStatus))
        {
            return HELPER_ApplicationStatus.toObject (getApplicationStatus ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Name))
        {
            return HELPER_Name;
        }
        else if (attribName.equals (FIELD_SortOrder))
        {
            return HELPER_SortOrder;
        }
        else if (attribName.equals (FIELD_ApplicationStatus))
        {
            return HELPER_ApplicationStatus;
        }
        else
        {
            return super.getAttributeHelper (attribName);
        }
    }


    public void setAttribute (String attribName, Object attribValue) throws FieldException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Name))
        {
            setName ((String)(HELPER_Name.fromObject (_Name, attribValue)));            
        }
        else if (attribName.equals (FIELD_SortOrder))
        {
            setSortOrder ((Integer)(HELPER_SortOrder.fromObject (_SortOrder, attribValue)));            
        }
        else if (attribName.equals (FIELD_ApplicationStatus))
        {
            setApplicationStatus ((ApplicationStatus)(HELPER_ApplicationStatus.fromObject (_ApplicationStatus, attribValue)));            
        }
        else
        {
            super.setAttribute (attribName, attribValue);
        }
    }



    public boolean isWriteable (String fieldName)
    {
        return getWriteable (fieldName) == FieldWriteability.TRUE;
    }


    public FieldWriteability getWriteable (String fieldName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (fieldName.equals (FIELD_Name))
        {
            return getWriteability_Name ();
        }
        else if (fieldName.equals (FIELD_SortOrder))
        {
            return getWriteability_SortOrder ();
        }
        else if (fieldName.equals (FIELD_ApplicationStatus))
        {
            return getWriteability_ApplicationStatus ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Job))
        {
            return getWriteability_Job ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


    public void putUnwriteable (Set<String> fields)
    {
        
            if (getWriteability_Name () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_Name);
            }
        
            if (getWriteability_SortOrder () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_SortOrder);
            }
        
            if (getWriteability_ApplicationStatus () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_ApplicationStatus);
            }
        

        super.putUnwriteable (fields);
    }

    public List<AbstractAttribute> getAttributes ()
    {
        List result = super.getAttributes ();

        
        result.add(HELPER_Name.getAttribObject (getClass (), _Name, true, FIELD_Name));
        result.add(HELPER_SortOrder.getAttribObject (getClass (), _SortOrder, true, FIELD_SortOrder));
        result.add(HELPER_ApplicationStatus.getAttribObject (getClass (), _ApplicationStatus, true, FIELD_ApplicationStatus));

        return result;
    }


    public Map getAttributeMetadata (String attribute)
    {
        if (ATTRIBUTES_METADATA_WorkFlow.containsKey (attribute))
        {
            return (Map)ATTRIBUTES_METADATA_WorkFlow.get (attribute);
        }
        else
        {
            return super.getAttributeMetadata (attribute);
        }
    }


    public Object getAttributeMetadata (String attribute, String metadata)
    {
        if (ATTRIBUTES_METADATA_WorkFlow.containsKey (attribute))
        {
            return ((Map)ATTRIBUTES_METADATA_WorkFlow.get (attribute)).get(metadata);
        }
        else
        {
            return super.getAttributeMetadata (attribute, metadata);
        }
    }

    public void preCommit (boolean willBeStored) throws Exception
    {
        super.preCommit(willBeStored);
        
        if(willBeStored)
        {
        
        }
    }

    public oneit.servlets.objstore.binary.BinaryContentHandler getBinaryContentHandler(String attribName)
    {
        
        
        return super.getBinaryContentHandler(attribName);
    }

    public static class WorkFlowBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<WorkFlow>
    {

    /**
     * Get the attribute Name
     */
    public String getName (WorkFlow obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute Name.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setName (WorkFlow obj, String newName) throws FieldException
    {
        return newName;
    }

    /**
     * Get the attribute SortOrder
     */
    public Integer getSortOrder (WorkFlow obj, Integer original)
    {
        return original;
    }

    /**
     * Change the value set for attribute SortOrder.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Integer setSortOrder (WorkFlow obj, Integer newSortOrder) throws FieldException
    {
        return newSortOrder;
    }

    /**
     * Get the attribute ApplicationStatus
     */
    public ApplicationStatus getApplicationStatus (WorkFlow obj, ApplicationStatus original)
    {
        return original;
    }

    /**
     * Change the value set for attribute ApplicationStatus.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public ApplicationStatus setApplicationStatus (WorkFlow obj, ApplicationStatus newApplicationStatus) throws FieldException
    {
        return newApplicationStatus;
    }

    }

    
    public ORMPipeLine pipes()
    {
        return new WorkFlowPipeLineFactory<WorkFlow, WorkFlow> ((WorkFlow)this);
    }
      
    /**
     * Use this instead of pipes() to get rid of type casting.
     */  
    public WorkFlowPipeLineFactory<WorkFlow, WorkFlow> pipelineWorkFlow()
    {
        return (WorkFlowPipeLineFactory<WorkFlow, WorkFlow>) pipes();
    }
    
    public static WorkFlowPipeLineFactory<WorkFlow, WorkFlow> pipesWorkFlow(Collection<WorkFlow> items)
    {
        return REFERENCE_WorkFlow.new WorkFlowPipeLineFactory<WorkFlow, WorkFlow> (items);
    }
    
    public static WorkFlowPipeLineFactory<WorkFlow, WorkFlow> pipesWorkFlow(WorkFlow[] _items)
    {
        return pipesWorkFlow(Arrays.asList (_items));
    }
    
    public static WorkFlowPipeLineFactory<WorkFlow, WorkFlow> pipesWorkFlow()
    {
        return pipesWorkFlow((Collection)null);
    }
    
    
    public class WorkFlowPipeLineFactory<From extends BaseBusinessClass, Me extends WorkFlow> extends BaseBusinessClass.ORMPipeLine<From, Me>    
    {
        public <Prev> WorkFlowPipeLineFactory (PipeLine<From, Prev> pipeLine, Pipe<Prev, Me> nextPipe)
        {
            super (pipeLine, nextPipe);
        }
        
        public WorkFlowPipeLineFactory (From seed)
        {
            super(seed);
        }

        public WorkFlowPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Name"))
            {
                return toName ();
            }                
            if (name.equals ("SortOrder"))
            {
                return toSortOrder ();
            }                
            if (name.equals ("ApplicationStatus"))
            {
                return toApplicationStatus ();
            }                
            if (name.equals ("Job"))
            {
                return toJob ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toName () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Name));  }
        
        public PipeLine<From, Integer> toSortOrder () { return pipe(new ORMAttributePipe<Me, Integer>(FIELD_SortOrder));  }
        
        public PipeLine<From, ApplicationStatus> toApplicationStatus () { return pipe(new ORMAttributePipe<Me, ApplicationStatus>(FIELD_ApplicationStatus));  }
        public Job.JobPipeLineFactory<From, Job> toJob () { return toJob (Filter.ALL); }

        public Job.JobPipeLineFactory<From, Job> toJob (Filter<Job> filter) 
        { 
            return Job.REFERENCE_Job.new JobPipeLineFactory<From, Job> (this, new ORMSingleAssocPipe<Me, Job>(SINGLEREFERENCE_Job, filter)); 
        }
        
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

class DummyWorkFlow extends WorkFlow
{
    // Default constructor primarily to support Externalisable
    public DummyWorkFlow()
    {
        super();
    }

    public void assertValid ()
    {
    }


    public Job getJob () throws StorageException
    {
        return (Job)(Job.DUMMY_Job);
    }


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getJobID ()
    {
        return Job.DUMMY_Job.getObjectID();
    }

}

    
