TransformationAbstract.cpp
4.16 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
#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;
}
}
bool TransformationAbstractClass::init(const char *srcFrame, const char *dstFrame, const char *srcVecDef,
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;
}
bool noTrans = (strcmp(srcFrame,dstFrame) == 0);
//set frames
if (!noTrans && !this->checkFrame(srcFrame))
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << srcFrame);
return false;
}
if (!noTrans && !this->checkFrame(dstFrame))
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown frame for this engine : " << dstFrame);
return false;
}
this->request->setFrames(srcFrame, dstFrame);
//load src data
if (!this->request->loadSrcData(srcDataFile))
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load source data : " << srcDataFile);
return false;
}
//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;
}
}
}