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

    // Reference instance for the object
    public static final AnswerOption DUMMY_AnswerOption = new DummyAnswerOption ();

    // Static constants corresponding to field names
    
    public static final String FIELD_IsSelected = "IsSelected";
    public static final String SINGLEREFERENCE_CandidateAnswer = "CandidateAnswer";
    public static final String BACKREF_CandidateAnswer = "";
    public static final String SINGLEREFERENCE_Answer = "Answer";

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

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

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

    // Private attributes corresponding to single references
    private SingleAssociation<AnswerOption, CandidateDiversityAnswer> _CandidateAnswer;
    private SingleAssociation<AnswerOption, DiversityAnswer> _Answer;
    

    // Private attributes corresponding to multiple references
    

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

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

    // Arrays of behaviour decorators
    private static final AnswerOptionBehaviourDecorator[] AnswerOption_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_CandidateAnswer = CandidateDiversityAnswer.BACKREF_Answers;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_CandidateAnswer();
            setupAssocMetaData_Answer();
            FIELD_IsSelected_Validators = (AttributeValidator[])setupAttribMetaData_IsSelected(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_AnswerOption.initialiseReference ();
            DUMMY_AnswerOption.initialiseReference ();
            AnswerOption_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(AnswerOption.class).toArray(new AnswerOptionBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "Answers");
        metaInfo.put ("dbcol", "candidate_answer_id");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "CandidateAnswer");
        metaInfo.put ("type", "CandidateDiversityAnswer");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AnswerOption.CandidateAnswer:", metaInfo);
        ATTRIBUTES_METADATA_AnswerOption.put (SINGLEREFERENCE_CandidateAnswer, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_Answer()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("dbcol", "answer_id");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "Answer");
        metaInfo.put ("type", "DiversityAnswer");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AnswerOption.Answer:", metaInfo);
        ATTRIBUTES_METADATA_AnswerOption.put (SINGLEREFERENCE_Answer, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_IsSelected(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "is_selected");
        metaInfo.put ("defaultValue", "Boolean.FALSE");
        metaInfo.put ("name", "IsSelected");
        metaInfo.put ("type", "Boolean");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AnswerOption.IsSelected:", metaInfo);
        ATTRIBUTES_METADATA_AnswerOption.put (FIELD_IsSelected, Collections.unmodifiableMap (metaInfo));

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

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


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


    


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

        
        _IsSelected = (Boolean)(Boolean.FALSE);
    }


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

        _CandidateAnswer = new SingleAssociation<AnswerOption, CandidateDiversityAnswer> (this, SINGLEREFERENCE_CandidateAnswer, CandidateDiversityAnswer.MULTIPLEREFERENCE_Answers, CandidateDiversityAnswer.REFERENCE_CandidateDiversityAnswer, "tl_answer_option");
        _Answer = new SingleAssociation<AnswerOption, DiversityAnswer> (this, SINGLEREFERENCE_Answer, null, DiversityAnswer.REFERENCE_DiversityAnswer, "tl_answer_option");
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _CandidateAnswer = new SingleAssociation<AnswerOption, CandidateDiversityAnswer> (this, SINGLEREFERENCE_CandidateAnswer, CandidateDiversityAnswer.MULTIPLEREFERENCE_Answers, CandidateDiversityAnswer.REFERENCE_CandidateDiversityAnswer, "tl_answer_option");
        _Answer = new SingleAssociation<AnswerOption, DiversityAnswer> (this, SINGLEREFERENCE_Answer, null, DiversityAnswer.REFERENCE_DiversityAnswer, "tl_answer_option");
        
        
        return this;
    }

    

    /**
     * Get the attribute IsSelected
     */
    public Boolean getIsSelected ()
    {
        assertValid();
        Boolean valToReturn = _IsSelected;

            for (AnswerOptionBehaviourDecorator bhd : AnswerOption_BehaviourDecorators)
            {
                valToReturn = bhd.getIsSelected ((AnswerOption)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 preIsSelectedChange (Boolean newIsSelected) 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 postIsSelectedChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute IsSelected.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setIsSelected (Boolean newIsSelected) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_IsSelected.compare (_IsSelected, newIsSelected);
        
        try
        {
            for (AnswerOptionBehaviourDecorator bhd : AnswerOption_BehaviourDecorators)
            {
                newIsSelected = bhd.setIsSelected ((AnswerOption)this, newIsSelected);
                oldAndNewIdentical = HELPER_IsSelected.compare (_IsSelected, newIsSelected);
            }

            

            if (FIELD_IsSelected_Validators.length > 0)
            {
                Object newIsSelectedObj = HELPER_IsSelected.toObject (newIsSelected);

                if (newIsSelectedObj != null)
                {
                    int     loopMax = FIELD_IsSelected_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_AnswerOption.get (FIELD_IsSelected);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_IsSelected () != FieldWriteability.FALSE, "Field  IsSelected is not writeable");
            preIsSelectedChange (newIsSelected);
            markFieldChange (FIELD_IsSelected);
            _IsSelected = newIsSelected;            
            postFieldChange (FIELD_IsSelected);
            postIsSelectedChange ();
        }
    }



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

        
        result.add("CandidateAnswer");
        
        result.add("Answer");
        

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_CandidateAnswer))
        {
            return _CandidateAnswer.getReferencedType ();
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            return _Answer.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_CandidateAnswer))
        {
            return CandidateDiversityAnswer.MULTIPLEREFERENCE_Answers ;
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            return null ;
        }
        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_CandidateAnswer))
        {
            return getCandidateAnswer ();
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            return getAnswer ();
        }
        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_CandidateAnswer))
        {
            return getCandidateAnswer (getType);
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            return getAnswer (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_CandidateAnswer))
        {
            return getCandidateAnswerID ();
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            return getAnswerID ();
        }
        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_CandidateAnswer))
        {
            setCandidateAnswer ((CandidateDiversityAnswer)(newValue));
        }else if (assocName.equals (SINGLEREFERENCE_Answer))
        {
            setAnswer ((DiversityAnswer)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



    /**
     * Get the reference CandidateAnswer
     */
    public CandidateDiversityAnswer getCandidateAnswer () throws StorageException
    {
        assertValid();
        
        try
        {
            return (CandidateDiversityAnswer)(_CandidateAnswer.get ());
        }
        catch (ClassCastException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Cache collision in AnswerOption:", this.getObjectID (), ", was trying to get CandidateDiversityAnswer:",  getCandidateAnswerID ());
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Instead I got:", _CandidateAnswer.get ().getClass ());
            throw e;
        }
    }


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getCandidateAnswerID ()
    {
        assertValid();
        
        if (_CandidateAnswer == null)
        {
            return null;
        }
        else
        {
            return _CandidateAnswer.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 preCandidateAnswerChange (CandidateDiversityAnswer newCandidateAnswer) 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 postCandidateAnswerChange () throws FieldException
    {
    }



    
    public FieldWriteability getWriteability_CandidateAnswer ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    /**
     * Set the reference CandidateAnswer.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setCandidateAnswer (CandidateDiversityAnswer newCandidateAnswer) throws StorageException, FieldException
    {
        BusinessObjectParser.assertFieldCondition (newCandidateAnswer != null, this, SINGLEREFERENCE_CandidateAnswer, "mandatory");
        

        if (_CandidateAnswer.wouldReferencedChange (newCandidateAnswer))
        {
            assertValid();
            Debug.assertion (getWriteability_CandidateAnswer () != FieldWriteability.FALSE, "Assoc CandidateAnswer is not writeable");
            preCandidateAnswerChange (newCandidateAnswer);
            CandidateDiversityAnswer oldCandidateAnswer = getCandidateAnswer ();

                    if (oldCandidateAnswer != null)
                    {
                        // This is to stop validation from triggering when we are removed
                        _CandidateAnswer.set (null);
                        oldCandidateAnswer.removeFromAnswers ((AnswerOption)(this));
                    }

                    _CandidateAnswer.set (newCandidateAnswer);

                    if (newCandidateAnswer != null)
                    {
                        newCandidateAnswer.addToAnswers ((AnswerOption)(this));
                    }
                
            postCandidateAnswerChange ();
        }
    }

    /**
     * Get the reference Answer
     */
    public DiversityAnswer getAnswer () throws StorageException
    {
        assertValid();
        
        try
        {
            return (DiversityAnswer)(_Answer.get ());
        }
        catch (ClassCastException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Cache collision in AnswerOption:", this.getObjectID (), ", was trying to get DiversityAnswer:",  getAnswerID ());
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Instead I got:", _Answer.get ().getClass ());
            throw e;
        }
    }


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getAnswerID ()
    {
        assertValid();
        
        if (_Answer == null)
        {
            return null;
        }
        else
        {
            return _Answer.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 preAnswerChange (DiversityAnswer newAnswer) 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 postAnswerChange () throws FieldException
    {
    }



    
    public FieldWriteability getWriteability_Answer ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    /**
     * Set the reference Answer.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setAnswer (DiversityAnswer newAnswer) throws StorageException, FieldException
    {
        BusinessObjectParser.assertFieldCondition (newAnswer != null, this, SINGLEREFERENCE_Answer, "mandatory");
        

        if (_Answer.wouldReferencedChange (newAnswer))
        {
            assertValid();
            Debug.assertion (getWriteability_Answer () != FieldWriteability.FALSE, "Assoc Answer is not writeable");
            preAnswerChange (newAnswer);
            
                    _Answer.set (newAnswer);
                
            postAnswerChange ();
        }
    }

    /**
     * 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 (_CandidateAnswer.isLoaded () || getTransaction ().isObjectLoaded (_CandidateAnswer.getReferencedType (), getCandidateAnswerID ()))
            {
                CandidateDiversityAnswer  referenced = getCandidateAnswer ();

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

        super.onDelete ();
    }


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


    public AnswerOption referenceInstance ()
    {
        return REFERENCE_AnswerOption;
    }


    public AnswerOption getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getAnswerOptionByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_AnswerOption;
    }


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





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

        tl_answer_optionPSet.setAttrib (FIELD_ObjectID, myID);
        tl_answer_optionPSet.setAttrib (FIELD_IsSelected, HELPER_IsSelected.toObject (_IsSelected)); //
        _CandidateAnswer.getPersistentSets (allSets);
        _Answer.getPersistentSets (allSets);
        
    }


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

        
        PersistentSet   tl_answer_optionPSet = allSets.getPersistentSet (objectID, "tl_answer_option");

        _IsSelected = (Boolean)(HELPER_IsSelected.fromObject (_IsSelected, tl_answer_optionPSet.getAttrib (FIELD_IsSelected))); //
        _CandidateAnswer.setFromPersistentSets (objectID, allSets);
        _Answer.setFromPersistentSets (objectID, allSets);
        
    }





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

        if (other instanceof AnswerOption)
        {
            AnswerOption otherAnswerOption = (AnswerOption)other;
            
            try
            {
                setIsSelected (otherAnswerOption.getIsSelected ());
            }
            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 BaseAnswerOption)
        {
            BaseAnswerOption sourceAnswerOption = (BaseAnswerOption)(source);

            _IsSelected = sourceAnswerOption._IsSelected;
            
        }
    }


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

            _CandidateAnswer.copyFrom (sourceAnswerOption._CandidateAnswer, linkToGhosts);
            _Answer.copyFrom (sourceAnswerOption._Answer, 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 BaseAnswerOption)
        {
            BaseAnswerOption sourceAnswerOption = (BaseAnswerOption)(source);

            
        }
    }


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

        
        context.check (getCandidateAnswerID() != null, this, SINGLEREFERENCE_CandidateAnswer, "mandatory");
        
        context.check (getAnswerID() != null, this, SINGLEREFERENCE_Answer, "mandatory");
        
    }


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

        _IsSelected = (Boolean)(HELPER_IsSelected.readExternal (_IsSelected, vals.get(FIELD_IsSelected))); //
        _CandidateAnswer.readExternalData(vals.get(SINGLEREFERENCE_CandidateAnswer));
        _Answer.readExternalData(vals.get(SINGLEREFERENCE_Answer));
        
    }


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

        vals.put (FIELD_IsSelected, HELPER_IsSelected.writeExternal (_IsSelected));
        vals.put (SINGLEREFERENCE_CandidateAnswer, _CandidateAnswer.writeExternalData());
        vals.put (SINGLEREFERENCE_Answer, _Answer.writeExternalData());
        
    }


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

        if (other instanceof BaseAnswerOption)
        {
            BaseAnswerOption otherAnswerOption = (BaseAnswerOption)(other);

            
            if (!HELPER_IsSelected.compare(this._IsSelected, otherAnswerOption._IsSelected))
            {
                listener.notifyFieldChange(this, other, FIELD_IsSelected, HELPER_IsSelected.toObject(this._IsSelected), HELPER_IsSelected.toObject(otherAnswerOption._IsSelected));
            }

            // Compare single assocs
            _CandidateAnswer.compare (otherAnswerOption._CandidateAnswer, listener);
            _Answer.compare (otherAnswerOption._Answer, 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_IsSelected, HELPER_IsSelected.toObject(getIsSelected()));
        visitor.visitAssociation (_CandidateAnswer);
        visitor.visitAssociation (_Answer);
        
    }


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

        if (scope.includes (_CandidateAnswer))
        {
            visitor.visit (_CandidateAnswer);
        }
        if (scope.includes (_Answer))
        {
            visitor.visit (_Answer);
        }
        
    }


    public static AnswerOption createAnswerOption (ObjectTransaction transaction) throws StorageException
    {
        AnswerOption     result = new AnswerOption ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static AnswerOption getAnswerOptionByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (AnswerOption)(transaction.getObjectByID (REFERENCE_AnswerOption, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_IsSelected))
        {
            return filter.matches (getIsSelected ());
        }
        else if (attribName.equals (SINGLEREFERENCE_CandidateAnswer))
        {
            return filter.matches (getCandidateAnswer ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Answer))
        {
            return filter.matches (getAnswer ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
            public SearchAll andIsSelected (QueryFilter<Boolean> filter)
            {
                filter.addFilter (context, "tl_answer_option.is_selected", "IsSelected");
                return this;
            }
            
            public SearchAll andCandidateAnswer (QueryFilter<CandidateDiversityAnswer> filter)
            {
                filter.addFilter (context, "tl_answer_option.candidate_answer_id", "CandidateAnswer");
                return this;
            }
            
            public SearchAll andAnswer (QueryFilter<DiversityAnswer> filter)
            {
                filter.addFilter (context, "tl_answer_option.answer_id", "Answer");
                return this;
            }
            
                public AnswerOption[]
             search (ObjectTransaction transaction) throws StorageException
        {
            
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_AnswerOption, SEARCH_All, criteria);
            Set<AnswerOption>   typedResults = new LinkedHashSet <AnswerOption> ();
            for (BaseBusinessClass bbcResult : results)
            {
                AnswerOption aResult = (AnswerOption)bbcResult;
                
                typedResults.add (aResult);
            }
            
                    return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new AnswerOption[0]);
                            
        }
    }

        
                public static AnswerOption[]
             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_IsSelected))
        {
            return HELPER_IsSelected.toObject (getIsSelected ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_IsSelected))
        {
            return HELPER_IsSelected;
        }
        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_IsSelected))
        {
            setIsSelected ((Boolean)(HELPER_IsSelected.fromObject (_IsSelected, 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_IsSelected))
        {
            return getWriteability_IsSelected ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_CandidateAnswer))
        {
            return getWriteability_CandidateAnswer ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Answer))
        {
            return getWriteability_Answer ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

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

        
        result.add(HELPER_IsSelected.getAttribObject (getClass (), _IsSelected, false, FIELD_IsSelected));

        return result;
    }


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


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

    /**
     * Get the attribute IsSelected
     */
    public Boolean getIsSelected (AnswerOption obj, Boolean original)
    {
        return original;
    }

    /**
     * Change the value set for attribute IsSelected.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Boolean setIsSelected (AnswerOption obj, Boolean newIsSelected) throws FieldException
    {
        return newIsSelected;
    }

    }

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

        public AnswerOptionPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("IsSelected"))
            {
                return toIsSelected ();
            }                
            if (name.equals ("CandidateAnswer"))
            {
                return toCandidateAnswer ();
            }                
            if (name.equals ("Answer"))
            {
                return toAnswer ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, Boolean> toIsSelected () { return pipe(new ORMAttributePipe<Me, Boolean>(FIELD_IsSelected));  }
        public CandidateDiversityAnswer.CandidateDiversityAnswerPipeLineFactory<From, CandidateDiversityAnswer> toCandidateAnswer () { return toCandidateAnswer (Filter.ALL); }

        public CandidateDiversityAnswer.CandidateDiversityAnswerPipeLineFactory<From, CandidateDiversityAnswer> toCandidateAnswer (Filter<CandidateDiversityAnswer> filter) 
        { 
            return CandidateDiversityAnswer.REFERENCE_CandidateDiversityAnswer.new CandidateDiversityAnswerPipeLineFactory<From, CandidateDiversityAnswer> (this, new ORMSingleAssocPipe<Me, CandidateDiversityAnswer>(SINGLEREFERENCE_CandidateAnswer, filter)); 
        }
        public DiversityAnswer.DiversityAnswerPipeLineFactory<From, DiversityAnswer> toAnswer () { return toAnswer (Filter.ALL); }

        public DiversityAnswer.DiversityAnswerPipeLineFactory<From, DiversityAnswer> toAnswer (Filter<DiversityAnswer> filter) 
        { 
            return DiversityAnswer.REFERENCE_DiversityAnswer.new DiversityAnswerPipeLineFactory<From, DiversityAnswer> (this, new ORMSingleAssocPipe<Me, DiversityAnswer>(SINGLEREFERENCE_Answer, filter)); 
        }
        
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public CandidateDiversityAnswer getCandidateAnswer () throws StorageException
    {
        return (CandidateDiversityAnswer)(CandidateDiversityAnswer.DUMMY_CandidateDiversityAnswer);
    }


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

    public DiversityAnswer getAnswer () throws StorageException
    {
        return (DiversityAnswer)(DiversityAnswer.DUMMY_DiversityAnswer);
    }


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

}

    
