RequestFileInfoAbstract.cpp 2.91 KB
#include "RequestFileInfoAbstract.h"

#include "../Common/Toolbox.h"

using namespace TREPS::Common;

namespace TREPS
{
	namespace RequestManager
	{
		RequestFileInfoAbstractClass::RequestFileInfoAbstractClass(void):RequestAbstractClass() , errorMsg("")
		{
			//this->useCache = true;
		}

		RequestFileInfoAbstractClass::~RequestFileInfoAbstractClass(void)
		{
			this->info.fields.clear();
			this->info.frames.clear();
			this->info.vectors.clear();
		}

		bool RequestFileInfoAbstractClass::load(RequestLoaderClass *loader)
		{
			this->errorMsg = "";

			//init the request and lock working dir
			if (!this->initOpId(loader,true))
			{
				this->errorMsg = "Internal error - Cannot get op Id";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			return true;
		}

		bool RequestFileInfoAbstractClass::run(void)
		{
			this->errorMsg = "";

			this->info.fields.clear();
			LOG4CXX_INFO(this->app->getLog()->getPtr(),"Run " << this->getRequestId() << " request");
			
			string logFile = this->getRequestId();
			logFile += ".log";

			this->dirMgr->setActiveForLog(this->opId.c_str(),logFile.c_str());
			
			//get file path
			string filePath = this->getFilePath();

			if (filePath.compare("") == 0)
			{
				this->errorMsg = "Internal error - Error to get file path";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
	
			//Init file manager
			FileLoaderManagerClass *fileLoaderMgr = new FileLoaderManagerClass();
			
			if (!fileLoaderMgr->init(filePath.c_str()))
			{
				this->errorMsg = fileLoaderMgr->getErrorMsg();
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				delete fileLoaderMgr;
                                return false;
			}
			
			//get file info
			bool res = fileLoaderMgr->getInfo(this->info);
			
			if (!res)
			{
				this->errorMsg = fileLoaderMgr->getErrorMsg();
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			delete fileLoaderMgr;

			return res;
		}
		
		void RequestFileInfoAbstractClass::writeResult(ResultWriterClass *writer)
		{
			if (this->info.fields.empty())
			{
				if (this->errorMsg.compare("") == 0)
					writer->setError("Cannot get file info (unknown error)");
				else
					writer->setError(this->errorMsg.c_str());
			}
			else
			{
				if (this->info.nbRecords > this->app->getConf()->getMaxSourceFileRecords())
				{
					stringstream msg;
					msg << "Too many records in source file (limited to ";
					msg << this->app->getConf()->getMaxSourceFileRecords();
					msg << ")";
					writer->setError(msg.str().c_str());
				}
				else
				{
					writer->setSuccess();
					writer->addFileInfoArg("info",this->info);
				}
			}
		}

		string RequestFileInfoAbstractClass::getResultFileSuffix(void)
		{
			return "";
		}

		string RequestFileInfoAbstractClass::getXMLFilePath(void)
		{
			return "";
		}

		string RequestFileInfoAbstractClass::getStringResult(void)
		{
			return "";
		}
	}
}