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

    // Reference instance for the object
    public static final Question DUMMY_Question = new DummyQuestion ();

    // Static constants corresponding to field names
    
    public static final String FIELD_Description = "Description";
    public static final String SINGLEREFERENCE_Section = "Section";
    public static final String BACKREF_Section = "";
    public static final String MULTIPLEREFERENCE_Factors = "Factors";
    public static final String BACKREF_Factors = "";

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

    // Static constants corresponding to attribute helpers
    private static final DefaultAttributeHelper<Question> HELPER_Description = DefaultAttributeHelper.INSTANCE;
    

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

    // Private attributes corresponding to single references
    private SingleAssociation<Question, Section> _Section;
    

    // Private attributes corresponding to multiple references
    private MultipleAssociation<Question, FactorQuestionLink> _Factors;
    

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

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

    // Arrays of behaviour decorators
    private static final QuestionBehaviourDecorator[] Question_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Factors = FactorQuestionLink.BACKREF_Question;
            String tmp_Section = Section.BACKREF_Questions;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Factors();
            setupAssocMetaData_Section();
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_Question.initialiseReference ();
            DUMMY_Question.initialiseReference ();
            Question_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(Question.class).toArray(new QuestionBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "Question");
        metaInfo.put ("name", "Factors");
        metaInfo.put ("type", "FactorQuestionLink");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.Factors:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (MULTIPLEREFERENCE_Factors, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_Section()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "Questions");
        metaInfo.put ("dbcol", "section_number");
        metaInfo.put ("name", "Section");
        metaInfo.put ("type", "Section");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.Section:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (SINGLEREFERENCE_Section, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_Description(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "left_quest");
        metaInfo.put ("length", "80");
        metaInfo.put ("name", "Description");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.Description:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (FIELD_Description, Collections.unmodifiableMap (metaInfo));

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

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


    protected BBCBehaviourDecorator[] getBehaviours()
    {
        return Question_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 ();

        _Section = new SingleAssociation<Question, Section> (this, SINGLEREFERENCE_Section, Section.MULTIPLEREFERENCE_Questions, Section.REFERENCE_Section, "tl_quest_lin");
        _Factors = new MultipleAssociation<Question, FactorQuestionLink> (this, MULTIPLEREFERENCE_Factors, FactorQuestionLink.SINGLEREFERENCE_Question, FactorQuestionLink.REFERENCE_FactorQuestionLink);
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Section = new SingleAssociation<Question, Section> (this, SINGLEREFERENCE_Section, Section.MULTIPLEREFERENCE_Questions, Section.REFERENCE_Section, "tl_quest_lin");
        _Factors = new MultipleAssociation<Question, FactorQuestionLink> (this, MULTIPLEREFERENCE_Factors, FactorQuestionLink.SINGLEREFERENCE_Question, FactorQuestionLink.REFERENCE_FactorQuestionLink);
        
        
        return this;
    }

    

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

            for (QuestionBehaviourDecorator bhd : Question_BehaviourDecorators)
            {
                valToReturn = bhd.getDescription ((Question)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 (QuestionBehaviourDecorator bhd : Question_BehaviourDecorators)
            {
                newDescription = bhd.setDescription ((Question)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_Question.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("Section");
        

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Section))
        {
            return _Section.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_Section))
        {
            return Section.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_Section))
        {
            return getSection ();
        }
        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_Section))
        {
            return getSection (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_Section))
        {
            return getSectionID ();
        }
        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_Section))
        {
            setSection ((Section)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getSectionID ()
    {
        assertValid();
        
        if (_Section == null)
        {
            return null;
        }
        else
        {
            return _Section.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 preSectionChange (Section newSection) 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 postSectionChange () throws FieldException
    {
    }



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

        if (_Section.wouldReferencedChange (newSection))
        {
            assertValid();
            Debug.assertion (getWriteability_Section () != FieldWriteability.FALSE, "Assoc Section is not writeable");
            preSectionChange (newSection);
            Section oldSection = getSection ();

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

                    _Section.set (newSection);

                    if (newSection != null)
                    {
                        newSection.addToQuestions ((Question)(this));
                    }
                
            postSectionChange ();
        }
    }

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

        
        result.add("Factors");
        

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return FactorQuestionLink.SINGLEREFERENCE_Question ;
        }
        

        return super.getMultiAssocBackReference(attribName);
    }


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

        return super.getMultiAssocCount(attribName);
    }


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

        return super.getMultiAssocAt(attribName, index);
    }


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

            return;
        }
        

        super.addToMultiAssoc(attribName, newElement);
    }

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

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


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

        return super.__isMultiAssocLoaded(attribName);
    }

    

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


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

            try
            {
                if (newElement.getQuestion () != this)
                {
                    newElement.setQuestion ((Question)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


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


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


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



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

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Questions from ", getObjectID (), " to ", referenced.getObjectID ());
                    _Section.set (null);
                    referenced.removeFromQuestions ((Question)this);
                }
            }
        
            for(FactorQuestionLink referenced : CollectionUtils.reverse(getFactorsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Question from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setQuestion(null);
            }

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

        super.onDelete ();
    }


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


    public Question referenceInstance ()
    {
        return REFERENCE_Question;
    }


    public Question getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getQuestionByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_Question;
    }


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





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

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


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

        
        PersistentSet   tl_quest_linPSet = allSets.getPersistentSet (objectID, "tl_quest_lin");

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





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

        if (other instanceof Question)
        {
            Question otherQuestion = (Question)other;
            
            try
            {
                setDescription (otherQuestion.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 BaseQuestion)
        {
            BaseQuestion sourceQuestion = (BaseQuestion)(source);

            _Description = sourceQuestion._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 BaseQuestion)
        {
            BaseQuestion sourceQuestion = (BaseQuestion)(source);

            _Section.copyFrom (sourceQuestion._Section, 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 BaseQuestion)
        {
            BaseQuestion sourceQuestion = (BaseQuestion)(source);

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


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

        
    }


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

        _Description = (String)(HELPER_Description.readExternal (_Description, vals.get(FIELD_Description))); //
        _Section.readExternalData(vals.get(SINGLEREFERENCE_Section));
        _Factors.readExternalData(vals.get(MULTIPLEREFERENCE_Factors));
        
    }


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

        vals.put (FIELD_Description, HELPER_Description.writeExternal (_Description));
        vals.put (SINGLEREFERENCE_Section, _Section.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Factors, _Factors.writeExternalData());
        
    }


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

        if (other instanceof BaseQuestion)
        {
            BaseQuestion otherQuestion = (BaseQuestion)(other);

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

            // Compare single assocs
            _Section.compare (otherQuestion._Section, listener);
            

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


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

        
    }


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

        visitor.visitField(this, FIELD_Description, HELPER_Description.toObject(getDescription()));
        visitor.visitAssociation (_Section);
        visitor.visitAssociation (_Factors);
        
    }


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

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


    public static Question createQuestion (ObjectTransaction transaction) throws StorageException
    {
        Question     result = new Question ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static Question getQuestionByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (Question)(transaction.getObjectByID (REFERENCE_Question, 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_Section))
        {
            return filter.matches (getSection ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
            public SearchAll andDescription (QueryFilter<String> filter)
            {
                filter.addFilter (context, "tl_quest_lin.left_quest", "Description");
                return this;
            }
            
            public SearchAll andSection (QueryFilter<Section> filter)
            {
                filter.addFilter (context, "tl_quest_lin.section_number", "Section");
                return this;
            }
            
                public Question[]
             search (ObjectTransaction transaction) throws StorageException
        {
            
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_Question, SEARCH_All, criteria);
            Set<Question>   typedResults = new LinkedHashSet <Question> ();
            for (BaseBusinessClass bbcResult : results)
            {
                Question aResult = (Question)bbcResult;
                
                typedResults.add (aResult);
            }
            
                    return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new Question[0]);
                            
        }
    }

        
                public static Question[]
             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_Factors))
        {
            return getWriteability_Factors ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Section))
        {
            return getWriteability_Section ();
        }
        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_Question.containsKey (attribute))
        {
            return (Map)ATTRIBUTES_METADATA_Question.get (attribute);
        }
        else
        {
            return super.getAttributeMetadata (attribute);
        }
    }


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

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

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

    }

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

        public QuestionPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Factors"))
            {
                return toFactors ();
            }                
            if (name.equals ("Description"))
            {
                return toDescription ();
            }                
            if (name.equals ("Section"))
            {
                return toSection ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Description));  }
        public Section.SectionPipeLineFactory<From, Section> toSection () { return toSection (Filter.ALL); }

        public Section.SectionPipeLineFactory<From, Section> toSection (Filter<Section> filter) 
        { 
            return Section.REFERENCE_Section.new SectionPipeLineFactory<From, Section> (this, new ORMSingleAssocPipe<Me, Section>(SINGLEREFERENCE_Section, filter)); 
        }
        public FactorQuestionLink.FactorQuestionLinkPipeLineFactory<From, FactorQuestionLink> toFactors () { return toFactors(Filter.ALL); }        

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

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public Section getSection () throws StorageException
    {
        return (Section)(Section.DUMMY_Section);
    }


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

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


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


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

}

    
