TransformationAbstract.cpp 4.16 KB
#include "TransformationAbstract.h"

#include <iostream>
#include <cstring>

#include "../Common/Toolbox.h"
#include "../File/FileWriterManager.h"
#include "../TimeManager/TimeManager.h"

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

namespace TREPS
{
	namespace Transformation
	{
		TransformationAbstractClass::TransformationAbstractClass(RequestAbstractClass *trepsRequest) : app(NULL), trepsRequest(trepsRequest),
							errorMsg(""), request(NULL), result(NULL)
		{
			this->app = ApplicationClass::getInstance();

			this->request = new TransformationRequestClass();

			this->result = new TransformationResultClass();
		}

		TransformationAbstractClass::~TransformationAbstractClass(void)
		{
			if (this->request != NULL)
			{
				delete this->request;
				this->request = NULL;
			}

			if (this->result != NULL)
			{
				delete this->result;
				this->result = NULL;
			}
		}

		bool TransformationAbstractClass::init(const char *srcFrame, const char *dstFrame, const char *srcVecDef,
			const char *timeFieldId, const char *timeFormatId, const char *timePattern, const char *srcDataFile,
			const char *transformationRequest)
		{
			this->trepsRequest->setStatus("Initialize transformation");

			if (this->request == NULL)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot init transformation request");
				return false;
			}

			bool noTrans = (strcmp(srcFrame,dstFrame) == 0);

			//set frames
			if (!noTrans && !this->checkFrame(srcFrame))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << srcFrame);
				return false;
			}

			if (!noTrans && !this->checkFrame(dstFrame))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << dstFrame);
				return false;
			}

			this->request->setFrames(srcFrame, dstFrame);

			//load src data
			if (!this->request->loadSrcData(srcDataFile))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load source data : " << srcDataFile);
				return false;
			}

			//set source vectors definition
			if (!this->request->setSrcVectorsDefinition(srcVecDef, srcFrame) && !noTrans)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error detected in source vectors definition : " << srcVecDef);
				return false;
			}

			//set time definition
			if (!this->request->setTimeDefinition(timeFieldId,timeFormatId,timePattern))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error in time definition");
				return false;
			}

			//load times in source data
			if (!this->request->getSrcDataRecordList()->loadTimes(  this->request->getTimeFieldId().c_str(),
										this->request->getTimeFormat(),
										this->request->getTimePattern().c_str()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to load times in source data");
				return false;
			}

			//Init result
			if (!this->result->init(this->request->getTimeFormat(),this->request->getSrcDataRecordList()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot init result");
				return false;
			}

			//save request file
			if (!this->request->saveToFile(transformationRequest))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot save transformation request file");
				return false;
			}

			return true;
		}

		bool TransformationAbstractClass::writeResult(const char *resultPath)
		{
			this->trepsRequest->setStatus("Finalize transformation");

			FileWriterManagerClass *fileWriterMgr = new FileWriterManagerClass();

			t_TimeFormat timeFormat = TF_NONE;
			string timePattern = "";

			if (this->request->getTimeFormat() != TF_NONE)
			{
				timeFormat  = TF_PATTERN;
				timePattern = RESULT_TIME_PATTERN;
			}

			map<string,string> attributes;
			attributes.clear(); 

			bool res = fileWriterMgr->saveData(resultPath, FF_CDF,
					this->result->getDstDataRecordList(),
					timeFormat,
					timePattern.c_str(),
					this->result->getDstFieldList(),
					&attributes
				);

			if (!res)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot save result file " << resultPath);
			}

			delete fileWriterMgr;
			return res;
		}

		string TransformationAbstractClass::getLastError(void)
		{
			return this->errorMsg;
		}
	}
}