//	Zinc Application Framework - Z_DATA1.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_data.hpp>
#include <z_string.hpp>

// ----- ZafDataRecord ------------------------------------------------------

ZafDataRecord::ZafDataRecord(void)
{
}

ZafDataRecord::~ZafDataRecord(void)
{
	ZafData *data1;
	ZafData *data2 = ZAF_NULLP(ZafData);
	for (data1 = First(); data1; data1 = data2)
	{
		data2 = data1->Next();
		data1->SubtractNotification(this);
		if (data1->NotifyCount() != 0)
			Subtract(data1);
	}
}

ZafData *ZafDataRecord::Add(ZafData *object, ZafData *position)
{
	// Check for a null.
	if (!object)
		return (ZAF_NULLP(ZafData));
	else if (position)
		ZafList::Add(object, position);	// position insert.
	else
		ZafList::Add(object);			// compareFunction insert.
	return (object);
}

void ZafDataRecord::Clear(void)
{
}

void ZafDataRecord::Destroy(void)
{
	// Destroy the list elements (low level).
	ZafList::Destroy();
}

ZafData *ZafDataRecord::Duplicate(void)
{
	return (new ZafDataRecord(*this));
}

ZafData *ZafDataRecord::GetObject(ZafNumberID matchID)
{
	// Try to find a matching entry.
	ZafData *match = ZafData::GetObject(matchID);
	if (match)
		return (match);
	for (ZafData *object = First(); object; object = object->Next())
	{
		match = object->GetObject(matchID);
		if (match)
		{
			object->AddNotification(this);
			break;
		}
	}

	// Return the object.
	return (match);
}

ZafData *ZafDataRecord::GetObject(const ZafIChar *matchID)
{
	// Try to find a matching entry.
	ZafData *match = ZafData::GetObject(matchID);
	if (match)
		return (match);
	for (ZafData *object = First(); object; object = object->Next())
	{
		match = object->GetObject(matchID);
		if (match)
		{
			object->AddNotification(this);
			break;
		}
	}

	// Return the object.
	return (match);
}

#if defined(ZAF_UNICODE)
#include <z_str.hpp>
ZafData *ZafDataRecord::GetObject(const char *matchID)
{
	ZafStringData stringData(matchID);
	return (GetObject(stringData.Text()));
}
#endif

bool ZafDataRecord::IsA(ZafClassID compareID) const
{
	return ((compareID == classID) ? true : ZafData::IsA(compareID));
}

bool ZafDataRecord::IsA(ZafClassName compareName) const
{
	return ((compareName == className) ? true : ZafData::IsA(compareName));
}

ZafData *ZafDataRecord::Subtract(ZafData *object)
{
	// Check for a null.
	if (!object)
		return (ZAF_NULLP(ZafData));
	object->SubtractNotification(this);
	return ((ZafData *)ZafList::Subtract(object));
}

// ----- Persistent functions -----------------------------------------------
#if defined(USE_ZAF_PERSISTENCE)
ZafDataRecord::ZafDataRecord(const ZafIChar *name, ZafDataPersistence &persist) :
	ZafData(name, persist.PushLevel(className, classID, ZAF_PERSIST_FILE))
{
	persist.PopLevel();
}

ZafElement *ZafDataRecord::Read(const ZafIChar *name, ZafDataPersistence &persist)
{
	return (new ZafDataRecord(name, persist));
}

void ZafDataRecord::Write(ZafDataPersistence &persist)
{
	// Write the object.
	ZafData::Write(persist.PushLevel(className, classID, ZAF_PERSIST_FILE));
	persist.PopLevel();
	if (persist.Error() != ZAF_ERROR_NONE)
		SetError(persist.Error());
}
#endif
