// WMSOAPStructType.cpp: implementation of the WMSOAPStructType class.
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2001 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// WMSOAPWSDLClient class - implementation of SOAP-ENC:Struct container
//  
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WMSOAPStructType.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


WMSOAPStructMember::WMSOAPStructMember(BSTR bstrName, const VARIANT& varValue)
{
	m_bstrName = bstrName;
	m_varValue = varValue;
}

WMSOAPStructMember::WMSOAPStructMember(BSTR bstrName, BSTR bstrNS, const VARIANT& varValue)
{
	m_bstrName = bstrName;
	m_bstrNS = bstrNS;
	m_varValue = varValue;
}

WMSOAPStructType::WMSOAPStructType()
{

}

WMSOAPStructType::~WMSOAPStructType()
{
	list<WMSOAPStructMember *>::iterator iter;
	for (iter = m_listMembers.begin(); iter != m_listMembers.end(); iter++)
	{
		delete *iter;
	}
}

bool WMSOAPStructType::SetMember(BSTR bstrName, VARIANT varValue)
{
    WMSOAPStructMember *pMember;
    try
    {
	    pMember = new WMSOAPStructMember(bstrName, varValue);
    }
    catch (_com_error e)
    {
        return false;
    }
	if (pMember != NULL)
	{
		m_listMembers.push_back(pMember);
		return true;
	}
	return false;
}

bool WMSOAPStructType::GetMember(BSTR bstrName, VARIANT *pvarValue)
{
	list<WMSOAPStructMember *>::iterator iter;
	for (iter = m_listMembers.begin(); iter != m_listMembers.end(); iter++)
	{
		if ((*iter)->GetName() == _bstr_t(bstrName))
		{
			if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
            {
                return false;
            }
			return true;
		}
	}
	return false;
}

bool WMSOAPStructType::GetFirstMember(BSTR *pbstrName, VARIANT *pvarValue, long *pPos)
{
	if (m_listMembers.empty() == true)
	{
		return false;
	}
	list<WMSOAPStructMember *>::iterator iter;
	iter = m_listMembers.begin();
	*pbstrName = (*iter)->GetName().copy();
	if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
    {
        return false;
    }
	*pPos = 0;
	return true;
}

bool WMSOAPStructType::GetNextMember(BSTR *pbstrName, VARIANT *pvarValue, long *pPos)
{
	list<WMSOAPStructMember *>::iterator iter = m_listMembers.begin();
	long lNext = *pPos + 1;
	for (long i = 0; i < lNext; i++)
	{
		iter++;
	}
	if (iter != m_listMembers.end())
	{
		*pPos = lNext;
		*pbstrName = (*iter)->GetName().copy();
	    if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
        {
            return false;
        }
		return true;
	}
	return false;
}

bool WMSOAPStructType::SetMemberQN(BSTR bstrName, BSTR bstrNS, VARIANT varValue)
{
    WMSOAPStructMember *pMember;
    try
    {
	    pMember = new WMSOAPStructMember(bstrName, bstrNS, varValue);
    }
    catch (_com_error e)
    {
        return false;
    }
	if (pMember != NULL)
	{
		m_listMembers.push_back(pMember);
		return true;
	}
	return false;
}

bool WMSOAPStructType::GetMemberQN(BSTR bstrName, BSTR bstrNS, VARIANT *pvarValue)
{
	list<WMSOAPStructMember *>::iterator iter;
	for (iter = m_listMembers.begin(); iter != m_listMembers.end(); iter++)
	{
		if ((*iter)->GetName() == _bstr_t(bstrName) && (*iter)->GetNS() == _bstr_t(bstrNS))
		{
	        if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
            {
                return false;
            }
			return true;
		}
	}
	return false;
}

bool WMSOAPStructType::GetFirstMemberQN(BSTR *pbstrName, BSTR *pbstrNS, VARIANT *pvarValue, long *pPos)
{
	if (m_listMembers.empty() == true)
	{
		return false;
	}
	list<WMSOAPStructMember *>::iterator iter;
	iter = m_listMembers.begin();
	*pbstrName = (*iter)->GetName().copy();
	if ((*iter)->GetNS().length() > 0)
	{
		*pbstrNS = (*iter)->GetNS().copy();
	}
	else
	{
		*pbstrNS = SysAllocString(L"");
	}
	if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
    {
        return false;
    }
	*pPos = 0;
	return true;
}

bool WMSOAPStructType::GetNextMemberQN(BSTR *pbstrName, BSTR *pbstrNS, VARIANT *pvarValue, long *pPos)
{
	list<WMSOAPStructMember *>::iterator iter = m_listMembers.begin();
	long lNext = *pPos + 1;
	for (long i = 0; i < lNext; i++)
	{
		iter++;
	}
	if (iter != m_listMembers.end())
	{
		*pPos = lNext;
		*pbstrName = (*iter)->GetName().copy();
		if ((*iter)->GetNS().length() > 0)
		{
			*pbstrNS = (*iter)->GetNS().copy();
		}
		else
		{
			*pbstrNS = SysAllocString(L"");
		}
	    if (FAILED(VariantCopy(pvarValue, (*iter)->GetValue())))
        {
            return false;
        }
		return true;
	}
	return false;
}
