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

    // Reference instance for the object
    public static final EmailMessage DUMMY_EmailMessage = new DummyEmailMessage ();

    // Static constants corresponding to field names
    
    public static final String FIELD_MessageId = "MessageId";
    public static final String FIELD_EmailFrom = "EmailFrom";
    public static final String FIELD_EmailTo = "EmailTo";
    public static final String FIELD_EmailCC = "EmailCC";
    public static final String FIELD_Subject = "Subject";
    public static final String FIELD_Description = "Description";
    public static final String FIELD_ReceivedDate = "ReceivedDate";
    public static final String SINGLEREFERENCE_Job = "Job";
    public static final String MULTIPLEREFERENCE_Attachments = "Attachments";
    public static final String BACKREF_Attachments = "";

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

    // Static constants corresponding to attribute helpers
    private static final DefaultAttributeHelper<EmailMessage> HELPER_MessageId = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_EmailFrom = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_EmailTo = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_EmailCC = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_Subject = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_Description = DefaultAttributeHelper.INSTANCE;
    private static final DefaultAttributeHelper<EmailMessage> HELPER_ReceivedDate = DefaultAttributeHelper.INSTANCE;
    

    // Private attributes corresponding to business object data
    private String _MessageId;
    private String _EmailFrom;
    private String _EmailTo;
    private String _EmailCC;
    private String _Subject;
    private String _Description;
    private Date _ReceivedDate;
    

    // Private attributes corresponding to single references
    private SingleAssociation<EmailMessage, Job> _Job;
    

    // Private attributes corresponding to multiple references
    private MultipleAssociation<EmailMessage, Attachment> _Attachments;
    

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

    // Arrays of validators for each attribute
    private static final AttributeValidator[] FIELD_MessageId_Validators;
    private static final AttributeValidator[] FIELD_EmailFrom_Validators;
    private static final AttributeValidator[] FIELD_EmailTo_Validators;
    private static final AttributeValidator[] FIELD_EmailCC_Validators;
    private static final AttributeValidator[] FIELD_Subject_Validators;
    private static final AttributeValidator[] FIELD_Description_Validators;
    private static final AttributeValidator[] FIELD_ReceivedDate_Validators;
    

    // Arrays of AttributeDecorators for each attribute
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_MessageId_Decorators;
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_EmailFrom_Decorators;
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_EmailTo_Decorators;
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_EmailCC_Decorators;
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_Subject_Decorators;
    private static final AttributeDecorator<EmailMessage, String>[] FIELD_Description_Decorators;
    private static final AttributeDecorator<EmailMessage, Date>[] FIELD_ReceivedDate_Decorators;
    
    
    // Arrays of SingleAssocDecorators for each attribute
    private static final SingleAssocDecorator<EmailMessage, Job>[] SINGLEREFERENCE_Job_Decorators;
    
    
    // Arrays of MultiAssocDecorators for each attribute
    private static final MultiAssocDecorator<EmailMessage, Attachment>[] MULTIPLEREFERENCE_Attachments_Decorators;
    

    // Arrays of behaviour decorators
    private static final EmailMessageBehaviourDecorator[] EmailMessage_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Attachments = Attachment.BACKREF_EmailMessage;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Attachments();
            setupAssocMetaData_Job();
            FIELD_MessageId_Validators = (AttributeValidator[])setupAttribMetaData_MessageId(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_EmailFrom_Validators = (AttributeValidator[])setupAttribMetaData_EmailFrom(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_EmailTo_Validators = (AttributeValidator[])setupAttribMetaData_EmailTo(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_EmailCC_Validators = (AttributeValidator[])setupAttribMetaData_EmailCC(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_Subject_Validators = (AttributeValidator[])setupAttribMetaData_Subject(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_Description_Validators = (AttributeValidator[])setupAttribMetaData_Description(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_ReceivedDate_Validators = (AttributeValidator[])setupAttribMetaData_ReceivedDate(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_MessageId_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_MessageId).toArray (new AttributeDecorator[0]);
            FIELD_EmailFrom_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_EmailFrom).toArray (new AttributeDecorator[0]);
            FIELD_EmailTo_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_EmailTo).toArray (new AttributeDecorator[0]);
            FIELD_EmailCC_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_EmailCC).toArray (new AttributeDecorator[0]);
            FIELD_Subject_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_Subject).toArray (new AttributeDecorator[0]);
            FIELD_Description_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_Description).toArray (new AttributeDecorator[0]);
            FIELD_ReceivedDate_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_EmailMessage, FIELD_ReceivedDate).toArray (new AttributeDecorator[0]);
            SINGLEREFERENCE_Job_Decorators = (SingleAssocDecorator[])AttributeDecoratorUtils.getSingleAssocDecorators(REFERENCE_EmailMessage, SINGLEREFERENCE_Job).toArray (new SingleAssocDecorator[0]);
            MULTIPLEREFERENCE_Attachments_Decorators = (MultiAssocDecorator[])AttributeDecoratorUtils.getMultiAssocDecorators(REFERENCE_EmailMessage, MULTIPLEREFERENCE_Attachments).toArray (new MultiAssocDecorator[0]);
            

            REFERENCE_EmailMessage.initialiseReference ();
            DUMMY_EmailMessage.initialiseReference ();
            EmailMessage_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(EmailMessage.class).toArray(new EmailMessageBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "EmailMessage");
        metaInfo.put ("name", "Attachments");
        metaInfo.put ("type", "Attachment");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.Attachments:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (MULTIPLEREFERENCE_Attachments, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static void setupAssocMetaData_Job()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("dbcol", "job_id");
        metaInfo.put ("name", "Job");
        metaInfo.put ("type", "Job");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.Job:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (SINGLEREFERENCE_Job, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static List setupAttribMetaData_MessageId(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "message_id");
        metaInfo.put ("length", "1000");
        metaInfo.put ("name", "MessageId");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.MessageId:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_MessageId, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_EmailFrom(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "email_from");
        metaInfo.put ("length", "500");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "EmailFrom");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.EmailFrom:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_EmailFrom, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_EmailTo(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "email_to");
        metaInfo.put ("length", "500");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "EmailTo");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.EmailTo:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_EmailTo, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_EmailCC(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "email_cc");
        metaInfo.put ("length", "500");
        metaInfo.put ("name", "EmailCC");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.EmailCC:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_EmailCC, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_Subject(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "subject");
        metaInfo.put ("length", "1000");
        metaInfo.put ("name", "Subject");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.Subject:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_Subject, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_Description(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "description");
        metaInfo.put ("name", "Description");
        metaInfo.put ("type", "String");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.Description:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_Description, Collections.unmodifiableMap (metaInfo));

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

        return validators;
    }
    
    private static List setupAttribMetaData_ReceivedDate(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "received_date");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "ReceivedDate");
        metaInfo.put ("type", "Date");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_email_message");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for EmailMessage.ReceivedDate:", metaInfo);
        ATTRIBUTES_METADATA_EmailMessage.put (FIELD_ReceivedDate, Collections.unmodifiableMap (metaInfo));

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

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


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

    


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

        
        _MessageId = (String)(HELPER_MessageId.initialise (_MessageId));
        _EmailFrom = (String)(HELPER_EmailFrom.initialise (_EmailFrom));
        _EmailTo = (String)(HELPER_EmailTo.initialise (_EmailTo));
        _EmailCC = (String)(HELPER_EmailCC.initialise (_EmailCC));
        _Subject = (String)(HELPER_Subject.initialise (_Subject));
        _Description = (String)(HELPER_Description.initialise (_Description));
        _ReceivedDate = (Date)(HELPER_ReceivedDate.initialise (_ReceivedDate));
    }


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

        _Job = new SingleAssociation<EmailMessage, Job> (this, SINGLEREFERENCE_Job, null, Job.REFERENCE_Job, "tl_email_message");
        _Attachments = new MultipleAssociation<EmailMessage, Attachment> (this, MULTIPLEREFERENCE_Attachments, Attachment.SINGLEREFERENCE_EmailMessage, Attachment.REFERENCE_Attachment);
    }


    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Job = new SingleAssociation<EmailMessage, Job> (this, SINGLEREFERENCE_Job, null, Job.REFERENCE_Job, "tl_email_message");
        _Attachments = new MultipleAssociation<EmailMessage, Attachment> (this, MULTIPLEREFERENCE_Attachments, Attachment.SINGLEREFERENCE_EmailMessage, Attachment.REFERENCE_Attachment);
        
        return this;
    }


    /**
     * Attribute MessageId
     */
    public String getMessageId ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_MessageId_Decorators, (EmailMessage)this, FIELD_MessageId, _MessageId);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getMessageId ((EmailMessage)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 preMessageIdChange (String newMessageId) 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 postMessageIdChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute MessageId.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setMessageId (String newMessageId) throws FieldException
    {
        newMessageId = AttributeDecoratorUtils.setValue (FIELD_MessageId_Decorators, (EmailMessage)this, FIELD_MessageId, newMessageId);

        boolean oldAndNewIdentical = HELPER_MessageId.compare (_MessageId, newMessageId);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newMessageId = bhd.setMessageId ((EmailMessage)this, newMessageId);
                oldAndNewIdentical = HELPER_MessageId.compare (_MessageId, newMessageId);
            }

            

            if (FIELD_MessageId_Validators.length > 0)
            {
                Object newMessageIdObj = HELPER_MessageId.toObject (newMessageId);

                if (newMessageIdObj != null)
                {
                    int     loopMax = FIELD_MessageId_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_MessageId);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_MessageId_Decorators, (EmailMessage)this, FIELD_MessageId ,getWriteability_MessageId ()) != FieldWriteability.FALSE, "Field  MessageId is not writeable");
            preMessageIdChange (newMessageId);
            markFieldChange (FIELD_MessageId);
            _MessageId = newMessageId;            
            postFieldChange (FIELD_MessageId);
            postMessageIdChange ();
        }
    }

    /**
     * Attribute EmailFrom
     */
    public String getEmailFrom ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_EmailFrom_Decorators, (EmailMessage)this, FIELD_EmailFrom, _EmailFrom);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getEmailFrom ((EmailMessage)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 preEmailFromChange (String newEmailFrom) 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 postEmailFromChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute EmailFrom.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setEmailFrom (String newEmailFrom) throws FieldException
    {
        newEmailFrom = AttributeDecoratorUtils.setValue (FIELD_EmailFrom_Decorators, (EmailMessage)this, FIELD_EmailFrom, newEmailFrom);

        boolean oldAndNewIdentical = HELPER_EmailFrom.compare (_EmailFrom, newEmailFrom);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newEmailFrom = bhd.setEmailFrom ((EmailMessage)this, newEmailFrom);
                oldAndNewIdentical = HELPER_EmailFrom.compare (_EmailFrom, newEmailFrom);
            }

            BusinessObjectParser.assertFieldCondition (newEmailFrom != null, this, FIELD_EmailFrom, "mandatory");
            

            if (FIELD_EmailFrom_Validators.length > 0)
            {
                Object newEmailFromObj = HELPER_EmailFrom.toObject (newEmailFrom);

                if (newEmailFromObj != null)
                {
                    int     loopMax = FIELD_EmailFrom_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_EmailFrom);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_EmailFrom_Decorators, (EmailMessage)this, FIELD_EmailFrom ,getWriteability_EmailFrom ()) != FieldWriteability.FALSE, "Field  EmailFrom is not writeable");
            preEmailFromChange (newEmailFrom);
            markFieldChange (FIELD_EmailFrom);
            _EmailFrom = newEmailFrom;            
            postFieldChange (FIELD_EmailFrom);
            postEmailFromChange ();
        }
    }

    /**
     * Attribute EmailTo
     */
    public String getEmailTo ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_EmailTo_Decorators, (EmailMessage)this, FIELD_EmailTo, _EmailTo);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getEmailTo ((EmailMessage)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 preEmailToChange (String newEmailTo) 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 postEmailToChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute EmailTo.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setEmailTo (String newEmailTo) throws FieldException
    {
        newEmailTo = AttributeDecoratorUtils.setValue (FIELD_EmailTo_Decorators, (EmailMessage)this, FIELD_EmailTo, newEmailTo);

        boolean oldAndNewIdentical = HELPER_EmailTo.compare (_EmailTo, newEmailTo);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newEmailTo = bhd.setEmailTo ((EmailMessage)this, newEmailTo);
                oldAndNewIdentical = HELPER_EmailTo.compare (_EmailTo, newEmailTo);
            }

            BusinessObjectParser.assertFieldCondition (newEmailTo != null, this, FIELD_EmailTo, "mandatory");
            

            if (FIELD_EmailTo_Validators.length > 0)
            {
                Object newEmailToObj = HELPER_EmailTo.toObject (newEmailTo);

                if (newEmailToObj != null)
                {
                    int     loopMax = FIELD_EmailTo_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_EmailTo);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_EmailTo_Decorators, (EmailMessage)this, FIELD_EmailTo ,getWriteability_EmailTo ()) != FieldWriteability.FALSE, "Field  EmailTo is not writeable");
            preEmailToChange (newEmailTo);
            markFieldChange (FIELD_EmailTo);
            _EmailTo = newEmailTo;            
            postFieldChange (FIELD_EmailTo);
            postEmailToChange ();
        }
    }

    /**
     * Attribute EmailCC
     */
    public String getEmailCC ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_EmailCC_Decorators, (EmailMessage)this, FIELD_EmailCC, _EmailCC);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getEmailCC ((EmailMessage)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 preEmailCCChange (String newEmailCC) 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 postEmailCCChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute EmailCC.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setEmailCC (String newEmailCC) throws FieldException
    {
        newEmailCC = AttributeDecoratorUtils.setValue (FIELD_EmailCC_Decorators, (EmailMessage)this, FIELD_EmailCC, newEmailCC);

        boolean oldAndNewIdentical = HELPER_EmailCC.compare (_EmailCC, newEmailCC);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newEmailCC = bhd.setEmailCC ((EmailMessage)this, newEmailCC);
                oldAndNewIdentical = HELPER_EmailCC.compare (_EmailCC, newEmailCC);
            }

            

            if (FIELD_EmailCC_Validators.length > 0)
            {
                Object newEmailCCObj = HELPER_EmailCC.toObject (newEmailCC);

                if (newEmailCCObj != null)
                {
                    int     loopMax = FIELD_EmailCC_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_EmailCC);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_EmailCC_Decorators, (EmailMessage)this, FIELD_EmailCC ,getWriteability_EmailCC ()) != FieldWriteability.FALSE, "Field  EmailCC is not writeable");
            preEmailCCChange (newEmailCC);
            markFieldChange (FIELD_EmailCC);
            _EmailCC = newEmailCC;            
            postFieldChange (FIELD_EmailCC);
            postEmailCCChange ();
        }
    }

    /**
     * Attribute Subject
     */
    public String getSubject ()
    {
        assertValid();
        String valToReturn = AttributeDecoratorUtils.getValue (FIELD_Subject_Decorators, (EmailMessage)this, FIELD_Subject, _Subject);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getSubject ((EmailMessage)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 preSubjectChange (String newSubject) 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 postSubjectChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute Subject.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setSubject (String newSubject) throws FieldException
    {
        newSubject = AttributeDecoratorUtils.setValue (FIELD_Subject_Decorators, (EmailMessage)this, FIELD_Subject, newSubject);

        boolean oldAndNewIdentical = HELPER_Subject.compare (_Subject, newSubject);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newSubject = bhd.setSubject ((EmailMessage)this, newSubject);
                oldAndNewIdentical = HELPER_Subject.compare (_Subject, newSubject);
            }

            

            if (FIELD_Subject_Validators.length > 0)
            {
                Object newSubjectObj = HELPER_Subject.toObject (newSubject);

                if (newSubjectObj != null)
                {
                    int     loopMax = FIELD_Subject_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_Subject);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_Subject_Decorators, (EmailMessage)this, FIELD_Subject ,getWriteability_Subject ()) != FieldWriteability.FALSE, "Field  Subject is not writeable");
            preSubjectChange (newSubject);
            markFieldChange (FIELD_Subject);
            _Subject = newSubject;            
            postFieldChange (FIELD_Subject);
            postSubjectChange ();
        }
    }

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

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getDescription ((EmailMessage)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, (EmailMessage)this, FIELD_Description, newDescription);

        boolean oldAndNewIdentical = HELPER_Description.compare (_Description, newDescription);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newDescription = bhd.setDescription ((EmailMessage)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_EmailMessage.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, (EmailMessage)this, FIELD_Description ,getWriteability_Description ()) != FieldWriteability.FALSE, "Field  Description is not writeable");
            preDescriptionChange (newDescription);
            markFieldChange (FIELD_Description);
            _Description = newDescription;            
            postFieldChange (FIELD_Description);
            postDescriptionChange ();
        }
    }

    /**
     * Attribute ReceivedDate
     */
    public Date getReceivedDate ()
    {
        assertValid();
        Date valToReturn = AttributeDecoratorUtils.getValue (FIELD_ReceivedDate_Decorators, (EmailMessage)this, FIELD_ReceivedDate, _ReceivedDate);

        
        for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
        {
            valToReturn = bhd.getReceivedDate ((EmailMessage)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 preReceivedDateChange (Date newReceivedDate) 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 postReceivedDateChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute ReceivedDate.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setReceivedDate (Date newReceivedDate) throws FieldException
    {
        newReceivedDate = AttributeDecoratorUtils.setValue (FIELD_ReceivedDate_Decorators, (EmailMessage)this, FIELD_ReceivedDate, newReceivedDate);

        boolean oldAndNewIdentical = HELPER_ReceivedDate.compare (_ReceivedDate, newReceivedDate);
        
        try
        {
            for (EmailMessageBehaviourDecorator bhd : EmailMessage_BehaviourDecorators)
            {
                newReceivedDate = bhd.setReceivedDate ((EmailMessage)this, newReceivedDate);
                oldAndNewIdentical = HELPER_ReceivedDate.compare (_ReceivedDate, newReceivedDate);
            }

            BusinessObjectParser.assertFieldCondition (newReceivedDate != null, this, FIELD_ReceivedDate, "mandatory");
            

            if (FIELD_ReceivedDate_Validators.length > 0)
            {
                Object newReceivedDateObj = HELPER_ReceivedDate.toObject (newReceivedDate);

                if (newReceivedDateObj != null)
                {
                    int     loopMax = FIELD_ReceivedDate_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_EmailMessage.get (FIELD_ReceivedDate);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_ReceivedDate_Decorators, (EmailMessage)this, FIELD_ReceivedDate ,getWriteability_ReceivedDate ()) != FieldWriteability.FALSE, "Field  ReceivedDate is not writeable");
            preReceivedDateChange (newReceivedDate);
            markFieldChange (FIELD_ReceivedDate);
            _ReceivedDate = newReceivedDate;            
            postFieldChange (FIELD_ReceivedDate);
            postReceivedDateChange ();
        }
    }



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

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Job))
        {
            return _Job.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_Job))
        {
            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_Job))
        {
            return getJob ();
        }
        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_Job))
        {
            return getJob (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_Job))
        {
            return getJobID ();
        }
        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_Job))
        {
            setJob ((Job)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getJobID ()
    {
        assertValid();
        
        if (_Job == null)
        {
            return null;
        }
        else
        {
            return _Job.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 preJobChange (Job newJob) 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 postJobChange () throws FieldException
    {
    }


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

        
    /**
     * Set the reference Job.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setJob (Job newJob) throws StorageException, FieldException
    {
        if (_Job.wouldReferencedChange (newJob))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (SINGLEREFERENCE_Job_Decorators, (EmailMessage)this, SINGLEREFERENCE_Job ,getWriteability_Job ()) != FieldWriteability.FALSE, "Assoc Job is not writeable");
            preJobChange (newJob);
            
            _Job.set (newJob);
                
            postJobChange ();
        }
    }

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

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        if (MULTIPLEREFERENCE_Attachments.equals(attribName))
        {
            return Attachment.SINGLEREFERENCE_EmailMessage ;
        }

        return super.getMultiAssocBackReference(attribName);
    }


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

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {
        if (MULTIPLEREFERENCE_Attachments.equals(attribName))
        {
            return this.getAttachmentsAt(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_Attachments.equals(attribName))
        {
            addToAttachments((Attachment)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_Attachments.equals(attribName))
        {
            removeFromAttachments((Attachment)oldElement);
            return;
        }

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        if (MULTIPLEREFERENCE_Attachments.equals(attribName))
        {
            return _Attachments.isLoaded ();
        }

        return super.__isMultiAssocLoaded(attribName);
    }


    public FieldWriteability getWriteability_Attachments ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getAttachmentsCount () throws StorageException
    {
        assertValid();
        return _Attachments.getReferencedObjectsCount ();
    }


    public void addToAttachments (Attachment newElement) throws StorageException
    {
        if (_Attachments.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (MULTIPLEREFERENCE_Attachments_Decorators, (EmailMessage)this, MULTIPLEREFERENCE_Attachments ,getWriteability_Attachments ()) != FieldWriteability.FALSE, "MultiAssoc Attachments is not writeable (add)");
            _Attachments.appendElement (newElement);

            try
            {
                if (newElement.getEmailMessage () != this)
                {
                    newElement.setEmailMessage ((EmailMessage)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromAttachments (Attachment elementToRemove) throws StorageException
    {
        if (_Attachments.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (MULTIPLEREFERENCE_Attachments_Decorators, (EmailMessage)this, MULTIPLEREFERENCE_Attachments ,getWriteability_Attachments ()) != FieldWriteability.FALSE, "MultiAssoc Attachments is not writeable (remove)");
            _Attachments.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getEmailMessage () != null)
                {
                    elementToRemove.setEmailMessage (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public Attachment getAttachmentsAt (int index) throws StorageException
    {
        return (Attachment)(_Attachments.getElementAt (index));
    }


    public SortedSet<Attachment> getAttachmentsSet () throws StorageException
    {
        return _Attachments.getSet ();
    }



    public void onDelete ()
    {
        try 
        {
            for(Attachment referenced : CollectionUtils.reverse(getAttachmentsSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null EmailMessage from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setEmailMessage(null);
            }

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

        super.onDelete ();
    }


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


    public EmailMessage referenceInstance ()
    {
        return REFERENCE_EmailMessage;
    }


    public EmailMessage getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getEmailMessageByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_EmailMessage;
    }


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



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

        tl_email_messagePSet.setAttrib (FIELD_ObjectID, myID);
        tl_email_messagePSet.setAttrib (FIELD_MessageId, HELPER_MessageId.toObject (_MessageId)); //
        tl_email_messagePSet.setAttrib (FIELD_EmailFrom, HELPER_EmailFrom.toObject (_EmailFrom)); //
        tl_email_messagePSet.setAttrib (FIELD_EmailTo, HELPER_EmailTo.toObject (_EmailTo)); //
        tl_email_messagePSet.setAttrib (FIELD_EmailCC, HELPER_EmailCC.toObject (_EmailCC)); //
        tl_email_messagePSet.setAttrib (FIELD_Subject, HELPER_Subject.toObject (_Subject)); //
        tl_email_messagePSet.setAttrib (FIELD_Description, HELPER_Description.toObject (_Description)); //
        tl_email_messagePSet.setAttrib (FIELD_ReceivedDate, HELPER_ReceivedDate.toObject (_ReceivedDate)); //
        _Job.getPersistentSets (allSets);
        
    }


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

        _MessageId = (String)(HELPER_MessageId.fromObject (_MessageId, tl_email_messagePSet.getAttrib (FIELD_MessageId))); //
        _EmailFrom = (String)(HELPER_EmailFrom.fromObject (_EmailFrom, tl_email_messagePSet.getAttrib (FIELD_EmailFrom))); //
        _EmailTo = (String)(HELPER_EmailTo.fromObject (_EmailTo, tl_email_messagePSet.getAttrib (FIELD_EmailTo))); //
        _EmailCC = (String)(HELPER_EmailCC.fromObject (_EmailCC, tl_email_messagePSet.getAttrib (FIELD_EmailCC))); //
        _Subject = (String)(HELPER_Subject.fromObject (_Subject, tl_email_messagePSet.getAttrib (FIELD_Subject))); //
        _Description = (String)(HELPER_Description.fromObject (_Description, tl_email_messagePSet.getAttrib (FIELD_Description))); //
        _ReceivedDate = (Date)(HELPER_ReceivedDate.fromObject (_ReceivedDate, tl_email_messagePSet.getAttrib (FIELD_ReceivedDate))); //
        _Job.setFromPersistentSets (objectID, allSets);
        
    }


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

        if (other instanceof EmailMessage)
        {
            EmailMessage otherEmailMessage = (EmailMessage)other;
            

            try
            {
                setMessageId (otherEmailMessage.getMessageId ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }

            try
            {
                setEmailFrom (otherEmailMessage.getEmailFrom ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }

            try
            {
                setEmailTo (otherEmailMessage.getEmailTo ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }

            try
            {
                setEmailCC (otherEmailMessage.getEmailCC ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }

            try
            {
                setSubject (otherEmailMessage.getSubject ());
            }
            catch (FieldException ex)
            {
                e.addException (ex);
            }

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

            try
            {
                setReceivedDate (otherEmailMessage.getReceivedDate ());
            }
            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 BaseEmailMessage)
        {
            BaseEmailMessage sourceEmailMessage = (BaseEmailMessage)(source);

            _MessageId = sourceEmailMessage._MessageId;
            _EmailFrom = sourceEmailMessage._EmailFrom;
            _EmailTo = sourceEmailMessage._EmailTo;
            _EmailCC = sourceEmailMessage._EmailCC;
            _Subject = sourceEmailMessage._Subject;
            _Description = sourceEmailMessage._Description;
            _ReceivedDate = sourceEmailMessage._ReceivedDate;
            
        }
    }


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

            _Job.copyFrom (sourceEmailMessage._Job, 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 BaseEmailMessage)
        {
            BaseEmailMessage sourceEmailMessage = (BaseEmailMessage)(source);

            _Attachments.copyFrom (sourceEmailMessage._Attachments, 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);

        _MessageId = (String)(HELPER_MessageId.readExternal (_MessageId, vals.get(FIELD_MessageId))); //
        _EmailFrom = (String)(HELPER_EmailFrom.readExternal (_EmailFrom, vals.get(FIELD_EmailFrom))); //
        _EmailTo = (String)(HELPER_EmailTo.readExternal (_EmailTo, vals.get(FIELD_EmailTo))); //
        _EmailCC = (String)(HELPER_EmailCC.readExternal (_EmailCC, vals.get(FIELD_EmailCC))); //
        _Subject = (String)(HELPER_Subject.readExternal (_Subject, vals.get(FIELD_Subject))); //
        _Description = (String)(HELPER_Description.readExternal (_Description, vals.get(FIELD_Description))); //
        _ReceivedDate = (Date)(HELPER_ReceivedDate.readExternal (_ReceivedDate, vals.get(FIELD_ReceivedDate))); //
        _Job.readExternalData(vals.get(SINGLEREFERENCE_Job));
        _Attachments.readExternalData(vals.get(MULTIPLEREFERENCE_Attachments));
        
    }


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

        vals.put (FIELD_MessageId, HELPER_MessageId.writeExternal (_MessageId));
        vals.put (FIELD_EmailFrom, HELPER_EmailFrom.writeExternal (_EmailFrom));
        vals.put (FIELD_EmailTo, HELPER_EmailTo.writeExternal (_EmailTo));
        vals.put (FIELD_EmailCC, HELPER_EmailCC.writeExternal (_EmailCC));
        vals.put (FIELD_Subject, HELPER_Subject.writeExternal (_Subject));
        vals.put (FIELD_Description, HELPER_Description.writeExternal (_Description));
        vals.put (FIELD_ReceivedDate, HELPER_ReceivedDate.writeExternal (_ReceivedDate));
        vals.put (SINGLEREFERENCE_Job, _Job.writeExternalData());
        vals.put (MULTIPLEREFERENCE_Attachments, _Attachments.writeExternalData());
        
    }


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

        if (other instanceof BaseEmailMessage)
        {
            BaseEmailMessage otherEmailMessage = (BaseEmailMessage)(other);

            
            if (!HELPER_MessageId.compare(this._MessageId, otherEmailMessage._MessageId))
            {
                listener.notifyFieldChange(this, other, FIELD_MessageId, HELPER_MessageId.toObject(this._MessageId), HELPER_MessageId.toObject(otherEmailMessage._MessageId));
            }
            if (!HELPER_EmailFrom.compare(this._EmailFrom, otherEmailMessage._EmailFrom))
            {
                listener.notifyFieldChange(this, other, FIELD_EmailFrom, HELPER_EmailFrom.toObject(this._EmailFrom), HELPER_EmailFrom.toObject(otherEmailMessage._EmailFrom));
            }
            if (!HELPER_EmailTo.compare(this._EmailTo, otherEmailMessage._EmailTo))
            {
                listener.notifyFieldChange(this, other, FIELD_EmailTo, HELPER_EmailTo.toObject(this._EmailTo), HELPER_EmailTo.toObject(otherEmailMessage._EmailTo));
            }
            if (!HELPER_EmailCC.compare(this._EmailCC, otherEmailMessage._EmailCC))
            {
                listener.notifyFieldChange(this, other, FIELD_EmailCC, HELPER_EmailCC.toObject(this._EmailCC), HELPER_EmailCC.toObject(otherEmailMessage._EmailCC));
            }
            if (!HELPER_Subject.compare(this._Subject, otherEmailMessage._Subject))
            {
                listener.notifyFieldChange(this, other, FIELD_Subject, HELPER_Subject.toObject(this._Subject), HELPER_Subject.toObject(otherEmailMessage._Subject));
            }
            if (!HELPER_Description.compare(this._Description, otherEmailMessage._Description))
            {
                listener.notifyFieldChange(this, other, FIELD_Description, HELPER_Description.toObject(this._Description), HELPER_Description.toObject(otherEmailMessage._Description));
            }
            if (!HELPER_ReceivedDate.compare(this._ReceivedDate, otherEmailMessage._ReceivedDate))
            {
                listener.notifyFieldChange(this, other, FIELD_ReceivedDate, HELPER_ReceivedDate.toObject(this._ReceivedDate), HELPER_ReceivedDate.toObject(otherEmailMessage._ReceivedDate));
            }

            // Compare single assocs
            _Job.compare (otherEmailMessage._Job, listener);
            

            // Compare multiple assocs
            _Attachments.compare (otherEmailMessage._Attachments, 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_MessageId, HELPER_MessageId.toObject(getMessageId()));
        visitor.visitField(this, FIELD_EmailFrom, HELPER_EmailFrom.toObject(getEmailFrom()));
        visitor.visitField(this, FIELD_EmailTo, HELPER_EmailTo.toObject(getEmailTo()));
        visitor.visitField(this, FIELD_EmailCC, HELPER_EmailCC.toObject(getEmailCC()));
        visitor.visitField(this, FIELD_Subject, HELPER_Subject.toObject(getSubject()));
        visitor.visitField(this, FIELD_Description, HELPER_Description.toObject(getDescription()));
        visitor.visitField(this, FIELD_ReceivedDate, HELPER_ReceivedDate.toObject(getReceivedDate()));
        visitor.visitAssociation (_Job);
        visitor.visitAssociation (_Attachments);
        
    }


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

        if (scope.includes (_Job))
        {
            visitor.visit (_Job);
        }
        if (scope.includes (_Attachments))
        {
            visitor.visit (_Attachments);
        }
        
    }


    public static EmailMessage createEmailMessage (ObjectTransaction transaction) throws StorageException
    {
        EmailMessage     result = new EmailMessage ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static EmailMessage getEmailMessageByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (EmailMessage)(transaction.getObjectByID (REFERENCE_EmailMessage, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_MessageId))
        {
            return filter.matches (getMessageId ());
        }
        else if (attribName.equals (FIELD_EmailFrom))
        {
            return filter.matches (getEmailFrom ());
        }
        else if (attribName.equals (FIELD_EmailTo))
        {
            return filter.matches (getEmailTo ());
        }
        else if (attribName.equals (FIELD_EmailCC))
        {
            return filter.matches (getEmailCC ());
        }
        else if (attribName.equals (FIELD_Subject))
        {
            return filter.matches (getSubject ());
        }
        else if (attribName.equals (FIELD_Description))
        {
            return filter.matches (getDescription ());
        }
        else if (attribName.equals (FIELD_ReceivedDate))
        {
            return filter.matches (getReceivedDate ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Job))
        {
            return filter.matches (getJob ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
        public SearchAll andMessageId (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.message_id", "MessageId");
            return this;
        }
            
        public SearchAll andEmailFrom (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.email_from", "EmailFrom");
            return this;
        }
            
        public SearchAll andEmailTo (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.email_to", "EmailTo");
            return this;
        }
            
        public SearchAll andEmailCC (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.email_cc", "EmailCC");
            return this;
        }
            
        public SearchAll andSubject (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.subject", "Subject");
            return this;
        }
            
        public SearchAll andDescription (QueryFilter<String> filter)
        {
            filter.addFilter (context, "tl_email_message.description", "Description");
            return this;
        }
            
        public SearchAll andReceivedDate (QueryFilter<Date> filter)
        {
            filter.addFilter (context, "tl_email_message.received_date", "ReceivedDate");
            return this;
        }
            
        public SearchAll andJob (QueryFilter<Job> filter)
        {
            filter.addFilter (context, "tl_email_message.job_id", "Job");
            return this;
        }
            
        public EmailMessage[] search (ObjectTransaction transaction) throws StorageException
        {
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_EmailMessage, SEARCH_All, criteria);
            Set<EmailMessage>   typedResults = new LinkedHashSet <EmailMessage> ();
            for (BaseBusinessClass bbcResult : results)
            {
                EmailMessage aResult = (EmailMessage)bbcResult;
                
                typedResults.add (aResult);
            }
            
            return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new EmailMessage[0]);
                            
        }
    }

        
    public static EmailMessage[] 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_MessageId))
        {
            return HELPER_MessageId.toObject (getMessageId ());
        }
        else if (attribName.equals (FIELD_EmailFrom))
        {
            return HELPER_EmailFrom.toObject (getEmailFrom ());
        }
        else if (attribName.equals (FIELD_EmailTo))
        {
            return HELPER_EmailTo.toObject (getEmailTo ());
        }
        else if (attribName.equals (FIELD_EmailCC))
        {
            return HELPER_EmailCC.toObject (getEmailCC ());
        }
        else if (attribName.equals (FIELD_Subject))
        {
            return HELPER_Subject.toObject (getSubject ());
        }
        else if (attribName.equals (FIELD_Description))
        {
            return HELPER_Description.toObject (getDescription ());
        }
        else if (attribName.equals (FIELD_ReceivedDate))
        {
            return HELPER_ReceivedDate.toObject (getReceivedDate ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_MessageId))
        {
            return HELPER_MessageId;
        }
        else if (attribName.equals (FIELD_EmailFrom))
        {
            return HELPER_EmailFrom;
        }
        else if (attribName.equals (FIELD_EmailTo))
        {
            return HELPER_EmailTo;
        }
        else if (attribName.equals (FIELD_EmailCC))
        {
            return HELPER_EmailCC;
        }
        else if (attribName.equals (FIELD_Subject))
        {
            return HELPER_Subject;
        }
        else if (attribName.equals (FIELD_Description))
        {
            return HELPER_Description;
        }
        else if (attribName.equals (FIELD_ReceivedDate))
        {
            return HELPER_ReceivedDate;
        }
        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_MessageId))
        {
            setMessageId ((String)(HELPER_MessageId.fromObject (_MessageId, attribValue)));            
        }
        else if (attribName.equals (FIELD_EmailFrom))
        {
            setEmailFrom ((String)(HELPER_EmailFrom.fromObject (_EmailFrom, attribValue)));            
        }
        else if (attribName.equals (FIELD_EmailTo))
        {
            setEmailTo ((String)(HELPER_EmailTo.fromObject (_EmailTo, attribValue)));            
        }
        else if (attribName.equals (FIELD_EmailCC))
        {
            setEmailCC ((String)(HELPER_EmailCC.fromObject (_EmailCC, attribValue)));            
        }
        else if (attribName.equals (FIELD_Subject))
        {
            setSubject ((String)(HELPER_Subject.fromObject (_Subject, attribValue)));            
        }
        else if (attribName.equals (FIELD_Description))
        {
            setDescription ((String)(HELPER_Description.fromObject (_Description, attribValue)));            
        }
        else if (attribName.equals (FIELD_ReceivedDate))
        {
            setReceivedDate ((Date)(HELPER_ReceivedDate.fromObject (_ReceivedDate, 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_MessageId))
        {
            return getWriteability_MessageId ();
        }
        else if (fieldName.equals (FIELD_EmailFrom))
        {
            return getWriteability_EmailFrom ();
        }
        else if (fieldName.equals (FIELD_EmailTo))
        {
            return getWriteability_EmailTo ();
        }
        else if (fieldName.equals (FIELD_EmailCC))
        {
            return getWriteability_EmailCC ();
        }
        else if (fieldName.equals (FIELD_Subject))
        {
            return getWriteability_Subject ();
        }
        else if (fieldName.equals (FIELD_Description))
        {
            return getWriteability_Description ();
        }
        else if (fieldName.equals (FIELD_ReceivedDate))
        {
            return getWriteability_ReceivedDate ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_Attachments))
        {
            return getWriteability_Attachments ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Job))
        {
            return getWriteability_Job ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


    public void putUnwriteable (Set<String> fields)
    {
        if (getWriteability_MessageId () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_MessageId);
        }
        
        if (getWriteability_EmailFrom () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_EmailFrom);
        }
        
        if (getWriteability_EmailTo () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_EmailTo);
        }
        
        if (getWriteability_EmailCC () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_EmailCC);
        }
        
        if (getWriteability_Subject () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_Subject);
        }
        
        if (getWriteability_Description () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_Description);
        }
        
        if (getWriteability_ReceivedDate () != FieldWriteability.TRUE)
        {
            fields.add (FIELD_ReceivedDate);
        }
        

        super.putUnwriteable (fields);
    }

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


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

        
        result.add(HELPER_MessageId.getAttribObject (getClass (), _MessageId, false, FIELD_MessageId));
        result.add(HELPER_EmailFrom.getAttribObject (getClass (), _EmailFrom, true, FIELD_EmailFrom));
        result.add(HELPER_EmailTo.getAttribObject (getClass (), _EmailTo, true, FIELD_EmailTo));
        result.add(HELPER_EmailCC.getAttribObject (getClass (), _EmailCC, false, FIELD_EmailCC));
        result.add(HELPER_Subject.getAttribObject (getClass (), _Subject, false, FIELD_Subject));
        result.add(HELPER_Description.getAttribObject (getClass (), _Description, false, FIELD_Description));
        result.add(HELPER_ReceivedDate.getAttribObject (getClass (), _ReceivedDate, true, FIELD_ReceivedDate));

        return result;
    }


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


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

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

        copyInto.putAll(ATTRIBUTES_METADATA_EmailMessage);
    }
            

    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 EmailMessageBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<EmailMessage>
    {

    /**
     * Get the attribute MessageId
     */
    public String getMessageId (EmailMessage obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute MessageId.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setMessageId (EmailMessage obj, String newMessageId) throws FieldException
    {
        return newMessageId;
    }

    /**
     * Get the attribute EmailFrom
     */
    public String getEmailFrom (EmailMessage obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute EmailFrom.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setEmailFrom (EmailMessage obj, String newEmailFrom) throws FieldException
    {
        return newEmailFrom;
    }

    /**
     * Get the attribute EmailTo
     */
    public String getEmailTo (EmailMessage obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute EmailTo.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setEmailTo (EmailMessage obj, String newEmailTo) throws FieldException
    {
        return newEmailTo;
    }

    /**
     * Get the attribute EmailCC
     */
    public String getEmailCC (EmailMessage obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute EmailCC.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setEmailCC (EmailMessage obj, String newEmailCC) throws FieldException
    {
        return newEmailCC;
    }

    /**
     * Get the attribute Subject
     */
    public String getSubject (EmailMessage obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute Subject.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setSubject (EmailMessage obj, String newSubject) throws FieldException
    {
        return newSubject;
    }

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

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

    /**
     * Get the attribute ReceivedDate
     */
    public Date getReceivedDate (EmailMessage obj, Date original)
    {
        return original;
    }

    /**
     * Change the value set for attribute ReceivedDate.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public Date setReceivedDate (EmailMessage obj, Date newReceivedDate) throws FieldException
    {
        return newReceivedDate;
    }

    }

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

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

        public EmailMessagePipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("Attachments"))
            {
                return toAttachments ();
            }                
            if (name.equals ("MessageId"))
            {
                return toMessageId ();
            }                
            if (name.equals ("EmailFrom"))
            {
                return toEmailFrom ();
            }                
            if (name.equals ("EmailTo"))
            {
                return toEmailTo ();
            }                
            if (name.equals ("EmailCC"))
            {
                return toEmailCC ();
            }                
            if (name.equals ("Subject"))
            {
                return toSubject ();
            }                
            if (name.equals ("Description"))
            {
                return toDescription ();
            }                
            if (name.equals ("ReceivedDate"))
            {
                return toReceivedDate ();
            }                
            if (name.equals ("Job"))
            {
                return toJob ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toMessageId () { return pipe(new ORMAttributePipe<Me, String>(FIELD_MessageId));  }
        
        public PipeLine<From, String> toEmailFrom () { return pipe(new ORMAttributePipe<Me, String>(FIELD_EmailFrom));  }
        
        public PipeLine<From, String> toEmailTo () { return pipe(new ORMAttributePipe<Me, String>(FIELD_EmailTo));  }
        
        public PipeLine<From, String> toEmailCC () { return pipe(new ORMAttributePipe<Me, String>(FIELD_EmailCC));  }
        
        public PipeLine<From, String> toSubject () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Subject));  }
        
        public PipeLine<From, String> toDescription () { return pipe(new ORMAttributePipe<Me, String>(FIELD_Description));  }
        
        public PipeLine<From, Date> toReceivedDate () { return pipe(new ORMAttributePipe<Me, Date>(FIELD_ReceivedDate));  }

        public Job.JobPipeLineFactory<From, Job> toJob () { return toJob (Filter.ALL); }

        public Job.JobPipeLineFactory<From, Job> toJob (Filter<Job> filter) 
        { 
            return Job.REFERENCE_Job.new JobPipeLineFactory<From, Job> (this, new ORMSingleAssocPipe<Me, Job>(SINGLEREFERENCE_Job, filter)); 
        }

        public Attachment.AttachmentPipeLineFactory<From, Attachment> toAttachments () { return toAttachments(Filter.ALL); }        

        public Attachment.AttachmentPipeLineFactory<From, Attachment> toAttachments (Filter<Attachment> filter)
        {
            return Attachment.REFERENCE_Attachment.new AttachmentPipeLineFactory<From, Attachment> (this, new ORMMultiAssocPipe<Me, Attachment>(MULTIPLEREFERENCE_Attachments, filter)); 
        }
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public Job getJob () throws StorageException
    {
        return (Job)(Job.DUMMY_Job);
    }


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

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


    public Attachment getAttachmentsAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association Attachments");
    }


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

}


