Blame view

server/kernel/src/Transformation/TransformationAbstract.cpp 4.35 KB
346b85c6   Benjamin Renard   First commit with...
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
#include "TransformationAbstract.h"

#include <iostream>
#include <cstring>

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

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

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

			this->request = new TransformationRequestClass();

			this->result = new TransformationResultClass();
		}

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

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

9524040e   Laurent BEIGBEDER   6926: Prise en co...
44
45
		bool TransformationAbstractClass::init(const char *srcFrame, const char *dstFrame, const char *srcCenter, const char *dstCenter,
			const t_Time startTime, const t_Time stopTime, const char *srcVecDef,
346b85c6   Benjamin Renard   First commit with...
46
47
48
49
50
51
52
53
54
55
56
			const char *timeFieldId, const char *timeFormatId, const char *timePattern, const char *srcDataFile,
			const char *transformationRequest)
		{
			this->trepsRequest->setStatus("Initialize transformation");

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

150096dc   Benjamin Renard   Fix some bugs + i...
57
58
			bool noTrans = (strcmp(srcFrame,dstFrame) == 0);

346b85c6   Benjamin Renard   First commit with...
59
			//set frames
9674b121   Benjamin Renard   Do not check fram...
60
			if (!noTrans && !this->checkFrame(srcFrame))
346b85c6   Benjamin Renard   First commit with...
61
62
63
64
65
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << srcFrame);
				return false;
			}

9674b121   Benjamin Renard   Do not check fram...
66
			if (!noTrans && !this->checkFrame(dstFrame))
346b85c6   Benjamin Renard   First commit with...
67
68
69
70
71
72
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << dstFrame);
				return false;
			}

			this->request->setFrames(srcFrame, dstFrame);
9524040e   Laurent BEIGBEDER   6926: Prise en co...
73
74
			this->request->setCenters(srcCenter,dstCenter);
			this->request->setTimes(startTime,stopTime);
5e3d9598   Laurent BEIGBEDER   11530: add input ...
75

9524040e   Laurent BEIGBEDER   6926: Prise en co...
76
			
346b85c6   Benjamin Renard   First commit with...
77
78
79
80
81
82
83
84

			//load src data
			if (!this->request->loadSrcData(srcDataFile))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load source data : " << srcDataFile);
				return false;
			}

346b85c6   Benjamin Renard   First commit with...
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
			//set source vectors definition
			if (!this->request->setSrcVectorsDefinition(srcVecDef, srcFrame) && !noTrans)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error detected in source vectors definition : " << srcVecDef);
				return false;
			}

			//set time definition
			if (!this->request->setTimeDefinition(timeFieldId,timeFormatId,timePattern))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error in time definition");
				return false;
			}

			//load times in source data
			if (!this->request->getSrcDataRecordList()->loadTimes(  this->request->getTimeFieldId().c_str(),
										this->request->getTimeFormat(),
										this->request->getTimePattern().c_str()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to load times in source data");
				return false;
			}

			//Init result
			if (!this->result->init(this->request->getTimeFormat(),this->request->getSrcDataRecordList()))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot init result");
				return false;
			}

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

			return true;
		}

		bool TransformationAbstractClass::writeResult(const char *resultPath)
		{
			this->trepsRequest->setStatus("Finalize transformation");

			FileWriterManagerClass *fileWriterMgr = new FileWriterManagerClass();

			t_TimeFormat timeFormat = TF_NONE;
			string timePattern = "";

			if (this->request->getTimeFormat() != TF_NONE)
			{
				timeFormat  = TF_PATTERN;
				timePattern = RESULT_TIME_PATTERN;
			}

			map<string,string> attributes;
			attributes.clear(); 

			bool res = fileWriterMgr->saveData(resultPath, FF_CDF,
					this->result->getDstDataRecordList(),
					timeFormat,
					timePattern.c_str(),
					this->result->getDstFieldList(),
					&attributes
				);

			if (!res)
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot save result file " << resultPath);
			}

			delete fileWriterMgr;
			return res;
		}

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