void InsertFieldElementCommand::execute(void)
{
    //Check for a valid Field Container
    if(_FC == NULL)
    {
        SWARNING << "FieldContainer is NULL." << std::endl;
        return;
    }

    //Check for valid Field
    EditFieldHandlePtr TheFieldHandle = _FC->editField(_FieldId);
    if(!TheFieldHandle->isValid())
    {
        SWARNING << "No Field with Id: " << _FieldId << " in FieldContainers of type " << _FC->getType().getName() << std::endl;
        return;
    }

    //Check for valid Field cardinality
    if(TheFieldHandle->getCardinality() != FieldType::MultiField)
    {
        SWARNING << "Field: " << _FC->getType().getName() << " is not a MultiField" << std::endl;
        return;
    }

    //Check for valid indexing
    if(TheFieldHandle->getCardinality() == FieldType::MultiField && _Index > TheFieldHandle->size())
    {
        SWARNING << "Cannot insert the value " << _Value << " at index " << _Index << ", on field " << TheFieldHandle->getDescription()->getName() 
                 << ", on FieldContianer of type " << _FC->getType().getName()
                 << " because that field has size " << TheFieldHandle->size() << std::endl;
        return;
    }
        
    if(TheFieldHandle->isPointerField())
    {
        EditMFieldHandle<FieldContainerPtrMFieldBase>* TheHandle(dynamic_cast<EditMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get()));
        FieldContainer* FC = FieldContainerFactory::the()->getContainer(boost::lexical_cast<UInt32>(_Value));
        //Check the pointer types match
        if(!isFieldContentDerivedFrom(TheFieldHandle->getType(),&FC->getType()))
        {
            SWARNING << "Cannot insert the value of field " << TheFieldHandle->getDescription()->getName() 
                     << ", on FieldContianer of type " << _FC->getType().getName()
                     << " because the value attemting to be set is not derived from the type the field stores." << std::endl;
            return;
        }

        TheHandle->insert(_Index, FC);
    }
    else
    {
        TheFieldHandle->insertIndexedValueFromCString(_Value.c_str(), _Index);
    }

    Inherited::execute();
	_HasBeenDone = true;
}
void SetFieldValueCommand::execute(void)
{
    //Check for a valid Field Container
    if(_FC == NULL)
    {
        SWARNING << "FieldContainer is NULL." << std::endl;
        return;
    }

    //Check for valid Field
    GetFieldHandlePtr TheFieldHandle = _FC->getField(_FieldId);
    if(!TheFieldHandle->isValid())
    {
        SWARNING << "No Field with Id: " << _FieldId << " in FieldContainers of type " << _FC->getType().getName() << std::endl;
        return;
    }

    //Check for valid indexing
    if(TheFieldHandle->getCardinality() == FieldType::SingleField && _Index != 0)
    {
        SWARNING << "Cannot reference index " << _Index << ", on field " << TheFieldHandle->getDescription()->getName() 
                 << ", on FieldContianer of type " << _FC->getType().getName()
                 << " because that field is a SingleField." << std::endl;
        return;
    }
    else if(TheFieldHandle->getCardinality() == FieldType::MultiField && _Index >= TheFieldHandle->size())
    {
        SWARNING << "Cannot set the value of index " << _Index << ", on field " << TheFieldHandle->getDescription()->getName() 
                 << ", on FieldContianer of type " << _FC->getType().getName()
                 << " because that field has size " << TheFieldHandle->size() << std::endl;
        return;
    }


    //Get the previous value
    if(_PrevValue.empty())
    {
        std::ostringstream StrStream;
        OutStream TheOutStream(StrStream);
        if(TheFieldHandle->getCardinality() == FieldType::SingleField)
        {
            if(TheFieldHandle->isPointerField())
            {
                _PrevPtrValue = dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get();
                if(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get())
                {
                    _PrevValue = boost::lexical_cast<std::string>(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get()->getId());
                }
                else
                {
                    _PrevValue = "0";
                }
            }
            else
            {
                TheFieldHandle->pushValueToStream(TheOutStream);
                _PrevValue = StrStream.str();
            }
        }
        else
        {
            if(TheFieldHandle->isPointerField())
            {
                _PrevPtrValue = dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get())->get(_Index);
                if(_PrevPtrValue)
                {
                    _PrevValue = boost::lexical_cast<std::string>(dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get())->get(_Index)->getId());
                }
                else
                {
                    _PrevValue = "0";
                }
            }
            else
            {
                TheFieldHandle->pushIndexedValueToStream(TheOutStream, _Index);
                _PrevValue = StrStream.str();
            }
        }

        //Remove quotes from strings
        if(TheFieldHandle->getType().getContentType() == FieldTraits<std::string>::getType())
        {
            _PrevValue = _PrevValue.substr(1,StrStream.str().size()-2);
        }
    }

    //Set the value
    if(TheFieldHandle->getCardinality() == FieldType::SingleField)
    {
        if(TheFieldHandle->isPointerField())
        {
            _PtrValue = FieldContainerFactory::the()->getContainer(boost::lexical_cast<UInt32>(_Value));
            
            //Check the pointer types match
            if(_PtrValue != NULL &&
                !isFieldContentDerivedFrom(TheFieldHandle->getType(),&_PtrValue->getType()))
            {
                SWARNING << "Cannot set the value of field " << TheFieldHandle->getDescription()->getName() 
                         << ", on FieldContianer of type " << _FC->getType().getName()
                         << " because the value attemting to be set is not derived from the type the field stores." << std::endl;
                return;
            }
            if(_PtrValue != _PrevPtrValue)
            {
                dynamic_cast<EditSFieldHandle<FieldContainerPtrSFieldBase>*>(_FC->editField(_FieldId).get())->set(_PtrValue);
            }
        }
        else
        {
            _FC->editField(_FieldId)->pushValueFromCString(_Value.c_str());
        }
    }
    else
    {
        if(TheFieldHandle->isPointerField())
        {
            _PtrValue = FieldContainerFactory::the()->getContainer(boost::lexical_cast<UInt32>(_Value));
            
            //Check the pointer types match
            if(_PtrValue != NULL && !isFieldContentDerivedFrom(TheFieldHandle->getType(),&_PtrValue->getType()))
            {
                SWARNING << "Cannot set the value of field " << TheFieldHandle->getDescription()->getName() 
                         << ", on FieldContianer of type " << _FC->getType().getName()
                         << " because the value attemting to be set is not derived from the type the field stores." << std::endl;
                return;
            }
            if(_PtrValue != _PrevPtrValue)
            {
                dynamic_cast<EditMFieldHandle<FieldContainerPtrMFieldBase>*>(_FC->editField(_FieldId).get())->replace(_Index, _PtrValue);
            }
        }
        else
        {
            _FC->editField(_FieldId)->pushIndexedValueFromCString(_Value.c_str(), _Index);
        }
    }

    Inherited::execute();
	_HasBeenDone = true;
}