/*
 * 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 FIELD_IsRightQuestion = "IsRightQuestion";
    public static final String SINGLEREFERENCE_Section = "Section";
    public static final String BACKREF_Section = "";
    public static final String SINGLEREFERENCE_RightQuestion = "RightQuestion";
    public static final String BACKREF_RightQuestion = "";
    public static final String SINGLEREFERENCE_HighLowFactor = "HighLowFactor";
    public static final String BACKREF_HighLowFactor = "";
    public static final String MULTIPLEREFERENCE_Factors = "Factors";
    public static final String BACKREF_Factors = "";
    public static final String MULTIPLEREFERENCE_LeftQuestions = "LeftQuestions";
    public static final String BACKREF_LeftQuestions = "";

    // 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 static final DefaultAttributeHelper<Question> HELPER_IsRightQuestion = DefaultAttributeHelper.INSTANCE;
    

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

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

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

    // 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;
    private static final AttributeValidator[] FIELD_IsRightQuestion_Validators;
    

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

            setupAssocMetaData_Factors();
            setupAssocMetaData_LeftQuestions();
            setupAssocMetaData_Section();
            setupAssocMetaData_RightQuestion();
            setupAssocMetaData_HighLowFactor();
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_IsRightQuestion_Validators = (AttributeValidator[])setupAttribMetaData_IsRightQuestion(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_LeftQuestions()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "RightQuestion");
        metaInfo.put ("name", "LeftQuestions");
        metaInfo.put ("type", "Question");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.LeftQuestions:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (MULTIPLEREFERENCE_LeftQuestions, 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 void setupAssocMetaData_RightQuestion()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "LeftQuestions");
        metaInfo.put ("dbcol", "right_quest_number");
        metaInfo.put ("name", "RightQuestion");
        metaInfo.put ("type", "Question");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.RightQuestion:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (SINGLEREFERENCE_RightQuestion, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_HighLowFactor()
    {
        Map metaInfo = new HashMap ();

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

        metaInfo.put ("dbcol", "description");
        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;
    }
    
    // Meta Info setup
    private static List setupAttribMetaData_IsRightQuestion(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "is_right_quest");
        metaInfo.put ("defaultValue", "Boolean.FALSE");
        metaInfo.put ("name", "IsRightQuestion");
        metaInfo.put ("type", "Boolean");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for Question.IsRightQuestion:", metaInfo);
        ATTRIBUTES_METADATA_Question.put (FIELD_IsRightQuestion, Collections.unmodifiableMap (metaInfo));

        List validators = BaseBusinessClass.getAttribValidators(Question.class, "IsRightQuestion", metaInfo, validatorMapping);
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Validators for Question.IsRightQuestion:", 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));
        _IsRightQuestion = (Boolean)(Boolean.FALSE);
    }


    // 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");
        _RightQuestion = new SingleAssociation<Question, Question> (this, SINGLEREFERENCE_RightQuestion, Question.MULTIPLEREFERENCE_LeftQuestions, Question.REFERENCE_Question, "tl_quest_lin");
        _HighLowFactor = new SingleAssociation<Question, Factor> (this, SINGLEREFERENCE_HighLowFactor, Factor.MULTIPLEREFERENCE_HighLowQuestions, Factor.REFERENCE_Factor, "tl_quest_lin");
        _Factors = new MultipleAssociation<Question, FactorQuestionLink> (this, MULTIPLEREFERENCE_Factors, FactorQuestionLink.SINGLEREFERENCE_Question, FactorQuestionLink.REFERENCE_FactorQuestionLink);
        _LeftQuestions = new MultipleAssociation<Question, Question> (this, MULTIPLEREFERENCE_LeftQuestions, Question.SINGLEREFERENCE_RightQuestion, Question.REFERENCE_Question);
        
    }


    // 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");
        _RightQuestion = new SingleAssociation<Question, Question> (this, SINGLEREFERENCE_RightQuestion, Question.MULTIPLEREFERENCE_LeftQuestions, Question.REFERENCE_Question, "tl_quest_lin");
        _HighLowFactor = new SingleAssociation<Question, Factor> (this, SINGLEREFERENCE_HighLowFactor, Factor.MULTIPLEREFERENCE_HighLowQuestions, Factor.REFERENCE_Factor, "tl_quest_lin");
        _Factors = new MultipleAssociation<Question, FactorQuestionLink> (this, MULTIPLEREFERENCE_Factors, FactorQuestionLink.SINGLEREFERENCE_Question, FactorQuestionLink.REFERENCE_FactorQuestionLink);
        _LeftQuestions = new MultipleAssociation<Question, Question> (this, MULTIPLEREFERENCE_LeftQuestions, Question.SINGLEREFERENCE_RightQuestion, Question.REFERENCE_Question);
        
        
        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 ();
        }
    }

    /**
     * Get the attribute IsRightQuestion
     */
    public Boolean getIsRightQuestion ()
    {
        assertValid();
        Boolean valToReturn = _IsRightQuestion;

            for (QuestionBehaviourDecorator bhd : Question_BehaviourDecorators)
            {
                valToReturn = bhd.getIsRightQuestion ((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 preIsRightQuestionChange (Boolean newIsRightQuestion) 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 postIsRightQuestionChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute IsRightQuestion.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setIsRightQuestion (Boolean newIsRightQuestion) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_IsRightQuestion.compare (_IsRightQuestion, newIsRightQuestion);
        
        try
        {
            for (QuestionBehaviourDecorator bhd : Question_BehaviourDecorators)
            {
                newIsRightQuestion = bhd.setIsRightQuestion ((Question)this, newIsRightQuestion);
                oldAndNewIdentical = HELPER_IsRightQuestion.compare (_IsRightQuestion, newIsRightQuestion);
            }

            

            if (FIELD_IsRightQuestion_Validators.length > 0)
            {
                Object newIsRightQuestionObj = HELPER_IsRightQuestion.toObject (newIsRightQuestion);

                if (newIsRightQuestionObj != null)
                {
                    int     loopMax = FIELD_IsRightQuestion_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_Question.get (FIELD_IsRightQuestion);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_IsRightQuestion () != FieldWriteability.FALSE, "Field  IsRightQuestion is not writeable");
            preIsRightQuestionChange (newIsRightQuestion);
            markFieldChange (FIELD_IsRightQuestion);
            _IsRightQuestion = newIsRightQuestion;            
            postFieldChange (FIELD_IsRightQuestion);
            postIsRightQuestionChange ();
        }
    }



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

        
        result.add("Section");
        
        result.add("RightQuestion");
        
        result.add("HighLowFactor");
        

        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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return _RightQuestion.getReferencedType ();
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return _HighLowFactor.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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return Question.MULTIPLEREFERENCE_LeftQuestions ;
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return Factor.MULTIPLEREFERENCE_HighLowQuestions ;
        }
        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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return getRightQuestion ();
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return getHighLowFactor ();
        }
        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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return getRightQuestion (getType);
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return getHighLowFactor (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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return getRightQuestionID ();
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return getHighLowFactorID ();
        }
        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 if (assocName.equals (SINGLEREFERENCE_RightQuestion))
        {
            setRightQuestion ((Question)(newValue));
        }else if (assocName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            setHighLowFactor ((Factor)(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 ();
        }
    }

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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getRightQuestionID ()
    {
        assertValid();
        
        if (_RightQuestion == null)
        {
            return null;
        }
        else
        {
            return _RightQuestion.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 preRightQuestionChange (Question newRightQuestion) 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 postRightQuestionChange () throws FieldException
    {
    }



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

        if (_RightQuestion.wouldReferencedChange (newRightQuestion))
        {
            assertValid();
            Debug.assertion (getWriteability_RightQuestion () != FieldWriteability.FALSE, "Assoc RightQuestion is not writeable");
            preRightQuestionChange (newRightQuestion);
            Question oldRightQuestion = getRightQuestion ();

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

                    _RightQuestion.set (newRightQuestion);

                    if (newRightQuestion != null)
                    {
                        newRightQuestion.addToLeftQuestions ((Question)(this));
                    }
                
            postRightQuestionChange ();
        }
    }

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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getHighLowFactorID ()
    {
        assertValid();
        
        if (_HighLowFactor == null)
        {
            return null;
        }
        else
        {
            return _HighLowFactor.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 preHighLowFactorChange (Factor newHighLowFactor) 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 postHighLowFactorChange () throws FieldException
    {
    }



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

        if (_HighLowFactor.wouldReferencedChange (newHighLowFactor))
        {
            assertValid();
            Debug.assertion (getWriteability_HighLowFactor () != FieldWriteability.FALSE, "Assoc HighLowFactor is not writeable");
            preHighLowFactorChange (newHighLowFactor);
            Factor oldHighLowFactor = getHighLowFactor ();

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

                    _HighLowFactor.set (newHighLowFactor);

                    if (newHighLowFactor != null)
                    {
                        newHighLowFactor.addToHighLowQuestions ((Question)(this));
                    }
                
            postHighLowFactorChange ();
        }
    }

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

        
        result.add("Factors");
        
        result.add("LeftQuestions");
        

        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 ;
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            return Question.REFERENCE_Question ;
        }
        

        return super.getMultiAssocReferenceInstance(attribName);
    }


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

        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();
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            return this.getLeftQuestionsCount();
        }
        

        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);
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            return this.getLeftQuestionsAt(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;
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            addToLeftQuestions((Question)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;
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            removeFromLeftQuestions((Question)oldElement);

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        
        if (MULTIPLEREFERENCE_Factors.equals(attribName))
        {
            return _Factors.isLoaded ();
        }
        
        if (MULTIPLEREFERENCE_LeftQuestions.equals(attribName))
        {
            return _LeftQuestions.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 FieldWriteability getWriteability_LeftQuestions ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getLeftQuestionsCount () throws StorageException
    {
        assertValid();
        return _LeftQuestions.getReferencedObjectsCount ();
    }


    public void addToLeftQuestions (Question newElement) throws StorageException
    {
        if (_LeftQuestions.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_LeftQuestions () != FieldWriteability.FALSE, "MultiAssoc LeftQuestions is not writeable (add)");
            _LeftQuestions.appendElement (newElement);

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


    public void removeFromLeftQuestions (Question elementToRemove) throws StorageException
    {
        if (_LeftQuestions.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_LeftQuestions () != FieldWriteability.FALSE, "MultiAssoc LeftQuestions is not writeable (remove)");
            _LeftQuestions.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getRightQuestion () != null)
                {
                    elementToRemove.setRightQuestion (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public Question getLeftQuestionsAt (int index) throws StorageException
    {
        return (Question)(_LeftQuestions.getElementAt (index));
    }


    public SortedSet<Question> getLeftQuestionsSet () throws StorageException
    {
        return _LeftQuestions.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);
                }
            }
        
            // Ensure we are removed from any loaded multi-associations that aren't mandatory
            if (_RightQuestion.isLoaded () || getTransaction ().isObjectLoaded (_RightQuestion.getReferencedType (), getRightQuestionID ()))
            {
                Question  referenced = getRightQuestion ();

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null LeftQuestions from ", getObjectID (), " to ", referenced.getObjectID ());
                    _RightQuestion.set (null);
                    referenced.removeFromLeftQuestions ((Question)this);
                }
            }
        
            // Ensure we are removed from any loaded multi-associations that aren't mandatory
            if (_HighLowFactor.isLoaded () || getTransaction ().isObjectLoaded (_HighLowFactor.getReferencedType (), getHighLowFactorID ()))
            {
                Factor  referenced = getHighLowFactor ();

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null HighLowQuestions from ", getObjectID (), " to ", referenced.getObjectID ());
                    _HighLowFactor.set (null);
                    referenced.removeFromHighLowQuestions ((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);
            }

            for(Question referenced : CollectionUtils.reverse(getLeftQuestionsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null RightQuestion from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setRightQuestion(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)); //
        tl_quest_linPSet.setAttrib (FIELD_IsRightQuestion, HELPER_IsRightQuestion.toObject (_IsRightQuestion)); //
        _Section.getPersistentSets (allSets);
        _RightQuestion.getPersistentSets (allSets);
        _HighLowFactor.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))); //
        _IsRightQuestion = (Boolean)(HELPER_IsRightQuestion.fromObject (_IsRightQuestion, tl_quest_linPSet.getAttrib (FIELD_IsRightQuestion))); //
        _Section.setFromPersistentSets (objectID, allSets);
        _RightQuestion.setFromPersistentSets (objectID, allSets);
        _HighLowFactor.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);
            }
            
            try
            {
                setIsRightQuestion (otherQuestion.getIsRightQuestion ());
            }
            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;
            _IsRightQuestion = sourceQuestion._IsRightQuestion;
            
        }
    }


    /**
     * 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);
            _RightQuestion.copyFrom (sourceQuestion._RightQuestion, linkToGhosts);
            _HighLowFactor.copyFrom (sourceQuestion._HighLowFactor, 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);
            _LeftQuestions.copyFrom (sourceQuestion._LeftQuestions, 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))); //
        _IsRightQuestion = (Boolean)(HELPER_IsRightQuestion.readExternal (_IsRightQuestion, vals.get(FIELD_IsRightQuestion))); //
        _Section.readExternalData(vals.get(SINGLEREFERENCE_Section));
        _RightQuestion.readExternalData(vals.get(SINGLEREFERENCE_RightQuestion));
        _HighLowFactor.readExternalData(vals.get(SINGLEREFERENCE_HighLowFactor));
        _Factors.readExternalData(vals.get(MULTIPLEREFERENCE_Factors));
        _LeftQuestions.readExternalData(vals.get(MULTIPLEREFERENCE_LeftQuestions));
        
    }


    /**
     * 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_IsRightQuestion, HELPER_IsRightQuestion.writeExternal (_IsRightQuestion));
        vals.put (SINGLEREFERENCE_Section, _Section.writeExternalData());
        vals.put (SINGLEREFERENCE_RightQuestion, _RightQuestion.writeExternalData());
        vals.put (SINGLEREFERENCE_HighLowFactor, _HighLowFactor.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Factors, _Factors.writeExternalData());
        vals.put (MULTIPLEREFERENCE_LeftQuestions, _LeftQuestions.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));
            }
            if (!HELPER_IsRightQuestion.compare(this._IsRightQuestion, otherQuestion._IsRightQuestion))
            {
                listener.notifyFieldChange(this, other, FIELD_IsRightQuestion, HELPER_IsRightQuestion.toObject(this._IsRightQuestion), HELPER_IsRightQuestion.toObject(otherQuestion._IsRightQuestion));
            }

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

            // Compare multiple assocs
            _Factors.compare (otherQuestion._Factors, listener);
            _LeftQuestions.compare (otherQuestion._LeftQuestions, 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_IsRightQuestion, HELPER_IsRightQuestion.toObject(getIsRightQuestion()));
        visitor.visitAssociation (_Section);
        visitor.visitAssociation (_RightQuestion);
        visitor.visitAssociation (_HighLowFactor);
        visitor.visitAssociation (_Factors);
        visitor.visitAssociation (_LeftQuestions);
        
    }


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

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


    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 (FIELD_IsRightQuestion))
        {
            return filter.matches (getIsRightQuestion ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Section))
        {
            return filter.matches (getSection ());
        }
        else if (attribName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return filter.matches (getRightQuestion ());
        }
        else if (attribName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return filter.matches (getHighLowFactor ());
        }
        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.description", "Description");
                return this;
            }
            
            public SearchAll andIsRightQuestion (QueryFilter<Boolean> filter)
            {
                filter.addFilter (context, "tl_quest_lin.is_right_quest", "IsRightQuestion");
                return this;
            }
            
            public SearchAll andSection (QueryFilter<Section> filter)
            {
                filter.addFilter (context, "tl_quest_lin.section_number", "Section");
                return this;
            }
            
            public SearchAll andRightQuestion (QueryFilter<Question> filter)
            {
                filter.addFilter (context, "tl_quest_lin.right_quest_number", "RightQuestion");
                return this;
            }
            
            public SearchAll andHighLowFactor (QueryFilter<Factor> filter)
            {
                filter.addFilter (context, "tl_quest_lin.high_low_factor_id", "HighLowFactor");
                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 if (attribName.equals (FIELD_IsRightQuestion))
        {
            return HELPER_IsRightQuestion.toObject (getIsRightQuestion ());
        }
        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_IsRightQuestion))
        {
            return HELPER_IsRightQuestion;
        }
        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_IsRightQuestion))
        {
            setIsRightQuestion ((Boolean)(HELPER_IsRightQuestion.fromObject (_IsRightQuestion, 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_IsRightQuestion))
        {
            return getWriteability_IsRightQuestion ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Factors))
        {
            return getWriteability_Factors ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_LeftQuestions))
        {
            return getWriteability_LeftQuestions ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Section))
        {
            return getWriteability_Section ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_RightQuestion))
        {
            return getWriteability_RightQuestion ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_HighLowFactor))
        {
            return getWriteability_HighLowFactor ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

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

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

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

    /**
     * Get the attribute IsRightQuestion
     */
    public Boolean getIsRightQuestion (Question obj, Boolean original)
    {
        return original;
    }

    /**
     * Change the value set for attribute IsRightQuestion.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Boolean setIsRightQuestion (Question obj, Boolean newIsRightQuestion) throws FieldException
    {
        return newIsRightQuestion;
    }

    }

    
    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 ("LeftQuestions"))
            {
                return toLeftQuestions ();
            }                
            if (name.equals ("Description"))
            {
                return toDescription ();
            }                
            if (name.equals ("IsRightQuestion"))
            {
                return toIsRightQuestion ();
            }                
            if (name.equals ("Section"))
            {
                return toSection ();
            }                
            if (name.equals ("RightQuestion"))
            {
                return toRightQuestion ();
            }                
            if (name.equals ("HighLowFactor"))
            {
                return toHighLowFactor ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Description));  }
        
        public PipeLine<From, Boolean> toIsRightQuestion () { return pipe(new ORMAttributePipe<Me, Boolean>(FIELD_IsRightQuestion));  }
        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 Question.QuestionPipeLineFactory<From, Question> toRightQuestion () { return toRightQuestion (Filter.ALL); }

        public Question.QuestionPipeLineFactory<From, Question> toRightQuestion (Filter<Question> filter) 
        { 
            return Question.REFERENCE_Question.new QuestionPipeLineFactory<From, Question> (this, new ORMSingleAssocPipe<Me, Question>(SINGLEREFERENCE_RightQuestion, filter)); 
        }
        public Factor.FactorPipeLineFactory<From, Factor> toHighLowFactor () { return toHighLowFactor (Filter.ALL); }

        public Factor.FactorPipeLineFactory<From, Factor> toHighLowFactor (Filter<Factor> filter) 
        { 
            return Factor.REFERENCE_Factor.new FactorPipeLineFactory<From, Factor> (this, new ORMSingleAssocPipe<Me, Factor>(SINGLEREFERENCE_HighLowFactor, 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 Question.QuestionPipeLineFactory<From, Question> toLeftQuestions () { return toLeftQuestions(Filter.ALL); }        

        public Question.QuestionPipeLineFactory<From, Question> toLeftQuestions (Filter<Question> filter)
        {
            return Question.REFERENCE_Question.new QuestionPipeLineFactory<From, Question> (this, new ORMMultiAssocPipe<Me, Question>(MULTIPLEREFERENCE_LeftQuestions, 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 Question getRightQuestion () throws StorageException
    {
        return (Question)(Question.DUMMY_Question);
    }


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

    public Factor getHighLowFactor () throws StorageException
    {
        return (Factor)(Factor.DUMMY_Factor);
    }


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getHighLowFactorID ()
    {
        return Factor.DUMMY_Factor.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();
    }

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


    public Question getLeftQuestionsAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association LeftQuestions");
    }


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

}

    
