/*
 * IMPORTANT!!!! XSL Autogenerated class, DO NOT EDIT!!!!!
 * Template: Infrastructure9.0 [oneit.objstore.BusinessObjectTemplate.xsl]
 *   Vendor: Apache Software Foundation (Xalan XSLTC) (Version: 1.0)
 */

    
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 BaseFactorQuestionLink extends BaseBusinessClass
{
    // Reference instance for the object
    public static final FactorQuestionLink REFERENCE_FactorQuestionLink = new FactorQuestionLink ();

    // Reference instance for the object
    public static final FactorQuestionLink DUMMY_FactorQuestionLink = new DummyFactorQuestionLink ();

    // Static constants corresponding to field names
    
    public static final String FIELD_ReverseScore = "ReverseScore";
    public static final String SINGLEREFERENCE_Factor = "Factor";
    public static final String BACKREF_Factor = "";
    public static final String SINGLEREFERENCE_Question = "Question";
    public static final String BACKREF_Question = "";

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

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

    // Private attributes corresponding to business object data
    private Boolean _ReverseScore;
    

    // Private attributes corresponding to single references
    private SingleAssociation<FactorQuestionLink, Factor> _Factor;
    private SingleAssociation<FactorQuestionLink, Question> _Question;
    

    // Private attributes corresponding to multiple references
    

    // Map of maps of metadata
    private static final Map ATTRIBUTES_METADATA_FactorQuestionLink = new LinkedHashMap ();

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

    // Arrays of AttributeDecorators for each attribute
    private static final AttributeDecorator<FactorQuestionLink, Boolean>[] FIELD_ReverseScore_Decorators;
    
    
    // Arrays of SingleAssocDecorators for each attribute
    private static final SingleAssocDecorator<FactorQuestionLink, Factor>[] SINGLEREFERENCE_Factor_Decorators;
    private static final SingleAssocDecorator<FactorQuestionLink, Question>[] SINGLEREFERENCE_Question_Decorators;
    
    
    // Arrays of MultiAssocDecorators for each attribute
    

    // Arrays of behaviour decorators
    private static final FactorQuestionLinkBehaviourDecorator[] FactorQuestionLink_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Factor = Factor.BACKREF_Questions;
            String tmp_Question = Question.BACKREF_Factors;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Factor();
            setupAssocMetaData_Question();
            FIELD_ReverseScore_Validators = (AttributeValidator[])setupAttribMetaData_ReverseScore(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_ReverseScore_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_FactorQuestionLink, FIELD_ReverseScore).toArray (new AttributeDecorator[0]);
            SINGLEREFERENCE_Factor_Decorators = (SingleAssocDecorator[])AttributeDecoratorUtils.getSingleAssocDecorators(REFERENCE_FactorQuestionLink, SINGLEREFERENCE_Factor).toArray (new SingleAssocDecorator[0]);
            SINGLEREFERENCE_Question_Decorators = (SingleAssocDecorator[])AttributeDecoratorUtils.getSingleAssocDecorators(REFERENCE_FactorQuestionLink, SINGLEREFERENCE_Question).toArray (new SingleAssocDecorator[0]);
            

            REFERENCE_FactorQuestionLink.initialiseReference ();
            DUMMY_FactorQuestionLink.initialiseReference ();
            FactorQuestionLink_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(FactorQuestionLink.class).toArray(new FactorQuestionLinkBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

    
    private static void setupAssocMetaData_Factor()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "Questions");
        metaInfo.put ("dbcol", "factor_number");
        metaInfo.put ("name", "Factor");
        metaInfo.put ("type", "Factor");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_factor_lin");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for FactorQuestionLink.Factor:", metaInfo);
        ATTRIBUTES_METADATA_FactorQuestionLink.put (SINGLEREFERENCE_Factor, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static void setupAssocMetaData_Question()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("backreferenceName", "Factors");
        metaInfo.put ("dbcol", "quest_number");
        metaInfo.put ("name", "Question");
        metaInfo.put ("type", "Question");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_factor_lin");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for FactorQuestionLink.Question:", metaInfo);
        ATTRIBUTES_METADATA_FactorQuestionLink.put (SINGLEREFERENCE_Question, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static List setupAttribMetaData_ReverseScore(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "reverse_score_flag");
        metaInfo.put ("name", "ReverseScore");
        metaInfo.put ("type", "Boolean");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_factor_lin");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for FactorQuestionLink.ReverseScore:", metaInfo);
        ATTRIBUTES_METADATA_FactorQuestionLink.put (FIELD_ReverseScore, Collections.unmodifiableMap (metaInfo));

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

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


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

    


    protected void _initialiseNewObjAttributes (ObjectTransaction transaction) throws StorageException
    {
        super._initialiseNewObjAttributes (transaction);

        
        _ReverseScore = (Boolean)(HELPER_ReverseScore.initialise (_ReverseScore));
    }


    protected void _initialiseAssociations ()
    {
        super._initialiseAssociations ();

        _Factor = new SingleAssociation<FactorQuestionLink, Factor> (this, SINGLEREFERENCE_Factor, Factor.MULTIPLEREFERENCE_Questions, Factor.REFERENCE_Factor, "tl_factor_lin");
        _Question = new SingleAssociation<FactorQuestionLink, Question> (this, SINGLEREFERENCE_Question, Question.MULTIPLEREFERENCE_Factors, Question.REFERENCE_Question, "tl_factor_lin");
        
    }


    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Factor = new SingleAssociation<FactorQuestionLink, Factor> (this, SINGLEREFERENCE_Factor, Factor.MULTIPLEREFERENCE_Questions, Factor.REFERENCE_Factor, "tl_factor_lin");
        _Question = new SingleAssociation<FactorQuestionLink, Question> (this, SINGLEREFERENCE_Question, Question.MULTIPLEREFERENCE_Factors, Question.REFERENCE_Question, "tl_factor_lin");
        
        
        return this;
    }


    /**
     * Attribute ReverseScore
     */
    public Boolean getReverseScore ()
    {
        assertValid();
        Boolean valToReturn = AttributeDecoratorUtils.getValue (FIELD_ReverseScore_Decorators, (FactorQuestionLink)this, FIELD_ReverseScore, _ReverseScore);

        
        for (FactorQuestionLinkBehaviourDecorator bhd : FactorQuestionLink_BehaviourDecorators)
        {
            valToReturn = bhd.getReverseScore ((FactorQuestionLink)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 preReverseScoreChange (Boolean newReverseScore) 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 postReverseScoreChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute ReverseScore.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setReverseScore (Boolean newReverseScore) throws FieldException
    {
        newReverseScore = AttributeDecoratorUtils.setValue (FIELD_ReverseScore_Decorators, (FactorQuestionLink)this, FIELD_ReverseScore, newReverseScore);

        boolean oldAndNewIdentical = HELPER_ReverseScore.compare (_ReverseScore, newReverseScore);
        
        try
        {
            for (FactorQuestionLinkBehaviourDecorator bhd : FactorQuestionLink_BehaviourDecorators)
            {
                newReverseScore = bhd.setReverseScore ((FactorQuestionLink)this, newReverseScore);
                oldAndNewIdentical = HELPER_ReverseScore.compare (_ReverseScore, newReverseScore);
            }

            

            if (FIELD_ReverseScore_Validators.length > 0)
            {
                Object newReverseScoreObj = HELPER_ReverseScore.toObject (newReverseScore);

                if (newReverseScoreObj != null)
                {
                    int     loopMax = FIELD_ReverseScore_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_FactorQuestionLink.get (FIELD_ReverseScore);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_ReverseScore_Decorators, (FactorQuestionLink)this, FIELD_ReverseScore ,getWriteability_ReverseScore ()) != FieldWriteability.FALSE, "Field  ReverseScore is not writeable");
            preReverseScoreChange (newReverseScore);
            markFieldChange (FIELD_ReverseScore);
            _ReverseScore = newReverseScore;            
            postFieldChange (FIELD_ReverseScore);
            postReverseScoreChange ();
        }
    }



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

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Factor))
        {
            return _Factor.getReferencedType ();
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            return _Question.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_Factor))
        {
            return Factor.MULTIPLEREFERENCE_Questions ;
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            return Question.MULTIPLEREFERENCE_Factors ;
        }
        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_Factor))
        {
            return getFactor ();
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            return getQuestion ();
        }
        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_Factor))
        {
            return getFactor (getType);
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            return getQuestion (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_Factor))
        {
            return getFactorID ();
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            return getQuestionID ();
        }
        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_Factor))
        {
            setFactor ((Factor)(newValue));
        }
        else if (assocName.equals (SINGLEREFERENCE_Question))
        {
            setQuestion ((Question)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getFactorID ()
    {
        assertValid();
        
        if (_Factor == null)
        {
            return null;
        }
        else
        {
            return _Factor.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 preFactorChange (Factor newFactor) 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 postFactorChange () throws FieldException
    {
    }


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

        
    /**
     * Set the reference Factor.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setFactor (Factor newFactor) throws StorageException, FieldException
    {
        if (_Factor.wouldReferencedChange (newFactor))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (SINGLEREFERENCE_Factor_Decorators, (FactorQuestionLink)this, SINGLEREFERENCE_Factor ,getWriteability_Factor ()) != FieldWriteability.FALSE, "Assoc Factor is not writeable");
            preFactorChange (newFactor);
            Factor oldFactor = getFactor ();

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

            _Factor.set (newFactor);

            if (newFactor != null)
            {
                newFactor.addToQuestions ((FactorQuestionLink)(this));
            }
                
            postFactorChange ();
        }
    }

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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getQuestionID ()
    {
        assertValid();
        
        if (_Question == null)
        {
            return null;
        }
        else
        {
            return _Question.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 preQuestionChange (Question newQuestion) 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 postQuestionChange () throws FieldException
    {
    }


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

        
    /**
     * Set the reference Question.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setQuestion (Question newQuestion) throws StorageException, FieldException
    {
        if (_Question.wouldReferencedChange (newQuestion))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (SINGLEREFERENCE_Question_Decorators, (FactorQuestionLink)this, SINGLEREFERENCE_Question ,getWriteability_Question ()) != FieldWriteability.FALSE, "Assoc Question is not writeable");
            preQuestionChange (newQuestion);
            Question oldQuestion = getQuestion ();

            if (oldQuestion != null)
            {
                // This is to stop validation from triggering when we are removed
                _Question.set (null);
                oldQuestion.removeFromFactors ((FactorQuestionLink)(this));
            }

            _Question.set (newQuestion);

            if (newQuestion != null)
            {
                newQuestion.addToFactors ((FactorQuestionLink)(this));
            }
                
            postQuestionChange ();
        }
    }

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

        return result;
    }


    /**
     * Get the reference instance for the multi assoc name.
     */
    public BaseBusinessClass getMultiAssocReferenceInstance(String attribName)
    {

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {

        return super.getMultiAssocBackReference(attribName);
    }


    /**
     * Get the assoc count for the multi assoc name.
     */
    public int getMultiAssocCount(String attribName) throws StorageException
    {

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {

        return super.getMultiAssocAt(attribName, index);
    }


    /**
     * Add to a multi assoc by attribute name
     */
    public void addToMultiAssoc(String attribName, BaseBusinessClass newElement) throws StorageException
    {

        super.addToMultiAssoc(attribName, newElement);
    }

    /**
     * Remove from a multi assoc by attribute name
     */
    public void removeFromMultiAssoc(String attribName, BaseBusinessClass oldElement) throws StorageException
    {

        super.removeFromMultiAssoc(attribName, oldElement);
    }


    protected void __loadMultiAssoc (String attribName, BaseBusinessClass[] elements)
    {

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {

        return super.__isMultiAssocLoaded(attribName);
    }




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

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Questions from ", getObjectID (), " to ", referenced.getObjectID ());
                    _Factor.set (null);
                    referenced.removeFromQuestions ((FactorQuestionLink)this);
                }
            }
        
            // Ensure we are removed from any loaded multi-associations that aren't mandatory
            if (_Question.isLoaded () || getTransaction ().isObjectLoaded (_Question.getReferencedType (), getQuestionID ()))
            {
                Question  referenced = getQuestion ();

                if (referenced != null)
                {
                    // Stop the callback
                    LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Factors from ", getObjectID (), " to ", referenced.getObjectID ());
                    _Question.set (null);
                    referenced.removeFromFactors ((FactorQuestionLink)this);
                }
            }
        
        }
        catch (Exception e)
        {
            throw NestedException.wrap(e);
        }

        super.onDelete ();
    }


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


    public FactorQuestionLink referenceInstance ()
    {
        return REFERENCE_FactorQuestionLink;
    }


    public FactorQuestionLink getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getFactorQuestionLinkByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_FactorQuestionLink;
    }


    public String getBaseSetName ()
    {
        return "tl_factor_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_factor_linPSet = allSets.getPersistentSet (myID, "tl_factor_lin", myPSetStatus);

        tl_factor_linPSet.setAttrib (FIELD_ObjectID, myID);
        tl_factor_linPSet.setAttrib (FIELD_ReverseScore, HELPER_ReverseScore.toObject (_ReverseScore)); //
        _Factor.getPersistentSets (allSets);
        _Question.getPersistentSets (allSets);
        
    }


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

        _ReverseScore = (Boolean)(HELPER_ReverseScore.fromObject (_ReverseScore, tl_factor_linPSet.getAttrib (FIELD_ReverseScore))); //
        _Factor.setFromPersistentSets (objectID, allSets);
        _Question.setFromPersistentSets (objectID, allSets);
        
    }


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

        if (other instanceof FactorQuestionLink)
        {
            FactorQuestionLink otherFactorQuestionLink = (FactorQuestionLink)other;
            

            try
            {
                setReverseScore (otherFactorQuestionLink.getReverseScore ());
            }
            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 BaseFactorQuestionLink)
        {
            BaseFactorQuestionLink sourceFactorQuestionLink = (BaseFactorQuestionLink)(source);

            _ReverseScore = sourceFactorQuestionLink._ReverseScore;
            
        }
    }


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

            _Factor.copyFrom (sourceFactorQuestionLink._Factor, linkToGhosts);
            _Question.copyFrom (sourceFactorQuestionLink._Question, 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 BaseFactorQuestionLink)
        {
            BaseFactorQuestionLink sourceFactorQuestionLink = (BaseFactorQuestionLink)(source);

            
        }
    }


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

        _ReverseScore = (Boolean)(HELPER_ReverseScore.readExternal (_ReverseScore, vals.get(FIELD_ReverseScore))); //
        _Factor.readExternalData(vals.get(SINGLEREFERENCE_Factor));
        _Question.readExternalData(vals.get(SINGLEREFERENCE_Question));
        
    }


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

        vals.put (FIELD_ReverseScore, HELPER_ReverseScore.writeExternal (_ReverseScore));
        vals.put (SINGLEREFERENCE_Factor, _Factor.writeExternalData());
        vals.put (SINGLEREFERENCE_Question, _Question.writeExternalData());
        
    }


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

        if (other instanceof BaseFactorQuestionLink)
        {
            BaseFactorQuestionLink otherFactorQuestionLink = (BaseFactorQuestionLink)(other);

            
            if (!HELPER_ReverseScore.compare(this._ReverseScore, otherFactorQuestionLink._ReverseScore))
            {
                listener.notifyFieldChange(this, other, FIELD_ReverseScore, HELPER_ReverseScore.toObject(this._ReverseScore), HELPER_ReverseScore.toObject(otherFactorQuestionLink._ReverseScore));
            }

            // Compare single assocs
            _Factor.compare (otherFactorQuestionLink._Factor, listener);
            _Question.compare (otherFactorQuestionLink._Question, listener);
            

            // Compare multiple assocs
            
        }
    }


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

        
    }


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

        visitor.visitField(this, FIELD_ReverseScore, HELPER_ReverseScore.toObject(getReverseScore()));
        visitor.visitAssociation (_Factor);
        visitor.visitAssociation (_Question);
        
    }


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

        if (scope.includes (_Factor))
        {
            visitor.visit (_Factor);
        }
        if (scope.includes (_Question))
        {
            visitor.visit (_Question);
        }
        
    }


    public static FactorQuestionLink createFactorQuestionLink (ObjectTransaction transaction) throws StorageException
    {
        FactorQuestionLink     result = new FactorQuestionLink ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static FactorQuestionLink getFactorQuestionLinkByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (FactorQuestionLink)(transaction.getObjectByID (REFERENCE_FactorQuestionLink, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_ReverseScore))
        {
            return filter.matches (getReverseScore ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Factor))
        {
            return filter.matches (getFactor ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Question))
        {
            return filter.matches (getQuestion ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
        public SearchAll andReverseScore (QueryFilter<Boolean> filter)
        {
            filter.addFilter (context, "tl_factor_lin.reverse_score_flag", "ReverseScore");
            return this;
        }
            
        public SearchAll andFactor (QueryFilter<Factor> filter)
        {
            filter.addFilter (context, "tl_factor_lin.factor_number", "Factor");
            return this;
        }
            
        public SearchAll andQuestion (QueryFilter<Question> filter)
        {
            filter.addFilter (context, "tl_factor_lin.quest_number", "Question");
            return this;
        }
            
        public FactorQuestionLink[] search (ObjectTransaction transaction) throws StorageException
        {
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_FactorQuestionLink, SEARCH_All, criteria);
            Set<FactorQuestionLink>   typedResults = new LinkedHashSet <FactorQuestionLink> ();
            for (BaseBusinessClass bbcResult : results)
            {
                FactorQuestionLink aResult = (FactorQuestionLink)bbcResult;
                
                typedResults.add (aResult);
            }
            
            return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new FactorQuestionLink[0]);
                            
        }
    }

        
    public static FactorQuestionLink[] 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_ReverseScore))
        {
            return HELPER_ReverseScore.toObject (getReverseScore ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_ReverseScore))
        {
            return HELPER_ReverseScore;
        }
        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_ReverseScore))
        {
            setReverseScore ((Boolean)(HELPER_ReverseScore.fromObject (_ReverseScore, attribValue)));            
        }
        else
        {
            super.setAttribute (attribName, attribValue);
        }
    }

    

    public boolean isWriteable (String fieldName)
    {        
        return getWriteable (fieldName) == FieldWriteability.TRUE;
    }

    public boolean isReadable ()
    {
        return super.isReadable();
    }

    public boolean isReadable (String fieldName)
    {
        return super.isReadable(fieldName);
    }


    public FieldWriteability getWriteable (String fieldName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (fieldName.equals (FIELD_ReverseScore))
        {
            return getWriteability_ReverseScore ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Factor))
        {
            return getWriteability_Factor ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Question))
        {
            return getWriteability_Question ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

    public String[] getAllTableNames()
    {
        return new String[] {"tl_factor_lin"};
    }
    
    public boolean isPolymorphic(String tableName)
    {
        return super.isPolymorphic(tableName);
    }


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

        
        result.add(HELPER_ReverseScore.getAttribObject (getClass (), _ReverseScore, false, FIELD_ReverseScore));

        return result;
    }


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


    public Object getAttributeMetadata (String attribute, String metadata)
    {
        if (ATTRIBUTES_METADATA_FactorQuestionLink.containsKey (attribute))
        {
            return ((Map)ATTRIBUTES_METADATA_FactorQuestionLink.get (attribute)).get(metadata);
        }
        else
        {
            return super.getAttributeMetadata (attribute, metadata);
        }
    }

    public void copyAllAttributeMetadata (Map copyInto)
    {
        super.copyAllAttributeMetadata(copyInto);

        copyInto.putAll(ATTRIBUTES_METADATA_FactorQuestionLink);
    }
            

    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 FactorQuestionLinkBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<FactorQuestionLink>
    {

    /**
     * Get the attribute ReverseScore
     */
    public Boolean getReverseScore (FactorQuestionLink obj, Boolean original)
    {
        return original;
    }

    /**
     * Change the value set for attribute ReverseScore.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Boolean setReverseScore (FactorQuestionLink obj, Boolean newReverseScore) throws FieldException
    {
        return newReverseScore;
    }

    }

    @Override
    public ORMPipeLine pipes()
    {
        return new FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> ((FactorQuestionLink)this);
    }

    @Override
    public ORMPipeLine pipes(Collection _items)
    {
        return pipesFactorQuestionLink(_items);
    }
      
    /**
     * Use this instead of pipes() to get rid of type casting.
     */  
    public FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> pipelineFactorQuestionLink()
    {
        return (FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink>) pipes();
    }
    
    public static FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> pipesFactorQuestionLink(Collection<FactorQuestionLink> items)
    {
        return REFERENCE_FactorQuestionLink.new FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> (items);
    }
    
    public static FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> pipesFactorQuestionLink(FactorQuestionLink[] _items)
    {
        return pipesFactorQuestionLink(Arrays.asList (_items));
    }
    
    public static FactorQuestionLinkPipeLineFactory<FactorQuestionLink, FactorQuestionLink> pipesFactorQuestionLink()
    {
        return pipesFactorQuestionLink((Collection)null);
    }
    
    
    public class FactorQuestionLinkPipeLineFactory<From extends BaseBusinessClass, Me extends FactorQuestionLink> extends BaseBusinessClass.ORMPipeLine<From, Me>    
    {
        public <Prev> FactorQuestionLinkPipeLineFactory (PipeLine<From, Prev> pipeLine, Pipe<Prev, Me> nextPipe)
        {
            super (pipeLine, nextPipe);
        }
        
        public FactorQuestionLinkPipeLineFactory (From seed)
        {
            super(seed);
        }

        public FactorQuestionLinkPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("ReverseScore"))
            {
                return toReverseScore ();
            }                
            if (name.equals ("Factor"))
            {
                return toFactor ();
            }                
            if (name.equals ("Question"))
            {
                return toQuestion ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, Boolean> toReverseScore () { return pipe(new ORMAttributePipe<Me, Boolean>(FIELD_ReverseScore));  }

        public Factor.FactorPipeLineFactory<From, Factor> toFactor () { return toFactor (Filter.ALL); }

        public Factor.FactorPipeLineFactory<From, Factor> toFactor (Filter<Factor> filter) 
        { 
            return Factor.REFERENCE_Factor.new FactorPipeLineFactory<From, Factor> (this, new ORMSingleAssocPipe<Me, Factor>(SINGLEREFERENCE_Factor, filter)); 
        }

        public Question.QuestionPipeLineFactory<From, Question> toQuestion () { return toQuestion (Filter.ALL); }

        public Question.QuestionPipeLineFactory<From, Question> toQuestion (Filter<Question> filter) 
        { 
            return Question.REFERENCE_Question.new QuestionPipeLineFactory<From, Question> (this, new ORMSingleAssocPipe<Me, Question>(SINGLEREFERENCE_Question, filter)); 
        }
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


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


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

    public Question getQuestion () throws StorageException
    {
        return (Question)(Question.DUMMY_Question);
    }


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

}

    
