/*
 * 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;



public abstract class BaseLevel extends BaseBusinessClass
{
    // Reference instance for the object
    public static final Level REFERENCE_Level = new Level ();

    // Reference instance for the object
    public static final Level DUMMY_Level = new DummyLevel ();

    // Static constants corresponding to field names
    
    public static final String FIELD_LevelNo = "LevelNo";
    public static final String FIELD_LevelDescription = "LevelDescription";
    public static final String FIELD_LevelNotes = "LevelNotes";
    public static final String MULTIPLEREFERENCE_Factors = "Factors";
    public static final String BACKREF_Factors = "";

    // Static constants corresponding to searches
    

    // Static constants corresponding to attribute helpers
    private static final DefaultAttributeHelper<Level> HELPER_LevelNo = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<Level> HELPER_LevelDescription = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<Level> HELPER_LevelNotes = DefaultAttributeHelper.INSTANCE;
    

    // Private attributes corresponding to business object data
    private Integer _LevelNo;
    private String _LevelDescription;
    private String _LevelNotes;
    

    // Private attributes corresponding to single references
    

    // Private attributes corresponding to multiple references
    private MultipleAssociation<Level, Factor> _Factors;
    

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

    // Arrays of validators for each attribute
    private static final AttributeValidator[] FIELD_LevelNo_Validators;
    private static final AttributeValidator[] FIELD_LevelDescription_Validators;
    private static final AttributeValidator[] FIELD_LevelNotes_Validators;
    

    // Arrays of behaviour decorators
    private static final LevelBehaviourDecorator[] Level_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Factors = Factor.BACKREF_Level;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Factors();
            FIELD_LevelNo_Validators = (AttributeValidator[])setupAttribMetaData_LevelNo(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_LevelDescription_Validators = (AttributeValidator[])setupAttribMetaData_LevelDescription(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_LevelNotes_Validators = (AttributeValidator[])setupAttribMetaData_LevelNotes(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_Level.initialiseReference ();
            DUMMY_Level.initialiseReference ();
            Level_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(Level.class).toArray(new LevelBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "Level");
        metaInfo.put ("name", "Factors");
        metaInfo.put ("type", "Factor");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Level.Factors:", metaInfo);
        ATTRIBUTES_METADATA_Level.put (MULTIPLEREFERENCE_Factors, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_LevelNo(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "level_number");
        metaInfo.put ("name", "LevelNo");
        metaInfo.put ("type", "Integer");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Level.LevelNo:", metaInfo);
        ATTRIBUTES_METADATA_Level.put (FIELD_LevelNo, Collections.unmodifiableMap (metaInfo));

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

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

        metaInfo.put ("dbcol", "level_desc");
        metaInfo.put ("length", "30");
        metaInfo.put ("name", "LevelDescription");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Level.LevelDescription:", metaInfo);
        ATTRIBUTES_METADATA_Level.put (FIELD_LevelDescription, Collections.unmodifiableMap (metaInfo));

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

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

        metaInfo.put ("dbcol", "level_notes");
        metaInfo.put ("name", "LevelNotes");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Level.LevelNotes:", metaInfo);
        ATTRIBUTES_METADATA_Level.put (FIELD_LevelNotes, Collections.unmodifiableMap (metaInfo));

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

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


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


    


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

        
        _LevelNo = (Integer)(HELPER_LevelNo.initialise (_LevelNo));
        _LevelDescription = (String)(HELPER_LevelDescription.initialise (_LevelDescription));
        _LevelNotes = (String)(HELPER_LevelNotes.initialise (_LevelNotes));
    }


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

        _Factors = new MultipleAssociation<Level, Factor> (this, MULTIPLEREFERENCE_Factors, Factor.SINGLEREFERENCE_Level, Factor.REFERENCE_Factor);
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Factors = new MultipleAssociation<Level, Factor> (this, MULTIPLEREFERENCE_Factors, Factor.SINGLEREFERENCE_Level, Factor.REFERENCE_Factor);
        
        
        return this;
    }

    

    /**
     * Get the attribute LevelNo
     */
    public Integer getLevelNo ()
    {
        assertValid();
        Integer valToReturn = _LevelNo;

            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                valToReturn = bhd.getLevelNo ((Level)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 preLevelNoChange (Integer newLevelNo) 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 postLevelNoChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute LevelNo.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setLevelNo (Integer newLevelNo) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_LevelNo.compare (_LevelNo, newLevelNo);
        
        try
        {
            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                newLevelNo = bhd.setLevelNo ((Level)this, newLevelNo);
                oldAndNewIdentical = HELPER_LevelNo.compare (_LevelNo, newLevelNo);
            }

            

            if (FIELD_LevelNo_Validators.length > 0)
            {
                Object newLevelNoObj = HELPER_LevelNo.toObject (newLevelNo);

                if (newLevelNoObj != null)
                {
                    int     loopMax = FIELD_LevelNo_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_Level.get (FIELD_LevelNo);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_LevelNo () != FieldWriteability.FALSE, "Field  LevelNo is not writeable");
            preLevelNoChange (newLevelNo);
            markFieldChange (FIELD_LevelNo);
            _LevelNo = newLevelNo;            
            postFieldChange (FIELD_LevelNo);
            postLevelNoChange ();
        }
    }

    /**
     * Get the attribute LevelDescription
     */
    public String getLevelDescription ()
    {
        assertValid();
        String valToReturn = _LevelDescription;

            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                valToReturn = bhd.getLevelDescription ((Level)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 preLevelDescriptionChange (String newLevelDescription) 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 postLevelDescriptionChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute LevelDescription.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setLevelDescription (String newLevelDescription) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_LevelDescription.compare (_LevelDescription, newLevelDescription);
        
        try
        {
            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                newLevelDescription = bhd.setLevelDescription ((Level)this, newLevelDescription);
                oldAndNewIdentical = HELPER_LevelDescription.compare (_LevelDescription, newLevelDescription);
            }

            

            if (FIELD_LevelDescription_Validators.length > 0)
            {
                Object newLevelDescriptionObj = HELPER_LevelDescription.toObject (newLevelDescription);

                if (newLevelDescriptionObj != null)
                {
                    int     loopMax = FIELD_LevelDescription_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_Level.get (FIELD_LevelDescription);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_LevelDescription () != FieldWriteability.FALSE, "Field  LevelDescription is not writeable");
            preLevelDescriptionChange (newLevelDescription);
            markFieldChange (FIELD_LevelDescription);
            _LevelDescription = newLevelDescription;            
            postFieldChange (FIELD_LevelDescription);
            postLevelDescriptionChange ();
        }
    }

    /**
     * Get the attribute LevelNotes
     */
    public String getLevelNotes ()
    {
        assertValid();
        String valToReturn = _LevelNotes;

            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                valToReturn = bhd.getLevelNotes ((Level)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 preLevelNotesChange (String newLevelNotes) 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 postLevelNotesChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute LevelNotes.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setLevelNotes (String newLevelNotes) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_LevelNotes.compare (_LevelNotes, newLevelNotes);
        
        try
        {
            for (LevelBehaviourDecorator bhd : Level_BehaviourDecorators)
            {
                newLevelNotes = bhd.setLevelNotes ((Level)this, newLevelNotes);
                oldAndNewIdentical = HELPER_LevelNotes.compare (_LevelNotes, newLevelNotes);
            }

            

            if (FIELD_LevelNotes_Validators.length > 0)
            {
                Object newLevelNotesObj = HELPER_LevelNotes.toObject (newLevelNotes);

                if (newLevelNotesObj != null)
                {
                    int     loopMax = FIELD_LevelNotes_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_Level.get (FIELD_LevelNotes);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_LevelNotes () != FieldWriteability.FALSE, "Field  LevelNotes is not writeable");
            preLevelNotesChange (newLevelNotes);
            markFieldChange (FIELD_LevelNotes);
            _LevelNotes = newLevelNotes;            
            postFieldChange (FIELD_LevelNotes);
            postLevelNotesChange ();
        }
    }



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

        

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


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


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


    public Long getSingleAssocID (String assocName) throws StorageException
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        
        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
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



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

        
        result.add("Factors");
        

        return result;
    }


    /**
     * Get the reference instance for the multi assoc name.
     */
    public BaseBusinessClass getMultiAssocReferenceInstance(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return Factor.REFERENCE_Factor ;
        }
        

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return Factor.SINGLEREFERENCE_Level ;
        }
        

        return super.getMultiAssocBackReference(attribName);
    }


    /**
     * Get the assoc count for the multi assoc name.
     */
    public int getMultiAssocCount(String attribName) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return this.getFactorsCount();
        }
        

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return this.getFactorsAt(index);
        }
        

        return super.getMultiAssocAt(attribName, index);
    }


    /**
     * Add to a multi assoc by attribute name
     */
    public void addToMultiAssoc(String attribName, BaseBusinessClass newElement) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            addToFactors((Factor)newElement);

            return;
        }
        

        super.addToMultiAssoc(attribName, newElement);
    }

    /**
     * Remove from a multi assoc by attribute name
     */
    public void removeFromMultiAssoc(String attribName, BaseBusinessClass oldElement) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            removeFromFactors((Factor)oldElement);

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


    protected void __loadMultiAssoc (String attribName, BaseBusinessClass[] elements)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            _Factors.__loadAssociation (elements);
            return;
        }
        

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return _Factors.isLoaded ();
        }
        

        return super.__isMultiAssocLoaded(attribName);
    }

    

    public FieldWriteability getWriteability_Factors ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getFactorsCount () throws StorageException
    {
        assertValid();
        return _Factors.getReferencedObjectsCount ();
    }


    public void addToFactors (Factor newElement) throws StorageException
    {
        if (_Factors.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_Factors () != FieldWriteability.FALSE, "MultiAssoc Factors is not writeable (add)");
            _Factors.appendElement (newElement);

            try
            {
                if (newElement.getLevel () != this)
                {
                    newElement.setLevel ((Level)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromFactors (Factor elementToRemove) throws StorageException
    {
        if (_Factors.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_Factors () != FieldWriteability.FALSE, "MultiAssoc Factors is not writeable (remove)");
            _Factors.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getLevel () != null)
                {
                    elementToRemove.setLevel (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public Factor getFactorsAt (int index) throws StorageException
    {
        return (Factor)(_Factors.getElementAt (index));
    }


    public SortedSet<Factor> getFactorsSet () throws StorageException
    {
        return _Factors.getSet ();
    }



    public void onDelete ()
    {
        try 
        {
            for(Factor referenced : CollectionUtils.reverse(getFactorsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Level from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setLevel(null);
            }

        }
        catch (Exception e)
        {
            throw NestedException.wrap(e);
        }

        super.onDelete ();
    }


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


    public Level referenceInstance ()
    {
        return REFERENCE_Level;
    }


    public Level getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getLevelByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_Level;
    }


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





    /**
     * 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   levelPSet = allSets.getPersistentSet (myID, "level", myPSetStatus);

        levelPSet.setAttrib (FIELD_ObjectID, myID);
        levelPSet.setAttrib (FIELD_LevelNo, HELPER_LevelNo.toObject (_LevelNo)); //
        levelPSet.setAttrib (FIELD_LevelDescription, HELPER_LevelDescription.toObject (_LevelDescription)); //
        levelPSet.setAttrib (FIELD_LevelNotes, HELPER_LevelNotes.toObject (_LevelNotes)); //
        
    }


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

        
        PersistentSet   levelPSet = allSets.getPersistentSet (objectID, "level");

        _LevelNo = (Integer)(HELPER_LevelNo.fromObject (_LevelNo, levelPSet.getAttrib (FIELD_LevelNo))); //
        _LevelDescription = (String)(HELPER_LevelDescription.fromObject (_LevelDescription, levelPSet.getAttrib (FIELD_LevelDescription))); //
        _LevelNotes = (String)(HELPER_LevelNotes.fromObject (_LevelNotes, levelPSet.getAttrib (FIELD_LevelNotes))); //
        
    }





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

        if (other instanceof Level)
        {
            Level otherLevel = (Level)other;
            
            try
            {
                setLevelNo (otherLevel.getLevelNo ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
            try
            {
                setLevelDescription (otherLevel.getLevelDescription ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
            try
            {
                setLevelNotes (otherLevel.getLevelNotes ());
            }
            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 BaseLevel)
        {
            BaseLevel sourceLevel = (BaseLevel)(source);

            _LevelNo = sourceLevel._LevelNo;
            _LevelDescription = sourceLevel._LevelDescription;
            _LevelNotes = sourceLevel._LevelNotes;
            
        }
    }


    /**
     * 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 BaseLevel)
        {
            BaseLevel sourceLevel = (BaseLevel)(source);

            
        }
    }


    /**
     * 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 BaseLevel)
        {
            BaseLevel sourceLevel = (BaseLevel)(source);

            _Factors.copyFrom (sourceLevel._Factors, linkToGhosts);
            
        }
    }


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

        
    }


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

        _LevelNo = (Integer)(HELPER_LevelNo.readExternal (_LevelNo, vals.get(FIELD_LevelNo))); //
        _LevelDescription = (String)(HELPER_LevelDescription.readExternal (_LevelDescription, vals.get(FIELD_LevelDescription))); //
        _LevelNotes = (String)(HELPER_LevelNotes.readExternal (_LevelNotes, vals.get(FIELD_LevelNotes))); //
        _Factors.readExternalData(vals.get(MULTIPLEREFERENCE_Factors));
        
    }


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

        vals.put (FIELD_LevelNo, HELPER_LevelNo.writeExternal (_LevelNo));
        vals.put (FIELD_LevelDescription, HELPER_LevelDescription.writeExternal (_LevelDescription));
        vals.put (FIELD_LevelNotes, HELPER_LevelNotes.writeExternal (_LevelNotes));
        vals.put (MULTIPLEREFERENCE_Factors, _Factors.writeExternalData());
        
    }


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

        if (other instanceof BaseLevel)
        {
            BaseLevel otherLevel = (BaseLevel)(other);

            
            if (!HELPER_LevelNo.compare(this._LevelNo, otherLevel._LevelNo))
            {
                listener.notifyFieldChange(this, other, FIELD_LevelNo, HELPER_LevelNo.toObject(this._LevelNo), HELPER_LevelNo.toObject(otherLevel._LevelNo));
            }
            if (!HELPER_LevelDescription.compare(this._LevelDescription, otherLevel._LevelDescription))
            {
                listener.notifyFieldChange(this, other, FIELD_LevelDescription, HELPER_LevelDescription.toObject(this._LevelDescription), HELPER_LevelDescription.toObject(otherLevel._LevelDescription));
            }
            if (!HELPER_LevelNotes.compare(this._LevelNotes, otherLevel._LevelNotes))
            {
                listener.notifyFieldChange(this, other, FIELD_LevelNotes, HELPER_LevelNotes.toObject(this._LevelNotes), HELPER_LevelNotes.toObject(otherLevel._LevelNotes));
            }

            // Compare single assocs
            

            // Compare multiple assocs
            _Factors.compare (otherLevel._Factors, listener);
            
        }
    }


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

        
    }


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

        visitor.visitField(this, FIELD_LevelNo, HELPER_LevelNo.toObject(getLevelNo()));
        visitor.visitField(this, FIELD_LevelDescription, HELPER_LevelDescription.toObject(getLevelDescription()));
        visitor.visitField(this, FIELD_LevelNotes, HELPER_LevelNotes.toObject(getLevelNotes()));
        visitor.visitAssociation (_Factors);
        
    }


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

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


    public static Level createLevel (ObjectTransaction transaction) throws StorageException
    {
        Level     result = new Level ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static Level getLevelByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (Level)(transaction.getObjectByID (REFERENCE_Level, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_LevelNo))
        {
            return filter.matches (getLevelNo ());
        }
        else if (attribName.equals (FIELD_LevelDescription))
        {
            return filter.matches (getLevelDescription ());
        }
        else if (attribName.equals (FIELD_LevelNotes))
        {
            return filter.matches (getLevelNotes ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }




    public Object getAttribute (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_LevelNo))
        {
            return HELPER_LevelNo.toObject (getLevelNo ());
        }
        else if (attribName.equals (FIELD_LevelDescription))
        {
            return HELPER_LevelDescription.toObject (getLevelDescription ());
        }
        else if (attribName.equals (FIELD_LevelNotes))
        {
            return HELPER_LevelNotes.toObject (getLevelNotes ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_LevelNo))
        {
            return HELPER_LevelNo;
        }
        else if (attribName.equals (FIELD_LevelDescription))
        {
            return HELPER_LevelDescription;
        }
        else if (attribName.equals (FIELD_LevelNotes))
        {
            return HELPER_LevelNotes;
        }
        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_LevelNo))
        {
            setLevelNo ((Integer)(HELPER_LevelNo.fromObject (_LevelNo, attribValue)));            
        }
        else if (attribName.equals (FIELD_LevelDescription))
        {
            setLevelDescription ((String)(HELPER_LevelDescription.fromObject (_LevelDescription, attribValue)));            
        }
        else if (attribName.equals (FIELD_LevelNotes))
        {
            setLevelNotes ((String)(HELPER_LevelNotes.fromObject (_LevelNotes, 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_LevelNo))
        {
            return getWriteability_LevelNo ();
        }
        else if (fieldName.equals (FIELD_LevelDescription))
        {
            return getWriteability_LevelDescription ();
        }
        else if (fieldName.equals (FIELD_LevelNotes))
        {
            return getWriteability_LevelNotes ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Factors))
        {
            return getWriteability_Factors ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


    public void putUnwriteable (Set<String> fields)
    {
        
            if (getWriteability_LevelNo () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_LevelNo);
            }
        
            if (getWriteability_LevelDescription () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_LevelDescription);
            }
        
            if (getWriteability_LevelNotes () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_LevelNotes);
            }
        

        super.putUnwriteable (fields);
    }

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

        
        result.add(HELPER_LevelNo.getAttribObject (getClass (), _LevelNo, false, FIELD_LevelNo));
        result.add(HELPER_LevelDescription.getAttribObject (getClass (), _LevelDescription, false, FIELD_LevelDescription));
        result.add(HELPER_LevelNotes.getAttribObject (getClass (), _LevelNotes, false, FIELD_LevelNotes));

        return result;
    }


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


    public Object getAttributeMetadata (String attribute, String metadata)
    {
        if (ATTRIBUTES_METADATA_Level.containsKey (attribute))
        {
            return ((Map)ATTRIBUTES_METADATA_Level.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 LevelBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<Level>
    {

    /**
     * Get the attribute LevelNo
     */
    public Integer getLevelNo (Level obj, Integer original)
    {
        return original;
    }

    /**
     * Change the value set for attribute LevelNo.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Integer setLevelNo (Level obj, Integer newLevelNo) throws FieldException
    {
        return newLevelNo;
    }

    /**
     * Get the attribute LevelDescription
     */
    public String getLevelDescription (Level obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute LevelDescription.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setLevelDescription (Level obj, String newLevelDescription) throws FieldException
    {
        return newLevelDescription;
    }

    /**
     * Get the attribute LevelNotes
     */
    public String getLevelNotes (Level obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute LevelNotes.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setLevelNotes (Level obj, String newLevelNotes) throws FieldException
    {
        return newLevelNotes;
    }

    }

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

        public LevelPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Factors"))
            {
                return toFactors ();
            }                
            if (name.equals ("LevelNo"))
            {
                return toLevelNo ();
            }                
            if (name.equals ("LevelDescription"))
            {
                return toLevelDescription ();
            }                
            if (name.equals ("LevelNotes"))
            {
                return toLevelNotes ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, Integer> toLevelNo () { return pipe(new ORMAttributePipe<Me, Integer>(FIELD_LevelNo));  }
        
        public PipeLine<From, String> toLevelDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_LevelDescription));  }
        
        public PipeLine<From, String> toLevelNotes () { return pipe(new ORMAttributePipe<Me, String>(FIELD_LevelNotes));  }
        public Factor.FactorPipeLineFactory<From, Factor> toFactors () { return toFactors(Filter.ALL); }        

        public Factor.FactorPipeLineFactory<From, Factor> toFactors (Filter<Factor> filter)
        {
            return Factor.REFERENCE_Factor.new FactorPipeLineFactory<From, Factor> (this, new ORMMultiAssocPipe<Me, Factor>(MULTIPLEREFERENCE_Factors, filter)); 
        }
        
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public int getFactorsCount () throws StorageException
    {
        return 0;
    }


    public Factor getFactorsAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Factors");
    }


    public SortedSet getFactorsSet () throws StorageException
    {
        return new TreeSet();
    }

}

    
