RequestFileGetAbstract.cpp 3.18 KB
#include "RequestFileGetAbstract.h"

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

using namespace TREPS::Common;

namespace TREPS
{
	namespace RequestManager
	{
		RequestFileGetAbstractClass::RequestFileGetAbstractClass(void):RequestAbstractClass(), start(0), limit(0), total(0), errorMsg("")
		{
			//this->useCache = true;
			this->data.clear();
		}

		RequestFileGetAbstractClass::~RequestFileGetAbstractClass(void)
		{
			this->data.clear();
		}

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

			this->start = 0;
			this->limit = 0;

			//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;
			}

			//get the page start and the page limite
			string pageStartStr = loader->getArgStrByName("start");
			if (pageStartStr.compare("") != 0)
				stringstream ( pageStartStr ) >> this->start;

			string pageLimitStr = loader->getArgStrByName("limit");
			if (pageLimitStr.compare("") != 0)
				stringstream ( pageLimitStr ) >> this->limit;

			return true;
		}

		bool RequestFileGetAbstractClass::run(void)
		{
			this->data.clear();
			this->total = 0;

			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 - Cannot get source file path";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
		
			//init the file loader manager
			FileLoaderManagerClass *fileLoaderMgr = new FileLoaderManagerClass();
			
			if (!fileLoaderMgr->init(filePath.c_str()))
			{
				this->errorMsg = this->errorMsg;
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				delete fileLoaderMgr;
                                return false;
			}
			
			//get file data
			bool res = fileLoaderMgr->getData(this->start,this->limit,&this->data,this->total);
			
			if (!res)
			{
				this->errorMsg = fileLoaderMgr->getErrorMsg();
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}
			
			delete fileLoaderMgr;

			return res;
		}
		
		void RequestFileGetAbstractClass::writeResult(ResultWriterClass *writer)
		{
			if (this->data.isEmpty())
			{
				if (this->errorMsg.compare("") == 0)
					writer->setError("Error to get file data (unknown error)");
				else
					writer->setError(this->errorMsg.c_str());
			}
			else
			{
				writer->setSuccess();
				writer->addIntArg("start",this->start);
				writer->addIntArg("limit",this->limit);
				writer->addIntArg("total",this->total);
				writer->addDataRecordListArg("data",&this->data);
			}
		}

		string RequestFileGetAbstractClass::getResultFileSuffix(void)
		{
			stringstream res;
			res << "_" << this->start << "_" << this->limit;
			return res.str();
		}

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

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