TransformationRequest.cpp 10.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
#include "TransformationRequest.h"

#include "../Common/Toolbox.h"
#include "../File/FileLoaderManager.h"
#include "../XMLManager/XMLManager.h"
#include "../TimeManager/TimeManager.h"

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

namespace TREPS
{
	namespace Transformation
	{
		TransformationRequestClass::TransformationRequestClass(void) : srcFrame(""), dstFrame(""), srcCenter(""), dstCenter(""), startTime(), stopTime(), timeFieldId(""), timeFormat(TF_NONE), timePattern("")
		{
			this->srcVectors.clear();
			this->srcData.clear();

			this->srcInfo.fields.clear();
			this->srcInfo.frames.clear();
			this->srcInfo.vectors.clear();
		}

		TransformationRequestClass::~TransformationRequestClass(void)
		{
			this->srcVectors.clear();
			this->srcData.clear();

			this->srcInfo.fields.clear();
			this->srcInfo.frames.clear();
			this->srcInfo.vectors.clear();
		}

		void TransformationRequestClass::setFrames(const char *srcFrame, const char *dstFrame)
		{
			this->srcFrame = srcFrame;
			this->dstFrame = dstFrame;
		}

		string TransformationRequestClass::getSrcFrame(void) const
		{
			return this->srcFrame;
		}

		string TransformationRequestClass::getDstFrame(void) const
		{
			return this->dstFrame;
		}

		void TransformationRequestClass::setCenters(const char *srcCenter, const char *dstCenter)
		{
			this->srcCenter = srcCenter;
			this->dstCenter = dstCenter;
		}

		string TransformationRequestClass::getSrcCenter(void) const
		{
			return this->srcCenter;
		}

		string TransformationRequestClass::getDstCenter(void) const
		{
			return this->dstCenter;
		}

		void TransformationRequestClass::setTimes(const t_Time startTime, const t_Time stopTime)
		{
			this->startTime = startTime;
			this->stopTime = stopTime;
		}

		t_Time TransformationRequestClass::getStartTime(void) const
		{
			return this->startTime;
		}

		t_Time TransformationRequestClass::getStopTime(void) const
		{
			return this->stopTime;
		}



		bool TransformationRequestClass::setSrcVectorsDefinition(const char *srcVecDef, const char *srcFrame)
		{
			this->srcVectors.clear();

			//trim srcVecDef and put it in a string
			string vectors = getTRIMStr(srcVecDef);

			//define some working var
			bool finish = (vectors.compare("") == 0);
			string vector, po;
			t_Vector vec;

			int index = 0;

			while (!finish)
			{
				//trim vectors string
				vectors = getTRIMStr(vectors.c_str());

				//find ';' char
				size_t p = vectors.find(";");
				if (p == string::npos)
				{
					//no ';' detected => only one vector defined (or none)
					vector = vectors;
					vectors = "";
				}
				else
				{
					//extract current vector
					vector = vectors.substr(0,p);
					//remove current vector to vectors string
					vectors = vectors.substr(p+1,vectors.length()-p-1);
				}
				
				//ApplicationClass *app = ApplicationClass::getInstance();
				//LOG4CXX_INFO(app->getLog()->getPtr(),vector.c_str());

				//trim vector string
				vector = getTRIMStr(vector.c_str());

				//if vector string is empty => error
				if (vector.compare("") == 0)
				{
					finish = true;
					continue;
				}

				//first char must be '(', and last ')'
				if ((vector[0] != '(') || (vector[vector.length()-1] != ')'))
				{
					finish = true;
					continue;
				}

				//remove '(' and ')'
				vector = vector.substr(1,vector.length()-1);
				vector = vector.substr(0,vector.length()-1);

				//new trim of vector string
				vector = getTRIMStr(vector.c_str());

				//get first component
				//find ',' char
				p = vector.find(",");
				if (p == string::npos)
				{
					finish = true;
					continue;
				}

				//extract comp id
				vec.fieldId[0] = vector.substr(0,p);
				//remove first component to vector string
				vector = vector.substr(p+1,vector.length()-p-1);
				//trim comp id
				vec.fieldId[0] = getTRIMStr(vec.fieldId[0].c_str());

				//get second component
				//find ',' char
				p = vector.find(",");
				if (p == string::npos)
				{
					finish = true;
					continue;
				}

				//extract comp id
				vec.fieldId[1] = vector.substr(0,p);
				//remove first component to vector string
				vector = vector.substr(p+1,vector.length()-p-1);
				//trim comp id
				vec.fieldId[1] = getTRIMStr(vec.fieldId[1].c_str());

				//get third component
				//find ',' char
				p = vector.find(",");
				if (p == string::npos)
				{
					finish = true;
					continue;
				}
				

				//extract comp id
				vec.fieldId[2] = vector.substr(0,p);
				//remove first component to vector string
				vector = vector.substr(p+1,vector.length()-p-1);
				//trim comp id
				vec.fieldId[2] = getTRIMStr(vec.fieldId[2].c_str());
				
				p = vector.find(",");
				if (p != string::npos)
				{
					//get isPos value
					po = vector.substr(0,p);
					po = getTRIMStr(po.c_str());
					vec.ispos = (po.compare("1") == 0);
					//vector = vector.substr(0,p);
					//find scale
					po = vector.substr(p+1,vector.length()-p-1);
					vec.scale = atof(getTRIMStr(po.c_str()).c_str());
				}
				else {
					vec.ispos = false;
					vec.scale = 1;
				}
				

				//vec.fieldId[2] = getTRIMStr(vector.c_str());

				vec.frame         = srcFrame;
				vec.id  = "v_";
				vec.id += intToStr(index);

				++index;

				this->srcVectors.push_back(vec);
			}

			return (!this->srcVectors.empty());
		}

		t_VectorList *TransformationRequestClass::getSrcVectors(void) const
		{
			return const_cast<t_VectorList *>(&this->srcVectors);
		}

		string TransformationRequestClass::getSrcVectorsDefinition(void)
		{
			string vecDef = "";
			for (t_VectorList::iterator it=this->srcVectors.begin(); it != this->srcVectors.end(); ++it)
			{
				t_Vector crtVec = (*it);
				if (vecDef.compare("") != 0)
					vecDef += ";";
				vecDef += "(";
				vecDef += crtVec.fieldId[0];
				vecDef += ",";
				vecDef += crtVec.fieldId[1];
				vecDef += ",";
				vecDef += crtVec.fieldId[2];
				if (crtVec.ispos)
					vecDef += ",1";
				else
					vecDef += ",0";
				
				vecDef += ",";
				vecDef += doubleToStr(crtVec.scale);
				vecDef += ")";
			}
			return vecDef;
		}

		bool TransformationRequestClass::setTimeDefinition(const char *timeFieldId, const char *timeFormatId, const char *timePattern)
		{
			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();

			this->timeFormat = timeMgr->getFormatFromTimeId(timeFormatId);

			this->timePattern = "";
			this->timeFieldId = "";

			if (this->timeFormat == TF_NONE)
				return true;
				
			if (this->timeFormat == TF_PATTERN)
			{
				//try to get pre defined pattern
				this->timePattern = timeMgr->getPatternFromTimeId(timeFormatId);

				if (this->timePattern.compare("") == 0)
					this->timePattern = timePattern;

				if (this->timePattern.compare("") == 0)
					return false;
			}

			this->timeFieldId = timeFieldId;

			if (this->timeFieldId.compare("") == 0)
				return false;

			return true;
		}

		string TransformationRequestClass::getTimeFieldId(void) const
		{
			return this->timeFieldId;
		}

		t_TimeFormat TransformationRequestClass::getTimeFormat(void) const
		{
			return this->timeFormat;
		}

		string TransformationRequestClass::getTimePattern(void) const
		{
			return this->timePattern;
		}

		bool TransformationRequestClass::loadSrcData(const char *srcFilePath)
		{
			this->srcData.clear();
			this->srcInfo.fields.clear();

			//init file loader manager to load source data
			FileLoaderManagerClass *fileLoaderMgr = new FileLoaderManagerClass();

			if (!fileLoaderMgr->init(srcFilePath))
			{
				delete fileLoaderMgr;
				return false;
			}

			if (!fileLoaderMgr->getInfo(this->srcInfo))
			{
				delete fileLoaderMgr;
				return false;
			}

			int total = 0;
			if (!fileLoaderMgr->getData(0,0,&this->srcData,total))
			{
				delete fileLoaderMgr;
				return false;
			}

			delete fileLoaderMgr;

			return true;
		}

		DataRecordListClass *TransformationRequestClass::getSrcDataRecordList(void) const
		{
			return const_cast<DataRecordListClass *>(&this->srcData);
		}

		t_FieldList *TransformationRequestClass::getSrcFields(void) const
		{
			return const_cast<t_FieldList *>(&this->srcInfo.fields);
		}

		bool TransformationRequestClass::saveToFile(const char *filePath)
		{
			XMLManagerClass *writer = new XMLManagerClass();

			if (!writer->create("treps"))
			{
				delete writer;
				return false;
			}

			Node *transNode = writer->addChildToRoot("transformation");

			if (transNode == NULL)
			{
				delete writer;
				return false;
			}

			writer->addAttributeToNode("srcFrame",this->getSrcFrame().c_str(),transNode);
			writer->addAttributeToNode("dstFrame",this->getDstFrame().c_str(),transNode);

			writer->addAttributeToNode("srcVectors",this->getSrcVectorsDefinition().c_str(),transNode);
			writer->addAttributeToNode("srcTimeFieldId",this->getTimeFieldId().c_str(),transNode);
			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();
			string timeFormatId = timeMgr->getTimeIdFromFormatAndPattern(this->getTimeFormat(),this->getTimePattern().c_str());
			writer->addAttributeToNode("srcTimeFormatId",timeFormatId.c_str(),transNode);
			writer->addAttributeToNode("srcTimePattern",this->getTimePattern().c_str(),transNode);

			bool res = writer->save(filePath);

			delete writer;

			return res;
		}

		bool TransformationRequestClass::loadFromFile(const char *filePath)
		{
			XMLManagerClass *loader = new XMLManagerClass();

			if (!loader->loadFile(filePath))
			{
				delete loader;
				return false;
			}

			Node *transNode = loader->getChildFromRoot("transformation");

			if (transNode == NULL)
			{
				delete loader;
				return false;
			}

			string srcFrame = loader->getAttributeFromNode("srcFrame",transNode);
			string dstFrame = loader->getAttributeFromNode("dstFrame",transNode);

			this->setFrames(srcFrame.c_str(), dstFrame.c_str());

			bool noTrans = (srcFrame.compare(dstFrame) == 0);

			string srcVecDef = loader->getAttributeFromNode("srcVectors",transNode);
		
			if (!this->setSrcVectorsDefinition(srcVecDef.c_str(), srcFrame.c_str()) && !noTrans)
			{
				delete loader;
				return false;
			}

			string timeFieldId  = loader->getAttributeFromNode("srcTimeFieldId",transNode);
			string timeFormatId = loader->getAttributeFromNode("srcTimeFormatId",transNode);
			string timePattern  = loader->getAttributeFromNode("srcTimePattern",transNode);

			if (!this->setTimeDefinition(timeFieldId.c_str(), timeFormatId.c_str(), timePattern.c_str()))
			{
				delete loader;
				return false;
			}
	
			delete loader;

			return true;
		}
	}
}