/*
 * IMPORTANT!!!! XSL Autogenerated class, DO NOT EDIT!!!!!
 * Template: Infrastructure8.2 rev3 [oneit.objstore.BusinessObjectTemplate.xsl]
 *
 *   Version: 1.0
 *   Vendor: Apache Software Foundation (Xalan XSLTC)
 *   Vendor URL: http://xml.apache.org/xalan-j
 */


    package performa.orm;

import java.io.*;
import java.util.*;
import oneit.appservices.config.*;
import oneit.logging.*;
import oneit.objstore.*;
import oneit.objstore.assocs.*;
import oneit.objstore.attributes.*;
import oneit.objstore.rdbms.filters.*;
import oneit.objstore.parser.*;
import oneit.objstore.validator.*;
import oneit.objstore.utils.*;
import oneit.utils.*;
import oneit.utils.filter.Filter;
import oneit.utils.transform.*;
import oneit.utils.parsers.FieldException;

import oneit.security.*;



public abstract class BaseAssessmentCriteriaTemplate extends BaseBusinessClass
{
    // Reference instance for the object
    public static final AssessmentCriteriaTemplate REFERENCE_AssessmentCriteriaTemplate = new AssessmentCriteriaTemplate ();

    // Reference instance for the object
    public static final AssessmentCriteriaTemplate DUMMY_AssessmentCriteriaTemplate = new DummyAssessmentCriteriaTemplate ();

    // Static constants corresponding to field names
    
    public static final String FIELD_TemplateName = "TemplateName";
    public static final String SINGLEREFERENCE_User = "User";
    public static final String MULTIPLEREFERENCE_AssessmentCriterias = "AssessmentCriterias";
    public static final String BACKREF_AssessmentCriterias = "";

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

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

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

    // Private attributes corresponding to single references
    private SingleAssociation<AssessmentCriteriaTemplate, SecUser> _User;
    

    // Private attributes corresponding to multiple references
    private MultipleAssociation<AssessmentCriteriaTemplate, AssessmentCriteria> _AssessmentCriterias;
    

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

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

    // Arrays of behaviour decorators
    private static final AssessmentCriteriaTemplateBehaviourDecorator[] AssessmentCriteriaTemplate_BehaviourDecorators;
    
    static
    {
        try
        {
            
            String tmp_AssessmentCriterias = AssessmentCriteria.BACKREF_Template;
            
            Map  validatorMapping = ((Map)ConfigMgr.getConfigObject ("CONFIG.ORMVALIDATOR", "ValidatorMapping"));

            setupAssocMetaData_AssessmentCriterias();
            setupAssocMetaData_User();
            FIELD_TemplateName_Validators = (AttributeValidator[])setupAttribMetaData_TemplateName(validatorMapping).toArray (new AttributeValidator[0]);
            

            REFERENCE_AssessmentCriteriaTemplate.initialiseReference ();
            DUMMY_AssessmentCriteriaTemplate.initialiseReference ();
            AssessmentCriteriaTemplate_BehaviourDecorators = BaseBusinessClass.getBBCBehaviours(AssessmentCriteriaTemplate.class).toArray(new AssessmentCriteriaTemplateBehaviourDecorator[0]);
        }
        catch (RuntimeException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR1, e, "Error initialising");
            throw e;
        }
    }

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

        metaInfo.put ("backreferenceName", "Template");
        metaInfo.put ("name", "AssessmentCriterias");
        metaInfo.put ("type", "AssessmentCriteria");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AssessmentCriteriaTemplate.AssessmentCriterias:", metaInfo);
        ATTRIBUTES_METADATA_AssessmentCriteriaTemplate.put (MULTIPLEREFERENCE_AssessmentCriterias, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static void setupAssocMetaData_User()
    {
        Map metaInfo = new HashMap ();

        metaInfo.put ("dbcol", "user_id");
        metaInfo.put ("name", "User");
        metaInfo.put ("type", "SecUser");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AssessmentCriteriaTemplate.User:", metaInfo);
        ATTRIBUTES_METADATA_AssessmentCriteriaTemplate.put (SINGLEREFERENCE_User, Collections.unmodifiableMap (metaInfo));
    }
    
    
    // Meta Info setup
    private static List setupAttribMetaData_TemplateName(Map validatorMapping)
    {
        Map  metaInfo   = new HashMap ();

        metaInfo.put ("dbcol", "template_name");
        metaInfo.put ("length", "100");
        metaInfo.put ("mandatory", "true");
        metaInfo.put ("name", "TemplateName");
        metaInfo.put ("type", "String");
        
        LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG2, "Metadata for AssessmentCriteriaTemplate.TemplateName:", metaInfo);
        ATTRIBUTES_METADATA_AssessmentCriteriaTemplate.put (FIELD_TemplateName, Collections.unmodifiableMap (metaInfo));

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

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


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


    


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

        
        _TemplateName = (String)(HELPER_TemplateName.initialise (_TemplateName));
    }


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

        _User = new SingleAssociation<AssessmentCriteriaTemplate, SecUser> (this, SINGLEREFERENCE_User, null, SecUser.REFERENCE_SecUser, "tl_assessment_template");
        _AssessmentCriterias = new MultipleAssociation<AssessmentCriteriaTemplate, AssessmentCriteria> (this, MULTIPLEREFERENCE_AssessmentCriterias, AssessmentCriteria.SINGLEREFERENCE_Template, AssessmentCriteria.REFERENCE_AssessmentCriteria);
        
    }


    // Initialise the associations
    protected BaseBusinessClass initialiseReference ()
    {
        super.initialiseReference ();
        
        _User = new SingleAssociation<AssessmentCriteriaTemplate, SecUser> (this, SINGLEREFERENCE_User, null, SecUser.REFERENCE_SecUser, "tl_assessment_template");
        _AssessmentCriterias = new MultipleAssociation<AssessmentCriteriaTemplate, AssessmentCriteria> (this, MULTIPLEREFERENCE_AssessmentCriterias, AssessmentCriteria.SINGLEREFERENCE_Template, AssessmentCriteria.REFERENCE_AssessmentCriteria);
        
        
        return this;
    }

    

    /**
     * Get the attribute TemplateName
     */
    public String getTemplateName ()
    {
        assertValid();
        String valToReturn = _TemplateName;

            for (AssessmentCriteriaTemplateBehaviourDecorator bhd : AssessmentCriteriaTemplate_BehaviourDecorators)
            {
                valToReturn = bhd.getTemplateName ((AssessmentCriteriaTemplate)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 preTemplateNameChange (String newTemplateName) 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 postTemplateNameChange () throws FieldException
    {
    }

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

    /**
     * Set the attribute TemplateName.  Checks to ensure a new value
     * has been supplied.  If so, marks the field as altered and sets the attribute.
     */
    public void setTemplateName (String newTemplateName) throws FieldException
    {
        boolean oldAndNewIdentical = HELPER_TemplateName.compare (_TemplateName, newTemplateName);
        
        try
        {
            for (AssessmentCriteriaTemplateBehaviourDecorator bhd : AssessmentCriteriaTemplate_BehaviourDecorators)
            {
                newTemplateName = bhd.setTemplateName ((AssessmentCriteriaTemplate)this, newTemplateName);
                oldAndNewIdentical = HELPER_TemplateName.compare (_TemplateName, newTemplateName);
            }

            BusinessObjectParser.assertFieldCondition (newTemplateName != null, this, FIELD_TemplateName, "mandatory");
            

            if (FIELD_TemplateName_Validators.length > 0)
            {
                Object newTemplateNameObj = HELPER_TemplateName.toObject (newTemplateName);

                if (newTemplateNameObj != null)
                {
                    int     loopMax = FIELD_TemplateName_Validators.length;
                    Map     metadata = (Map)ATTRIBUTES_METADATA_AssessmentCriteriaTemplate.get (FIELD_TemplateName);

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

        if (!oldAndNewIdentical)
        {
            assertValid();
            Debug.assertion (getWriteability_TemplateName () != FieldWriteability.FALSE, "Field  TemplateName is not writeable");
            preTemplateNameChange (newTemplateName);
            markFieldChange (FIELD_TemplateName);
            _TemplateName = newTemplateName;            
            postFieldChange (FIELD_TemplateName);
            postTemplateNameChange ();
        }
    }



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

        
        result.add("User");
        

        return result;
    }
    
    
    public BaseBusinessClass getSingleAssocReferenceInstance (String assocName)
    {
        if (assocName == null)
        {
            throw new RuntimeException ("Game over == null!");
        }
        else if (assocName.equals (SINGLEREFERENCE_User))
        {
            return _User.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_User))
        {
            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_User))
        {
            return getUser ();
        }
        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_User))
        {
            return getUser (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_User))
        {
            return getUserID ();
        }
        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_User))
        {
            setUser ((SecUser)(newValue));
        }
        else
        {
            super.setSingleAssoc (assocName, newValue);
        }
    }



    /**
     * Get the reference User
     */
    public SecUser getUser () throws StorageException
    {
        assertValid();
        
        try
        {
            return (SecUser)(_User.get ());
        }
        catch (ClassCastException e)
        {
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Cache collision in AssessmentCriteriaTemplate:", this.getObjectID (), ", was trying to get SecUser:",  getUserID ());
            LogMgr.log (BUSINESS_OBJECTS, LogLevel.SYSTEMERROR2, "Instead I got:", _User.get ().getClass ());
            throw e;
        }
    }


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


    /**
     * Get the object id for the referenced object.  Does not force a DB access.
     */
    public Long getUserID ()
    {
        assertValid();
        
        if (_User == null)
        {
            return null;
        }
        else
        {
            return _User.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 preUserChange (SecUser newUser) 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 postUserChange () throws FieldException
    {
    }



    
    public FieldWriteability getWriteability_User ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    /**
     * Set the reference User.  Checks to ensure a new value
     * has been supplied.  If so, marks the reference as altered and sets it.
     */
    public void setUser (SecUser newUser) throws StorageException, FieldException
    {
        

        if (_User.wouldReferencedChange (newUser))
        {
            assertValid();
            Debug.assertion (getWriteability_User () != FieldWriteability.FALSE, "Assoc User is not writeable");
            preUserChange (newUser);
            
                    _User.set (newUser);
                
            postUserChange ();
        }
    }

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

        
        result.add("AssessmentCriterias");
        

        return result;
    }


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

        return super.getMultiAssocReferenceInstance(attribName);
    }


    public String getMultiAssocBackReference(String attribName)
    {
        
        if (MULTIPLEREFERENCE_AssessmentCriterias.equals(attribName))
        {
            return AssessmentCriteria.SINGLEREFERENCE_Template ;
        }
        

        return super.getMultiAssocBackReference(attribName);
    }


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

        return super.getMultiAssocCount(attribName);
    }


    /**
     * Get the assoc at a particular index
     */
    public BaseBusinessClass getMultiAssocAt(String attribName, int index) throws StorageException
    {
        
        if (MULTIPLEREFERENCE_AssessmentCriterias.equals(attribName))
        {
            return this.getAssessmentCriteriasAt(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_AssessmentCriterias.equals(attribName))
        {
            addToAssessmentCriterias((AssessmentCriteria)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_AssessmentCriterias.equals(attribName))
        {
            removeFromAssessmentCriterias((AssessmentCriteria)oldElement);

            return;
        }
        

        super.removeFromMultiAssoc(attribName, oldElement);
    }


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

        super.__loadMultiAssoc(attribName, elements);
    }


    protected boolean __isMultiAssocLoaded (String attribName)
    {
        
        if (MULTIPLEREFERENCE_AssessmentCriterias.equals(attribName))
        {
            return _AssessmentCriterias.isLoaded ();
        }
        

        return super.__isMultiAssocLoaded(attribName);
    }

    

    public FieldWriteability getWriteability_AssessmentCriterias ()
    {
        return getFieldWritabilityUtil (FieldWriteability.TRUE);
    }
    
    
    public int getAssessmentCriteriasCount () throws StorageException
    {
        assertValid();
        return _AssessmentCriterias.getReferencedObjectsCount ();
    }


    public void addToAssessmentCriterias (AssessmentCriteria newElement) throws StorageException
    {
        if (_AssessmentCriterias.wouldAddChange (newElement))
        {
            assertValid();
            Debug.assertion (getWriteability_AssessmentCriterias () != FieldWriteability.FALSE, "MultiAssoc AssessmentCriterias is not writeable (add)");
            _AssessmentCriterias.appendElement (newElement);

            try
            {
                if (newElement.getTemplate () != this)
                {
                    newElement.setTemplate ((AssessmentCriteriaTemplate)(this));
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public void removeFromAssessmentCriterias (AssessmentCriteria elementToRemove) throws StorageException
    {
        if (_AssessmentCriterias.wouldRemoveChange (elementToRemove))
        {
            assertValid();
            Debug.assertion (getWriteability_AssessmentCriterias () != FieldWriteability.FALSE, "MultiAssoc AssessmentCriterias is not writeable (remove)");
            _AssessmentCriterias.removeElement (elementToRemove);
            try
            {
                if (elementToRemove.getTemplate () != null)
                {
                    elementToRemove.setTemplate (null);
                }
            }
            catch (Exception e)
            {
                throw NestedException.wrap(e);
            }
        }
    }


    public AssessmentCriteria getAssessmentCriteriasAt (int index) throws StorageException
    {
        return (AssessmentCriteria)(_AssessmentCriterias.getElementAt (index));
    }


    public SortedSet<AssessmentCriteria> getAssessmentCriteriasSet () throws StorageException
    {
        return _AssessmentCriterias.getSet ();
    }



    public void onDelete ()
    {
        try 
        {
            for(AssessmentCriteria referenced : CollectionUtils.reverse(getAssessmentCriteriasSet()))
            { 
                LogMgr.log (BUSINESS_OBJECTS, LogLevel.DEBUG1, "Setting backreference to null Template from ", getObjectID (), " to ", referenced.getObjectID ());
                referenced.setTemplate(null);
            }

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

        super.onDelete ();
    }


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


    public AssessmentCriteriaTemplate referenceInstance ()
    {
        return REFERENCE_AssessmentCriteriaTemplate;
    }


    public AssessmentCriteriaTemplate getInTransaction (ObjectTransaction t) throws StorageException
    {
        return getAssessmentCriteriaTemplateByID (t, getObjectID());
    }
    
    
    public BaseBusinessClass dummyInstance ()
    {
        return DUMMY_AssessmentCriteriaTemplate;
    }


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





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

        tl_assessment_templatePSet.setAttrib (FIELD_ObjectID, myID);
        tl_assessment_templatePSet.setAttrib (FIELD_TemplateName, HELPER_TemplateName.toObject (_TemplateName)); //
        _User.getPersistentSets (allSets);
        
    }


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

        
        PersistentSet   tl_assessment_templatePSet = allSets.getPersistentSet (objectID, "tl_assessment_template");

        _TemplateName = (String)(HELPER_TemplateName.fromObject (_TemplateName, tl_assessment_templatePSet.getAttrib (FIELD_TemplateName))); //
        _User.setFromPersistentSets (objectID, allSets);
        
    }





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

        if (other instanceof AssessmentCriteriaTemplate)
        {
            AssessmentCriteriaTemplate otherAssessmentCriteriaTemplate = (AssessmentCriteriaTemplate)other;
            
            try
            {
                setTemplateName (otherAssessmentCriteriaTemplate.getTemplateName ());
            }
            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 BaseAssessmentCriteriaTemplate)
        {
            BaseAssessmentCriteriaTemplate sourceAssessmentCriteriaTemplate = (BaseAssessmentCriteriaTemplate)(source);

            _TemplateName = sourceAssessmentCriteriaTemplate._TemplateName;
            
        }
    }


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

            _User.copyFrom (sourceAssessmentCriteriaTemplate._User, 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 BaseAssessmentCriteriaTemplate)
        {
            BaseAssessmentCriteriaTemplate sourceAssessmentCriteriaTemplate = (BaseAssessmentCriteriaTemplate)(source);

            _AssessmentCriterias.copyFrom (sourceAssessmentCriteriaTemplate._AssessmentCriterias, 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);

        _TemplateName = (String)(HELPER_TemplateName.readExternal (_TemplateName, vals.get(FIELD_TemplateName))); //
        _User.readExternalData(vals.get(SINGLEREFERENCE_User));
        _AssessmentCriterias.readExternalData(vals.get(MULTIPLEREFERENCE_AssessmentCriterias));
        
    }


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

        vals.put (FIELD_TemplateName, HELPER_TemplateName.writeExternal (_TemplateName));
        vals.put (SINGLEREFERENCE_User, _User.writeExternalData());
        vals.put (MULTIPLEREFERENCE_AssessmentCriterias, _AssessmentCriterias.writeExternalData());
        
    }


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

        if (other instanceof BaseAssessmentCriteriaTemplate)
        {
            BaseAssessmentCriteriaTemplate otherAssessmentCriteriaTemplate = (BaseAssessmentCriteriaTemplate)(other);

            
            if (!HELPER_TemplateName.compare(this._TemplateName, otherAssessmentCriteriaTemplate._TemplateName))
            {
                listener.notifyFieldChange(this, other, FIELD_TemplateName, HELPER_TemplateName.toObject(this._TemplateName), HELPER_TemplateName.toObject(otherAssessmentCriteriaTemplate._TemplateName));
            }

            // Compare single assocs
            _User.compare (otherAssessmentCriteriaTemplate._User, listener);
            

            // Compare multiple assocs
            _AssessmentCriterias.compare (otherAssessmentCriteriaTemplate._AssessmentCriterias, 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_TemplateName, HELPER_TemplateName.toObject(getTemplateName()));
        visitor.visitAssociation (_User);
        visitor.visitAssociation (_AssessmentCriterias);
        
    }


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

        if (scope.includes (_User))
        {
            visitor.visit (_User);
        }
        if (scope.includes (_AssessmentCriterias))
        {
            visitor.visit (_AssessmentCriterias);
        }
        
    }


    public static AssessmentCriteriaTemplate createAssessmentCriteriaTemplate (ObjectTransaction transaction) throws StorageException
    {
        AssessmentCriteriaTemplate     result = new AssessmentCriteriaTemplate ();

        result.initialiseNewObject (transaction);

        return result;
    }


    public static AssessmentCriteriaTemplate getAssessmentCriteriaTemplateByID (ObjectTransaction transaction, Long objectID) throws StorageException
    {
        return (AssessmentCriteriaTemplate)(transaction.getObjectByID (REFERENCE_AssessmentCriteriaTemplate, objectID));
    }

    public boolean testFilter (String attribName, QueryFilter filter) throws StorageException
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (attribName.equals (FIELD_TemplateName))
        {
            return filter.matches (getTemplateName ());
        }
        else if (attribName.equals (SINGLEREFERENCE_User))
        {
            return filter.matches (getUser ());
        }
        else
        {
            return super.testFilter (attribName, filter);
        }
    }


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

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

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

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

            
            public SearchAll andTemplateName (QueryFilter<String> filter)
            {
                filter.addFilter (context, "tl_assessment_template.template_name", "TemplateName");
                return this;
            }
            
            public SearchAll andUser (QueryFilter<SecUser> filter)
            {
                filter.addFilter (context, "tl_assessment_template.user_id", "User");
                return this;
            }
            
                public AssessmentCriteriaTemplate[]
             search (ObjectTransaction transaction) throws StorageException
        {
            
            
            BaseBusinessClass[]     results = super.search (transaction, REFERENCE_AssessmentCriteriaTemplate, SEARCH_All, criteria);
            Set<AssessmentCriteriaTemplate>   typedResults = new LinkedHashSet <AssessmentCriteriaTemplate> ();
            for (BaseBusinessClass bbcResult : results)
            {
                AssessmentCriteriaTemplate aResult = (AssessmentCriteriaTemplate)bbcResult;
                
                typedResults.add (aResult);
            }
            
                    return ObjstoreUtils.removeDeleted(transaction, typedResults).toArray (new AssessmentCriteriaTemplate[0]);
                            
        }
    }

        
                public static AssessmentCriteriaTemplate[]
             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_TemplateName))
        {
            return HELPER_TemplateName.toObject (getTemplateName ());
        }
        else
        {
            return super.getAttribute (attribName);
        }
    }


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



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


    public FieldWriteability getWriteable (String fieldName)
    {
        if (false)
        {
            throw new RuntimeException ("Game over man!!");
        }
        else if (fieldName.equals (FIELD_TemplateName))
        {
            return getWriteability_TemplateName ();
        }
        else if (fieldName.equals (MULTIPLEREFERENCE_AssessmentCriterias))
        {
            return getWriteability_AssessmentCriterias ();
        }
        else if (fieldName.equals (SINGLEREFERENCE_User))
        {
            return getWriteability_User ();
        }
        else
        {
            return super.getWriteable (fieldName);
        }
    }


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

        super.putUnwriteable (fields);
    }

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

        
        result.add(HELPER_TemplateName.getAttribObject (getClass (), _TemplateName, true, FIELD_TemplateName));

        return result;
    }


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


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

    public void preCommit (boolean willBeStored) throws Exception
    {
        super.preCommit(willBeStored);
        
        if(willBeStored)
        {
        
        }
    }

    public oneit.servlets.objstore.binary.BinaryContentHandler getBinaryContentHandler(String attribName)
    {
        
        
        return super.getBinaryContentHandler(attribName);
    }

    public static class AssessmentCriteriaTemplateBehaviourDecorator extends BaseBusinessClass.BBCBehaviourDecorator<AssessmentCriteriaTemplate>
    {

    /**
     * Get the attribute TemplateName
     */
    public String getTemplateName (AssessmentCriteriaTemplate obj, String original)
    {
        return original;
    }

    /**
     * Change the value set for attribute TemplateName.
     * May modify the field beforehand
     * Occurs before validation.
     */
    public String setTemplateName (AssessmentCriteriaTemplate obj, String newTemplateName) throws FieldException
    {
        return newTemplateName;
    }

    }

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

        public AssessmentCriteriaTemplatePipeLineFactory (Collection<From> seed)
        {
            super(seed);
        }
        
        
        public PipeLine<From, ? extends Object> to(String name)
        {
            if (name.equals ("AssessmentCriterias"))
            {
                return toAssessmentCriterias ();
            }                
            if (name.equals ("TemplateName"))
            {
                return toTemplateName ();
            }                
            if (name.equals ("User"))
            {
                return toUser ();
            }                
            
            
            return super.to(name);
        }
        
        
        public PipeLine<From, String> toTemplateName () { return pipe(new ORMAttributePipe<Me, String>(FIELD_TemplateName));  }
        public SecUser.SecUserPipeLineFactory<From, SecUser> toUser () { return toUser (Filter.ALL); }

        public SecUser.SecUserPipeLineFactory<From, SecUser> toUser (Filter<SecUser> filter) 
        { 
            return SecUser.REFERENCE_SecUser.new SecUserPipeLineFactory<From, SecUser> (this, new ORMSingleAssocPipe<Me, SecUser>(SINGLEREFERENCE_User, filter)); 
        }
        public AssessmentCriteria.AssessmentCriteriaPipeLineFactory<From, AssessmentCriteria> toAssessmentCriterias () { return toAssessmentCriterias(Filter.ALL); }        

        public AssessmentCriteria.AssessmentCriteriaPipeLineFactory<From, AssessmentCriteria> toAssessmentCriterias (Filter<AssessmentCriteria> filter)
        {
            return AssessmentCriteria.REFERENCE_AssessmentCriteria.new AssessmentCriteriaPipeLineFactory<From, AssessmentCriteria> (this, new ORMMultiAssocPipe<Me, AssessmentCriteria>(MULTIPLEREFERENCE_AssessmentCriterias, filter)); 
        }
        
    }
    
    public boolean isTransientAttrib(String attribName)
    {
        

        return super.isTransientAttrib(attribName);
    }

    public boolean isTransientSingleReference(String assocName)
    {
        

        return super.isTransientSingleReference(assocName);
    }
}

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

    public void assertValid ()
    {
    }


    public SecUser getUser () throws StorageException
    {
        return (SecUser)(SecUser.DUMMY_SecUser);
    }


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

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


    public AssessmentCriteria getAssessmentCriteriasAt (int index) throws StorageException
    {
        throw new RuntimeException ("No elements in a dummy object in association AssessmentCriterias");
    }


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

}

    
