// FictStepData.cpp


#include "stdafx.h"
#include "StepData.h"
#include "Resource.h"
#include "EfLoadDlg.h"

 CaplFictiveStepData::~CaplFictiveStepData()
 {
 	entities.Clear();
	attrs.Clear();
	instances.Clear();
 };

//************************************************************
void CaplFictiveStepData::SetDictionaryFolder(LPCTSTR folder)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
}
//**********************************************************************
bool CaplFictiveStepData::LoadDictionary(LPCTSTR schema)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}

//**********************************************************************
bool CaplFictiveStepData::LoadDictionaryFromFile(LPCTSTR schema)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
//************************************************************************
CaplInstance  *CaplFictiveStepData::CreateInstance(CaplEntity *entity)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return 0;
}
CaplInstance  *CaplFictiveStepData::CreateInstanceBN(LPCTSTR type_name)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return 0;
}

//************************************************************************
bool CaplFictiveStepData::DeleteInstance(CaplInstance *inst)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
//**********************************************************************
//**********************************************************************
//**********************************************************************
bool CaplFictiveStepData::LoadFromTextFile(LPCTSTR file_name)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
//**********************************************************************
bool CaplFictiveStepData::LoadP21(LPCTSTR file_name, aplP21Header &header)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
//**********************************************************************
bool CaplFictiveStepData::SaveChangesToDataBuf(CaplDataBuf &dbuf)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
bool CaplFictiveStepData::LoadFromFile(LPCTSTR file_name, bool appload_mode)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
bool CaplFictiveStepData::ApploadFromDataBuf(CaplDataBuf &dbuf,aplExtent *ext_inst)
{
	SetLastErrorWithFileInfo(APLAPIERR_FICTIVE_DATA);
	return false;
}
//**********************************************************************
//**********************************************************************
//**********************************************************************
bool CaplFictiveStepData::Initialize(CaplStepData *data, aplExtent *ext)
{
	if(data==0) return false;
	if(ext==0) return false;

	m_CurSchema=data->m_CurSchema;
	m_CurSchemaVersion=data->m_CurSchemaVersion;

	//m_CurSchema2=data->m_CurSchema2;
	//m_CurSchemaVersion2=data->m_CurSchemaVersion2;

	int i;
	m_CurAddSchemes.Clear();
	for(i=0; i<=data->m_CurAddSchemes.GetSize(); i++)
	{
		CaplStepData::CAddScheme *addscheme=data->m_CurAddSchemes.GetAt(i);
		if(0!=addscheme) m_CurAddSchemes.Add(new CAddScheme(addscheme->m_scheme, addscheme->m_version));
	}

	entities.Clear();
	attrs.Clear();
	instances.Clear();
	
	entities.SetSize(data->entities.Size);
	for(i=0;i<data->entities.Size;i++){	entities.SetAt(i,data->entities[i]);}

	attrs.SetSize(data->attrs.Size);
	memcpy(attrs.Data, data->attrs.Data, data->attrs.Size*sizeof(CaplAttr*));
	attrs.Size = data->attrs.Size;
//	for(i=0;i<data->attrs.Size;i++){attrs.SetAt(i,data->attrs[i]);}

	instances.SetSize(ext->Size);
	memcpy(instances.Data, ext->Data, sizeof(CaplInstance*)*ext->Size);
	instances.Size = ext->Size;
	instances.Sort();
	for(i=instances.Size; i>1; --i)
	{
		if(instances[i]==instances[i-1])
		{
			instances.Remove(i);
		}
	}
//	for(i=0;i<ext->Size;i++){instances.SetAt(i,ext->GetAt(i));}

	return true;
}
//**********************************************************************
//**********************************************************************
//**********************************************************************

bool CaplFictiveStepData::GetEntityExtentBN(LPCTSTR ent_name,aplExtent &extent)
{
	extent.Clear();
	if(ent_name==0) {SetLastErrorWithFileInfo(APLAPIERR_BADENT); return false;}
	return GetEntityExtent(GetEntityBN(ent_name),extent);
}

bool CaplFictiveStepData::GetEntityExtent(CaplEntity *entity,aplExtent &extent)
{
	extent.Clear();
	if(entity==0) {SetLastErrorWithFileInfo(APLAPIERR_BADENT); return false;}
	SetLastError(0);
	bool u=extent.Unique;
	extent.Unique=false;
	for(int i=0;i<instances.Size;i++)
	{
		CaplEntity *ent=instances[i]->GetType();
		if(ent==entity) extent.Add(instances[i]);
		else
		{
			for(int j=0;j<entity->all_subtypes.Size;j++)
			{
				if(entity->all_subtypes[j]==ent)
				{
					extent.Add(instances[i]);
					break;
				}
			}
		}
	}
	extent.Unique=u;
	return true;
}