BodiesAbstract.cpp 2.49 KB
#include "BodiesAbstract.h"

#include <iostream>
#include <cstring>

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

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

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

			this->request = new BodiesRequestClass();

			this->result = new BodiesResultClass();
		}

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

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

		bool BodiesAbstractClass::init(const bool isSc, const t_Time *startTime, const t_Time *endTime)
		{
			this->trepsRequest->setStatus("Initialize bodies abstract class");

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

			this->request->setIsSc(isSc);
			this->request->setTimes(startTime, endTime);

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

		bool BodiesAbstractClass::writeResult(const char *resultPath)
		{
			this->trepsRequest->setStatus("Finalize getbodies XML result");

			FileWriterBodiesClass *fileWriter = new FileWriterBodiesClass();
			map<string,string> attributes;
			attributes.clear(); 

			//init the file writer
			if (!fileWriter->init(resultPath, &attributes))
                        {
                                LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to init the file to write " << resultPath);
                                delete fileWriter;
                                return false;
                        }

			//write file data
			if (!fileWriter->writeData(result->getBodies()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to write data");
				delete fileWriter;
				return false;
			}

			//save file
			bool res = fileWriter->save();

			if (!res)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to save file");
			}

			delete fileWriter;
			return res;
		}

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