RequestExportOp.cpp 5.6 KB
#include "RequestExportOp.h"

#include "../TimeManager/TimeManager.h"
#include "../Export/ExportManager.h"
#include "../File/FileFormatManager.h"

using namespace TREPS::TimeManager;
using namespace TREPS::Export;
using namespace TREPS::File;

namespace TREPS
{
	namespace RequestManager
	{
		RequestExportOpClass::RequestExportOpClass(void):format(""), structure(""), timeFormat(TF_NONE), timePattern(""), success(false)
		{

		}

		RequestExportOpClass::~RequestExportOpClass(void)
		{

		}

		string RequestExportOpClass::getRequestId(void)
		{
			return "export_op";
		}

		bool RequestExportOpClass::load(RequestLoaderClass *loader)
		{
			this->success = false;

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

			//get export format
			this->format = loader->getArgStrByName("format");
			if (this->format.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get export file format");
				return false;
			}

			//get export structure
			this->structure = loader->getArgStrByName("structure");
			if (this->format.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get export file structure");
				return false;
			}

			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();

			//get time format
			string format = loader->getArgStrByName("timeformat");

			this->timeFormat = timeMgr->getFormatFromTimeId(format.c_str());

			//get timepattern
			this->timePattern = "";
			if (this->timeFormat == TF_PATTERN)
			{
				this->timePattern = loader->getArgStrByName("timepattern");

				if (this->timePattern.compare("") == 0)
                                //try to get pre defined pattern
					this->timePattern = timeMgr->getPatternFromTimeId(format.c_str());

				if (this->timePattern.compare("") == 0)
                                {
					LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get time pattern");
					return false;
				}
 			}

			return true;
		}

		bool RequestExportOpClass::run(void)
		{
			this->success = false;

			//set log to working dir
			this->dirMgr->setActiveForLog(this->opId.c_str(),"export_op.log");

			//get source, result and export path
			string srcPath = this->dirMgr->getSourcePathFromId(this->opId.c_str());

			if (srcPath.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get source file path");
				return false;
			}

			string resPath = this->dirMgr->getResultPathFromId(this->opId.c_str());

			if (resPath.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get result file path");
				return false;
			}

			string expPath = this->dirMgr->getExportedPathFromId(this->opId.c_str());

			if (expPath.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get exported file path");
				return false;
			}

			//get transformation request file path (to load information about the transformation request)
			string requestPath = this->dirMgr->getTransformationRequestFromId(this->opId.c_str());

			if (requestPath.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to get transformation request path");
				return false;
			}

			//get export format
			string formatsFile = this->app->getConf()->getFormatsFilePath();

			if (formatsFile.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get files formats file path");
				return false;
			}

			FileFormatManagerClass *formatMgr = new FileFormatManagerClass();

			if (!formatMgr->init(formatsFile.c_str()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot init file format manager");
				delete formatMgr;
				return false;
			}

			t_FileFormat exportFormat = formatMgr->getFileFormatFromId(this->format.c_str());

			if (exportFormat == FF_NONE)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown export format id " << this->format);
				delete formatMgr;
				return false;
			}

			delete formatMgr;

			//get export structure
			string exportsFile = this->app->getConf()->getExportsFilePath();

			if (exportsFile.compare("") == 0)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot get exports file path");
				return false;
			}

			ExportManagerClass *exportMgr = new ExportManagerClass();

			if (!exportMgr->init(exportsFile.c_str()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot init export manager");
				delete exportMgr;
				return false;
			}

			t_ExportStructure exportStruct = exportMgr->getExportStructureFromId(this->structure.c_str());

			if (exportStruct == ES_NONE)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown export structure id " << this->structure);
				delete exportMgr;
				return false;
			}

			//run exportation
			this->success = exportMgr->runExportOp(exportStruct, exportFormat, this->timeFormat,
							this->timePattern.c_str(), requestPath.c_str(), 
							srcPath.c_str(), resPath.c_str(), expPath.c_str());

			if (!this->success)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to run exportation");
			}

			delete exportMgr;

			return this->success;
		}

		void RequestExportOpClass::writeResult(ResultWriterClass *writer)
		{
			if (!this->success)
				writer->setError("Internal error during exportation request");
			else
				writer->setSuccess();
		}

		string RequestExportOpClass::getResultFileSuffix(void)
		{
			//nothing to do
			return "";
		}

		string RequestExportOpClass::getXMLFilePath(void)
		{
			//nothing to do
			return "";
		}

		string RequestExportOpClass::getStringResult(void)
		{
			//nothing to do
			return "";
		}
	}
}