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

    // Reference instance for the object
    public static final ChatAttachment DUMMY_ChatAttachment = new DummyChatAttachment ();

    // Static constants corresponding to field names
    
    public static final String FIELD_File = "File";
    public static final String SINGLEREFERENCE_Message = "Message";
    public static final String BACKREF_Message = "";

    // Static constants corresponding to searches
    

    // Static constants corresponding to attribute helpers
    private static final BLOBAttributeHelper HELPER_File = BLOBAttributeHelper.INSTANCE;
    

    // Private attributes corresponding to business object data
    private BinaryContent _File;
    

    // Private attributes corresponding to single references
    private SingleAssociation<ChatAttachment, Message> _Message;
    

    // Private attributes corresponding to multiple references
    

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

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

    // Arrays of AttributeDecorators for each attribute
    private static final AttributeDecorator<ChatAttachment, BinaryContent>[] FIELD_File_Decorators;
    
    
    // Arrays of SingleAssocDecorators for each attribute
    private static final SingleAssocDecorator<ChatAttachment, Message>[] SINGLEREFERENCE_Message_Decorators;
    
    
    // Arrays of MultiAssocDecorators for each attribute
    

    // Arrays of behaviour decorators
    private static final ChatAttachmentBehaviourDecorator[] ChatAttachment_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_Message = Message.BACKREF_Attachments;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_Message();
            FIELD_File_Validators = (AttributeValidator[])setupAttribMetaData_File(validatorMapping).toArray (new AttributeValidator[0]);
            FIELD_File_Decorators = (AttributeDecorator[])AttributeDecoratorUtils.getAttributeDecorators(REFERENCE_ChatAttachment, FIELD_File).toArray (new AttributeDecorator[0]);
            SINGLEREFERENCE_Message_Decorators = (SingleAssocDecorator[])AttributeDecoratorUtils.getSingleAssocDecorators(REFERENCE_ChatAttachment, SINGLEREFERENCE_Message).toArray (new SingleAssocDecorator[0]);
            

            REFERENCE_ChatAttachment.initialiseReference ();
            DUMMY_ChatAttachment.initialiseReference ();
            ChatAttachment_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(ChatAttachment.class).toArray(new ChatAttachmentBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "Attachments");
        metaInfo.put ("dbcol", "message_id");
        metaInfo.put ("name", "Message");
        metaInfo.put ("type", "Message");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_chat_attachment");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for ChatAttachment.Message:", metaInfo);
        ATTRIBUTES_METADATA_ChatAttachment.put (SINGLEREFERENCE_Message, Collections.unmodifiableMap (metaInfo));
    }
    
    
    private static List setupAttribMetaData_File(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("attribHelper", "BLOBAttributeHelper");
        metaInfo.put ("attribHelperInstance", "BLOBAttributeHelper.INSTANCE");
        metaInfo.put ("binaryHandler", "loggedin");
        metaInfo.put ("dbcol", "file");
        metaInfo.put ("name", "File");
        metaInfo.put ("type", "BinaryContent");
        metaInfo.put (METADATA_DB_TABLENAME, "tl_chat_attachment");
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for ChatAttachment.File:", metaInfo);
        ATTRIBUTES_METADATA_ChatAttachment.put (FIELD_File, Collections.unmodifiableMap (metaInfo));

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

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


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

    


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

        
        _File = (BinaryContent)(HELPER_File.initialise (_File));
    }


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

        _Message = new SingleAssociation<ChatAttachment, Message> (this, SINGLEREFERENCE_Message, Message.MULTIPLEREFERENCE_Attachments, Message.REFERENCE_Message, "tl_chat_attachment");
        
    }


    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _Message = new SingleAssociation<ChatAttachment, Message> (this, SINGLEREFERENCE_Message, Message.MULTIPLEREFERENCE_Attachments, Message.REFERENCE_Message, "tl_chat_attachment");
        
        
        return this;
    }


    /**
     * Attribute File
     */
    public BinaryContent getFile ()
    {
        assertValid();
        BinaryContent valToReturn = AttributeDecoratorUtils.getValue (FIELD_File_Decorators, (ChatAttachment)this, FIELD_File, _File);

        
        for (ChatAttachmentBehaviourDecorator bhd : ChatAttachment_BehaviourDecorators)
        {
            valToReturn = bhd.getFile ((ChatAttachment)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 preFileChange (BinaryContent newFile) 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 postFileChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute File.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setFile (BinaryContent newFile) throws FieldException
    {
        newFile = AttributeDecoratorUtils.setValue (FIELD_File_Decorators, (ChatAttachment)this, FIELD_File, newFile);

        boolean oldAndNewIdentical = HELPER_File.compare (_File, newFile);
        
        try
        {
            for (ChatAttachmentBehaviourDecorator bhd : ChatAttachment_BehaviourDecorators)
            {
                newFile = bhd.setFile ((ChatAttachment)this, newFile);
                oldAndNewIdentical = HELPER_File.compare (_File, newFile);
            }

            

            if (FIELD_File_Validators.length > 0)
            {
                Object newFileObj = HELPER_File.toObject (newFile);

                if (newFileObj != null)
                {
                    int     loopMax = FIELD_File_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_ChatAttachment.get (FIELD_File);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (FIELD_File_Decorators, (ChatAttachment)this, FIELD_File ,getWriteability_File ()) != FieldWriteability.FALSE, "Field  File is not writeable");
            preFileChange (newFile);
            markFieldChange (FIELD_File);
            _File = newFile;            
            postFieldChange (FIELD_File);
            postFileChange ();
        }
    }



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

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_Message))
        {
            return _Message.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_Message))
        {
            return Message.MULTIPLEREFERENCE_Attachments ;
        }
        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_Message))
        {
            return getMessage ();
        }
        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_Message))
        {
            return getMessage (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_Message))
        {
            return getMessageID ();
        }
        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_Message))
        {
            setMessage ((Message)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



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


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getMessageID ()
    {
        assertValid();
        
        if (_Message == null)
        {
            return null;
        }
        else
        {
            return _Message.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 preMessageChange (Message newMessage) 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 postMessageChange () throws FieldException
    {
    }


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

        
    /**
     * Set the reference Message.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setMessage (Message newMessage) throws StorageException, FieldException
    {
        if (_Message.wouldReferencedChange (newMessage))
        {
            assertValid();
            Debug.assertion (AttributeDecoratorUtils.getWriteability (SINGLEREFERENCE_Message_Decorators, (ChatAttachment)this, SINGLEREFERENCE_Message ,getWriteability_Message ()) != FieldWriteability.FALSE, "Assoc Message is not writeable");
            preMessageChange (newMessage);
            Message oldMessage = getMessage ();

            if (oldMessage != null)
            {
                // This is to stop validation from triggering when we are removed
                _Message.set (null);
                oldMessage.removeFromAttachments ((ChatAttachment)(this));
            }

            _Message.set (newMessage);

            if (newMessage != null)
            {
                newMessage.addToAttachments ((ChatAttachment)(this));
            }
                
            postMessageChange ();
        }
    }

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

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {

        return super.getMultiAssocBackReference(attribName);
    }


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

        return super.getMultiAssocCount(attribName);
    }


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

        return super.getMultiAssocAt(attribName, index);
    }


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

        super.addToMultiAssoc(attribName, newElement);
    }

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

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {

        return super.__isMultiAssocLoaded(attribName);
    }




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

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

        super.onDelete ();
    }


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


    public ChatAttachment referenceInstance ()
    {
        return REFERENCE_ChatAttachment;
    }


    public ChatAttachment getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getChatAttachmentByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_ChatAttachment;
    }


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



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

        tl_chat_attachmentPSet.setAttrib (FIELD_ObjectID, myID);
        tl_chat_attachmentPSet.setAttrib (FIELD_File, HELPER_File.toObject (_File)); //
        _Message.getPersistentSets (allSets);
        
    }


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

        _File = (BinaryContent)(HELPER_File.fromObject (_File, tl_chat_attachmentPSet.getAttrib (FIELD_File))); //
        _Message.setFromPersistentSets (objectID, allSets);
        
    }


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

        if (other instanceof ChatAttachment)
        {
            ChatAttachment otherChatAttachment = (ChatAttachment)other;
            

            try
            {
                setFile (otherChatAttachment.getFile ());
            }
            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 BaseChatAttachment)
        {
            BaseChatAttachment sourceChatAttachment = (BaseChatAttachment)(source);

            _File = sourceChatAttachment._File;
            
        }
    }


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

            _Message.copyFrom (sourceChatAttachment._Message, 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 BaseChatAttachment)
        {
            BaseChatAttachment sourceChatAttachment = (BaseChatAttachment)(source);

            
        }
    }


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

        
    }


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

        _File = (BinaryContent)(HELPER_File.readExternal (_File, vals.get(FIELD_File))); //
        _Message.readExternalData(vals.get(SINGLEREFERENCE_Message));
        
    }


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

        vals.put (FIELD_File, HELPER_File.writeExternal (_File));
        vals.put (SINGLEREFERENCE_Message, _Message.writeExternalData());
        
    }


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

        if (other instanceof BaseChatAttachment)
        {
            BaseChatAttachment otherChatAttachment = (BaseChatAttachment)(other);

            
            if (!HELPER_File.compare(this._File, otherChatAttachment._File))
            {
                listener.notifyFieldChange(this, other, FIELD_File, HELPER_File.toObject(this._File), HELPER_File.toObject(otherChatAttachment._File));
            }

            // Compare single assocs
            _Message.compare (otherChatAttachment._Message, listener);
            

            // Compare multiple assocs
            
        }
    }


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

        
    }


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

        visitor.visitField(this, FIELD_File, HELPER_File.toObject(getFile()));
        visitor.visitAssociation (_Message);
        
    }


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

        if (scope.includes (_Message))
        {
            visitor.visit (_Message);
        }
        
    }


    public static ChatAttachment createChatAttachment (ObjectTransaction transaction) throws StorageException
    {
        ChatAttachment     result = new ChatAttachment ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static ChatAttachment getChatAttachmentByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (ChatAttachment)(transaction.getObjectByID (REFERENCE_ChatAttachment, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_File))
        {
            return filter.matches (getFile ());
        }
        else if (attribName.equals (SINGLEREFERENCE_Message))
        {
            return filter.matches (getMessage ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }




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


    public AttributeHelper getAttributeHelper (String attribName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_File))
        {
            return HELPER_File;
        }
        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_File))
        {
            setFile ((BinaryContent)(HELPER_File.fromObject (_File, 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_File))
        {
            return getWriteability_File ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_Message))
        {
            return getWriteability_Message ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

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


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

        
        result.add(HELPER_File.getAttribObject (getClass (), _File, false, FIELD_File));

        return result;
    }


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


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

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

        copyInto.putAll(ATTRIBUTES_METADATA_ChatAttachment);
    }
            

    public void preCommit (boolean willBeStored) throws Exception
    {
        super.preCommit(willBeStored);
        
        if(willBeStored)
        {
        
          {  
            oneit.servlets.objstore.binary.BinaryContentHandler bchandler = oneit.servlets.objstore.binary.BinaryContentServlet.getHandler("loggedin");
              
            if(bchandler != null)
            {
                bchandler.preCommit(willBeStored, this, "File");
            }
            else
            {
                LogMgr.log(BUSINESS_OBJECTS, LogLevel.SYSTEMWARNING, "Unknown BinaryContentHandler loggedin on attribute File");
            }
          }
          
        }
    }

    public oneit.servlets.objstore.binary.BinaryContentHandler getBinaryContentHandler(String attribName)
    {
        if(CollectionUtils.equals(attribName, "File"))      
        {  
            return oneit.servlets.objstore.binary.BinaryContentServlet.getHandler("loggedin");
        }     
          
        
        return super.getBinaryContentHandler(attribName);
    }

    public static class ChatAttachmentBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<ChatAttachment>
    {

    /**
     * Get the attribute File
     */
    public BinaryContent getFile (ChatAttachment obj, BinaryContent original)
    {
        return original;
    }

    /**
     * Change the value set for attribute File.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public BinaryContent setFile (ChatAttachment obj, BinaryContent newFile) throws FieldException
    {
        return newFile;
    }

    }

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

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

        public ChatAttachmentPipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("File"))
            {
                return toFile ();
            }                
            if (name.equals ("Message"))
            {
                return toMessage ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, BinaryContent> toFile () { return pipe(new ORMAttributePipe<Me, BinaryContent>(FIELD_File));  }

        public Message.MessagePipeLineFactory<From, Message> toMessage () { return toMessage (Filter.ALL); }

        public Message.MessagePipeLineFactory<From, Message> toMessage (Filter<Message> filter) 
        { 
            return Message.REFERENCE_Message.new MessagePipeLineFactory<From, Message> (this, new ORMSingleAssocPipe<Me, Message>(SINGLEREFERENCE_Message, filter)); 
        }
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public Message getMessage () throws StorageException
    {
        return (Message)(Message.DUMMY_Message);
    }


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

}


