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

    // Reference instance for the object
    public static final CultureElementQuestion DUMMY_CultureElementQuestion = new DummyCultureElementQuestion ();

    // Static constants corresponding to field names
    
    public static final String FIELD_Description = "Description";
    public static final String SINGLEREFERENCE_CultureElement = "CultureElement";
    public static final String BACKREF_CultureElement = "";
    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<CultureElementQuestion> HELPER_Description = DefaultAttributeHelper.INSTANCE;
    

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

    // Private attributes corresponding to single references
    private SingleAssociation<CultureElementQuestion, CultureElement> _CultureElement;
    

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

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

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

    // Arrays of behaviour decorators
    private static final CultureElementQuestionBehaviourDecorator[] CultureElementQuestion_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Narratives = CultureNarrative.BACKREF_CultureElementQuestion;
            String tmp_CultureElement = CultureElement.BACKREF_Questions;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Narratives();
            setupAssocMetaData_CultureElement();
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_CultureElementQuestion.initialiseReference ();
            DUMMY_CultureElementQuestion.initialiseReference ();
            CultureElementQuestion_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(CultureElementQuestion.class).toArray(new CultureElementQuestionBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

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

        metaInfo.put ("backreferenceName", "Questions");
        metaInfo.put ("dbcol", "culture_element_number");
        metaInfo.put ("name", "CultureElement");
        metaInfo.put ("type", "CultureElement");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElementQuestion.CultureElement:", metaInfo);
        ATTRIBUTES_METADATA_CultureElementQuestion.put (SINGLEREFERENCE_CultureElement, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_Description(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

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

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

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


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


    


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

        
        _Description = (String)(HELPER_Description.initialise (_Description));
    }


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

        _CultureElement = new SingleAssociation<CultureElementQuestion, CultureElement> (this, SINGLEREFERENCE_CultureElement, CultureElement.MULTIPLEREFERENCE_Questions, CultureElement.REFERENCE_CultureElement, "tl_culture_element_quest");
        _Narratives = new MultipleAssociation<CultureElementQuestion, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElementQuestion, CultureNarrative.REFERENCE_CultureNarrative);
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _CultureElement = new SingleAssociation<CultureElementQuestion, CultureElement> (this, SINGLEREFERENCE_CultureElement, CultureElement.MULTIPLEREFERENCE_Questions, CultureElement.REFERENCE_CultureElement, "tl_culture_element_quest");
        _Narratives = new MultipleAssociation<CultureElementQuestion, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElementQuestion, CultureNarrative.REFERENCE_CultureNarrative);
        
        
        return this;
    }

    

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

            for (CultureElementQuestionBehaviourDecorator bhd : CultureElementQuestion_BehaviourDecorators)
            {
                valToReturn = bhd.getDescription ((CultureElementQuestion)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 (CultureElementQuestionBehaviourDecorator bhd : CultureElementQuestion_BehaviourDecorators)
            {
                newDescription = bhd.setDescription ((CultureElementQuestion)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_CultureElementQuestion.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 ();
        }
    }



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

        
        result.add("CultureElement");
        

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


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


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


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


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



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


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


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

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


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



    
    public FieldWriteability getWriteability_CultureElement ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    /**
     * Set the reference CultureElement.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setCultureElement (CultureElement newCultureElement) throws StorageException, FieldException
    {
        

        if (_CultureElement.wouldReferencedChange (newCultureElement))
        {
            assertValid();
            Debug.assertion (getWriteability_CultureElement () != FieldWriteability.FALSE, "Assoc CultureElement is not writeable");
            preCultureElementChange (newCultureElement);
            CultureElement oldCultureElement = getCultureElement ();

                    if (oldCultureElement != null)
                    {
                        // This is to stop validation from triggering when we are removed
                        _CultureElement.set (null);
                        oldCultureElement.removeFromQuestions ((CultureElementQuestion)(this));
                    }

                    _CultureElement.set (newCultureElement);

                    if (newCultureElement != null)
                    {
                        newCultureElement.addToQuestions ((CultureElementQuestion)(this));
                    }
                
            postCultureElementChange ();
        }
    }

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

        
        result.add("Narratives");
        

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            return CultureNarrative.SINGLEREFERENCE_CultureElementQuestion ;
        }
        

        return super.getMultiAssocBackReference(attribName);
    }


    /**
     * Get the assoc count for the multi assoc name.
     */
    public int getMultiAssocCount(String attribName) throws StorageException
    {
        
        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_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_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_Narratives.equals(attribName))
        {
            removeFromNarratives((CultureNarrative)oldElement);

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


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

        return super.__isMultiAssocLoaded(attribName);
    }

    

    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.getCultureElementQuestion () != this)
                {
                    newElement.setCultureElementQuestion ((CultureElementQuestion)(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.getCultureElementQuestion () != null)
                {
                    elementToRemove.setCultureElementQuestion (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 
        {
            // Ensure we are removed from any loaded multi-associations that aren't mandatory
            if (_CultureElement.isLoaded () || getTransaction ().isObjectLoaded (_CultureElement.getReferencedType (), getCultureElementID ()))
            {
                CultureElement  referenced = getCultureElement ();

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Questions from ", getObjectID (), " to ", referenced.getObjectID ());
                    _CultureElement.set (null);
                    referenced.removeFromQuestions ((CultureElementQuestion)this);
                }
            }
        
            for(CultureNarrative referenced : CollectionUtils.reverse(getNarrativesSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null CultureElementQuestion from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setCultureElementQuestion(null);
            }

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

        super.onDelete ();
    }


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


    public CultureElementQuestion referenceInstance ()
    {
        return REFERENCE_CultureElementQuestion;
    }


    public CultureElementQuestion getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getCultureElementQuestionByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_CultureElementQuestion;
    }


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





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

        tl_culture_element_questPSet.setAttrib (FIELD_ObjectID, myID);
        tl_culture_element_questPSet.setAttrib (FIELD_Description, HELPER_Description.toObject (_Description)); //
        _CultureElement.getPersistentSets (allSets);
        
    }


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

        
        PersistentSet   tl_culture_element_questPSet = allSets.getPersistentSet (objectID, "tl_culture_element_quest");

        _Description = (String)(HELPER_Description.fromObject (_Description, tl_culture_element_questPSet.getAttrib (FIELD_Description))); //
        _CultureElement.setFromPersistentSets (objectID, allSets);
        
    }





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

        if (other instanceof CultureElementQuestion)
        {
            CultureElementQuestion otherCultureElementQuestion = (CultureElementQuestion)other;
            
            try
            {
                setDescription (otherCultureElementQuestion.getDescription ());
            }
            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 BaseCultureElementQuestion)
        {
            BaseCultureElementQuestion sourceCultureElementQuestion = (BaseCultureElementQuestion)(source);

            _Description = sourceCultureElementQuestion._Description;
            
        }
    }


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

            _CultureElement.copyFrom (sourceCultureElementQuestion._CultureElement, linkToGhosts);
            
        }
    }


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

        if (source instanceof BaseCultureElementQuestion)
        {
            BaseCultureElementQuestion sourceCultureElementQuestion = (BaseCultureElementQuestion)(source);

            _Narratives.copyFrom (sourceCultureElementQuestion._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))); //
        _CultureElement.readExternalData(vals.get(SINGLEREFERENCE_CultureElement));
        _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 (SINGLEREFERENCE_CultureElement, _CultureElement.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Narratives, _Narratives.writeExternalData());
        
    }


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

        if (other instanceof BaseCultureElementQuestion)
        {
            BaseCultureElementQuestion otherCultureElementQuestion = (BaseCultureElementQuestion)(other);

            
            if (!HELPER_Description.compare(this._Description, otherCultureElementQuestion._Description))
            {
                listener.notifyFieldChange(this, other, FIELD_Description, HELPER_Description.toObject(this._Description), HELPER_Description.toObject(otherCultureElementQuestion._Description));
            }

            // Compare single assocs
            _CultureElement.compare (otherCultureElementQuestion._CultureElement, listener);
            

            // Compare multiple assocs
            _Narratives.compare (otherCultureElementQuestion._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.visitAssociation (_CultureElement);
        visitor.visitAssociation (_Narratives);
        
    }


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

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


    public static CultureElementQuestion createCultureElementQuestion (ObjectTransaction transaction) throws StorageException
    {
        CultureElementQuestion     result = new CultureElementQuestion ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static CultureElementQuestion getCultureElementQuestionByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (CultureElementQuestion)(transaction.getObjectByID (REFERENCE_CultureElementQuestion, 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 (SINGLEREFERENCE_CultureElement))
        {
            return filter.matches (getCultureElement ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

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

        
                public static CultureElementQuestion[]
             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
        {
            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
        {
            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
        {
            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 (MULTIPLEREFERENCE_Narratives))
        {
            return getWriteability_Narratives ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_CultureElement))
        {
            return getWriteability_CultureElement ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

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

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

        return result;
    }


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


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

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

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

    }

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

        public CultureElementQuestionPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Narratives"))
            {
                return toNarratives ();
            }                
            if (name.equals ("Description"))
            {
                return toDescription ();
            }                
            if (name.equals ("CultureElement"))
            {
                return toCultureElement ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Description));  }
        public CultureElement.CultureElementPipeLineFactory<From, CultureElement> toCultureElement () { return toCultureElement (Filter.ALL); }

        public CultureElement.CultureElementPipeLineFactory<From, CultureElement> toCultureElement (Filter<CultureElement> filter) 
        { 
            return CultureElement.REFERENCE_CultureElement.new CultureElementPipeLineFactory<From, CultureElement> (this, new ORMSingleAssocPipe<Me, CultureElement>(SINGLEREFERENCE_CultureElement, 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 DummyCultureElementQuestion extends CultureElementQuestion
{
    // Default constructor primarily to support Externalisable
    public DummyCultureElementQuestion()
    {
        super();
    }

    public void assertValid ()
    {
    }


    public CultureElement getCultureElement () throws StorageException
    {
        return (CultureElement)(CultureElement.DUMMY_CultureElement);
    }


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

    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();
    }

}

    
