/*
 * 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 BaseCultureElement extends BaseBusinessClass
{
    // Reference instance for the object
    public static final CultureElement REFERENCE_CultureElement = new CultureElement ();

    // Reference instance for the object
    public static final CultureElement DUMMY_CultureElement = new DummyCultureElement ();

    // Static constants corresponding to field names
    
    public static final String FIELD_Description = "Description";
    public static final String FIELD_CultureClass = "CultureClass";
    public static final String MULTIPLEREFERENCE_Questions = "Questions";
    public static final String BACKREF_Questions = "";
    public static final String MULTIPLEREFERENCE_Ratings = "Ratings";
    public static final String BACKREF_Ratings = "";
    public static final String MULTIPLEREFERENCE_Narratives = "Narratives";
    public static final String BACKREF_Narratives = "";

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

    // Static constants corresponding to attribute helpers
    private static final DefaultAttributeHelper<CultureElement> HELPER_Description = DefaultAttributeHelper.INSTANCE;
    private static final EnumeratedAttributeHelper<CultureElement, CultureClass> HELPER_CultureClass =  new EnumeratedAttributeHelper<CultureElement, CultureClass> (CultureClass.FACTORY_CultureClass);
    

    // Private attributes corresponding to business object data
    private String _Description;
    private CultureClass _CultureClass;
    

    // Private attributes corresponding to single references
    

    // Private attributes corresponding to multiple references
    private MultipleAssociation<CultureElement, CultureElementQuestion> _Questions;
    private MultipleAssociation<CultureElement, CultureElementRating> _Ratings;
    private MultipleAssociation<CultureElement, CultureNarrative> _Narratives;
    

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

    // Arrays of validators for each attribute
    private static final AttributeValidator[] FIELD_Description_Validators;
    private static final AttributeValidator[] FIELD_CultureClass_Validators;
    

    // Arrays of behaviour decorators
    private static final CultureElementBehaviourDecorator[] CultureElement_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Questions = CultureElementQuestion.BACKREF_CultureElement;
            String tmp_Ratings = CultureElementRating.BACKREF_CultureElement;
            String tmp_Narratives = CultureNarrative.BACKREF_CultureElement;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Questions();
            setupAssocMetaData_Ratings();
            setupAssocMetaData_Narratives();
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_CultureClass_Validators = (AttributeValidator[])setupAttribMetaData_CultureClass(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_CultureElement.initialiseReference ();
            DUMMY_CultureElement.initialiseReference ();
            CultureElement_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(CultureElement.class).toArray(new CultureElementBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "CultureElement");
        metaInfo.put ("name", "Questions");
        metaInfo.put ("type", "CultureElementQuestion");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElement.Questions:", metaInfo);
        ATTRIBUTES_METADATA_CultureElement.put (MULTIPLEREFERENCE_Questions, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_Ratings()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "CultureElement");
        metaInfo.put ("name", "Ratings");
        metaInfo.put ("type", "CultureElementRating");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElement.Ratings:", metaInfo);
        ATTRIBUTES_METADATA_CultureElement.put (MULTIPLEREFERENCE_Ratings, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_Narratives()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "CultureElement");
        metaInfo.put ("name", "Narratives");
        metaInfo.put ("type", "CultureNarrative");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElement.Narratives:", metaInfo);
        ATTRIBUTES_METADATA_CultureElement.put (MULTIPLEREFERENCE_Narratives, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_Description(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "culture_element_desc");
        metaInfo.put ("length", "200");
        metaInfo.put ("name", "Description");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElement.Description:", metaInfo);
        ATTRIBUTES_METADATA_CultureElement.put (FIELD_Description, Collections.unmodifiableMap (metaInfo));

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

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

        metaInfo.put ("attribHelper", "EnumeratedAttributeHelper");
        metaInfo.put ("dbcol", "culture_class_code");
        metaInfo.put ("name", "CultureClass");
        metaInfo.put ("type", "CultureClass");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElement.CultureClass:", metaInfo);
        ATTRIBUTES_METADATA_CultureElement.put (FIELD_CultureClass, Collections.unmodifiableMap (metaInfo));

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

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


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


    


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

        
        _Description = (String)(HELPER_Description.initialise (_Description));
        _CultureClass = (CultureClass)(HELPER_CultureClass.initialise (_CultureClass));
    }


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

        _Questions = new MultipleAssociation<CultureElement, CultureElementQuestion> (this, MULTIPLEREFERENCE_Questions, CultureElementQuestion.SINGLEREFERENCE_CultureElement, CultureElementQuestion.REFERENCE_CultureElementQuestion);
        _Ratings = new MultipleAssociation<CultureElement, CultureElementRating> (this, MULTIPLEREFERENCE_Ratings, CultureElementRating.SINGLEREFERENCE_CultureElement, CultureElementRating.REFERENCE_CultureElementRating);
        _Narratives = new MultipleAssociation<CultureElement, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElement, CultureNarrative.REFERENCE_CultureNarrative);
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Questions = new MultipleAssociation<CultureElement, CultureElementQuestion> (this, MULTIPLEREFERENCE_Questions, CultureElementQuestion.SINGLEREFERENCE_CultureElement, CultureElementQuestion.REFERENCE_CultureElementQuestion);
        _Ratings = new MultipleAssociation<CultureElement, CultureElementRating> (this, MULTIPLEREFERENCE_Ratings, CultureElementRating.SINGLEREFERENCE_CultureElement, CultureElementRating.REFERENCE_CultureElementRating);
        _Narratives = new MultipleAssociation<CultureElement, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElement, CultureNarrative.REFERENCE_CultureNarrative);
        
        
        return this;
    }

    

    /**
     * Get the attribute Description
     */
    public String getDescription ()
    {
        assertValid();
        String valToReturn = _Description;

            for (CultureElementBehaviourDecorator bhd : CultureElement_BehaviourDecorators)
            {
                valToReturn = bhd.getDescription ((CultureElement)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 preDescriptionChange (String newDescription) 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 postDescriptionChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute Description.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setDescription (String newDescription) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_Description.compare (_Description, newDescription);
        
        try
        {
            for (CultureElementBehaviourDecorator bhd : CultureElement_BehaviourDecorators)
            {
                newDescription = bhd.setDescription ((CultureElement)this, newDescription);
                oldAndNewIdentical = HELPER_Description.compare (_Description, newDescription);
            }

            

            if (FIELD_Description_Validators.length > 0)
            {
                Object newDescriptionObj = HELPER_Description.toObject (newDescription);

                if (newDescriptionObj != null)
                {
                    int     loopMax = FIELD_Description_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_CultureElement.get (FIELD_Description);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_Description () != FieldWriteability.FALSE, "Field  Description is not writeable");
            preDescriptionChange (newDescription);
            markFieldChange (FIELD_Description);
            _Description = newDescription;            
            postFieldChange (FIELD_Description);
            postDescriptionChange ();
        }
    }

    /**
     * Get the attribute CultureClass
     */
    public CultureClass getCultureClass ()
    {
        assertValid();
        CultureClass valToReturn = _CultureClass;

            for (CultureElementBehaviourDecorator bhd : CultureElement_BehaviourDecorators)
            {
                valToReturn = bhd.getCultureClass ((CultureElement)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 preCultureClassChange (CultureClass newCultureClass) 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 postCultureClassChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute CultureClass.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setCultureClass (CultureClass newCultureClass) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_CultureClass.compare (_CultureClass, newCultureClass);
        
        try
        {
            for (CultureElementBehaviourDecorator bhd : CultureElement_BehaviourDecorators)
            {
                newCultureClass = bhd.setCultureClass ((CultureElement)this, newCultureClass);
                oldAndNewIdentical = HELPER_CultureClass.compare (_CultureClass, newCultureClass);
            }

            

            if (FIELD_CultureClass_Validators.length > 0)
            {
                Object newCultureClassObj = HELPER_CultureClass.toObject (newCultureClass);

                if (newCultureClassObj != null)
                {
                    int     loopMax = FIELD_CultureClass_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_CultureElement.get (FIELD_CultureClass);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_CultureClass () != FieldWriteability.FALSE, "Field  CultureClass is not writeable");
            preCultureClassChange (newCultureClass);
            markFieldChange (FIELD_CultureClass);
            _CultureClass = newCultureClass;            
            postFieldChange (FIELD_CultureClass);
            postCultureClassChange ();
        }
    }



    /**
     * 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("Questions");
        
        result.add("Ratings");
        
        result.add("Narratives");
        

        return result;
    }


    /**
     * Get the reference instance for the multi assoc name.
     */
    public BaseBusinessClass getMultiAssocReferenceInstance(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            return CultureElementQuestion.REFERENCE_CultureElementQuestion ;
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            return CultureElementRating.REFERENCE_CultureElementRating ;
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return CultureNarrative.REFERENCE_CultureNarrative ;
        }
        

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            return CultureElementQuestion.SINGLEREFERENCE_CultureElement ;
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            return CultureElementRating.SINGLEREFERENCE_CultureElement ;
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return CultureNarrative.SINGLEREFERENCE_CultureElement ;
        }
        

        return super.getMultiAssocBackReference(attribName);
    }


    /**
     * Get the assoc count for the multi assoc name.
     */
    public int getMultiAssocCount(String attribName) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            return this.getQuestionsCount();
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            return this.getRatingsCount();
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return this.getNarrativesCount();
        }
        

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            return this.getQuestionsAt(index);
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            return this.getRatingsAt(index);
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return this.getNarrativesAt(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_Questions.equals(attribName))
        {
            addToQuestions((CultureElementQuestion)newElement);

            return;
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            addToRatings((CultureElementRating)newElement);

            return;
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            addToNarratives((CultureNarrative)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_Questions.equals(attribName))
        {
            removeFromQuestions((CultureElementQuestion)oldElement);

            return;
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            removeFromRatings((CultureElementRating)oldElement);

            return;
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            removeFromNarratives((CultureNarrative)oldElement);

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


    protected void __loadMultiAssoc (String attribName, BaseBusinessClass[] elements)
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            _Questions.__loadAssociation (elements);
            return;
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            _Ratings.__loadAssociation (elements);
            return;
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            _Narratives.__loadAssociation (elements);
            return;
        }
        

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        
        if (MULTIPLEREFERENCE_Questions.equals(attribName))
        {
            return _Questions.isLoaded ();
        }
        
        if (MULTIPLEREFERENCE_Ratings.equals(attribName))
        {
            return _Ratings.isLoaded ();
        }
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return _Narratives.isLoaded ();
        }
        

        return super.__isMultiAssocLoaded(attribName);
    }

    

    public FieldWriteability getWriteability_Questions ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getQuestionsCount () throws StorageException
    {
        assertValid();
        return _Questions.getReferencedObjectsCount ();
    }


    public void addToQuestions (CultureElementQuestion newElement) throws StorageException
    {
        if (_Questions.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_Questions () != FieldWriteability.FALSE, "MultiAssoc Questions is not writeable (add)");
            _Questions.appendElement (newElement);

            try
            {
                if (newElement.getCultureElement () != this)
                {
                    newElement.setCultureElement ((CultureElement)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromQuestions (CultureElementQuestion elementToRemove) throws StorageException
    {
        if (_Questions.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_Questions () != FieldWriteability.FALSE, "MultiAssoc Questions is not writeable (remove)");
            _Questions.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getCultureElement () != null)
                {
                    elementToRemove.setCultureElement (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public CultureElementQuestion getQuestionsAt (int index) throws StorageException
    {
        return (CultureElementQuestion)(_Questions.getElementAt (index));
    }


    public SortedSet<CultureElementQuestion> getQuestionsSet () throws StorageException
    {
        return _Questions.getSet ();
    }

    public FieldWriteability getWriteability_Ratings ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getRatingsCount () throws StorageException
    {
        assertValid();
        return _Ratings.getReferencedObjectsCount ();
    }


    public void addToRatings (CultureElementRating newElement) throws StorageException
    {
        if (_Ratings.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_Ratings () != FieldWriteability.FALSE, "MultiAssoc Ratings is not writeable (add)");
            _Ratings.appendElement (newElement);

            try
            {
                if (newElement.getCultureElement () != this)
                {
                    newElement.setCultureElement ((CultureElement)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromRatings (CultureElementRating elementToRemove) throws StorageException
    {
        if (_Ratings.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_Ratings () != FieldWriteability.FALSE, "MultiAssoc Ratings is not writeable (remove)");
            _Ratings.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getCultureElement () != null)
                {
                    elementToRemove.setCultureElement (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public CultureElementRating getRatingsAt (int index) throws StorageException
    {
        return (CultureElementRating)(_Ratings.getElementAt (index));
    }


    public SortedSet<CultureElementRating> getRatingsSet () throws StorageException
    {
        return _Ratings.getSet ();
    }

    public FieldWriteability getWriteability_Narratives ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getNarrativesCount () throws StorageException
    {
        assertValid();
        return _Narratives.getReferencedObjectsCount ();
    }


    public void addToNarratives (CultureNarrative newElement) throws StorageException
    {
        if (_Narratives.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_Narratives () != FieldWriteability.FALSE, "MultiAssoc Narratives is not writeable (add)");
            _Narratives.appendElement (newElement);

            try
            {
                if (newElement.getCultureElement () != this)
                {
                    newElement.setCultureElement ((CultureElement)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromNarratives (CultureNarrative elementToRemove) throws StorageException
    {
        if (_Narratives.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_Narratives () != FieldWriteability.FALSE, "MultiAssoc Narratives is not writeable (remove)");
            _Narratives.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getCultureElement () != null)
                {
                    elementToRemove.setCultureElement (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public CultureNarrative getNarrativesAt (int index) throws StorageException
    {
        return (CultureNarrative)(_Narratives.getElementAt (index));
    }


    public SortedSet<CultureNarrative> getNarrativesSet () throws StorageException
    {
        return _Narratives.getSet ();
    }



    public void onDelete ()
    {
        try 
        {
            for(CultureElementQuestion referenced : CollectionUtils.reverse(getQuestionsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null CultureElement from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setCultureElement(null);
            }

            for(CultureElementRating referenced : CollectionUtils.reverse(getRatingsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null CultureElement from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setCultureElement(null);
            }

            for(CultureNarrative referenced : CollectionUtils.reverse(getNarrativesSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null CultureElement from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setCultureElement(null);
            }

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

        super.onDelete ();
    }


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


    public CultureElement referenceInstance ()
    {
        return REFERENCE_CultureElement;
    }


    public CultureElement getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getCultureElementByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_CultureElement;
    }


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





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

        tl_culture_elementPSet.setAttrib (FIELD_ObjectID, myID);
        tl_culture_elementPSet.setAttrib (FIELD_Description, HELPER_Description.toObject (_Description)); //
        tl_culture_elementPSet.setAttrib (FIELD_CultureClass, HELPER_CultureClass.toObject (_CultureClass)); //
        
    }


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

        
        PersistentSet   tl_culture_elementPSet = allSets.getPersistentSet (objectID, "tl_culture_element");

        _Description = (String)(HELPER_Description.fromObject (_Description, tl_culture_elementPSet.getAttrib (FIELD_Description))); //
        _CultureClass = (CultureClass)(HELPER_CultureClass.fromObject (_CultureClass, tl_culture_elementPSet.getAttrib (FIELD_CultureClass))); //
        
    }





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

        if (other instanceof CultureElement)
        {
            CultureElement otherCultureElement = (CultureElement)other;
            
            try
            {
                setDescription (otherCultureElement.getDescription ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
            
            try
            {
                setCultureClass (otherCultureElement.getCultureClass ());
            }
            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 BaseCultureElement)
        {
            BaseCultureElement sourceCultureElement = (BaseCultureElement)(source);

            _Description = sourceCultureElement._Description;
            _CultureClass = sourceCultureElement._CultureClass;
            
        }
    }


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

            _Questions.copyFrom (sourceCultureElement._Questions, linkToGhosts);
            _Ratings.copyFrom (sourceCultureElement._Ratings, linkToGhosts);
            _Narratives.copyFrom (sourceCultureElement._Narratives, 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);

        _Description = (String)(HELPER_Description.readExternal (_Description, vals.get(FIELD_Description))); //
        _CultureClass = (CultureClass)(HELPER_CultureClass.readExternal (_CultureClass, vals.get(FIELD_CultureClass))); //
        _Questions.readExternalData(vals.get(MULTIPLEREFERENCE_Questions));
        _Ratings.readExternalData(vals.get(MULTIPLEREFERENCE_Ratings));
        _Narratives.readExternalData(vals.get(MULTIPLEREFERENCE_Narratives));
        
    }


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

        vals.put (FIELD_Description, HELPER_Description.writeExternal (_Description));
        vals.put (FIELD_CultureClass, HELPER_CultureClass.writeExternal (_CultureClass));
        vals.put (MULTIPLEREFERENCE_Questions, _Questions.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Ratings, _Ratings.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Narratives, _Narratives.writeExternalData());
        
    }


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

        if (other instanceof BaseCultureElement)
        {
            BaseCultureElement otherCultureElement = (BaseCultureElement)(other);

            
            if (!HELPER_Description.compare(this._Description, otherCultureElement._Description))
            {
                listener.notifyFieldChange(this, other, FIELD_Description, HELPER_Description.toObject(this._Description), HELPER_Description.toObject(otherCultureElement._Description));
            }
            if (!HELPER_CultureClass.compare(this._CultureClass, otherCultureElement._CultureClass))
            {
                listener.notifyFieldChange(this, other, FIELD_CultureClass, HELPER_CultureClass.toObject(this._CultureClass), HELPER_CultureClass.toObject(otherCultureElement._CultureClass));
            }

            // Compare single assocs
            

            // Compare multiple assocs
            _Questions.compare (otherCultureElement._Questions, listener);
            _Ratings.compare (otherCultureElement._Ratings, listener);
            _Narratives.compare (otherCultureElement._Narratives, 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_Description, HELPER_Description.toObject(getDescription()));
        visitor.visitField(this, FIELD_CultureClass, HELPER_CultureClass.toObject(getCultureClass()));
        visitor.visitAssociation (_Questions);
        visitor.visitAssociation (_Ratings);
        visitor.visitAssociation (_Narratives);
        
    }


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

        if (scope.includes (_Questions))
        {
            visitor.visit (_Questions);
        }
        if (scope.includes (_Ratings))
        {
            visitor.visit (_Ratings);
        }
        if (scope.includes (_Narratives))
        {
            visitor.visit (_Narratives);
        }
        
    }


    public static CultureElement createCultureElement (ObjectTransaction transaction) throws StorageException
    {
        CultureElement     result = new CultureElement ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static CultureElement getCultureElementByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (CultureElement)(transaction.getObjectByID (REFERENCE_CultureElement, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            return filter.matches (getDescription ());
        }
        else if (attribName.equals (FIELD_CultureClass))
        {
            return filter.matches (getCultureClass ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
            public SearchAll andDescription (QueryFilter<String> filter)
            {
                filter.addFilter (context, "tl_culture_element.culture_element_desc", "Description");
                return this;
            }
            
            public SearchAll andCultureClass (QueryFilter<CultureClass> filter)
            {
                filter.addFilter (context, "tl_culture_element.culture_class_code", "CultureClass");
                return this;
            }
            
                public CultureElement[]
             search (ObjectTransaction transaction) throws StorageException
        {
            
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_CultureElement, SEARCH_All, criteria);
            Set<CultureElement>   typedResults = new LinkedHashSet <CultureElement> ();
            for (BaseBusinessClass bbcResult : results)
            {
                CultureElement aResult = (CultureElement)bbcResult;
                
                typedResults.add (aResult);
            }
            
                    return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new CultureElement[0]);
                            
        }
    }

        
                public static CultureElement[]
             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_Description))
        {
            return HELPER_Description.toObject (getDescription ());
        }
        else if (attribName.equals (FIELD_CultureClass))
        {
            return HELPER_CultureClass.toObject (getCultureClass ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            return HELPER_Description;
        }
        else if (attribName.equals (FIELD_CultureClass))
        {
            return HELPER_CultureClass;
        }
        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_Description))
        {
            setDescription ((String)(HELPER_Description.fromObject (_Description, attribValue)));            
        }
        else if (attribName.equals (FIELD_CultureClass))
        {
            setCultureClass ((CultureClass)(HELPER_CultureClass.fromObject (_CultureClass, 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_Description))
        {
            return getWriteability_Description ();
        }
        else if (fieldName.equals (FIELD_CultureClass))
        {
            return getWriteability_CultureClass ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Questions))
        {
            return getWriteability_Questions ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Ratings))
        {
            return getWriteability_Ratings ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Narratives))
        {
            return getWriteability_Narratives ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


    public void putUnwriteable (Set<String> fields)
    {
        
            if (getWriteability_Description () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_Description);
            }
        
            if (getWriteability_CultureClass () != FieldWriteability.TRUE)
            {
                fields.add (FIELD_CultureClass);
            }
        

        super.putUnwriteable (fields);
    }

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

        
        result.add(HELPER_Description.getAttribObject (getClass (), _Description, false, FIELD_Description));
        result.add(HELPER_CultureClass.getAttribObject (getClass (), _CultureClass, false, FIELD_CultureClass));

        return result;
    }


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


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

    /**
     * Get the attribute Description
     */
    public String getDescription (CultureElement obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute Description.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setDescription (CultureElement obj, String newDescription) throws FieldException
    {
        return newDescription;
    }

    /**
     * Get the attribute CultureClass
     */
    public CultureClass getCultureClass (CultureElement obj, CultureClass original)
    {
        return original;
    }

    /**
     * Change the value set for attribute CultureClass.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public CultureClass setCultureClass (CultureElement obj, CultureClass newCultureClass) throws FieldException
    {
        return newCultureClass;
    }

    }

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

        public CultureElementPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Questions"))
            {
                return toQuestions ();
            }                
            if (name.equals ("Ratings"))
            {
                return toRatings ();
            }                
            if (name.equals ("Narratives"))
            {
                return toNarratives ();
            }                
            if (name.equals ("Description"))
            {
                return toDescription ();
            }                
            if (name.equals ("CultureClass"))
            {
                return toCultureClass ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Description));  }
        
        public PipeLine<From, CultureClass> toCultureClass () { return pipe(new ORMAttributePipe<Me, CultureClass>(FIELD_CultureClass));  }
        public CultureElementQuestion.CultureElementQuestionPipeLineFactory<From, CultureElementQuestion> toQuestions () { return toQuestions(Filter.ALL); }        

        public CultureElementQuestion.CultureElementQuestionPipeLineFactory<From, CultureElementQuestion> toQuestions (Filter<CultureElementQuestion> filter)
        {
            return CultureElementQuestion.REFERENCE_CultureElementQuestion.new CultureElementQuestionPipeLineFactory<From, CultureElementQuestion> (this, new ORMMultiAssocPipe<Me, CultureElementQuestion>(MULTIPLEREFERENCE_Questions, filter)); 
        }
        public CultureElementRating.CultureElementRatingPipeLineFactory<From, CultureElementRating> toRatings () { return toRatings(Filter.ALL); }        

        public CultureElementRating.CultureElementRatingPipeLineFactory<From, CultureElementRating> toRatings (Filter<CultureElementRating> filter)
        {
            return CultureElementRating.REFERENCE_CultureElementRating.new CultureElementRatingPipeLineFactory<From, CultureElementRating> (this, new ORMMultiAssocPipe<Me, CultureElementRating>(MULTIPLEREFERENCE_Ratings, filter)); 
        }
        public CultureNarrative.CultureNarrativePipeLineFactory<From, CultureNarrative> toNarratives () { return toNarratives(Filter.ALL); }        

        public CultureNarrative.CultureNarrativePipeLineFactory<From, CultureNarrative> toNarratives (Filter<CultureNarrative> filter)
        {
            return CultureNarrative.REFERENCE_CultureNarrative.new CultureNarrativePipeLineFactory<From, CultureNarrative> (this, new ORMMultiAssocPipe<Me, CultureNarrative>(MULTIPLEREFERENCE_Narratives, filter)); 
        }
        
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


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


    public CultureElementQuestion getQuestionsAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Questions");
    }


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

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


    public CultureElementRating getRatingsAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Ratings");
    }


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

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


    public CultureNarrative getNarrativesAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Narratives");
    }


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

}

    
