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

    // Reference instance for the object
    public static final CultureElementRating DUMMY_CultureElementRating = new DummyCultureElementRating ();

    // Static constants corresponding to field names
    
    public static final String FIELD_Description = "Description";
    public static final String SINGLEREFERENCE_CultureElement = "CultureElement";
    public static final String BACKREF_CultureElement = "";
    public static final String MULTIPLEREFERENCE_Narratives = "Narratives";
    public static final String BACKREF_Narratives = "";

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

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

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

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

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

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

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

    // Arrays of AttributeDecorators for each attribute
    private static final AttributeDecorator<CultureElementRating, String>[] FIELD_Description_Decorators;
    
    
    // Arrays of SingleAssocDecorators for each attribute
    private static final SingleAssocDecorator<CultureElementRating, CultureElement>[] SINGLEREFERENCE_CultureElement_Decorators;
    
    
    // Arrays of MultiAssocDecorators for each attribute
    private static final MultiAssocDecorator<CultureElementRating, CultureNarrative>[] MULTIPLEREFERENCE_Narratives_Decorators;
    

    // Arrays of behaviour decorators
    private static final CultureElementRatingBehaviourDecorator[] CultureElementRating_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Narratives = CultureNarrative.BACKREF_CultureElementRating;
            String tmp_CultureElement = CultureElement.BACKREF_Ratings;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Narratives();
            setupAssocMetaData_CultureElement();
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_Description_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_CultureElementRating, FIELD_Description).toArray (new AttributeDecorator[0]);
            SINGLEREFERENCE_CultureElement_Decorators = (SingleAssocDecorator[])AttributeDecoratorUtils.getSingleAssocDecorators(REFERENCE_CultureElementRating, SINGLEREFERENCE_CultureElement).toArray (new SingleAssocDecorator[0]);
            MULTIPLEREFERENCE_Narratives_Decorators = (MultiAssocDecorator[])AttributeDecoratorUtils.getMultiAssocDecorators(REFERENCE_CultureElementRating, MULTIPLEREFERENCE_Narratives).toArray (new MultiAssocDecorator[0]);
            

            REFERENCE_CultureElementRating.initialiseReference ();
            DUMMY_CultureElementRating.initialiseReference ();
            CultureElementRating_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(CultureElementRating.class).toArray(new CultureElementRatingBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

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

        metaInfo.put ("backreferenceName", "Ratings");
        metaInfo.put ("dbcol", "culture_element_number");
        metaInfo.put ("name", "CultureElement");
        metaInfo.put ("type", "CultureElement");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_culture_element_rating");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElementRating.CultureElement:", metaInfo);
        ATTRIBUTES_METADATA_CultureElementRating.put (SINGLEREFERENCE_CultureElement, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static List setupAttribMetaData_Description(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "culture_element_rating_desc");
        metaInfo.put ("length", "200");
        metaInfo.put ("name", "Description");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_culture_element_rating");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for CultureElementRating.Description:", metaInfo);
        ATTRIBUTES_METADATA_CultureElementRating.put (FIELD_Description, Collections.unmodifiableMap (metaInfo));

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

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


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

    


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

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


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

        _CultureElement = new SingleAssociation<CultureElementRating, CultureElement> (this, SINGLEREFERENCE_CultureElement, CultureElement.MULTIPLEREFERENCE_Ratings, CultureElement.REFERENCE_CultureElement, "tl_culture_element_rating");
        _Narratives = new MultipleAssociation<CultureElementRating, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElementRating, CultureNarrative.REFERENCE_CultureNarrative);
    }


    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _CultureElement = new SingleAssociation<CultureElementRating, CultureElement> (this, SINGLEREFERENCE_CultureElement, CultureElement.MULTIPLEREFERENCE_Ratings, CultureElement.REFERENCE_CultureElement, "tl_culture_element_rating");
        _Narratives = new MultipleAssociation<CultureElementRating, CultureNarrative> (this, MULTIPLEREFERENCE_Narratives, CultureNarrative.SINGLEREFERENCE_CultureElementRating, CultureNarrative.REFERENCE_CultureNarrative);
        
        return this;
    }


    /**
     * Attribute Description
     */
    public String getDescription ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_Description_Decorators, (CultureElementRating)this, FIELD_Description, _Description);

        
        for (CultureElementRatingBehaviourDecorator bhd : CultureElementRating_BehaviourDecorators)
        {
            valToReturn = bhd.getDescription ((CultureElementRating)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
    {
        newDescription = AttributeDecoratorUtils.setValue (FIELD_Description_Decorators, (CultureElementRating)this, FIELD_Description, newDescription);

        boolean oldAndNewIdentical = HELPER_Description.compare (_Description, newDescription);
        
        try
        {
            for (CultureElementRatingBehaviourDecorator bhd : CultureElementRating_BehaviourDecorators)
            {
                newDescription = bhd.setDescription ((CultureElementRating)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_CultureElementRating.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 (AttributeDecoratorUtils.getWriteability (FIELD_Description_Decorators, (CultureElementRating)this, FIELD_Description ,getWriteability_Description ()) != FieldWriteability.FALSE, "Field  Description is not writeable");
            preDescriptionChange (newDescription);
            markFieldChange (FIELD_Description);
            _Description = newDescription;            
            postFieldChange (FIELD_Description);
            postDescriptionChange ();
        }
    }



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

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


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


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


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


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



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


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


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

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


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


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

        
    /**
     * Set the reference CultureElement.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setCultureElement (CultureElement newCultureElement) throws StorageException, FieldException
    {
        if (_CultureElement.wouldReferencedChange (newCultureElement))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (SINGLEREFERENCE_CultureElement_Decorators, (CultureElementRating)this, SINGLEREFERENCE_CultureElement ,getWriteability_CultureElement ()) != FieldWriteability.FALSE, "Assoc CultureElement is not writeable");
            preCultureElementChange (newCultureElement);
            CultureElement oldCultureElement = getCultureElement ();

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

            _CultureElement.set (newCultureElement);

            if (newCultureElement != null)
            {
                newCultureElement.addToRatings ((CultureElementRating)(this));
            }
                
            postCultureElementChange ();
        }
    }

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

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


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

        return super.getMultiAssocBackReference(attribName);
    }


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

        return super.getMultiAssocCount(attribName);
    }


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

        return super.getMultiAssocAt(attribName, index);
    }


    /**
     * Add to a multi assoc by attribute name
     */
    public void addToMultiAssoc(String attribName, BaseBusinessClass newElement) throws StorageException
    {
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            addToNarratives((CultureNarrative)newElement);
            return;
        }

        super.addToMultiAssoc(attribName, newElement);
    }

    /**
     * Remove from a multi assoc by attribute name
     */
    public void removeFromMultiAssoc(String attribName, BaseBusinessClass oldElement) throws StorageException
    {
        if (MULTIPLEREFERENCE_Narratives.equals(attribName))
        {
            removeFromNarratives((CultureNarrative)oldElement);
            return;
        }

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


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

        return super.__isMultiAssocLoaded(attribName);
    }


    public FieldWriteability getWriteability_Narratives ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getNarrativesCount () throws StorageException
    {
        assertValid();
        return _Narratives.getReferencedObjectsCount ();
    }


    public void addToNarratives (CultureNarrative newElement) throws StorageException
    {
        if (_Narratives.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (MULTIPLEREFERENCE_Narratives_Decorators, (CultureElementRating)this, MULTIPLEREFERENCE_Narratives ,getWriteability_Narratives ()) != FieldWriteability.FALSE, "MultiAssoc Narratives is not writeable (add)");
            _Narratives.appendElement (newElement);

            try
            {
                if (newElement.getCultureElementRating () != this)
                {
                    newElement.setCultureElementRating ((CultureElementRating)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromNarratives (CultureNarrative elementToRemove) throws StorageException
    {
        if (_Narratives.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (MULTIPLEREFERENCE_Narratives_Decorators, (CultureElementRating)this, MULTIPLEREFERENCE_Narratives ,getWriteability_Narratives ()) != FieldWriteability.FALSE, "MultiAssoc Narratives is not writeable (remove)");
            _Narratives.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getCultureElementRating () != null)
                {
                    elementToRemove.setCultureElementRating (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public CultureNarrative getNarrativesAt (int index) throws StorageException
    {
        return (CultureNarrative)(_Narratives.getElementAt (index));
    }


    public SortedSet<CultureNarrative> getNarrativesSet () throws StorageException
    {
        return _Narratives.getSet ();
    }



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

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

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

        super.onDelete ();
    }


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


    public CultureElementRating referenceInstance ()
    {
        return REFERENCE_CultureElementRating;
    }


    public CultureElementRating getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getCultureElementRatingByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_CultureElementRating;
    }


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



    /**
     * This is where an object returns the Persistent sets that will
     * store it into the database.
     * The should be entered into allSets
     */
    public void getPersistentSets (PersistentSetCollection allSets)
    {
        ObjectStatus        myStatus = getStatus ();
        PersistentSetStatus myPSetStatus = myStatus.getPSetStatus();
        ObjectID            myID = getID();

        super.getPersistentSets (allSets);
        
        PersistentSet   tl_culture_element_ratingPSet = allSets.getPersistentSet (myID, "tl_culture_element_rating", myPSetStatus);

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


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

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


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

        if (other instanceof CultureElementRating)
        {
            CultureElementRating otherCultureElementRating = (CultureElementRating)other;
            

            try
            {
                setDescription (otherCultureElementRating.getDescription ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }
        }
    }


    /**
     * Set the attributes in this to copies of the attributes in source.
     */
    public void copyAttributesFrom (BaseBusinessClass source)
    {
        super.copyAttributesFrom (source);

        if (source instanceof BaseCultureElementRating)
        {
            BaseCultureElementRating sourceCultureElementRating = (BaseCultureElementRating)(source);

            _Description = sourceCultureElementRating._Description;
            
        }
    }


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

        if (source instanceof BaseCultureElementRating)
        {
            BaseCultureElementRating sourceCultureElementRating = (BaseCultureElementRating)(source);

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


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

        if (source instanceof BaseCultureElementRating)
        {
            BaseCultureElementRating sourceCultureElementRating = (BaseCultureElementRating)(source);

            _Narratives.copyFrom (sourceCultureElementRating._Narratives, linkToGhosts);
            
        }
    }


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

        
    }


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

        _Description = (String)(HELPER_Description.readExternal (_Description, vals.get(FIELD_Description))); //
        _CultureElement.readExternalData(vals.get(SINGLEREFERENCE_CultureElement));
        _Narratives.readExternalData(vals.get(MULTIPLEREFERENCE_Narratives));
        
    }


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

        vals.put (FIELD_Description, HELPER_Description.writeExternal (_Description));
        vals.put (SINGLEREFERENCE_CultureElement, _CultureElement.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Narratives, _Narratives.writeExternalData());
        
    }


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

        if (other instanceof BaseCultureElementRating)
        {
            BaseCultureElementRating otherCultureElementRating = (BaseCultureElementRating)(other);

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

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

            // Compare multiple assocs
            _Narratives.compare (otherCultureElementRating._Narratives, listener);
            
        }
    }


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

        
    }


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

        visitor.visitField(this, FIELD_Description, HELPER_Description.toObject(getDescription()));
        visitor.visitAssociation (_CultureElement);
        visitor.visitAssociation (_Narratives);
        
    }


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

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


    public static CultureElementRating createCultureElementRating (ObjectTransaction transaction) throws StorageException
    {
        CultureElementRating     result = new CultureElementRating ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static CultureElementRating getCultureElementRatingByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (CultureElementRating)(transaction.getObjectByID (REFERENCE_CultureElementRating, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            return filter.matches (getDescription ());
        }
        else if (attribName.equals (SINGLEREFERENCE_CultureElement))
        {
            return filter.matches (getCultureElement ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

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

        
    public static CultureElementRating[] searchAll (ObjectTransaction transaction) throws StorageException
    {
        return SearchByAll ()
               .search (transaction);
    }



    public Object getAttribute (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            return HELPER_Description.toObject (getDescription ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            return HELPER_Description;
        }
        else
        {
            return super.getAttributeHelper (attribName);
        }
    }


    public void setAttribute (String attribName, Object attribValue) throws FieldException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_Description))
        {
            setDescription ((String)(HELPER_Description.fromObject (_Description, attribValue)));            
        }
        else
        {
            super.setAttribute (attribName, attribValue);
        }
    }

    

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

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


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

        super.putUnwriteable (fields);
    }

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


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

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

        return result;
    }


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


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

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

        copyInto.putAll(ATTRIBUTES_METADATA_CultureElementRating);
    }
            

    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 CultureElementRatingBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<CultureElementRating>
    {

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

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

    }

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

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

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

        public CultureElement.CultureElementPipeLineFactory<From, CultureElement> toCultureElement () { return toCultureElement (Filter.ALL); }

        public CultureElement.CultureElementPipeLineFactory<From, CultureElement> toCultureElement (Filter<CultureElement> filter) 
        { 
            return CultureElement.REFERENCE_CultureElement.new CultureElementPipeLineFactory<From, CultureElement> (this, new ORMSingleAssocPipe<Me, CultureElement>(SINGLEREFERENCE_CultureElement, filter)); 
        }

        public CultureNarrative.CultureNarrativePipeLineFactory<From, CultureNarrative> toNarratives () { return toNarratives(Filter.ALL); }        

        public CultureNarrative.CultureNarrativePipeLineFactory<From, CultureNarrative> toNarratives (Filter<CultureNarrative> filter)
        {
            return CultureNarrative.REFERENCE_CultureNarrative.new CultureNarrativePipeLineFactory<From, CultureNarrative> (this, new ORMMultiAssocPipe<Me, CultureNarrative>(MULTIPLEREFERENCE_Narratives, filter)); 
        }
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


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


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

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


    public CultureNarrative getNarrativesAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Narratives");
    }


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

}

    
