RequestNewOp.cpp 3.63 KB
#include "RequestNewOp.h"

#include "../DataRetriever/DataRetriever.h"

using namespace TREPS::DataRetriever;

namespace TREPS
{
	namespace RequestManager
	{
		RequestNewOpClass::RequestNewOpClass(void):RequestAbstractClass() , fileRetrieved(false), isTestDir(false), type("") , location(""), errorMsg("")
		{
		}

		string RequestNewOpClass::getRequestId(void)
		{
			return "new_op";
		}

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

			this->fileRetrieved = false;

			//use test dir for the new operation?
			string useTestDir = loader->getArgStrByName("testdir");

			this->isTestDir = (useTestDir.compare("true") == 0);

			//get new op type
			// * local = from source local file path
			// * url = from Web URL
			this->type      = loader->getArgStrByName("type");
			if (this->type.compare("") == 0)
			{
				this->errorMsg = "Cannot load newop type";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			//source local file path or Web URL
			this->location  = loader->getArgStrByName("location");
			if (this->location.compare("") == 0)
			{
				this->errorMsg = "Cannot load newop location";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			return true;
		}

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

			this->fileRetrieved = false;

			LOG4CXX_INFO(this->app->getLog()->getPtr(),"Run request " << this->getRequestId());
			
			//create a new working directory (ie = a new operation)
			string dirId = this->dirMgr->createNewDir(this->isTestDir);
			
			if (dirId.compare("") == 0)
			{
				this->errorMsg = "Cannot create new working directory";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
			
			LOG4CXX_INFO(this->app->getLog()->getPtr(),"New operation has been created : " << dirId);
			
			this->opId = dirId;

			//lock dir
			this->dirMgr->lockDir(this->opId.c_str());
			
			//copy source file in working directory.
			//Note: source file name in working directory = data.src
			DataRetrieverClass *dataRtv = new DataRetrieverClass();
			
			string dirPath = this->dirMgr->getWorkingDirFromId(this->opId.c_str());
			
			if (type.compare("local") == 0)
			{
				this->fileRetrieved = dataRtv->copyLocalFile(location.c_str(),dirPath.c_str(),"data.src",true);
				if (!this->fileRetrieved)
					this->errorMsg = dataRtv->getErrorMsg();
			}
			else if (type.compare("url") == 0)
			{
				this->fileRetrieved = dataRtv->copyWebFile(location.c_str(),dirPath.c_str(),"data.src",true);
				if (!this->fileRetrieved)
					this->errorMsg = dataRtv->getErrorMsg();
			}
			else
			{
				this->errorMsg = "Unknown resource type ";
				this->errorMsg += type;
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			delete dataRtv;
			
			return this->fileRetrieved;
		}
		
		void RequestNewOpClass::writeResult(ResultWriterClass *writer)
		{
			if (this->opId.compare("") == 0)
			{
				if (this->errorMsg.compare("") == 0)
					writer->setError("Cannot create a new operation");
				else
					writer->setError(this->errorMsg.c_str());
				return;
			}
			
			if (!this->fileRetrieved)
			{
				if (this->errorMsg.compare("") == 0)
					writer->setError("Cannot retrieve data file");
				else
					writer->setError(this->errorMsg.c_str());
				return;
			}

			writer->setSuccess();
			//only return the new op id
			writer->addStrArg("id",this->opId.c_str());
		}

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

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

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