TransformationRequest.cpp 8.91 KB
#include "TransformationRequest.h"

#include "../Common/Toolbox.h"
#include "../File/FileLoaderManager.h"
#include "../XMLManager/XMLManager.h"
#include "../TimeManager/TimeManager.h"

using namespace TREPS::Common;
using namespace TREPS::File;
using namespace TREPS::XMLManager;
using namespace TREPS::TimeManager;

namespace TREPS
{
	namespace Transformation
	{
		TransformationRequestClass::TransformationRequestClass(void) : srcFrame(""), dstFrame(""), timeFieldId(""), timeFormat(TF_NONE), timePattern("")
		{
			this->srcVectors.clear();
			this->srcData.clear();

			this->srcInfo.fields.clear();
			this->srcInfo.frames.clear();
			this->srcInfo.vectors.clear();
		}

		TransformationRequestClass::~TransformationRequestClass(void)
		{
			this->srcVectors.clear();
			this->srcData.clear();

			this->srcInfo.fields.clear();
			this->srcInfo.frames.clear();
			this->srcInfo.vectors.clear();
		}

		void TransformationRequestClass::setFrames(const char *srcFrame, const char *dstFrame)
		{
			this->srcFrame = srcFrame;
			this->dstFrame = dstFrame;
		}

		string TransformationRequestClass::getSrcFrame(void) const
		{
			return this->srcFrame;
		}

		string TransformationRequestClass::getDstFrame(void) const
		{
			return this->dstFrame;
		}

		bool TransformationRequestClass::setSrcVectorsDefinition(const char *srcVecDef, const char *srcFrame)
		{
			this->srcVectors.clear();

			//trim srcVecDef and put it in a string
			string vectors = getTRIMStr(srcVecDef);

			//define some working var
			bool finish = (vectors.compare("") == 0);
			string vector, po;
			t_Vector vec;

			int index = 0;

			while (!finish)
			{
				//trim vectors string
				vectors = getTRIMStr(vectors.c_str());

				//find ';' char
				size_t p = vectors.find(";");
				if (p == string::npos)
				{
					//no ';' detected => only one vector defined (or none)
					vector = vectors;
					vectors = "";
				}
				else
				{
					//extract current vector
					vector = vectors.substr(0,p);
					//remove current vector to vectors string
					vectors = vectors.substr(p+1,vectors.length()-p-1);
				}

				//trim vector string
				vector = getTRIMStr(vector.c_str());

				//if vector string is empty => error
				if (vector.compare("") == 0)
				{
					finish = true;
					continue;
				}

				//first char must be '(', and last ')'
				if ((vector[0] != '(') || (vector[vector.length()-1] != ')'))
				{
					finish = true;
					continue;
				}

				//remove '(' and ')'
				vector = vector.substr(1,vector.length()-1);
				vector = vector.substr(0,vector.length()-1);

				//new trim of vector string
				vector = getTRIMStr(vector.c_str());

				//get first component
				//find ',' char
				p = vector.find(",");
				if (p == string::npos)
				{
					finish = true;
					continue;
				}

				//extract comp id
				vec.fieldId[0] = vector.substr(0,p);
				//remove first component to vector string
				vector = vector.substr(p+1,vector.length()-p-1);
				//trim comp id
				vec.fieldId[0] = getTRIMStr(vec.fieldId[0].c_str());

				//get second component
				//find ',' char
				p = vector.find(",");
				if (p == string::npos)
				{
					finish = true;
					continue;
				}

				//extract comp id
				vec.fieldId[1] = vector.substr(0,p);
				//remove first component to vector string
				vector = vector.substr(p+1,vector.length()-p-1);
				//trim comp id
				vec.fieldId[1] = getTRIMStr(vec.fieldId[1].c_str());

				//get third component
				//find ',' char
				p = vector.find(",");
				if (p != string::npos)
				{
					//get isPos value
					po = vector.substr(p+1,vector.length()-p-1);
					po = getTRIMStr(po.c_str());
					vec.ispos = (po.compare("1") == 0);
					vector = vector.substr(0,p);
				}
				else
					vec.ispos = false;

				vec.fieldId[2] = getTRIMStr(vector.c_str());

				vec.frame         = srcFrame;

				vec.id  = "v_";
				vec.id += intToStr(index);

				++index;

				this->srcVectors.push_back(vec);
			}

			return (!this->srcVectors.empty());
		}

		t_VectorList *TransformationRequestClass::getSrcVectors(void) const
		{
			return const_cast<t_VectorList *>(&this->srcVectors);
		}

		string TransformationRequestClass::getSrcVectorsDefinition(void)
		{
			string vecDef = "";
			for (t_VectorList::iterator it=this->srcVectors.begin(); it != this->srcVectors.end(); ++it)
			{
				t_Vector crtVec = (*it);
				if (vecDef.compare("") != 0)
					vecDef += ";";
				vecDef += "(";
				vecDef += crtVec.fieldId[0];
				vecDef += ",";
				vecDef += crtVec.fieldId[1];
				vecDef += ",";
				vecDef += crtVec.fieldId[2];
				if (crtVec.ispos)
					vecDef += ",1";
				vecDef += ")";
			}
			return vecDef;
		}

		bool TransformationRequestClass::setTimeDefinition(const char *timeFieldId, const char *timeFormatId, const char *timePattern)
		{
			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();

			this->timeFormat = timeMgr->getFormatFromTimeId(timeFormatId);

			this->timePattern = "";
			this->timeFieldId = "";

			if (this->timeFormat == TF_NONE)
				return true;
				
			if (this->timeFormat == TF_PATTERN)
			{
				//try to get pre defined pattern
				this->timePattern = timeMgr->getPatternFromTimeId(timeFormatId);

				if (this->timePattern.compare("") == 0)
					this->timePattern = timePattern;

				if (this->timePattern.compare("") == 0)
					return false;
			}

			this->timeFieldId = timeFieldId;

			if (this->timeFieldId.compare("") == 0)
				return false;

			return true;
		}

		string TransformationRequestClass::getTimeFieldId(void) const
		{
			return this->timeFieldId;
		}

		t_TimeFormat TransformationRequestClass::getTimeFormat(void) const
		{
			return this->timeFormat;
		}

		string TransformationRequestClass::getTimePattern(void) const
		{
			return this->timePattern;
		}

		bool TransformationRequestClass::loadSrcData(const char *srcFilePath)
		{
			this->srcData.clear();
			this->srcInfo.fields.clear();

			//init file loader manager to load source data
			FileLoaderManagerClass *fileLoaderMgr = new FileLoaderManagerClass();

			if (!fileLoaderMgr->init(srcFilePath))
			{
				delete fileLoaderMgr;
				return false;
			}

			if (!fileLoaderMgr->getInfo(this->srcInfo))
			{
				delete fileLoaderMgr;
				return false;
			}

			int total = 0;
			if (!fileLoaderMgr->getData(0,0,&this->srcData,total))
			{
				delete fileLoaderMgr;
				return false;
			}

			delete fileLoaderMgr;

			return true;
		}

		DataRecordListClass *TransformationRequestClass::getSrcDataRecordList(void) const
		{
			return const_cast<DataRecordListClass *>(&this->srcData);
		}

		t_FieldList *TransformationRequestClass::getSrcFields(void) const
		{
			return const_cast<t_FieldList *>(&this->srcInfo.fields);
		}

		bool TransformationRequestClass::saveToFile(const char *filePath)
		{
			XMLManagerClass *writer = new XMLManagerClass();

			if (!writer->create("treps"))
			{
				delete writer;
				return false;
			}

			Node *transNode = writer->addChildToRoot("transformation");

			if (transNode == NULL)
			{
				delete writer;
				return false;
			}

			writer->addAttributeToNode("srcFrame",this->getSrcFrame().c_str(),transNode);
			writer->addAttributeToNode("dstFrame",this->getDstFrame().c_str(),transNode);

			writer->addAttributeToNode("srcVectors",this->getSrcVectorsDefinition().c_str(),transNode);
			writer->addAttributeToNode("srcTimeFieldId",this->getTimeFieldId().c_str(),transNode);
			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();
			string timeFormatId = timeMgr->getTimeIdFromFormatAndPattern(this->getTimeFormat(),this->getTimePattern().c_str());
			writer->addAttributeToNode("srcTimeFormatId",timeFormatId.c_str(),transNode);
			writer->addAttributeToNode("srcTimePattern",this->getTimePattern().c_str(),transNode);

			bool res = writer->save(filePath);

			delete writer;

			return res;
		}

		bool TransformationRequestClass::loadFromFile(const char *filePath)
		{
			XMLManagerClass *loader = new XMLManagerClass();

			if (!loader->loadFile(filePath))
			{
				delete loader;
				return false;
			}

			Node *transNode = loader->getChildFromRoot("transformation");

			if (transNode == NULL)
			{
				delete loader;
				return false;
			}

			string srcFrame = loader->getAttributeFromNode("srcFrame",transNode);
			string dstFrame = loader->getAttributeFromNode("dstFrame",transNode);

			this->setFrames(srcFrame.c_str(), dstFrame.c_str());

			bool noTrans = (srcFrame.compare(dstFrame) == 0);

			string srcVecDef = loader->getAttributeFromNode("srcVectors",transNode);
		
			if (!this->setSrcVectorsDefinition(srcVecDef.c_str(), srcFrame.c_str()) && !noTrans)
			{
				delete loader;
				return false;
			}

			string timeFieldId  = loader->getAttributeFromNode("srcTimeFieldId",transNode);
			string timeFormatId = loader->getAttributeFromNode("srcTimeFormatId",transNode);
			string timePattern  = loader->getAttributeFromNode("srcTimePattern",transNode);

			if (!this->setTimeDefinition(timeFieldId.c_str(), timeFormatId.c_str(), timePattern.c_str()))
			{
				delete loader;
				return false;
			}
	
			delete loader;

			return true;
		}
	}
}